Tic-tac-toe (American English), noughts and crosses (British English), or Xs and Os is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row is the winner.
tkinter Python library is used to create the GUI. Two options are available to play the game, along with the system or with another player.
A small winning strategy is used to play with the system. The system will try to find the best place to put its naught or cross by which the system will win or try to stop players to win.
Approach:
- Create a landing page containing selection buttons: Single-player or multiplayer.
- Create a game board containing nine tiles to play the game along with other details (i.e. playing with a system or another player, whose turn etc.).
- Allow the player to press the tile and check the status of the game (i.e. Tie game, any one of the players won the match or the game is still running).
- Display the message, of who won the match.
Description of other functions:
- gameboard_pc() and gameboard_pl() will create another geometry to play the game. It will add 9 buttons on the 3×3 board of the game (Three rows of buttons containing three buttons each).
- get_text_pc() and get_text() functions will put the text on buttons as it is pressed.
- pc() function will decide the next move of the system.
- winner() function will check whether the player won the match or not.
- isfree() function will check whether the player can put it’s a coin or not.
- isfull() function will check whether the board is full or not.
Example 1: GUI of Tic Tac Toe
Python
#importing Packages from tkinter
from tkinterimport *
from tkinterimport messagebox
Player1from0 from1
from2from0 from4
from5 from6
from7
from7from9
from7tkinter1 Player1
from7tkinter4
from7tkinter6 Player1from0from0 import0 import1 import2from0from0 import5 import1 from2from0from0 from4*1
*2*3from0 import0*6
*2import2from0 from1
*2Player1from0from4
from7
from7tkinter6 Player1from0from0 from4 import1 import2from0from0 import5 import1 from2from0from0 from4*1
*2*3from0 from4*6
*2import2from0 messagebox1
*2Player1from0 import0
from7messagebox7
from7messagebox9
from7Player11
from7Player13
from7Player15
from5 Player17
from7tkinter1 from2
from7from02
from7from04 from05from06 from07from08from09from10
*2tkinter6 from13import5from15from0from0 from13from19from15from0from0 from13from24from25from0import5*1
from29from2from0 from32
from29from34from0 from36from37from38import5from15from41 from42*6
from29from45
from29from47
from7from49
*2from51 from52import5from54from0from0 from57from19from54from0from0 from57from24from64from0 import5*1
from29from2from0 from32
from29from34from0 from36from37from77import5from54from41 from81*6
from29from47
*2from51 from57import5from89import5from15from0from0 from57from19from89from19from15from0from0 from57from24from89from24from25from0import5*1
from29from2from0 from32
from29from34from0 from36from37from77import5from89import5from15from41 from81*6
from29from47
*2from51 from57import5from89from24from15from0from0 from57from19from89from19from15from0from0 from57from24from89import5from25from0import5*1
from29from2from0 from32
from29from34from0 from36from37 from77import5from89from24from15from41 from81*6
from29from47
*2from51 from57import5from89import5from15import1 from57import5from89from19from15import1 from57import5from89from24from15import1 from57from19from89import5from15import1 from57from19from89from19from15import1 from57from19from89from24from15import1 from57from24from89import5from15import1 from57from24from89from19from15import1 from57from24from89from24from25from0 import5*1
from29from2from0 from32
from29from34from0 from36import37import38import39*6
import41
import42
import43from0 import45
import46
import47import48import49
import50import5import38import5*6
import55
import56from0 import58
import59import58import5import38import5import38import5import66
import59import58import5import38import5import38import5import66
import59import58import5import38import5import38import5import82
import83
import84from0 import58
import59import58import5import38import5import38import5import66
import59import58import5import38import5import38import5import66
import59import58import5import38import5import38import5import82
from04 from05from06 from07from08from09from10
from7from04 *20from06 from07from08from09from10
*26
*2*28from0 *30
*31*32from0 *34*35from0 *37import38
*31*40from0 from08*43import38*45*46
*31*48from0 *50 *51from0 *53from0 *55
*2*57from0 *59from0 *61
*62
Output:
Code Explanation:
- The code starts by importing the tkinter package.
- This is a library that allows us to create graphical user interfaces in Python.
- Next, we import the messagebox function from this same library.
- The messagebox function creates a window with an OK button and text input field for the player to enter their move.
- The next line of code sets up our variables: Player1 = ‘X’, stop_game = False.
- These are global variables which means they can be accessed anywhere in the program without having to use parentheses or any other special syntax (e. g., if Player1 == “X” and states[r] == 0 ).
- Global variables are often used when you want your program’s logic to be able to access all parts of it without needing extra lines of code for each part (e.g., checking if Player1 == “O” and states[r] == 0).
- Next, we have two functions: clicked(r,c) and check_if_win().
- The first one checks whether player X has won or not based on what state r is currently in; if so, then it changes state r back into X; otherwise, it changes state r into O depending on who won last time.
- The code is a function that checks if the player has won or lost.
- The code starts by importing tkinter, which is a library for creating graphical user interfaces in Python.
- The next line imports messagebox, which allows the program to display messages on screen.
- The first line of the function sets up Player1 as “X”.
- Then states[r] is set to 0 and stop_game is set to False because this function will be checking if the player has won or lost.
- If they have won then states[r] will be changed to “X” and Player1 will be changed to “O”.
- If they have lost then states[r] will be changed to “O” and Player1 will be changed.
- The code starts by declaring a global variable called stop_game.
- The code then declares three loops that iterate through the states of the game board.
- Inside each loop, if any state is true, then stop_game is set to True, and winner is displayed in messagebox with the corresponding state value.
- The check_if_win() function checks if there are any winning combinations on the board.
- If so, it displays “Winner” in messagebox with the corresponding state value and sets stop_game to True.
- The code checks if the player has won.
- If they have, it displays a messagebox with the states of the game and their corresponding values.
- If they haven’t, it displays a messagebox with “tie” and returns from the function.
Example 2) GUI of Tic Tac Toe
Python3
*63
*64
*65
import *67
import tkinter
from tkinterimport *
from *75import *77
from tkinterimport messagebox
from *83import *85
*86
*87from0 import5
*90
tkinter1 *92
*92from0 *95*96 from04 *98from06 from07from08from09from03from04 from05from06 from07from08from09from03
from11
from12
from5 from14
from7from16 from17import5from89import5from15from0from0 from24import1 from26import5from89from19from15from0from0 from24import1 from26import5from89from24from15from0from0 from42from43
from29from45from19from89import5from15from0from0 from24import1 from26from19from89from19from15from0from0 from24import1 from26from19from89from24from15from0from0 from42from43
from29from45from24from89import5from15from0from0 from24import1 from26from24from89from19from15from0from0 from24import1 from26from24from89from24from15from0from0 from42from43
from29from45import5from89import5from15from0from0 from24import1 from26from19from89import5from15from0from0 from24import1 from26from24from89import5from15from0from0 from42from43
from29from45import5from89from19from15from0from0 from24import1 from26from19from89from19from15from0from0 from24import1 from26from24from89from19from15from0from0 from42from43
from29from45import5from89from24from15from0from0 from24import1 from26from19from89from24from15from0from0 from24import1 from26from24from89from24from15from0from0 from42from43
from29from45import5from89import5from15from0from0 from24import1 from26from19from89from19from15from0from0 from24import1 from26from24from89from24from15from0from0 from42from43
from29from45import5from89from24from15from0from0 from24import1 from26from19from89from19from15from0from0 from24import1 from26from24from89import5from15from0from0 import38
import39
from5 import41
from7tkinter1 *87
from7tkinter6 import47from0from0 import50*1
*2tkinter6 *87import55 from24 from0from0 import5*1
from29import62from0import64
from29import66from0import68
from29import47from0 import0
*2import74*1
from29import66from0import64
from29import62from0import68
from29import47from0 messagebox1
*2*87from41from0 from19
*2import94from0import96
from7tkinter6 import99import0from10
*2messagebox03
*2messagebox05from0 from36from37import38messagebox10*6
from7from51 import99messagebox1from10
*2messagebox03
*2messagebox05from0 from36from37import38messagebox25*6
from7from51messagebox29
*2messagebox03
*2messagebox05from0 from36messagebox36import38messagebox36*6
messagebox40
from5 messagebox42
from7from16 import47from0from0 *96
messagebox49
from5 messagebox51
from7messagebox53from0 from32
from7from04 from05from06 messagebox60
*2tkinter6messagebox63import50messagebox65import5from10
from29messagebox53from0 from4
from7from16 messagebox53
messagebox75
from5 messagebox77
from7tkinter1 messagebox80
from7messagebox80from0 messagebox84
from7from04 from05from06 from07from08from09from10
*2messagebox94from0 from09from41from05
*2Player100
*2Player102from0 messagebox84
*2from04 *20from06 from07from08from09from10
from29Player114from0 *20
from29Player118
from29Player120from0 Player122
from29Player124from0 *30
Player127Player128from0Player130Player131from0Player133from0*34*35from0*37*6
from29Player141from0Player143from0Player145
from7Player147
Player148
from5 Player150
from7Player152from0 messagebox84
from7from04 from05from06 from07from08Player161Player162
*2from04 *20from06 from07from08Player161Player170
from29tkinter6 import47from0from0 import50*1
Player127Player179
from7Player181from0 messagebox84
from7tkinter6 Player152from0from0 Player189
*2from16
from7import74*1
*2from04 Player197from06 import58from4import38from1from003
from29from04 from05from06 from008
Player127from010from0 from012
Player127from014import5from016from19import82from0 Player197
Player127tkinter6 from023
from024from16 from05
*2from028from0 messagebox84
*2from04 from05from06 from008
from29tkinter6 from05from06 *95import5import38import5from044import5import38from24from044from24import38import5from044from24import38from24from056
Player127from058
*2tkinter6 Player161from062import5*1
from29Player181from0 from068import5import38Player161from072from073from19*6
from29from16 from078
*2from080from0 messagebox84
*2from04 from05from06 from008
from29tkinter6 from05from06 *95import5import38from19from044from19import38import5from044from19import38from24from044from24import38from19from056
Player127from110
*2tkinter6 Player161from114import5*1
from29Player181from0 from068import5import38Player161from124from073from19*6
from29from16 from130
from131
from5 from133
from7tkinter1 *87
from7tkinter6 import47from0from0 import50*1
*2tkinter6 *87import55 from24 from0from0 import5*1
from29import62from0import64
from29import66from0import68
from29import47from0 import0
*2import74*1
from29from169from0import68
from29import66from0import64
from29import62from0import68
from29import47from0 messagebox1
*2*87from41from0 from19
*2import94from0import96
from7*98from0 from32
from7tkinter6 import99import0from10
*2messagebox03
*2*98from0 from4
*2messagebox05from0 from36from37import38from214*6
from7from51 import99messagebox1from10
*2messagebox03
*2*98from0 from4
*2messagebox05from0 from36from37import38from233*6
from7from51messagebox29
*2messagebox03
*2*98from0 from4
*2messagebox05from0 from36messagebox36import38messagebox36*6
from7tkinter6from254
*2tkinter6 *87import55 from24 from260from0 import5*1
from29Player181from0 from267
from29from269import5from271from19from273from0import64
from29from277import5from279from19from281
from282
from5 from284
from7tkinter1 messagebox80
from7messagebox80from0 messagebox84
from7from04 from05from06 from07from08from09from10
*2messagebox94from0 from09from41from05
*2Player100
*2Player102from0 messagebox84
*2from04 *20from06 from07from08from09from10
from29Player114from0 *20
from29Player118
from29Player120from0 from329
from29Player124from0 *30
Player127Player128from0Player130Player131from0Player133from0*34*35from0*37*6
from29Player141from0Player143from0Player145
from7Player147
from355
from5 from357
from7from359
from7from361from0 import45
from7from365from366*6
from7from369from0 from371from0from373*35from0from376*6
from7from379from0from19from382from0from19*6
from7from387from0 from371from0from391import38
Player127from394from0from514from515from0from517from518from0Player130*6