Question

In: Computer Science

Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...

Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below:

The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players after every successful move, and pronounce the winner.

This is the the code so far:

public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
       int[][] board = new int[3][3];
      

   }

Solutions

Expert Solution

So, here is the solution of the given problem.
I am attaching the well-commented source code along with the Screenshots of the code as well as output.

Source Code:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[][] board = new int[3][3];
        int x,y;
        boolean flag = true;

        //function to print instruction
        printInstruction();

        //run game infinitely till one player won or game ties
        while (true){
            //function to print board
            print(board);

            //to print player whose chance is there
            System.out.println("Player" + ((flag)?"1":"2"));

            //taking co-ordinated
            x = in.nextInt();
            y = in.nextInt();

            //checking if is vacant or not
            if (board[x-1][y-1] == 0){
                board[x-1][y-1] = ((flag)? 1 : 2 );
                flag = !flag;
            }
            else
                System.out.println("Invalid Move!");

            //check if player won or not
            if(check(board)){
                System.out.println("Player " + ((flag)?"2":"1") + " won.");
                break;
            }
            //to check if game ties or not
            if(completeFill(board)) {
                System.out.println("Game Tie");
                break;
            }
        }

    }
//to check board is completely filled and no player won
    private static boolean completeFill(int[][] board){
        for(int i = 0;i<3;i++){
            for(int j = 0;j<3;j++){
                if(board[i][j] != 0)
                    return false;
            }
        }
        return true;
    }
//to print instructions
    private static void printInstruction() {
        System.out.println("1. You have to enter row number (1-3) and column number (1-3) to place your element: ");
        System.out.println("2. Game will start from player 1 and one by one each player's turn will come.");
    }


    //function to check row is matched
    private static boolean rowCrossed(int[][] board)
    {
        for (int i=0; i<3; i++)
        {
            if (board[i][0] == board[i][1] &&
                    board[i][1] == board[i][2] &&
                    board[i][0] != 0)
                return true;
        }
        return false;
    }

    //function to check column is matched
   private static boolean columnCrossed(int[][] board)
    {
        for (int i=0; i<3; i++)
        {
            if (board[0][i] == board[1][i] &&
                    board[1][i] == board[2][i] &&
                    board[0][i] != 0)
                return true;
        }
        return false;
    }


    //function to check diagonal is matched
    private static boolean diagonalCrossed(int[][] board)
    {
        if (board[0][0] == board[1][1] &&
                board[1][1] == board[2][2] &&
                board[0][0] != 0)
            return true;

        return board[0][2] == board[1][1] &&
                board[1][1] == board[2][0] &&
                board[0][2] != 0;
    }

    //function to check if any of the condition is satisfied i.e row,column,diagonal
    private static boolean check(int[][] board){
            return rowCrossed(board) || columnCrossed(board) || diagonalCrossed(board);
    }

    //function to print board
    private static void print(int[][] board){
        for(int i = 0;i<3;i++) {
            for(int j = 0;j<3;j++)
                System.out.print(" | " + board[i][j]);
            System.out.println();
            if(i<2)
               System.out.println("--------------");
        }
    }
}

Screenshot of Source Code:

Output:

So, this was the solutions of the problem.
I hope I am able to solve your problem, if yes then do give it a like.
It really helps :)


Related Solutions

PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has...
Python: Write a program that plays Tic-tac-toe with a user. Each round, the game prints out...
Python: Write a program that plays Tic-tac-toe with a user. Each round, the game prints out the state of the board, asks the user where they would like to place their mark, and implements this decision. The program then places its own mark on a randomly chosen available position. Once one of the player won, the program declares the result and asks if the user would like to continue. The first player is selected at random.
Write a LISP program to play the game Tic-Tac-Toe on a size 4x4 game board. Your...
Write a LISP program to play the game Tic-Tac-Toe on a size 4x4 game board. Your program must use min-max search and should be invoked by the function call: > (Tic-Tac-Toe) The game is single player, human vs the computer AI.
The objective of this assignment is to implement the tic-tac-toe game with a C program. The...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with ‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of...
Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a...
Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a human, so each player makes their own moves. Follow the design below, creating the methods indicated and invoking them in the main program. Use a char array of size 9 as the board; initialize with the characters 0 to 8 so that it starts out looking something like the board on the left. 0|1|2 3|4|5 6|7|8 and then as moves are entered the board...
Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional...
Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row...
How to make tic tac toe game in javascript with vue.js
How to make tic tac toe game in javascript with vue.js
If you were to write a game of tic-tac-toe, you may store the representation of the...
If you were to write a game of tic-tac-toe, you may store the representation of the game board as a two dimensional list such as   [['X', '', 'X'], ['O', 'X', ''], ['', 'O', 'X']] where each sublist is a row in the board.   Empty strings ('') denote a space that does not yet have a value. Assuming this representation, write functions (contained in the same file) that do the following: a) Create a new empty tic-tac-toe board. This function should...
If anyone can please write a code for a 5x5 tic tac toe game in matlab...
If anyone can please write a code for a 5x5 tic tac toe game in matlab I would greatly appreciate it. Its extra credit. PLEASE HELP ME :(
How to make a 2D array Tic Tac Toe game in C?
How to make a 2D array Tic Tac Toe game in C?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT