Question

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 m

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

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 then 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.moveXC1,4); fb.moveXC2,5); fb.move0(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

//FBoard.h

enum state{
   X_WON,O_WON,UNFINISHED
};
class FBoard{
   private:
       char **board;
       enum state gameState;
       int x,y;
      
   public:
       FBoard();
       enum state getGameState();
       bool moveX(int ,int);
       bool moveO(int ,int ,int ,int);
      
};

//FBoard.cpp

#include"FBoard.h"
#include<math.h>


FBoard::FBoard(){
   board = new char*[8];
   for(int i=0;i<8;i++)board[i] = new char[8];
  
   for(int i=0;i<8;i++){
       for(int j=0;j<8;j++)board[i][j]='_';
   }
   board[7][0] = 'o';
   board[7][2] = 'o';
board[7][4] = 'o';
   board[7][6] = 'o';
   board[0][3] = 'x';
  
   x=0;
   y=3;
   gameState = UNFINISHED;
}
enum state FBoard::getGameState(){
   return gameState;
}
bool FBoard::moveX(int row,int col){
   if(gameState!=UNFINISHED)return false;
   if(row<0||row>7||col<0||col>7||abs(row-x)!=1||abs(col-y)!=1||board[row][col]!='_'){
       return false;
   }
   board[x][y] = '_';
   board[row][col]='x';
   x=row;
   y=col;
   if(x==7){
       gameState = X_WON;
   }
   return true;
}

bool FBoard::moveO(int i_row,int i_col,int f_row,int f_col){
   if(gameState!=UNFINISHED){
      
       return false;
   }
   if(i_row<0||i_row>7||i_col<0||i_col>7||f_row<0||f_row>7||f_col<0||f_col>7){
  
       return false;
   }
   if(board[i_row][i_col]!='o'||board[f_row][f_col]!='_'){
       return false;
   }
   if(i_row!=f_row+1||(i_col!=f_col+1 && i_col!=f_col-1)){
      
       return false;
   }
  
   board[f_row][f_col] = 'o';
   board[i_row][i_col] = '_';
  
   if(board[x-1][y-1]=='o'&&board[x-1][y+1]=='o'&&board[x+1][y+1]=='o'&&board[x+1][y-1]=='o')gameState = O_WON;
   return true;
}

//main.cpp

#include<iostream>
#include"FBoard.cpp"
using namespace std;
int main(){
   FBoard fb;
   if(fb.moveX(1,4))cout<<"X moves to given position\n";
   else cout<<"Invalid position\n";
   if(fb.moveX(2,5))cout<<"X moves to given position\n";
   else cout<<"Invalid position\n";
   if(fb.moveO(7,0,6,1))cout<<"O moves to given position\n";
   else cout<<"Invalid position\n";
  
   cout<<fb.getGameState();
   return 0;
}

//sample output

CAUsers\IshuManish Documents\mainboard.exe moves to given position Xmoves to given position 0 moves to given position 2 Proce

Add a comment
Know the answer?
Add Answer to:
Write a class named FBoard for playing a game... PLEASE USE C++ PLEASE DO NOT USE "THIS -->". NOT ALLOWED PLE...
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
  • 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...

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

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

    18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use 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: Write . 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...

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

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

  • For this exercise, you will complete the TicTacToe Board that we started in the 2D Arrays...

    For this exercise, you will complete the TicTacToe Board that we started in the 2D Arrays Lesson. We will add a couple of methods to the TicTacToe class. To track whose turn it is, we will use a counter turn. This is already declared as a private instance variable. Create a getTurn method that returns the value of turn. Other methods to implement: printBoard()- This method should print the TicTacToe array onto the console. The board should include numbers that...

  • I need help writing a Java program for a game of TicTacToe. It is a two...

    I need help writing a Java program for a game of TicTacToe. It is a two player game, and the rules are as follows: 1. The game begins with an empty, 3 × 3 grid. 2. The two players then take turns placing a mark in an empty grid cell. Player O will use the ‘O’ (letter ‘O’, not zero) mark and Player X will use the ‘X’ mark. Player O moves first. 3. The game is over in either...

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

  • Write a JAVA program that plays a simple Pac-man game. The game plays on a 10...

    Write a JAVA program that plays a simple Pac-man game. The game plays on a 10 by 10 grid (absolutely, you can make it bigger). Pac-man is depicted as “#” and others as “*”. Please see the below.   You should ask how many pac-man plays before starting the game. Then, you should create the number of pac-man objects. The pac-mans are defined as classes, and the objects are stored in an array. The pac-man class has a “move()” method, which...

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

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