Created
December 13, 2018 18:20
-
-
Save 3211133/abd0d2904acb7b3dc82c9a7fc671b8b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# gosero2.pyw - GUIosero002 | |
# 2018.12.06 | |
import sys | |
import tkinter as tk | |
import random | |
# メイン関数 | |
def main(): | |
root = tk.Tk() | |
root.title("オセロ") | |
den = Osero(root) | |
root.mainloop() | |
# クラス | |
class Osero(): | |
# 作成 | |
def __init__(self, root): | |
self.tf = tk.Frame(root) # トップレベルのフレーム | |
self.tf.grid(column = 0, row = 0, padx = 15, pady = 15) | |
self.ban = [[0] * 8 for n in range(8)] #8*8の全て0のリスト生成(一応それ以外のサイズでも出来るように書いた) | |
self.ban[3][3], self.ban[4][3], self.ban[3][4], self.ban[4][4], self.ban[2][4], self.ban[4][2], self.ban[5][3], self.ban[3][5] = 1, -1, -1, 1, 2, 2, 2, 2 #0でないものを書き換え | |
self.mask = ("〇", "×", "●", "☆")#数字を書き換えて見やすく | |
self.turn = 1 #誰のターンか示す1なら黒-1なら白 | |
self.fugo =((1, 1), (0, 1), (-1, 1), (1, 0), (-1, 0), (1, -1), (0, -1), (-1, -1)) #周囲8方向を示す足したり引いたりして移動する | |
self.syori = 0 #パスや試合終了を管理して試合結果を収集するのにも使う | |
self.okeru = [] #☆の座標を記録しておく | |
# 数字が表示される「エントリー」 | |
root.option_add('*Entry.font', 'MSゴシック 32') | |
self.entVar = tk.StringVar() # コントロール変数 | |
self.entVar.set(str(self.mask[self.turn + 1]) + 'のターンです') | |
self.NumBox = tk.Entry(self.tf, textvariable = self.entVar, width = 15, justify = tk.RIGHT) | |
self.NumBox.bind("<Button-1>", self.syokika) | |
self.NumBox.grid(column = 0, columnspan = 6, row = 0) | |
# ボタンを配置行 | |
self.btnvar = [[tk.StringVar() for x in range(8)] for y in range(8)] #表示更新用 | |
root.option_add('*Button.font', 'MSゴシック 28') | |
for x in range(8): | |
for y in range(8): | |
self.btnvar[x][y].set(str(self.mask[self.ban[x][y] + 1])) | |
Button = tk.Button(self.tf, textvariable = self.btnvar[x][y]) | |
Button.bind("<Button-1>", self.osero(x,y)) | |
Button.grid(column = y, row = x + 1, sticky = 'news') | |
# ボタン毎の動作を定義(イベントドライバ群) | |
def osero(self, x, y):# 数字キー | |
def a(e, x = x, y = y): | |
if self.ban[x][y] == 2: | |
while True: | |
#ひっくり返す | |
for X, Y in self.fugo: | |
cx, cy = x + X, y + Y | |
if -1 < cx < 8 and -1 < cy < 8 and self.ban[cx][cy] == -1 * self.turn: | |
while -1 < cx + X < 8 and -1 < cy + Y < 8 and self.ban[cx + X][cy + Y] != 0 and self.ban[cx + X][cy + Y] != 2: | |
cx += X | |
cy += Y | |
if self.ban[cx][cy] == -1 * self.turn: | |
continue | |
elif self.ban[cx][cy] == self.turn: | |
while cx != x or cy != y:#一個ずつ戻りながら自駒にしてる | |
cx -= X | |
cy -= Y | |
self.ban[cx][cy] = self.turn | |
break | |
self.turn *= -1#最後にself.turn切り替えて終了 | |
while True: | |
#置ける場所を調べる | |
self.okeru = [] | |
for x in range(8):#全てのxyの組み合わせで調べてる | |
for y in range(8): | |
if self.ban[x][y] == 2:#前回☆にしたところを×に戻してる | |
self.ban[x][y] = 0 | |
if self.ban[x][y] == 0:#×の中でself.okeruを探す | |
for X, Y in self.fugo: | |
cx, cy = x + X, y + Y#隣のマスが盤内で敵コマだったら調査続行 | |
if -1 < cx < 8 and -1 < cy < 8 and self.ban[cx][cy] == -1 * self.turn and self.ban[x][y] != 2: | |
while -1 < cx + X < 8 and -1 < cy + Y < 8 and self.ban[cx + X][cy + Y] != 0 and self.ban[cx + X][cy + Y] != 2: | |
cx += X | |
cy += Y | |
if self.ban[cx][cy] == -1 * self.turn:#更に隣のマスが敵コマだったらさらに隣のマスへ | |
continue | |
elif self.ban[cx][cy] == self.turn: | |
self.ban[x][y] = 2 | |
self.okeru.append([x, y]) | |
break | |
print('*y|12345678')#上枠 | |
print('x |________') | |
for rx in range(8): | |
print(rx + 1, '|', end ='')#左枠 | |
for ry in range(8): | |
print(self.mask[self.ban[rx][ry] + 1], end = '')#中身 | |
self.btnvar[rx][ry].set(str(self.mask[self.ban[rx][ry] + 1]))#表示更新 | |
print()#改行 | |
print(self.mask[self.turn + 1] + 'のターンです') | |
self.entVar.set(str(self.mask[self.turn + 1]) + 'のターンです') | |
if len(self.okeru): | |
self.syori = 0 | |
break | |
elif self.syori:#試合終了 | |
self.syori = 0 | |
for x in range(8): | |
for y in range(8): | |
self.syori += self.ban[x][y] | |
if self.syori < 0: | |
self.syori = -1 | |
elif self.syori > 0: | |
self.syori = 1 | |
self.entVar.set(str(self.mask[self.syori + 1]) + 'の勝利です')#引き分けなら×の勝利ですとなる | |
print(str(self.mask[self.syori + 1]) + 'の勝利です') | |
break | |
else:#パス | |
self.syori = 1 | |
self.turn *= -1 | |
continue | |
if not len(self.okeru):#試合が終わったらここに飛ぶ | |
print('試合終了') | |
break | |
elif self.turn == 1:#自分の番になったら終わり | |
break | |
elif self.turn == -1:#ここにAIのコードを書く | |
x, y = random.choice(self.okeru) | |
return a | |
def syokika(self, e):#文字窓クリックで初期化する | |
self.ban = [[0] * 8 for n in range(8)] | |
self.ban[3][3], self.ban[4][3], self.ban[3][4], self.ban[4][4], self.ban[2][4], self.ban[4][2], self.ban[5][3], self.ban[3][5] = 1, -1, -1, 1, 2, 2, 2, 2 | |
self.turn = 1 | |
self.syori = 0 | |
for x in range(8):#ボタンの表示更新をした | |
for y in range(8): | |
self.btnvar[x][y].set(str(self.mask[self.ban[x][y] + 1])) | |
self.entVar.set(str(self.mask[self.turn + 1]) + 'のターンです') | |
def AI(self, e):#ここにAIを書いてself.AI(e)で呼んでもいいかも? | |
pass | |
if __name__ == '__main__': | |
main() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# gosero2.pyw - GUIosero002 | |
# 2018.12.06 | |
import sys | |
import tkinter as tk | |
import random | |
# メイン関数 | |
def main(): | |
root = tk.Tk() | |
root.title("オセロ") | |
den = Osero(root) | |
root.mainloop() | |
# クラス | |
class Osero(): | |
# 作成 | |
def __init__(self, root): | |
self.tf = tk.Frame(root) # トップレベルのフレーム | |
self.tf.grid(column = 0, row = 0, padx = 15, pady = 15) | |
self.ban = [[0] * 8 for n in range(8)] #8*8の全て0のリスト生成(一応それ以外のサイズでも出来るように書いた) | |
self.ban[3][3], self.ban[4][3], self.ban[3][4], self.ban[4][4], self.ban[2][4], self.ban[4][2], self.ban[5][3], self.ban[3][5] = 1, -1, -1, 1, 2, 2, 2, 2 #0でないものを書き換え | |
self.mask = ("〇", "×", "●", "☆")#数字を書き換えて見やすく | |
self.turn = 1 #誰のターンか示す1なら黒-1なら白 | |
self.fugo =((1, 1), (0, 1), (-1, 1), (1, 0), (-1, 0), (1, -1), (0, -1), (-1, -1)) #周囲8方向を示す足したり引いたりして移動する | |
self.syori = 0 #パスや試合終了を管理して試合結果を収集するのにも使う | |
self.okeru = [] #☆の座標を記録しておく | |
# 数字が表示される「エントリー」 | |
root.option_add('*Entry.font', 'MSゴシック 32') | |
self.entVar = tk.StringVar() # コントロール変数 | |
self.entVar.set(str(self.mask[self.turn + 1]) + 'のターンです') | |
self.NumBox = tk.Entry(self.tf, textvariable = self.entVar, width = 15, justify = tk.RIGHT) | |
self.NumBox.bind("<Button-1>", self.syokika) | |
self.NumBox.grid(column = 0, columnspan = 6, row = 0) | |
# ボタンを配置行 | |
self.btnvar = [[tk.StringVar() for x in range(8)] for y in range(8)] #表示更新用 | |
root.option_add('*Button.font', 'MSゴシック 28') | |
for x in range(8): | |
for y in range(8): | |
self.btnvar[x][y].set(str(self.mask[self.ban[x][y] + 1])) | |
Button = tk.Button(self.tf, textvariable = self.btnvar[x][y]) | |
Button.bind("<Button-1>", self.osero(x,y)) | |
Button.grid(column = y, row = x + 1, sticky = 'news') | |
# ボタン毎の動作を定義(イベントドライバ群) | |
def osero(self, x, y):# 数字キー | |
def a(e, x = x, y = y): | |
if self.ban[x][y] == 2: | |
while True: | |
#ひっくり返す | |
for X, Y in self.fugo: | |
cx, cy = x + X, y + Y | |
if -1 < cx < 8 and -1 < cy < 8 and self.ban[cx][cy] == -1 * self.turn: | |
while -1 < cx + X < 8 and -1 < cy + Y < 8 and self.ban[cx + X][cy + Y] != 0 and self.ban[cx + X][cy + Y] != 2: | |
cx += X | |
cy += Y | |
if self.ban[cx][cy] == -1 * self.turn: | |
continue | |
elif self.ban[cx][cy] == self.turn: | |
while cx != x or cy != y:#一個ずつ戻りながら自駒にしてる | |
cx -= X | |
cy -= Y | |
self.ban[cx][cy] = self.turn | |
break | |
self.turn *= -1#最後にself.turn切り替えて終了 | |
while True: | |
#置ける場所を調べる | |
self.okeru = [] | |
for x in range(8):#全てのxyの組み合わせで調べてる | |
for y in range(8): | |
if self.ban[x][y] == 2:#前回☆にしたところを×に戻してる | |
self.ban[x][y] = 0 | |
if self.ban[x][y] == 0:#×の中でself.okeruを探す | |
for X, Y in self.fugo: | |
cx, cy = x + X, y + Y#隣のマスが盤内で敵コマだったら調査続行 | |
if -1 < cx < 8 and -1 < cy < 8 and self.ban[cx][cy] == -1 * self.turn and self.ban[x][y] != 2: | |
while -1 < cx + X < 8 and -1 < cy + Y < 8 and self.ban[cx + X][cy + Y] != 0 and self.ban[cx + X][cy + Y] != 2: | |
cx += X | |
cy += Y | |
if self.ban[cx][cy] == -1 * self.turn:#更に隣のマスが敵コマだったらさらに隣のマスへ | |
continue | |
elif self.ban[cx][cy] == self.turn: | |
self.ban[x][y] = 2 | |
self.okeru.append([x, y]) | |
break | |
print('*y|12345678')#上枠 | |
print('x |________') | |
for rx in range(8): | |
print(rx + 1, '|', end ='')#左枠 | |
for ry in range(8): | |
print(self.mask[self.ban[rx][ry] + 1], end = '')#中身 | |
self.btnvar[rx][ry].set(str(self.mask[self.ban[rx][ry] + 1]))#表示更新 | |
print()#改行 | |
print(self.mask[self.turn + 1] + 'のターンです') | |
self.entVar.set(str(self.mask[self.turn + 1]) + 'のターンです') | |
if len(self.okeru): | |
self.syori = 0 | |
break | |
elif self.syori:#試合終了 | |
self.syori = 0 | |
for x in range(8): | |
for y in range(8): | |
self.syori += self.ban[x][y] | |
if self.syori < 0: | |
self.syori = -1 | |
elif self.syori > 0: | |
self.syori = 1 | |
self.entVar.set(str(self.mask[self.syori + 1]) + 'の勝利です')#引き分けなら×の勝利ですとなる | |
print(str(self.mask[self.syori + 1]) + 'の勝利です') | |
break | |
else:#パス | |
self.syori = 1 | |
self.turn *= -1 | |
continue | |
if not len(self.okeru):#試合が終わったらここに飛ぶ | |
print('試合終了') | |
break | |
elif self.turn == 1:#自分の番になったら終わり | |
break | |
elif self.turn == -1:#ここにAIのコードを書く | |
x, y = random.choice(self.okeru) | |
return a | |
def syokika(self, e):#文字窓クリックで初期化する | |
self.ban = [[0] * 8 for n in range(8)] | |
self.ban[3][3], self.ban[4][3], self.ban[3][4], self.ban[4][4], self.ban[2][4], self.ban[4][2], self.ban[5][3], self.ban[3][5] = 1, -1, -1, 1, 2, 2, 2, 2 | |
self.turn = 1 | |
self.syori = 0 | |
for x in range(8):#ボタンの表示更新をした | |
for y in range(8): | |
self.btnvar[x][y].set(str(self.mask[self.ban[x][y] + 1])) | |
self.entVar.set(str(self.mask[self.turn + 1]) + 'のターンです') | |
def AI(self, e):#ここにAIを書いてself.AI(e)で呼んでもいいかも? | |
pass | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment