Nov 26, 2017 at 7:29am UTC
Thank you!!! I actually just did figured it out but I did it a completely different way. Now I cant get my counter to work properly. Kind of stressing because i'm nearing my due date. I don't know why the counter is ending after only two rounds even though it is set to 14.
#include <iostream>
#include <process.h>
#include <string>
using namespace std;
const int ROWS = 3;
const int COLS = 3;
void gameplay1(char array[][COLS], int);
void gameplay2(char array[][COLS], int);
void board(char array[][COLS],int);
int main()
{
int count = 0;
char array[3][3] = { '*','*','*','*','*','*','*','*','*' };
cout << "Welcome to Tic Tac Toe!" << endl;
board(array, ROWS);
for (int count = 1; count < 14; count++)
{
gameplay1(array, ROWS), count++;
gameplay2(array, ROWS), count++;
if (count = 14)
cout << "Game Draw! No spaces left." << endl;
system("pause");
exit(0);
}
}
void board(char array[][COLS],int rows)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < rows; j++)
{
cout << array[i][j] << " ";
}
cout << endl;
}
}
void gameplay1(char array[][COLS], int rows)
{
int row;
int col;
int count = 0;
cout << endl;
cout << "Player 1 you are X." << endl;
cout << "Please pick row number." << endl;
cin >> row;
cout << "Please pick a column number." << endl;
cin >> col;
if (row == 0 || row == 1 || row == 2 || col == 0 || col == 1 || col == 2)
{
if (array[row][col] == '*')
{
array[row][col] = 'X';
board(array, ROWS);
}
else
{
cout << "This position is taken, please pick another" << endl;
return gameplay1(array, ROWS);
}
if (array[0][0] == 'X' & array[0][1] == 'X' & array[0][2] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}
else if (array[1][0] == 'X' && array[1][1] == 'X' && array[1][2] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}
else if (array[2][0] == 'X' && array[2][1] == 'X' && array[2][2] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}
else if (array[0][0] == 'X' && array[1][0] == 'X' && array[2][0] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}
else if (array[0][1] == 'X' && array[1][1] == 'X' && array[2][1] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}
else if (array[0][0] == 'X' && array[1][0] == 'X' && array[2][0] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}
else if (array[0][0] == 'X' && array[1][1] == 'X' && array[2][2] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}
else if (array[0][2] == 'X' && array[1][1] == 'X' && array[2][2] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}
}
else
{
cout << " INVALID ENTRY" << endl;
return gameplay1(array, ROWS);
}
}
void gameplay2(char array[][COLS], int)
{
int row;
int col;
cout << "Player 2, you are O." << endl;
cout << "Please pick row number." << endl;
cin >> row;
cout << "Please pick a column number." << endl;
cin >> col;
if (row == 0 || row == 1 || row == 2 || col == 0 || col == 1 || col == 2)
{
if (array[row][col] == '*')
{
array[row][col] = 'O';
board(array, ROWS);
}
else
{
cout << "This position is taken, please pick another" << endl;
return gameplay2(array, ROWS);
}
if (array[0][0] == 'Y' & array[0][1] == 'Y' & array[0][2] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}
else if (array[1][0] == 'X' & array[1][1] == 'X' & array[1][2] == 'X')
{
cout << "Y has won." << endl;
exit(0);
}
else if (array[2][0] == 'Y' & array[2][1] == 'Y' & array[2][2] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}
else if (array[0][0] == 'Y' & array[1][0] == 'Y' & array[2][0] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}
else if (array[0][1] == 'Y' & array[1][1] == 'Y' & array[2][1] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}
else if (array[0][0] == 'Y' & array[1][0] == 'Y' & array[2][0] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}
else if (array[0][0] == 'Y' & array[1][1] == 'Y' & array[2][2] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}
else if (array[0][2] == 'Y' & array[1][1] == 'Y' & array[2][2] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}
}
else
{
cout << " INVALID ENTRY" << endl;
return gameplay2(array, ROWS);
}
}
Last edited on Nov 26, 2017 at 7:36am UTC
Nov 26, 2017 at 9:21am UTC
I see a few issues.
if (count = 14) You are actually setting count to 14. It should be if (count ==14), you use == to check for equality.
Also you need to look at the placement of your braces {}. The way you have them now the exit (0) command will execute the first time through your loop and exit the program. I think you need brackets around the instructions you want to execute after the nested if statement like :
if (count == 14){
cout << "....";
system("pause");
exit (0);
} //this closes the nested or second if statement
} // this closes the first if loop statement
return 0; //returns 0 to the main function, signifies program ended successfully
} // closes main function
Last edited on Nov 26, 2017 at 9:31am UTC
Nov 26, 2017 at 6:52pm UTC
Thank you! so I changed what you said but I think I have the counter wrong, it needs to go 9 rounds. The game is tic tac toe so after the 9th turn you have ran out of spots to place the X or O. So after 9 turns I would like to prompt the users that the game is a draw, however The count either ends at 8 turns and not 9 no matter what I try.
#include <iostream>
#include <process.h>
#include <string>
using namespace std;
const int ROWS = 3;
const int COLS = 3;
void gameplay1(char array[][COLS], int);
void gameplay2(char array[][COLS], int);
void board(char array[][COLS],int);
int main()
{
int count = 0;
char array[3][3] = { '*','*','*','*','*','*','*','*','*' };
cout << "Welcome to Tic Tac Toe!" << endl;
board(array, ROWS);
for (int count = 1; count < 13; count++)
{
gameplay1(array, ROWS), count++;
gameplay2(array, ROWS), count++;
if (count == 13)
{
cout << "Game Draw! No spaces left." << endl;
system("pause");
exit(0);
}
}
}
void board(char array[][COLS],int rows)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < rows; j++)
{
cout << array[i][j] << " ";
}
cout << endl;
}
}
void gameplay1(char array[][COLS], int rows)
{
int row;
int col;
int count = 0;
cout << endl;
cout << "Player 1 you are X." << endl;
cout << "Please pick row number." << endl;
cin >> row;
cout << "Please pick a column number." << endl;
cin >> col;
if (row == 0 || row == 1 || row == 2 || col == 0 || col == 1 || col == 2)
{
if (array[row][col] == '*')
{
array[row][col] = 'X';
board(array, ROWS);
}
else
{
cout << "This position is taken, please pick another" << endl;
return gameplay1(array, ROWS);
}
if (array[0][0] == 'X' & array[0][1] == 'X' & array[0][2] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}
else if (array[1][0] == 'X' && array[1][1] == 'X' && array[1][2] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}
else if (array[2][0] == 'X' && array[2][1] == 'X' && array[2][2] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}
else if (array[0][0] == 'X' && array[1][0] == 'X' && array[2][0] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}
else if (array[0][1] == 'X' && array[1][1] == 'X' && array[2][1] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}
else if (array[0][0] == 'X' && array[1][0] == 'X' && array[2][0] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}
else if (array[0][0] == 'X' && array[1][1] == 'X' && array[2][2] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}
else if (array[0][2] == 'X' && array[1][1] == 'X' && array[2][2] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}
}
else
{
cout << " INVALID ENTRY" << endl;
return gameplay1(array, ROWS);
}
}
void gameplay2(char array[][COLS], int)
{
int row;
int col;
cout << "Player 2, you are O." << endl;
cout << "Please pick row number." << endl;
cin >> row;
cout << "Please pick a column number." << endl;
cin >> col;
if (row == 0 || row == 1 || row == 2 || col == 0 || col == 1 || col == 2)
{
if (array[row][col] == '*')
{
array[row][col] = 'O';
board(array, ROWS);
}
else
{
cout << "This position is taken, please pick another" << endl;
return gameplay2(array, ROWS);
}
if (array[0][0] == 'Y' & array[0][1] == 'Y' & array[0][2] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}
else if (array[1][0] == 'X' & array[1][1] == 'X' & array[1][2] == 'X')
{
cout << "Y has won." << endl;
exit(0);
}
else if (array[2][0] == 'Y' & array[2][1] == 'Y' & array[2][2] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}
else if (array[0][0] == 'Y' & array[1][0] == 'Y' & array[2][0] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}
else if (array[0][1] == 'Y' & array[1][1] == 'Y' & array[2][1] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}
else if (array[0][0] == 'Y' & array[1][0] == 'Y' & array[2][0] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}
else if (array[0][0] == 'Y' & array[1][1] == 'Y' & array[2][2] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}
else if (array[0][2] == 'Y' & array[1][1] == 'Y' & array[2][2] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}
}
else
{
cout << " INVALID ENTRY" << endl;
return gameplay2(array, ROWS);
}
}
Nov 27, 2017 at 5:16am UTC
Try this, I changed you for loop to a while loop and changed the way your count variable is incremented.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
int main()
{
unsigned int count = 0;
char array[3][3] = { '*' ,'*' ,'*' ,'*' ,'*' ,'*' ,'*' ,'*' ,'*' };
cout << "Welcome to Tic Tac Toe!" << endl;
board(array, ROWS);
while (true )
{
gameplay1(array, ROWS), ++count;
if (count == 9)
{
cout << "Game Draw! No spaces left." << endl;
system("pause" );
exit(0);
}
gameplay2(array, ROWS), ++count;
}
return 0;
}
Last edited on Nov 27, 2017 at 7:49pm UTC