Cov txheej txheem:

Python Tic Tac Toe Game: 4 Kauj Ruam
Python Tic Tac Toe Game: 4 Kauj Ruam

Video: Python Tic Tac Toe Game: 4 Kauj Ruam

Video: Python Tic Tac Toe Game: 4 Kauj Ruam
Video: Tic Tac Toe in only 4 minutes! | Python Projects 2024, Lub Xya hli ntuj
Anonim
Python Tic Tac Toe Game
Python Tic Tac Toe Game

nab hab sej tic tac ntiv taw game

qhov kev ua si no yog ua los ntawm sej uas yog lus siv computer

Kuv tau siv tus kws kho mob nab hab sej hu ua: pycharm koj kuj tseem tuaj yeem siv cov nab hab sej code ib txwm hloov kho ib yam nkaus

Kauj ruam 1: Ntshuam

Ntshuam
Ntshuam

los ntawm tkinter ntshuam *

Kauj ruam 2: Hauv paus

Hauv paus
Hauv paus

hauv paus = Tk ()

root.title ("3T TIC TAC TOE") # Title

text = Nkag mus (hauv paus, font = ("ds-digital", 15)) text.pack (sau = X, padx = 5, pady = 5, ipadx = 5, ipady = 5) # Text

pawg = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] # Pawg Thawj Coj

Kauj Ruam 3: Ua Haujlwm Tom Qab Tso Rau Hauv Tus Kws Kho Mob Nws Qhia Warrnings Nyem rau Warrnigs thiab Muab Assambel Txhua

# Kev ua haujlwm

player = "X" khawm = stop_game = cuav stop_game_tie = Cuav

def Qhib (): # Luam Ntawv Hloov cov ntawv.delete (0, END) text.insert (0, "{} 'S TURN".format (player))

def Tawm (): root.destroy () tawm ()

def Game_Over (): # Chech yog tias game dhau Check_Winner ()

yog stop_game == Muaj tseeb: Hplayer () text.delete (0, END) text.insert (0, "'{}' WINNER".format (player))

lwm: Check_Tie ()

yog stop_game_tie == Muaj tseeb: text.delete (0, END) text.insert (0, "TIE MATCH")

def Check_Winner (): # Txheeb xyuas yog tias muaj tus yeej thoob ntiaj teb stop_game

row_1 = board [0] == board [1] == board [2]! = "-" row_2 = board [3] == board [4] == board [5]! = "-" row_3 = pawg thawj coj [6] == board [7] == board [8]! = "-" yog row_1 lossis row_2 lossis row_3: stop_game = True

column_1 = board [0] == board [3] == board [6]! = "-" column_2 = board [1] == board [4] == board [7]! = "-" column_3 = pawg thawj coj [2] == board [5] == board [8]! = "-" yog column_1 lossis column_2 lossis column_3: stop_game = True

diagonal_1 = board [0] == board [4] == board [8]! = "-" diagonal_2 = board [2] == board [4] == board [6]! = "-" if diagonal_1 or diagonal_2: stop_game = Tseeb

def Check_Tie (): # Txheeb xyuas yog tias nws yog kev sib tw sib tw thoob ntiaj teb stop_game_tie

yog "-" tsis nyob hauv pawg: stop_game_tie = Tseeb

def Hplayer (): # Tswj Kev ua si thoob ntiaj teb

yog neeg uas ua ntawv == "X": player = "O"

lwm tus: player = "X"

def Add_Text (pos, ua si): # Ntxiv Cov ntawv thoob ntiaj teb khawm

yog tias pos tsis nyob hauv khawm thiab stop_game == Tsis tseeb thiab stop_game_tie == Tsis tseeb thiab pos! = 9: Tig ()

def_buttons [pos].configure (text = play) board [pos] = player

khawm.append (pos) Hplayer () Tig () Game_Over ()

Kauj Ruam 4: Txoj Cai Kawg Nws Yuav Muab Qhov Kawg?

Txoj Cai Kawg Nws Yuav Muab Qhov Kawg Kawg?
Txoj Cai Kawg Nws Yuav Muab Qhov Kawg Kawg?
Txoj Cai Kawg Nws Yuav Muab Qhov Kawg Kawg?
Txoj Cai Kawg Nws Yuav Muab Qhov Kawg Kawg?

# GRID

def New_Match (): # Tshiab Match Button Function thoob ntiaj teb pawg neeg ncaws pob thoob ntiaj teb khawm thoob ntiaj teb stop_game ntiaj teb stop_game_tie

button_1.configure (text = "") button_2.configure (text = "") button_3.configure (text = "") button_4.configure (text = "") button_5.configure (text = "") button_6.configure (ntawv nyeem = "") button_7.configure (text = "") button_8.configure (text = "") button_9.configure (text = "") board = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] player = "X" khawm = stop_game = cuav stop_game_tie = Cuav Turn ()

# Nyees khawm

Tig ()

ncej = Ncej (hauv paus) thav duab.pack (sab = TOP, thauj tog rau nkoj = NW)

ncej1 = Ncej (thav duab) thav duab1.pack ()

button_1 = Button (frame1, text = "", width = 8, height = 3, command = lambda: Add_Text (0, player), bg = 'pink') button_1.pack (sab = LEFT)

button_2 = Button (frame1, text = "", width = 8, height = 3, command = lambda: Add_Text (1, player), bg = 'pink') button_2.pack (side = LEFT)

button_3 = Button (frame1, text = "", width = 8, height = 3, command = lambda: Add_Text (2, player), bg = 'pink') button_3.pack (side = LEFT)

ncej2 = Ncej (thav duab) thav duab2.pack ()

button_4 = Button (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (3, player), bg = 'blue') button_4.pack (side = LEFT)

button_5 = Button (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (4, player), bg = 'blue') button_5.pack (side = LEFT)

button_6 = Button (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (5, player), bg = 'blue') button_6.pack (side = LEFT)

frame3 = Ncej (thav duab) thav duab3.pack ()

button_7 = Button (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (6, player), bg = 'orange') button_7.pack (side = LEFT)

button_8 = Button (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (7, player), bg = 'orange') button_8.pack (side = LEFT)

button_9 = Button (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (8, player), bg = 'orange') button_9.pack (side = LEFT)

frame4 = Ncej (thav duab) thav duab4.pack ()

button_clear = Button (frame4, text = "NEW MATCH", width = 13, height = 3, command = lambda: New_Match (), bg = 'violet', foreground = 'red') button_clear.pack (side = LEFT)

exit_button = Button (frame4, text = "EXIT", dav = 12, qhov siab = 3, hais kom ua = lambda: Tawm (), bg = 'ntsuab', foreground = 'txiv kab ntxwv') exit_button.pack (sab = LEFT)

def_buttons = [button_1, button_2, button_3, button_4, button_5, button_6, button_7, button_8, button_9]

hauv paus.mainloop ()

Pom zoo: