When I was little, there was a time when it was my boom to play a game called tic-tac-toe with my friends in a short time. So I decided to make it this time.
Make screens and buttons
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
squares = 3
class TictacApp(ttk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.create_widgets()
self.set_value()
def create_widgets(self):
for i in range(squares):
for j in range(squares):
button = ttk.Button(self, command=self.record(i, j))
button.bind('<Button-1>', self.mark)
button.grid(column=i, row=j, sticky=(N, S, E, W))
for i in range(squares):
self.columnconfigure(i, weight=1)
self.rowconfigure(i, weight=1)
self.master.columnconfigure(0, weight=1)
self.master.rowconfigure(0, weight=1)
self.grid(column=0, row=0, sticky=(N, S, E, W))
def main():
root = Tk()
root.title('〇✕ Game')
TictapApp(root)
root.mainloop()
if __name__ == '__main__':
main()
〇 and ✕ are displayed
def mark(self, event):
if not event.widget['text']:
if self.player == 1:
event.widget['text'] = str('〇')
else:
event.widget['text'] = str('×')
Remember the button you pressed
def record(self, i, j):
def x():
if not self.field[i][j]:
self.field[i][j] = self.player
self.line_check()
self.change_player()
self.clear()
return x
def set_value(self):
self.player = 1
self.field = []
for i in range(squares):
self.field.append(['' for i in range(squares)])
self.finish = 0
Judgment whether the third line is lined up
def line_check(self):
cross = 0
for i in range(squares):
horizon = 0
vertical = 0
for j in range(squares):
if self.field[i][j] == self.player:
horizon += 1
if self.field[j][i] == self.player:
vertical += 1
if self.field[i][i] == self.player:
cross += 1
if horizon == 3 or vertical == 3 or cross == 3:
self.game_end()
if self.field[0][2] == self.field[1][1] == self.field[2][0] == self.player:
self.game_end()
Inform the victory or defeat
def game_end(self):
if self.player == 1:
messagebox.showinfo('Congrats!', 'Leading victory')
else:
messagebox.showinfo('Congrats!', 'Second strike victory')
self.finish = 1
If you can't settle
def change_player(self):
if self.finish == 0:
self.player = -self.player
def clear(self):
if self.finish == 1:
self.create_widgets()
self.set_value()
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
squares = 3
class TictacApp(ttk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.create_widgets()
self.set_value()
def set_value(self):
self.player = 1
self.field = []
for i in range(squares):
self.field.append(['' for i in range(squares)])
self.finish = 0
def create_widgets(self):
for i in range(squares):
for j in range(squares):
button = ttk.Button(self, command=self.record(i, j))
button.bind('<Button-1>', self.mark)
button.grid(column=i, row=j, sticky=(N, S, E, W))
for i in range(squares):
self.columnconfigure(i, weight=1)
self.rowconfigure(i, weight=1)
self.master.columnconfigure(0, weight=1)
self.master.rowconfigure(0, weight=1)
self.grid(column=0, row=0, sticky=(N, S, E, W))
def mark(self, event):
if not event.widget['text']:
if self.player == 1:
event.widget['text'] = str('〇')
else:
event.widget['text'] = str('×')
def record(self, i, j):
def x():
if not self.field[i][j]:
self.field[i][j] = self.player
self.line_check()
self.change_player()
self.clear()
return x
def change_player(self):
if self.finish == 0:
self.player = -self.player
def line_check(self):
cross = 0
for i in range(squares):
horizon = 0
vertical = 0
for j in range(squares):
if self.field[i][j] == self.player:
horizon += 1
if self.field[j][i] == self.player:
vertical += 1
if self.field[i][i] == self.player:
cross += 1
if horizon == 3 or vertical == 3 or cross == 3:
self.game_end()
if self.field[0][2] == self.field[1][1] == self.field[2][0] == self.player:
self.game_end()
def game_end(self):
if self.player == 1:
messagebox.showinfo('Congrats!', 'Leading victory')
else:
messagebox.showinfo('Congrats!', 'Second strike victory')
self.finish = 1
def clear(self):
if self.finish == 1:
self.create_widgets()
self.set_value()
def main():
root = Tk()
root.title('OX game')
TictacApp(root)
root.mainloop()
if __name__ == '__main__':
main()
While looking at "I tried to make GUI tic-tac-toe with Python and Tkinter" that I referred to this time, I wrote the program while checking what I was doing. 〇✕ I had a lot of trouble just making a game. Also, this time there was something I was able to refer to, but I thought it would be very difficult to make it from scratch. From now on, I decided to study so that I could make it from scratch by myself.
--I tried to make GUI tic-tac-toe with Python and Tkinter
https://qiita.com/TN0321/items/d0ba918eefa5d9c7e587
--The easiest Python introductory class book Fumitaka Osawa [Author]
Recommended Posts