• We just launched and are currently in beta. Join us as we build and grow the community.

How to Create a Tic-Tac-Toe Game in Python

alessiod

Indie Game Explorer
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
178
Likes
131
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Introduction:
This tutorial is going to cover how to create a simple, text based tic-tac-toe (three in a row) game in Python.

The Variables:
First we need to create some variable that will be used throughout the game. The constants are used to hold the board size, and the character value of each possible state of one field within the board...

  1. X =

    "X"

    ;
  2. O =

    "O"

    ;
  3. EMPTY =

    " "

    ;
  4. SIZE =

    9

    ;

Next we need some variables which may change throughout the game...

  1. turn =

    ""

    ;
  2. human =

    ""

    ;
  3. computer =

    ""

    ;

The Main Game Loop:
Next we will start with the main game loop. First we ask if the human wants to go first, if they do, we set their value to O and set the current player to human, otherwise we perform the opposite and set the player value to X.

Once we have done that, we create a new variable list named board which will be our game board to hold all of the field values (character moves), and set up the board.

Then we show the instructinos to the player, checks it the board is setup correctly, then begin the main loop which runs while there are spare spaces on the board.

Within the main game loop, we allow the user (or computer) to take their turn, draw the new board, check for any wins, and if they have, end the game/loop...

  1. def

    main(

    )

    :
  2. if

    (

    yes_no(

    "Would you like to go first?"

    )

    ==

    "y"

    )

    :
  3. human =

    X;
  4. computer =

    O;
  5. turn =

    human;
  6. else

    :
  7. human =

    O;
  8. computer =

    X;
  9. turn =

    computer;
  10. board =

    [

    ]

    ;
  11. setup_board(

    board)

    ;
  12. showInstructions(

    )

    ;
  13. isInGame =

    inGame(

    board)

    ;
  14. while

    (

    isInGame)

    :
  15. takeTurn(

    turn,

    board,

    human,

    computer)

    ;
  16. drawBoard(

    board,

    human,

    computer)

    ;
  17. if

    (

    checkWin(

    board,

    human,

    computer)

    ==

    "human"

    )

    :
  18. print

    (

    "\n

    "

    +win(

    "human"

    )

    )

    ;
  19. break

    ;
  20. elif

    (

    checkWin(

    board,

    human,

    computer)

    ==

    "computer"

    )

    :
  21. print

    (

    "\n

    "

    +win(

    "computer"

    )

    )

    ;
  22. break

    ;
  23. turn =

    switchTurn(

    turn,

    human,

    computer)

    ;
  24. temp =

    inGame(

    board)

    ;
  25. if

    (

    temp ==

    "yes"

    )

    :
  26. isInGame =

    "yes"

    ;
  27. else

    :
  28. break

    ;
  29. return

    ""

    ;

Show Instructions:
This function is simple, we display the text instructions to the user...

  1. def

    showInstructions(

    )

    :
  2. sep =

    " | "

    ;
  3. print

    (

    "\n

    0 "

    + sep + " 1 "

    + sep + " 2 "

    )

    ;
  4. print

    (

    " 3 "

    + sep + " 4 "

    + sep + " 5 "

    )

    ;
  5. print

    (

    " 6 "

    + sep + " 7 "

    + sep + " 8 \n

    "

    )

    ;

Check Win:
The Check Win function gets all the possible combination of moves to win the game, and checks if either the player or computer has all their pieces in all the places... if so, they've won!...

  1. def

    checkWin(

    b,

    h,

    c)

    :
  2. 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

    ]

    )

    ;
  3. for

    comb in

    combinations:
  4. if

    (

    b[

    comb[

    0

    ]

    ]

    ==

    h and

    b[

    comb[

    1

    ]

    ]

    ==

    h and

    b[

    comb[

    2

    ]

    ]

    ==

    h)

    :
  5. return

    "human"

    ;
  6. if

    (

    b[

    comb[

    0

    ]

    ]

    ==

    c and

    b[

    comb[

    1

    ]

    ]

    ==

    c and

    b[

    comb[

    2

    ]

    ]

    ==

    c)

    :
  7. return

    "computer"

    ;
  8. return

    "no"

    ;

DrawBoard:
Up next we have the draw board function, which simply runs through each place on the board and outputs the equvilient character...

  1. def

    drawBoard(

    b,

    h,

    c)

    :
  2. sep =

    " | "

    ;
  3. print

    (

    "\n

    "

    +b[

    0

    ]

    +sep+ b[

    1

    ]

    +sep+b[

    2

    ]

    )

    ;
  4. print

    (

    b[

    3

    ]

    +sep+b[

    4

    ]

    +sep+b[

    5

    ]

    )

    ;
  5. print

    (

    b[

    6

    ]

    +sep+b[

    7

    ]

    +sep+b[

    8

    ]

    +"\n

    "

    )

    ;
  6. print

    (

    "---KEY---\n

    Player - "

    + h + "\n

    Computer - "

    + c)

    ;

Switch and Take Turn:
The switch turn function simply swaps the current turn variable from player to computer, or vice versa.

While the take turn function allows the human to select and enter a place to move a piece to, or the computer gets a list of legal moves/empty board places and moves to the first one within the list...

  1. def

    switchTurn(

    turn,

    h,

    c)

    :
  2. if

    (

    turn ==

    h)

    :
  3. return

    c;
  4. else

    :
  5. return

    h;


  6. def

    takeTurn(

    t,

    b,

    h,

    c)

    :
  7. if

    (

    t ==

    h)

    :
  8. legal =

    legalMoves(

    b)

    ;
  9. index =

    int

    (

    input

    (

    "\n

    Where would you like to go?\n

    "

    )

    )

    ;
  10. while

    (

    index not

    in

    legal)

    :
  11. index =

    int

    (

    input

    (

    "That is not a legal move, please try again. The legal moves are:\n

    "

    + str

    (

    legal)

    )

    )

    ;
  12. b[

    index]

    =

    h;
  13. else

    :
  14. legal =

    legalMoves(

    b)

    ;
  15. index =

    legal[

    0

    ]

    ;
  16. print

    (

    "Computer taking the position of: "

    + str

    (

    index)

    )

    ;
  17. b[

    index]

    =

    c;
  18. return

    ;

Will Win:
This function checks if the computer is able to win by placing their piece in any certain places on the board...

  1. def

    willWin(

    space,

    h,

    b)

    :
  2. 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

    ]

    )

    ;
  3. for

    combination in

    combinations:
  4. if

    (

    space in

    combination)

    :
  5. for

    com in

    combination:
  6. if

    (

    com !=

    space)

    :
  7. if

    (

    b[

    com]

    ==

    h)

    :
  8. return

    "no"

    ;
  9. return

    "yes"

    ;

Win:
The win function simply returns a final result string for the winner...

  1. def

    win(

    winner)

    :
  2. if

    (

    winner ==

    "human"

    )

    :
  3. return

    "You win! Congratulations."

    ;
  4. elif

    (

    winner ==

    "computer"

    )

    :
  5. return

    "You lose."

    ;
  6. else

    :
  7. return

    "Huh?"

    ;

In Game and Legal Move:
In Game function checks whether there are any more spaces left on the board.

Legal moves function returns where there are spaces left on the board for the computer to move to...

  1. def

    inGame(

    b)

    :
  2. if

    (

    len

    (

    legalMoves(

    b)

    )

    >

    0

    )

    :
  3. return

    "yes"

    ;
  4. return

    "no"

    ;


  5. def

    legalMoves(

    b)

    :
  6. legal =

    [

    ]

    ;
  7. c =

    0

    ;
  8. for

    square in

    b:
  9. if

    (

    square ==

    EMPTY)

    :
  10. legal.append

    (

    c)

    ;
  11. c +=

    1

    ;
  12. return

    legal;

Setup Board and Yes No:
The SetupBoard function simply sets each of the fields on the empty board list to our EMPTY variable to specify a blank space, ready to hold a human/computer piece.

The yes no function simply asks the human for a yes/no answer to a question and returns the input...

  1. def

    setup_board(

    b)

    :
  2. counter =

    0

    ;
  3. while

    (

    counter <

    SIZE)

    :
  4. b.append

    (

    EMPTY)

    ;
  5. counter +=

    1

    ;
  6. return

    ;


  7. def

    yes_no(

    q)

    :
  8. i =

    input

    (

    "\n

    "

    + q + " (y/n)\n

    "

    )

    ;
  9. while

    (

    i is

    not

    "y"

    and

    i is

    not

    "Y"

    and

    i is

    not

    "N"

    and

    i is

    not

    "n"

    )

    :
  10. i =

    input

    (

    "\n

    "

    + q + "(y/n)\n

    "

    )

    ;
  11. return

    i.lower

    (

    )

    ;

Finally...
Finally we start the main, game loop and then once it is over (the game has ended) we output the message "Thank you for playing!"...

  1. main(

    )

    ;
  2. print

    (

    "Thank you for playing."

    )

    ;

 

452,292

325,378

325,386

Top