Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Create a Simple Tic-Tac-Toe Game Class

Python Create a Simple Tic-Tac-Toe Game Class

Document Object Reference Manual Python3 Examples

Tic-Tac-Toe is a classic two-player game in which players take turns placing their marks (usually "X" and "O") on a 3x3 grid. The goal is to be the first to form a line of three identical marks horizontally, vertically or diagonally. Below we will create a simple Tic-Tac-Toe game class with basic game logic and display functionality.

Example

class

TicTacToe:

   

def

init

(

self

)

:

       

self

.

board

=

[

[

' '

for

_

in

range

(

3

)

]

for

_

in

range

(

3

)

]

       

self

.

current_player

=

'X'

   

def

print_board

(

self

)

:

       

for

row

in

self

.

board

:

           

print

(

'|'

.

join

(

row

)

)

           

print

(

'-'

5

)

   

def

make_move

(

self

,

row

,

col

)

:

       

if

self

.

board

[

row

]

[

col

]

==

' '

:

           

self

.

board

[

row

]

[

col

]

=

self

.

current_player

           

self

.

current_player

=

'O'

if

self

.

current_player

==

'X'

else

'X'

       

else

:

           

print

(

"Invalid move! The cell is already occupied."

)

   

def

check_winner

(

self

)

:

       

Check rows

       

for

row

in

self

.

board

:

           

if

row

[

0

]

==

row

[

1

]

==

row

[

2

]

!=

' '

:

               

return

row

[

0

]

       

Check columns

       

for

col

in

range

(

3

)

:

           

if

self

.

board

[

0

]

[

col

]

==

self

.

board

[

1

]

[

col

]

==

self

.

board

[

2

]

[

col

]

!=

' '

:

               

return

self

.

board

[

0

]

[

col

]

       

Check diagonals

       

if

self

.

board

[

0

]

[

0

]

==

self

.

board

[

1

]

[

1

]

==

self

.

board

[

2

]

[

2

]

!=

' '

:

           

return

self

.

board

[

0

]

[

0

]

       

if

self

.

board

[

0

]

[

2

]

==

self

.

board

[

1

]

[

1

]

==

self

.

board

[

2

]

[

0

]

!=

' '

:

           

return

self

.

board

[

0

]

[

2

]

       

Check for a tie

       

if

all

(

cell

!=

' '

for

row

in

self

.

board

for

cell

in

row

)

:

           

return

'Tie'

       

return

None

   

def

play_game

(

self

)

:

       

while

True

:

           

self

.

print_board

(

)

           

print

(

f

"Player {self.current_player}'s turn"

)

            row

=

int

(

input

(

"Enter row (0, 1, 2): "

)

)

            col

=

int

(

input

(

"Enter column (0, 1, 2): "

)

)

           

self

.

make_move

(

row

,

col

)

            winner

=

self

.

check_winner

(

)

           

if

winner:

               

self

.

print_board

(

)

               

if

winner

==

'Tie'

:

                   

print

(

"It's a tie!"

)

               

else

:

                   

print

(

f

"Player {winner} wins!"

)

               

break

Code analysis:

  1. __init__ The method initializes an empty 3x3 board and sets the current player to "X".
  2. print_board The method prints the current state of the board.
  3. make_move The method lets a player place a mark at a specified row and column, and switches the current player.
  4. check_winner The method checks whether a player has won or the game is a draw. It checks all rows, columns and diagonals for three identical marks, and returns the winning player or "Tie" for a draw.
  5. play_game The method is the game's main loop, letting players take turns entering a row and column to make a move until someone wins or the game is a draw.

Output: run the play_game method and the game starts; players take turns entering a row and column to make a move. The game continues until a player wins or the game is a draw. For example:

 | | 
-----
 | | 
-----
 | | 
-----
Player X's turn
Enter row (0, 1, 2): 0
Enter column (0, 1, 2): 0
X| | 
-----
 | | 
-----
 | | 
-----
Player O's turn
Enter row (0, 1, 2): 1
Enter column (0, 1, 2): 1
X| | 
-----
 |O| 
-----
 | | 
-----
...

The game continues until a player wins or the game is a draw.

Document Object Reference Manual Python3 Examples