[python] chatGPT에게 TicTacToe 만들어달라고 했다.
import tkinter as tk
from tkinter import messagebox
class TicTacToe:
def __init__(self, root):
self.root = root
self.root.title("Tic-Tac-Toe")
self.current_player = "X"
self.board = [""] * 9
self.buttons = []
for i in range(9):
button = tk.Button(root, text="", width=10, height=3, command=lambda i=i: self.make_move(i))
button.grid(row=i // 3, column=i % 3)
self.buttons.append(button)
def make_move(self, position):
if not self.board[position]:
self.board[position] = self.current_player
self.buttons[position].config(text=self.current_player)
if self.check_winner():
messagebox.showinfo("Game Over", f"Player {self.current_player} wins!")
self.reset_game()
elif all(cell != "" for cell in self.board):
messagebox.showinfo("Game Over", "It's a draw!")
self.reset_game()
else:
self.current_player = "O" if self.current_player == "X" else "X"
def check_winner(self):
winning_combinations = [(0, 1, 2), (3, 4, 5), (6, 7, 8),
(0, 3, 6), (1, 4, 7), (2, 5, 8),
(0, 4, 8), (2, 4, 6)]
for combo in winning_combinations:
if self.board[combo[0]] == self.board[combo[1]] == self.board[combo[2]] != "":
return True
return False
def reset_game(self):
self.current_player = "X"
self.board = [""] * 9
for button in self.buttons:
button.config(text="")
if __name__ == "__main__":
root = tk.Tk()
game = TicTacToe(root)
root.mainloop()
'프로그래밍 > Python' 카테고리의 다른 글
[python] cowsay (0) | 2023.09.14 |
---|---|
[PYTHON] Python tkinter 강좌 (0) | 2023.08.25 |
[python] send email app (0) | 2023.08.18 |
[python] chatGPT가 만들어준 tkinter 이용한 영화추천 프로그램 (0) | 2023.08.17 |
[python] PyQt5 어플리케이션 프레임워크에 대한 파이썬 버전 (0) | 2023.08.16 |