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

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
I am having an issue with the Java program with a tic tac toe. it isn't...
I am having an issue with the Java program with a tic tac toe. it isn't a game. user puts in the array and it prints out this. 1. Write a method print that will take a two dimensional character array as input, and print the content of the array. This two dimensional character array represents a Tic Tac Toe game. 2. Write a main method that creates several arrays and calls the print method. Below is an example of...
Python Code Needed Two-Player, Two-Dimensional Tic-Tac-Toe Write a script to play two-dimensional Tic-Tac-Toe between two human...
Python Code Needed Two-Player, Two-Dimensional Tic-Tac-Toe Write a script to play two-dimensional Tic-Tac-Toe between two human players who alternate entering their moves on the same computer. Create a 3-by-3 two-dimensional array. Each player indicates their moves by entering a pair of numbers representing the row and column indices of the square in which they want to place their mark, either an 'X' or an 'O'. When the first player moves, place an 'X' in the specified square. When the second...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT