Question

In C++. Write a class named FBoard for playing a game, where player x is trying to get her piece to row 7 and player o i...

In C++. Write a class named FBoard for playing a game, where player x is trying to get her piece to row 7 and player o is trying to make it so player x doesn't have any legal moves. It should have:

  • An 8x8 array of char for tracking the positions of the pieces.
  • A data member called gameState that holds one of the following values: X_WON, O_WON, or UNFINISHED - use an enum type for this, not string (the enum definition should go in Board.hpp, before the class, not inside it).
  • Data members to keep track of where the x piece is.
  • A default constructor that initializes the array to empty (you can use whatever character you want to represent empty). It should put four o pieces on row 7, in columns 0, 2, 4, and 6. It should put an x piece on row 0, column 3. It should also initialize the other data members.
  • A method called getGameState that just returns the value of gameState.
  • A method called moveX that takes as parameters the row and column of the square to move to. If the desired move is not allowed, or if the game has already been won, it should just return false. Otherwise it should make the move and return true. A piece belonging to x can move 1 square diagonally in any direction. A piece is not allowed to move off the board or to an occupied square. If x's move gets her piece to row 7, gameState should be set to X_WON.
  • A method called moveO that takes as parameters the row and column to move from, and the row and column to move to. If the first pair of coordinates doesn't hold o's piece, or if the desired move is not allowed, or if the game has already been won, it should just return false. Othewise it should make the move and return true. A piece belonging to o can move 1 square diagonally, but the row cannot increase, so any o piece has at most two available moves. For example, if player o has a piece at (5, 2), it could move to (4, 1) or (4, 3), but not (6, 1) or (6, 3). It is not allowed to move off the board or to an occupied square. If o's move leaves no legal move for x, gameState should be set to O_WON.

You do not need to track whose turn it is. Either move method can be called multiple times in a row.

It doesn't matter which index of the array you consider the row and which you consider the column as long as you're consistent.

Feel free to add private helper functions if you want. You may also find it useful to add a public print function to help with debugging.

Do not access the array out of bounds.  Make sure values are in bounds before using them to index into the array.

Here's a very simple example of how the class could be used:

   FBoard fb;
   fb.moveX(1,4);
   fb.moveX(2,5);
   fb.moveO(7,0,6,1);
   fb.getGameState();

The files must be named: FBoard.hpp and FBoard.cpp

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Since we do not need to find an algorithm or simulate the algorithm (which would have been fun kind of) as far as I can understand the question, so we need to just set up the base for the question using just a class in c++. The implementation would be straightforward as has been pointed out by the question, so the code will be easy to read where I am just doing the things as mentioned in the problem statement. However, if you have any kind of problem understanding the code just leave a comment down below and I will be more than happy to help you out. I will write out a written documentary for the code since it is quite big and some of the things need worth mentioning. So here goes the explanation:-

  • First, I have declared all the necessary enums, enum variable, pos array, and variable storing the position of 'X'.
  • Then the constructor initializes the array with null, sets the position for 'O's and 'X's and also updates the position variables of 'X's. The method getGameState() just prints the current state of the game.
  • The movex() function moves the 'X' along the four diagonals in a straight direction and checks if the destination position is present or not in its path. If present we update the current coordinate with the final coordinate of 'X' and here we also check if 'X' has reached the row number 7 or not, if it has reached then we update the state of the game to "X_WON".
  • The moveo() function moves the 'O' along the upper diagonals in a straight manner but before that, we check if the current position we at contains the variable 'o' in the array pos or not because remember that here we are providing to different coordinates as there is more than one 'o' present in the grid. So we check if the initial position has 'o' or not if it has then we check if we can move accordingly the 'o' to the final position. After that, we do a simple check that, after all these actions have taken place we can still move 'X' from its position legally or not by checking its diagonal direction of step length 1.

I hope I explained it to you in a clear fashion and I hope the code will make a much clearer idea. However, if you still have any doubts just leave a comment down below and I will be more than happy to help you out anytime. Much talk here's the code for you, Thank you. in c++.'

#include <bits/stdc++.h> // imports all necessary headers

using namespace std;

class FBoard
{
public:
   enum gamestate{X_WON, O_WON, UNFINISHED};
   enum gamestate state;
   char pos[8][8];
   int curx_x, curx_y; // data member to point the initial position of x
   FBoard()
   {
       // initializing the pos array.
       for(int i = 0; i < 8; i++)
           for(int j = 0; j < 8; j++)
               pos[i][j] = '\0';
       // initial position of O cells.
       pos[7][0] = 'o';
       pos[7][2] = 'o';
       pos[7][4] = 'o';
       // initial position of X cells.
       pos[7][6] = 'o';
       pos[0][3] = 'x';
       // initial state value
       state = UNFINISHED;
       // initial position of x
       curx_x = 0;
       curx_y = 3;
   }
   string getGameState()
   {
       if(state == X_WON)
           return "X_WON";
       if(state == O_WON)
           return "O_WON";
       if(state == UNFINISHED)
           return "UNFINISHED";
   }
   bool moveX(int x, int y)
   {
       if(state == X_WON || state == O_WON)
           return false;
       bool ok = false;
       int tx = curx_x - 1;
       int ty = curx_y + 1;
       while(tx >= 0 && ty < 8)
       {
           if(pos[tx][ty] == '\0' && (tx == x && ty == y))
           {
               pos[curx_x][curx_y] = '\0';
               pos[tx][ty] = 'x';
               curx_x = x;
               curx_y = y;
               ok = true;
               if(tx == 7)
                   state = X_WON;
               break;
           }
           tx -= 1;
           ty += 1;

       }
       if(ok)
           return true;
       tx = curx_x - 1;
       ty = curx_y - 1;
       while(tx >= 0 && ty >= 0)
       {
           if(pos[tx][ty] == '\0' && (tx == x && ty == y))
           {
               pos[curx_x][curx_y] = '\0';
               pos[tx][ty] = 'x';
               curx_x = x;
               curx_y = y;
               ok = true;
               if(tx == 7)
                   state = X_WON;
               break;
           }
           tx -= 1;
           ty -= 1;

       }
       if(ok)
           return true;
       tx = curx_x + 1;
       ty = curx_y - 1;
       while(tx < 8 && ty >= 0)
       {
           if(pos[tx][ty] == '\0' && (tx == x && ty == y))
           {
               pos[curx_x][curx_y] = '\0';
               pos[tx][ty] = 'x';
               curx_x = x;
               curx_y = y;
               ok = true;
               if(tx == 7)
                   state = X_WON;
               break;
           }
           tx += 1;
           ty -= 1;
       }
       if(ok)
           return true;
       tx = curx_x + 1;
       ty = curx_y + 1;
       while(tx < 8 && ty < 8)
       {
           if(pos[tx][ty] == '\0' && (tx == x && ty == y))
           {
               pos[curx_x][curx_y] = '\0';
               pos[tx][ty] = 'x';
               curx_x = x;
               curx_y = y;
               ok = true;
               if(tx == 7)
                   state = X_WON;
               break;
           }
           tx += 1;
           ty += 1;
       }
       if(ok)
           return true;
       return false;
   }
   bool moveO(int from_x, int from_y, int to_x, int to_y)
   {
       if(state == X_WON || state == O_WON)
           return false;
       if(pos[from_x][from_y] != 'o')
           return false;
       bool ok = false;
       int tx = from_x - 1;
       int ty = from_y + 1;
       while(tx >= 0 && ty < 8)
       {
           if(pos[tx][ty] == '\0' && (tx == to_x && ty == to_y))
           {
               pos[from_x][from_y] = '\0';
               pos[tx][ty] = 'x';
               ok = true;
               if(tx == 7)
                   state = X_WON;
               break;
           }
           tx -= 1;
           ty += 1;

       }
       if(ok)
           return true;
       tx = from_x - 1;
       ty = from_y - 1;
       while(tx >= 0 && ty >= 0)
       {
           if(pos[tx][ty] == '\0' && (tx == to_x && ty == to_y))
           {
               pos[from_x][from_y] = '\0';
               pos[tx][ty] = 'x';
               ok = true;
               if(tx == 7)
                   state = X_WON;
               break;
           }
           tx -= 1;
           ty -= 1;
       }
       if(ok)
           return true;
       // checking for any available legal moves for x or not.
       int kount = 0;
       if(curx_x - 1 >= 0 && curx_y + 1 < 8)
       {
           if(pos[curx_x - 1][curx_y + 1] != '\0')
               kount++;
       }
       if(curx_x - 1 >= 0 && curx_y - 1 >= 0)
       {
           if(pos[curx_x - 1][curx_y - 1] != '\0')
               kount++;
       }
       if(curx_x + 1 < 8 && curx_y - 1 >= 0)
       {
           if(pos[curx_x + 1][curx_y - 1] != '\0')
               kount++;
       }
       if(curx_x + 1 < 8 && curx_y + 1 < 8)
       {
           if(pos[curx_x + 1][curx_y + 1] != '\0')
               kount++;
       }
       if(kount == 4)
       {
           state = O_WON;
       }
   }
};

int main()
{

   return 0;  
}

Add a comment
Know the answer?
Add Answer to:
In C++. Write a class named FBoard for playing a game, where player x is trying to get her piece to row 7 and player o i...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a class named FBoard for playing a game... PLEASE USE C++ PLEASE DO NOT USE "THIS -->". NOT ALLOWED PLE...

    Write a class named FBoard for playing a game... PLEASE USE C++ PLEASE DO NOT USE "THIS -->". NOT ALLOWED PLEASE PROVIDE COMMENTS AND OUTPUT! Write a class named FBoard for playing a game, where player x is trying to get her piece to row 7 and player o is trying to make it so player x doesn't have any legal moves. It should have: An 8x8 array of char for tracking the positions of the pieces. A data member...

  • Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use...

    Tic-Tac-Toe Game 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...

  • Using C Programming: (use printf, scanf) 18. Tic-Tac-Toc Game Write a program that allows two players...

    Using C Programming: (use printf, scanf) 18. Tic-Tac-Toc Game Write a program that allows two players to play a game of tic-tac-toc. 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 Displays the contents of the board array Allows player 1 to select a location on the board for an X. The program should...

  • I need screenshots for this solution done in Flowgorithm. Thank you. Tic-Tac-Toe Game Design a program...

    I need screenshots for this solution done in Flowgorithm. Thank you. Tic-Tac-Toe Game Design a program that allows two players to play a game of tic-tac-toe. Use a two- dimensional String 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: a. Displays the contents of the board array. b. Allows player 1 to select a location...

  • (C++) Please create a tic tac toe game. Must write a Player class to store each...

    (C++) Please create a tic tac toe game. Must write a Player class to store each players’ information (name and score) and to update their information (increase their score if appropriate) and to retrieve their information (name and score).The players must be called by name during the game. The turns alternate starting with player 1 in the first move of round 1. Whoever plays last in a round will play second in the next round (assuming there is one).The rows...

  • This is an advanced version of the game tic tac toe, where the player has to...

    This is an advanced version of the game tic tac toe, where the player has to get five in a row to win. The board is a 30 x 20. Add three more functions that will check for a win vertically, diagonally, and backwards diagonally. Add an AI that will play against the player, with random moves. Bound check each move to make sure it is in the array. Use this starter code to complete the assignment in C++. Write...

  • Please help me write a Pseudocode (please do NOT answer in JAVA or Python - I...

    Please help me write a Pseudocode (please do NOT answer in JAVA or Python - I will not be able to use those). Please ALSO create a flowchart using Flowgarithm program. Thank you so much, if answer is provided in Pseudocode and Flowchart, I will provide good feedback and thumbs up to the resonder. Thank you! Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use a two- dimensional char array with three rows...

  • 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...

  • Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game....

    Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game. The objective of this project is to demonstrate your understanding of various programming concepts including Object Oriented Programming (OOP) and design. Tic Tac Toe is a two player game. In your implementation one opponent will be a human player and the other a computer player. ? The game is played on a 3 x 3 game board. ? The first player is known as...

  • Code in JAVA You are asked to implement “Connect 4” which is a two player game....

    Code in JAVA You are asked to implement “Connect 4” which is a two player game. The game will be demonstrated in class. The game is played on a 6x7 grid, and is similar to tic-tac-toe, except that instead of getting three in a row, you must get four in a row. To take your turn, you choose a column, and slide a checker of your color into the column that you choose. The checker drops into the slot, falling...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT