Tic-Tac-Toe program by me & GPT
Game Rule:
- Players: Two players, X and O.
- Board: The game is played on a 3×3 grid.
- Goal: Get three of your marks (X or O) in a row – horizontally, vertically, or diagonally.
- Turns: Players take turns to place their mark in an empty square.
- Winning: The first player to make a line of three wins.
- Draw: If all squares are filled and no one has three in a row, the game is a draw.
- Restart: After a win or draw, the board can be reset to play again.
#Source Code
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root.title("틱택토 게임")
current_player = "X"
def button_click(row, col):
global current_player
if buttons[row][col]["text"] == "" :
buttons[row][col]["text"] = current_player
if check_winner():
messagebox.showinfo("게임 종료", f"{current_player} 승리!")
reset_board()
elif is_draw():
messagebox.showinfo("게임 종료", "무승부!")
reset_board()
else:
current_player = "O" if current_player == "X" else "X"
def check_winner():
for i in range(3):
if buttons[i][0]["text"] == buttons[i][1]["text"] == buttons[i][2]["text"] != "":
return True
if buttons[0][i]["text"] == buttons[1][i]["text"] == buttons[2][i]["text"] != "":
return True
if buttons[0][0]["text"] == buttons[1][1]["text"] == buttons[2][2]["text"] != "":
return True
if buttons[0][2]["text"] == buttons[1][1]["text"] == buttons[2][0]["text"] != "":
return True
return False
def is_draw():
for row in buttons:
for btn in row:
if btn["text"] == "":
return False
return True
def reset_board():
global current_player
for row in buttons:
for btn in row:
btn["text"] = ""
current_player = "X"
buttons = []
for i in range(3):
row = []
for j in range(3):
btn = tk.Button(root, text="", font=("Arial", 40), width=5, height=2,
command=lambda r=i, c=j: button_click(r, c))
btn.grid(row=i, column=j)
row.append(btn)
buttons.append(row)
root.mainloop()

