 GFG App
Open App Browser
Continue

# Implementation of Tic-Tac-Toe game

## Python

 `# Set up the game board as a list` `board ``=` `[``"-"``, ``"-"``, ``"-"``,` `         ``"-"``, ``"-"``, ``"-"``,` `         ``"-"``, ``"-"``, ``"-"``]`   `# Define a function to print the game board` `def` `print_board():` `    ``print``(board[``0``] ``+` `" | "` `+` `board[``1``] ``+` `" | "` `+` `board[``2``])` `    ``print``(board[``3``] ``+` `" | "` `+` `board[``4``] ``+` `" | "` `+` `board[``5``])` `    ``print``(board[``6``] ``+` `" | "` `+` `board[``7``] ``+` `" | "` `+` `board[``8``])`   `# Define a function to handle a player's turn` `def` `take_turn(player):` `    ``print``(player ``+` `"'s turn."``)` `    ``position ``=` `input``(``"Choose a position from 1-9: "``)` `    ``while` `position ``not` `in` `[``"1"``, ``"2"``, ``"3"``, ``"4"``, ``"5"``, ``"6"``, ``"7"``, ``"8"``, ``"9"``]:` `        ``position ``=` `input``(``"Invalid input. Choose a position from 1-9: "``)` `    ``position ``=` `int``(position) ``-` `1` `    ``while` `board[position] !``=` `"-"``:` `        ``position ``=` `int``(``input``(``"Position already taken. Choose a different position: "``)) ``-` `1` `    ``board[position] ``=` `player` `    ``print_board()`   `# Define a function to check if the game is over` `def` `check_game_over():` `    ``# Check for a win` `    ``if` `(board[``0``] ``=``=` `board[``1``] ``=``=` `board[``2``] !``=` `"-"``) ``or` `\` `       ``(board[``3``] ``=``=` `board[``4``] ``=``=` `board[``5``] !``=` `"-"``) ``or` `\` `       ``(board[``6``] ``=``=` `board[``7``] ``=``=` `board[``8``] !``=` `"-"``) ``or` `\` `       ``(board[``0``] ``=``=` `board[``3``] ``=``=` `board[``6``] !``=` `"-"``) ``or` `\` `       ``(board[``1``] ``=``=` `board[``4``] ``=``=` `board[``7``] !``=` `"-"``) ``or` `\` `       ``(board[``2``] ``=``=` `board[``5``] ``=``=` `board[``8``] !``=` `"-"``) ``or` `\` `       ``(board[``0``] ``=``=` `board[``4``] ``=``=` `board[``8``] !``=` `"-"``) ``or` `\` `       ``(board[``2``] ``=``=` `board[``4``] ``=``=` `board[``6``] !``=` `"-"``):` `        ``return` `"win"` `    ``# Check for a tie` `    ``elif` `"-"` `not` `in` `board:` `        ``return` `"tie"` `    ``# Game is not over` `    ``else``:` `        ``return` `"play"`   `# Define the main game loop` `def` `play_game():` `    ``print_board()` `    ``current_player ``=` `"X"` `    ``game_over ``=` `False` `    ``while` `not` `game_over:` `        ``take_turn(current_player)` `        ``game_result ``=` `check_game_over()` `        ``if` `game_result ``=``=` `"win"``:` `            ``print``(current_player ``+` `" wins!"``)` `            ``game_over ``=` `True` `        ``elif` `game_result ``=``=` `"tie"``:` `            ``print``(``"It's a tie!"``)` `            ``game_over ``=` `True` `        ``else``:` `            ``# Switch to the other player` `            ``current_player ``=` `"O"` `if` `current_player ``=``=` `"X"` `else` `"X"`   `# Start the game` `play_game()`

Rules of the Game

• The game is to be played between two people (in this program between HUMAN and COMPUTER).
• One of the player chooses ‘O’ and the other ‘X’ to mark their respective cells.
• The game starts with one of the players and the game ends when one of the players has one whole row/ column/ diagonal filled with his/her respective character (‘O’ or ‘X’).
• If no one wins, then the game is said to be draw. Implementation In our program the moves taken by the computer and the human are chosen randomly. We use rand() function for this. What more can be done in the program? The program is in not played optimally by both sides because the moves are chosen randomly. The program can be easily modified so that both players play optimally (which will fall under the category of Artificial Intelligence). Also the program can be modified such that the user himself gives the input (using scanf() or cin). The above changes are left as an exercise to the readers. Winning Strategy – An Interesting Fact If both the players play optimally then it is destined that you will never lose (“although the match can still be drawn”). It doesn’t matter whether you play first or second.In another ways – “ Two expert players will always draw ”. Isn’t this interesting ?

## Cpp

 `// A C++ Program to play tic-tac-toe`   `#include` `using` `namespace` `std;`   `#define COMPUTER 1` `#define HUMAN 2`   `#define SIDE 3 // Length of the board`   `// Computer will move with 'O'` `// and human with 'X'` `#define COMPUTERMOVE 'O'` `#define HUMANMOVE 'X'`   `// A function to show the current board status` `void` `showBoard(``char` `board[][SIDE])` `{` `    ``printf``("\n\n");` `    `  `    ``printf``("\t\t\t  %c | %c  | %c  \n", board,` `                             ``board, board);` `    ``printf``("\t\t\t--------------\n");` `    ``printf``("\t\t\t  %c | %c  | %c  \n", board,` `                             ``board, board);` `    ``printf``("\t\t\t--------------\n");` `    ``printf``("\t\t\t  %c | %c  | %c  \n\n", board,` `                             ``board, board);` ` `  `    ``return``;` `}`   `// A function to show the instructions` `void` `showInstructions()` `{` `    ``printf``("\t\t\t  Tic-Tac-Toe\n\n");` `    ``printf``("Choose a cell numbered from 1 to 9 as below"` `            ``" and play\n\n");` `    `  `    ``printf``("\t\t\t  1 | 2  | 3  \n");` `    ``printf``("\t\t\t--------------\n");` `    ``printf``("\t\t\t  4 | 5  | 6  \n");` `    ``printf``("\t\t\t--------------\n");` `    ``printf``("\t\t\t  7 | 8  | 9  \n\n");` `    `  `    ``printf``("-\t-\t-\t-\t-\t-\t-\t-\t-\t-\n\n");`   `    ``return``;` `}`     `// A function to initialise the game ` `void` `initialise(``char` `board[][SIDE], ``int` `moves[])` `{` `    ``// Initiate the random number generator so that ` `    ``// the same configuration doesn't arises` `    ``srand``(``time``(NULL)); ` `    `  `    ``// Initially the board is empty` `    ``for` `(``int` `i=0; i

• Output:
```                Tic-Tac-Toe

Choose a cell numbered from 1 to 9 as below and play

1 | 2  | 3
--------------
4 | 5  | 6
--------------
7 | 8  | 9

-    -    -    -    -    -    -    -    -    -

COMPUTER has put a O in cell 6

|    |
--------------
|    | O
--------------
|    |

HUMAN has put a X in cell 7

|    |
--------------
|    | O
--------------
X |    |

COMPUTER has put a O in cell 5

|    |
--------------
| O  | O
--------------
X |    |

HUMAN has put a X in cell 1

X |    |
--------------
| O  | O
--------------
X |    |

COMPUTER has put a O in cell 9

X |    |
--------------
| O  | O
--------------
X |    | O

HUMAN has put a X in cell 8

X |    |
--------------
| O  | O
--------------
X | X  | O

COMPUTER has put a O in cell 4

X |    |
--------------
O | O  | O
--------------
X | X  | O

COMPUTER has won```