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

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 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 :(
In a game of tic tac toe. How do you check if all the positions in...
In a game of tic tac toe. How do you check if all the positions in a double array are taken, the double arrays are used to store the x's and o's?
In C# A Tic Tac Toe program that uses functions and arrays. The program should start...
In C# A Tic Tac Toe program that uses functions and arrays. The program should start by asking the player one for their name and then player two for their name. Then should print the board and ask the user what column and then what row they would like to put their x (or o, depending on the player) . Use the clear function to make it look as though the board is never repeating.. Thanks!
Game of Tic Tac Toe with the following conditions A point system where a  move that leads...
Game of Tic Tac Toe with the following conditions A point system where a  move that leads to a winning game is given 1 point, a move that leads to a tie is given 0 point, and a  lead to a losing game will get -1 point. Grid size of 5x5 A timer that can be set for how long a game can be played 5 symbols in a row to get a point Connected lines cannot be crossed (No diagonal lines)...
please create a tic tac toe game with python using only graphics.py library
please create a tic tac toe game with python using only graphics.py library
Provided is code for checking horizontal and vertical win in a tic tac toe game. Please...
Provided is code for checking horizontal and vertical win in a tic tac toe game. Please provide code for checking diagonal win. function checkWinVertical(player, row, col) { var counter = 0; //each button var btn = document.getElementsByTagName("button"); for (counterC = 0; counterC < col; counterC++){ for (countR = 0; countR < row; countR++){ if (player != btn[counter].innerHTML){ counter += 1; break; } else if (countR + 1 == col) { for (countW = 0; countW < col; countW++){ btn[counter -...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT