Open In App

Implementation of Tic-Tac-Toe game

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Java




import java.util.Scanner;
 
public class TicTacToe {
    // Set up the game board as an array
    static String[] board = {"-", "-", "-", "-", "-", "-", "-", "-", "-"};
 
    // Define a function to print the game board
    static void printBoard() {
        System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
        System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
        System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
    }
 
    // Define a function to handle a player's turn
    static void takeTurn(String player) {
        Scanner scanner = new Scanner(System.in);
        System.out.println(player + "'s turn.");
        System.out.print("Choose a position from 1-9: ");
        int position = scanner.nextInt() - 1;
        while (position < 0 || position > 8 || !board[position].equals("-")) {
            System.out.print("Invalid input or position already taken. Choose a different position: ");
            position = scanner.nextInt() - 1;
        }
        board[position] = player;
        printBoard();
    }
 
    // Define a function to check if the game is over
    static String checkGameOver() {
        // Check for a win
        if ((board[0].equals(board[1]) && board[1].equals(board[2]) && !board[0].equals("-")) ||
                (board[3].equals(board[4]) && board[4].equals(board[5]) && !board[3].equals("-")) ||
                (board[6].equals(board[7]) && board[7].equals(board[8]) && !board[6].equals("-")) ||
                (board[0].equals(board[3]) && board[3].equals(board[6]) && !board[0].equals("-")) ||
                (board[1].equals(board[4]) && board[4].equals(board[7]) && !board[1].equals("-")) ||
                (board[2].equals(board[5]) && board[5].equals(board[8]) && !board[2].equals("-")) ||
                (board[0].equals(board[4]) && board[4].equals(board[8]) && !board[0].equals("-")) ||
                (board[2].equals(board[4]) && board[4].equals(board[6]) && !board[2].equals("-"))) {
            return "win";
        }
        // Check for a tie
        else if (!String.join("", board).contains("-")) {
            return "tie";
        }
        // Game is not over
        else {
            return "play";
        }
    }
 
    // Define the main game loop
    public static void main(String[] args) {
        printBoard();
        String currentPlayer = "X";
        boolean gameOver = false;
        while (!gameOver) {
            takeTurn(currentPlayer);
            String gameResult = checkGameOver();
            if (gameResult.equals("win")) {
                System.out.println(currentPlayer + " wins!");
                gameOver = true;
            } else if (gameResult.equals("tie")) {
                System.out.println("It's a tie!");
                gameOver = true;
            } else {
                // Switch to the other player
                currentPlayer = currentPlayer.equals("X") ? "O" : "X";
            }
        }
    }
}


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<bits/stdc++.h>
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[0][0],
                             board[0][1], board[0][2]);
    printf("\t\t\t--------------\n");
    printf("\t\t\t  %c | %c  | %c  \n", board[1][0],
                             board[1][1], board[1][2]);
    printf("\t\t\t--------------\n");
    printf("\t\t\t  %c | %c  | %c  \n\n", board[2][0],
                             board[2][1], board[2][2]);
  
    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<SIDE; i++)
    {
        for (int j=0; j<SIDE; j++)
            board[i][j] = ' ';
    }
     
    // Fill the moves with numbers
    for (int i=0; i<SIDE*SIDE; i++)
        moves[i] = i;
 
    // randomise the moves
    random_shuffle(moves, moves + SIDE*SIDE);
     
    return;
}
 
// A function to declare the winner of the game
void declareWinner(int whoseTurn)
{
    if (whoseTurn == COMPUTER)
        printf("COMPUTER has won\n");
    else
        printf("HUMAN has won\n");
    return;
}
 
// A function that returns true if any of the row
// is crossed with the same player's move
bool rowCrossed(char board[][SIDE])
{
    for (int i=0; i<SIDE; i++)
    {
        if (board[i][0] == board[i][1] &&
            board[i][1] == board[i][2] &&
            board[i][0] != ' ')
            return (true);
    }
    return(false);
}
 
// A function that returns true if any of the column
// is crossed with the same player's move
bool columnCrossed(char board[][SIDE])
{
    for (int i=0; i<SIDE; i++)
    {
        if (board[0][i] == board[1][i] &&
            board[1][i] == board[2][i] &&
            board[0][i] != ' ')
            return (true);
    }
    return(false);
}
 
// A function that returns true if any of the diagonal
// is crossed with the same player's move
bool diagonalCrossed(char board[][SIDE])
{
    if (board[0][0] == board[1][1] &&
        board[1][1] == board[2][2] &&
        board[0][0] != ' ')
        return(true);
         
    if (board[0][2] == board[1][1] &&
        board[1][1] == board[2][0] &&
         board[0][2] != ' ')
        return(true);
 
    return(false);
}
 
// A function that returns true if the game is over
// else it returns a false
bool gameOver(char board[][SIDE])
{
    return(rowCrossed(board) || columnCrossed(board)
            || diagonalCrossed(board) );
}
 
// A function to play Tic-Tac-Toe
void playTicTacToe(int whoseTurn)
{
    // A 3*3 Tic-Tac-Toe board for playing
    char board[SIDE][SIDE];
     
    int moves[SIDE*SIDE];
     
    // Initialise the game
    initialise(board, moves);
     
    // Show the instructions before playing
    showInstructions();
     
    int moveIndex = 0, x, y;
     
    // Keep playing till the game is over or it is a draw
    while (gameOver(board) == false &&
            moveIndex != SIDE*SIDE)
    {
        if (whoseTurn == COMPUTER)
        {
            x = moves[moveIndex] / SIDE;
            y = moves[moveIndex] % SIDE;
            board[x][y] = COMPUTERMOVE;
            printf("COMPUTER has put a %c in cell %d\n",
                    COMPUTERMOVE, moves[moveIndex]+1);
            showBoard(board);
            moveIndex ++;
            whoseTurn = HUMAN;
        }
         
        else if (whoseTurn == HUMAN)
        {
            x = moves[moveIndex] / SIDE;
            y = moves[moveIndex] % SIDE;
            board[x][y] = HUMANMOVE;
            printf ("HUMAN has put a %c in cell %d\n",
                    HUMANMOVE, moves[moveIndex]+1);
            showBoard(board);
            moveIndex ++;
            whoseTurn = COMPUTER;
        }
    }
 
    // If the game has drawn
    if (gameOver(board) == false &&
            moveIndex == SIDE * SIDE)
        printf("It's a draw\n");
    else
    {
        // Toggling the user to declare the actual
        // winner
        if (whoseTurn == COMPUTER)
            whoseTurn = HUMAN;
        else if (whoseTurn == HUMAN)
            whoseTurn = COMPUTER;
         
        // Declare the winner
        declareWinner(whoseTurn);
    }
    return;
}
 
// Driver program
int main()
{
    // Let us play the game with COMPUTER starting first
    playTicTacToe(COMPUTER);
     
    return (0);
}


  • 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
  • An Interesting Variant of this game As said above, if two experienced players are playing the Tic-Tac-Toe, then the game will always draw. There is another viral variant of this game- Ultimate Tic-Tac-Toe, which aims to make the normal Tic-Tac-Toe more interesting and less predictable. Have a look at the game here- Link1 Link2 The above article implements simple Tic-Tac-Toe where moves are randomly made. Please refer below article to see how optimal moves are made. Minimax Algorithm in Game Theory | Set 3 (Tic-Tac-Toe AI – Finding optimal move) Great discussions on the “winning/never losing” strategy Quora Wikihow


Last Updated : 28 Feb, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads