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:
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
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:-
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;
}
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...
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 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 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 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 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 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 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 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. 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. 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...