Question: Hello! I'm programming this code where two players can play a tic tac toe game. It alternates back and forth until they win tie or
Hello! I'm programming this code where two players can play a tic tac toe game. It alternates back and forth until they win tie or lose. Afterwards it's recorded into the system. If the user chooses to play more games, then the possibility is offered. If not, then the game displays the wins losses and "cats" a.k.a. ties. So far I've had a few issues with the program.
1. The array meant to maintain the x's and o's on the board keeps resetting.
2. The players turn doesn't switch from player 1 to player 2.
I'm not sure if there were any other problems because that's as much as I was able to test. Thank you for your time!
#include
using namespace std;
class Board {
private:
int space;
int playerturn;
int win;
int cats;
int loss;
char continuecheck;
char boardspaces[3][3];
public:
Board () {
space = 0;
playerturn = 2;
win = 0;
cats = 0;
loss = 0;
continuecheck = 'a';
boardspaces[2][2] = '9';
boardspaces[2][1] = '8';
boardspaces[2][0] = '7';
boardspaces[1][2] = '6';
boardspaces[1][1] = '5';
boardspaces[1][0] = '4';
boardspaces[0][2] = '3';
boardspaces[0][1] = '2';
boardspaces[0][0] = '1';
}
Board(int n_space, int n_playerturn, int n_win, int n_cats, int n_loss, char n_continuecheck) {
space = n_space;
playerturn = n_playerturn;
win = n_win;
cats = n_cats;
loss = n_loss;
continuecheck = n_continuecheck;
}
int drawboard(int playerchoice) {
if (playerchoice == 1) {
boardspaces[0][0] = 'x';
}
else if (playerchoice == -1) {
boardspaces[0][0] = 'o';
}
else if (playerchoice == 2) {
boardspaces[0][1] = 'x';
}
else if (playerchoice == -2) {
boardspaces[0][1] = 'o';
}
else if (playerchoice == 3) {
boardspaces[0][2] = 'x';
}
else if (playerchoice == -3) {
boardspaces[0][2] = 'o';
}
else if (playerchoice == 4) {
boardspaces[1][0] = 'x';
}
else if (playerchoice == -4) {
boardspaces[1][0] = 'o';
}
else if (playerchoice == 5) {
boardspaces[1][1] = 'x';
}
else if (playerchoice == -5) {
boardspaces[1][1] = 'o';
}
else if (playerchoice == 6) {
boardspaces[1][2] = 'x';
}
else if (playerchoice == -6) {
boardspaces[1][2] = 'o';
}
else if (playerchoice == 7) {
boardspaces[2][0] = 'x';
}
else if (playerchoice == -7) {
boardspaces[2][0] = 'o';
}
else if (playerchoice == 8) {
boardspaces[2][1] = 'x';
}
else if (playerchoice == -8) {
boardspaces[2][1] = 'o';
}
else if (playerchoice == 9) {
boardspaces[2][2] = 'x';
}
else if (playerchoice == -9) {
boardspaces[2][2] = 'o';
}
cout << boardspaces[2][0] << " | " << boardspaces[2][1] << " | " << boardspaces[2][2] << endl;
cout << "--------- ";
cout << boardspaces[1][0] << " | " << boardspaces[1][1] << " | " << boardspaces[1][2] << endl;
cout << "--------- ";
cout << boardspaces[0][0] << " | " << boardspaces[0][1] << " | " << boardspaces[0][2] << endl;
}
int checkwin() {
int xcheck = 0;
int ocheck = 0;
if ((boardspaces[2][0] == boardspaces[2][1]) && (boardspaces[2][1] == boardspaces[2][2])) {
if (boardspaces[2][0] == 'x') {
xcheck = 1;
}
else {
ocheck = 1;
}
}
else if ((boardspaces[1][0] == boardspaces[1][1]) && (boardspaces[1][1] == boardspaces[1][2])) {
if (boardspaces[1][0] == 'x') {
xcheck = 1;
}
else {
ocheck = 1;
}
}
else if ((boardspaces[0][0] == boardspaces[0][1]) && (boardspaces[0][1] == boardspaces[0][2])) {
if (boardspaces[0][0] == 'x') {
xcheck = 1;
}
else {
ocheck = 1;
}
}
else if ((boardspaces[2][0] == boardspaces[1][0]) && (boardspaces[1][0] == boardspaces[0][0])) {
if (boardspaces[2][0] == 'x') {
xcheck = 1;
}
else {
ocheck = 1;
}
}
else if ((boardspaces[2][1] == boardspaces[1][1]) && (boardspaces[1][1] == boardspaces[0][1])) {
if (boardspaces[2][1] == 'x') {
xcheck = 1;
}
else {
ocheck = 1;
}
}
else if ((boardspaces[0][0] == boardspaces[0][1]) && (boardspaces[0][1] == boardspaces[0][2])) {
if (boardspaces[0][0] == 'x') {
xcheck = 1;
}
else {
ocheck = 1;
}
}
else if ((boardspaces[2][0] == boardspaces[1][1]) && (boardspaces[1][1] == boardspaces[0][2])) {
if (boardspaces[2][0] == 'x') {
xcheck = 1;
}
else {
ocheck = 1;
}
}
else if ((boardspaces[0][0] == boardspaces[1][1]) && (boardspaces[1][1] == boardspaces[2][2])) {
if (boardspaces[0][0] == 'x') {
xcheck = 1;
}
else {
ocheck = 1;
}
}
int n_cats = 0;
if ((boardspaces[2][2] == 'x' || boardspaces[2][2] == 'o') && (boardspaces[2][1] == 'x' || boardspaces[2][1] == 'o') && (boardspaces[2][0] == 'x' || boardspaces[2][0] == 'o')
&& (boardspaces[1][2] == 'x' || boardspaces[1][2] == 'o') && (boardspaces[1][1] == 'x' || boardspaces[1][1] == 'o') && (boardspaces[1][0] == 'x' || boardspaces[1][0] == 'o')
&& (boardspaces[0][2] == 'x' || boardspaces[0][2] == 'o') && (boardspaces[0][1] == 'x' || boardspaces[0][1] == 'o') && (boardspaces[0][0] == 'x' || boardspaces[0][0] == 'o')) {
n_cats = 1;
cout << "It's a tie!";
}
int n_win = 0;
int n_loss = 0;
if (xcheck == 1) {
n_win = 1;
cout << "Player 1 wins!";
}
else if (ocheck ==1) {
n_loss = 1;
cout << "Player 2 wins!";
}
win += n_win;
loss += n_loss;
cats += n_cats;
int game_check;
game_check = n_win + n_loss + n_cats;
return game_check;
}
char checkcontinue() {
char n_continuecheck;
while (1) {
cout << "Do you wish to continue? (y/n) ";
if (cin >> n_continuecheck) {
if (n_continuecheck == 'y' || n_continuecheck =='n') {
return n_continuecheck;
}
else {
cout << "Try again ";
cin.clear();
cin.ignore(4096, ' ');
}
} else {
cout << "Try again ";
cin.clear();
cin.ignore(4096, ' ');
}
}
}
void endgame() {
cout << " Player 1 ";
cout << "Wins: " << win << " Losses: " << loss << " Cats: " << cats << endl << endl;
cout << "Player 2 ";
cout << "Wins: " << loss << " Losses: " << win << " Cats: " << cats;
}
int playercheck(int n_playerturn) {
if (n_playerturn == 2) {
cout << "It's Player 1's Turn! " << "Choose a tile between 1-9 that is not occupied: ";
n_playerturn = 1;
}
else if (n_playerturn == 1) {
cout << "It's Player 2's Turn! " << "Choose a tile between 1-9 that is not occupied: ";
n_playerturn = 2;
}
playerturn = n_playerturn;
return playerturn;
}
int playerchoice(int playerturn) {
int playercheck;
if (playerturn = 1) {
playercheck = 1;
}
else {
playercheck = -1;
}
int tilenumber;
for (int counter = 1; counter > 0; --counter) {
if (cin >> tilenumber) {
if (tilenumber >= 1 && tilenumber <= 9) {
if (tilenumber == 1) {
if (boardspaces[0][0] == 'x' || boardspaces[0][0] == 'o') {
cout << "This spot has already been taken, try again";
counter += 100;
continue;
}
}
else if (tilenumber == 2) {
if (boardspaces[0][1] == 'x' || boardspaces[0][1] == 'o') {
cout << "This spot has already been taken, try again";
counter += 100;
continue;
}
}
else if (tilenumber == 3) {
if (boardspaces[0][2] == 'x' || boardspaces[0][2] == 'o') {
cout << "This spot has already been taken, try again";
counter += 100;
continue;
}
}
else if (tilenumber == 4) {
if (boardspaces[1][0] == 'x' || boardspaces[1][0] == 'o') {
cout << "This spot has already been taken, try again";
counter += 100;
continue;
}
}
else if (tilenumber == 5) {
if (boardspaces[1][1] == 'x' || boardspaces[1][1] == 'o') {
cout << "This spot has already been taken, try again";
counter += 100;
continue;
}
}
else if (tilenumber == 6) {
if (boardspaces[1][2] == 'x' || boardspaces[1][2] == 'o') {
cout << "This spot has already been taken, try again";
counter += 100;
continue;
}
}
else if (tilenumber == 7) {
if (boardspaces[2][0] == 'x' || boardspaces[2][0] == 'o') {
cout << "This spot has already been taken, try again";
counter += 100;
continue;
}
}
else if (tilenumber == 8) {
if (boardspaces[2][1] == 'x' || boardspaces[2][1] == 'o') {
cout << "This spot has already been taken, try again";
counter += 100;
continue;
}
}
else if (tilenumber == 9) {
if (boardspaces[2][2] == 'x' || boardspaces[2][2] == 'o') {
cout << "This spot has already been taken, try again";
counter += 100;
continue;
}
}
}
else {
cout << "Type a number BETWEEN 1-9: ";
counter += 100;
continue;
}
}
else {
cout << "Try again, type a number between 1-9: ";
cin.clear();
cin.ignore(4096, ' ');
counter += 100;
continue;
}
}
return playercheck*tilenumber;
}
};
int main () {
int playerturn = 2;
char n_spaces[3][3] = {{'1','2','3'}, {'4','5','6'}, {'7','8','9'}};
int playerchoice = 0;
while (true) {
Board game;
game.drawboard(playerchoice);
int game_check;
game_check = game.checkwin();
char continuecheck;
if (game_check == 1) {
continuecheck = game.checkcontinue();
if (continuecheck == 'n') {
game.endgame();
break;
}
else {
n_spaces[2][2] = '9';
n_spaces[2][1] = '8';
n_spaces[2][0] = '7';
n_spaces[1][2] = '6';
n_spaces[1][1] = '5';
n_spaces[1][0] = '4';
n_spaces[0][2] = '3';
n_spaces[0][1] = '2';
n_spaces[0][0] = '1';
}
}
playerturn = game.playercheck(playerturn);
playerchoice = game.playerchoice(playerturn);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
