Question

In: Computer Science

Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes....

Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed (such as #include <stdio.h> etc).
Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players (you and a friend) take turns placing X’s and O’s onto the board. After each turn, the current board configuration should be displayed, and once a player connects three of a kind, the game should end declaring that player the winner.

class tictactoeGame
{
    public:

        char boardConfig[3][3];   // two dimensional array stores current board configuration  

    // Constructor:
        // set boardConfig[i][j] to be ' ' (the space character)
        // for 0<= i <= 2, 0<= j <= 2
        tictactoeGame()
        {
           //fill this in
        }

   //put an 'X' character at the given location        
        bool placeX(int x, int y)
        {
           //fill this in
        }

   //put an 'O' character at the given location       
        bool placeO(int x, int y)
        {
           //fill this in
        }

   //set all positions to character ' '.
        void clear()
        {
           //fill this in
        }

        // Return true if there are 3 'X' marks placed in a single
        // column, row, or diagnol. Return false otherwise.
        bool xWins()
        {
           //fill this in
        }

        // Return true if there are 3 'O' marks placed in a single
        // column, row, or diagnol. Return false otherwise.    
        bool oWins()
        {
           //fill this in
        }

        // Return true if there are either 3 'X' marks or 3 'O' marks
        // placed in a single column, row, or diagnol, or if the board is full.
    // Return false otherwise.
        bool gameOver()
        {
           //fill this in
        }

        // cout a nice looking picture of the board configuration
        void display()
        {
           //fill this in
        }


};

int main()
{
return 0;
}

Solutions

Expert Solution

public class tictactoegame {

    private static int state;

    private static char[][] board;

    

    public static void main(String[] args) {

        //variables and initializing

        board = new char[3][3];

        initializeArray();

        

        //start playing

        while(!won()) {

            move();

        }

        if(won()) {

            if(state%2 == 0) System.out.println("Player 1 won");

            else System.out.println("Player 2 won");

        }

    }

    

    //method to initialize 3x3 2D array

    public static char[][] initializeArray(){

        state = 0;

        for(int i=0; i<=2; i++) {

            for(int j=0; j<=2; j++) {

                board[i][j] = '-';

            }

        }

        

        return board;

    }

    

    public static boolean isBoardFull() {

        for(int i = 0; i<=2; i++) {

            for(int j = 0; j<=2; j++) {

                if(board[i][j] == '-') return false;

            }

        }

        return true;

    }

    

    

    //method to determine the move the current player entered

        //validate the move in this method

    public static int move() {

        Scanner sc = new Scanner(System.in);

        while(!isBoardFull() && state!=9) {

            System.out.println("Player, enter a row");

            int row = sc.nextInt();

            System.out.println("Player, enter a col");

            int col = sc.nextInt();

            if(board[row][col] == 'x' || board[row][col] == 'o') {

                System.out.println("Position filled. Try again");

                row = sc.nextInt();

                col = sc.nextInt();

            }

            if(board[row][col] == '-') {

                if(state%2==0) {

                    board[row][col] = 'x';

                }

                else board[row][col] = 'o';

            }

            printBoard();

            state++;

        }

        if(draw()) System.out.println("Game ended in draw");

        sc.close();

        return state;

    }

        

    //method to update the game immediately if the game has been won

    public static boolean won() {

        boolean won = false;

        if(draw()) System.out.println("Game Over - No one wins");

        for(int i=0; i<3; i++) {

            if(board[i][0] == board[i][1] && board[i][1] == board[i][2]) {

                System.out.println("We have a winner!");

                if(board[i][0] == 'x') System.out.println("Player 1 wins");

                else System.out.println("Player 2 wins");

                won = true;

            }

        }

        for(int i=0; i<3; i++) {

            if(board[0][i] == board[1][i] && board[2][i] == board[1][i]) {

                System.out.println("We have a winner!");

                if(board[0][i] == 'x') System.out.println("Player 1 wins");

                else System.out.println("Player 2 wins");

                won = true;

            }

        }

        

        if(board[0][0]==board[1][1] && board[1][1] == board[2][2]) {

            System.out.println("We have a winner!");

            if(board[0][0] == 'x') System.out.println("Player 1 wins");

            else System.out.println("Player 2 wins");

            won = true;

        }

        

        if(board[2][0]==board[1][1] && board[0][2] == board[1][1]) {

            System.out.println("We have a winner!");

            if(board[2][0] == 'x') System.out.println("Player 1 wins");

            else System.out.println("Player 2 wins");

            won = true;

        }

            

        return won;

    }

    

    

    //a method to determine if the game has ended a draw

    public static boolean draw() {

        boolean draw = true;

        for(int i=0; i<=2; i++) {

            for(int j=0; j<=2; j++) {

                if(board[i][j] == '-') draw = false;

            }

        }

        return draw;

    }

    //a method to print the board

    public static void printBoard() {

         System.out.println("-------------");

         for (int i = 0; i < 3; i++) {

             System.out.print("| ");

             for (int j = 0; j < 3; j++) {

                 System.out.print(board[i][j] + " | ");

             }

             System.out.println();

             System.out.println("-------------");

         }

    }

    

}

Note: This is my version of tic tac toe game in java, just go through it and ask me if you have any doubts or queries related to the program

And please give a positive feedback if you like my answer

Happy Learning


Related Solutions

C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed. I never said what type of code I needed in a previous question. I apologize and I can't go back and change it so here is the same question with more information Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players...
How to make a 2D array Tic Tac Toe game in C?
How to make a 2D array Tic Tac Toe game in C?
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...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications for a tic tac toe game object: class tictactoeGame { public: char boardConfig[3][3]; // two dimensional array stores current board configuration // Constructor: // set boardConfig[i][j] to be ' ' (the space character) // for 0<= i <= 2, 0<= j <= 2 tictactoeGame() { //fill this in } //put an 'X' character at the given location bool placeX(int x, int y) { //fill...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications for a tic tac toe game object: class tictactoeGame { public: char boardConfig[3][3]; // two dimensional array stores current board configuration // Constructor: // set boardConfig[i][j] to be ' ' (the space character) // for 0<= i <= 2, 0<= j <= 2 tictactoeGame() { //fill this in } //put an 'X' character at the given location bool placeX(int x, int y) { //fill...
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
Using C language Problem!! <3 x 3 tic tac toe> 2 players are doing tic tac...
Using C language Problem!! <3 x 3 tic tac toe> 2 players are doing tic tac toe alternatively. Anyone could win when they first make a bingo for any line(horizontal, vertical, diagonal) [Constraints] 1. Use 2dimensional Array(3 X 3) 2. Each elements are 1 or 2 so that can show their player1, player2's mark. 3. Inputs are 1 through 9 as you can see 1 2 3 4 5 6 7 8 9 4. No inputs are duplicated 5. if...
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...
In a game of Tic Tac Toe, two players take turns making an available cell in...
In a game of Tic Tac Toe, two players take turns making 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 stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT