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?
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
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...
- Make a 2-D array to represent a Tic-Tac-Toe game/grid. Place a couple of 'X's and...
- Make a 2-D array to represent a Tic-Tac-Toe game/grid. Place a couple of 'X's and 'O's in the grid and display it. Do some extra printing to make it look like a Tic-Tac-Toe board we are suppose to use rows and columns so just make it simple thanks!
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
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT