HELP WITH TIC TAC TOE

Stuck at trying to put the user inputs into the array and displaying the board after each input, also need help with finding a winner.


/*
Designing the Board
*/
#include <iostream>
#include <string.h>
using namespace std;
const int MAXROW = 3;
const int MAXCOL = 3;
void initializeBoard(char initializeBoard[MAXROW][MAXCOL]);
void printBoard(char printBoard[MAXROW][MAXCOL]);
float player1(char initBoard[MAXROW][MAXCOL]);
float player2(char initBoard[MAXROW][MAXCOL]);

//Fills up the initial board with asterisks
void initializeBoard(char initBoard[MAXROW][MAXCOL])
{
// Loop through rows
for (int row = 0; row < MAXROW; row++)
{
// Loop through columns
for (int column = 0; column < MAXCOL; column++)
for (int i = 0; i < 7; i++)
initBoard[row][column] = i;
}
}

//Display the board
void printBoard(char theBoard[MAXROW][MAXCOL])
{
cout << "Welcome to Tic-Tac-Toe!" << endl;
cout << endl;
for (int row = 0; row < MAXROW; row++)
{
cout << " | ";
for (int column = 0; column < MAXCOL; column++)
cout << theBoard[row][column] << " | ";
cout << endl;
}
cout << endl;
cout << "Here is how to play:" << endl;
cout << "Step 1: Player 1 enter an X in a desired location" << endl;
cout << "Step 2: Player 2 enter an O in a desired location" << endl;
cout << "Step 3: Repeat until there is a winner, loser, or a draw" << endl;
cout << "Step 4: Have fun and play again!" << endl;
cout << endl;
}

float player1(char initBoard[MAXROW][MAXCOL])
{
int row = 0;
int column = 0;
cout << "Player 1 enter row number: ";
cin >> row;

while ((isdigit(row) == 0) || (int(row) < 48) || (int(row) > 51))
{
cout << "Invalid number \n";
cout << "Please enter 0, 1, 2 for tic tac toe row:";
cin >> initBoard[row][MAXCOL];
}
cout << "Player 1 enter column number: ";
cin >> column;
//input validation
while ((isdigit(column) == 0) || (int(column) < 48) || (int(column) > 51))
{
cout << "Invalid number \n";
cout << " Please enter 0, 1, 2 for tic tc toe column:";
cin >> initBoard[MAXROW][column];
}

return row, column;
}

float player2(char initBoard[MAXROW][MAXCOL])
{
char row = 0;
char column = 0;
cout << "Player 2 enter row number: ";
cin >> row;

while ((isdigit(row) == 0) || (int(row) < 48) || (int(row) > 51))
{
cout << "Invalid number \n";
cout << "Please enter 0, 1, 2 for tic tac toe row:";
cin >> row;
}
cout << "Player 2 enter column number: ";
cin >> column;
//input validation
while ((isdigit(column) == 0) || (int(column) < 48) || (int(column) > 51))
{
cout << "Invalid number \n";
cout << " Please enter 0, 1, 2 for tic tc toe column:";
cin >> column;
}
return row, column;
}



int main(char initBoard[MAXROW][MAXCOL])
{
float Player1;
float Player2;
char board[MAXROW][MAXCOL];

initializeBoard(board);
printBoard(board);
Player1 = player1(initBoard);
Player2 = player2(initBoard);

cout << endl;
system("pause");
return 0;
}
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
//Fills up the initial board with asterisks
void initializeBoard(char initBoard[MAXROW][MAXCOL])
{
	// Loop through rows
	for (int row = 0; row < MAXROW; row++)
	{
		// Loop through columns
		for (int column = 0; column < MAXCOL; column++)
			initBoard[row][column] = '*';
	}
}
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
Designing the Board
*/
#include <iostream>
#include <string.h>

using namespace std;

const int MAXROW = 3;
const int MAXCOL = 3;

void initializeBoard(char initializeBoard[MAXROW][MAXCOL]);
void printBoard(char printBoard[MAXROW][MAXCOL]);

int getRow(int player);
int getCol(int player);

int main()
{
	int Player1 = 1, row1 = 0, col1 = 0;
	int Player2 = 2, row2 = 0, col2 = 0;

	char board[MAXROW][MAXCOL];

	initializeBoard(board);
	printBoard(board);

	while (1) // tidy up later
	{
		//Player 1
		row1 = getRow(Player1);
		col1 = getCol(Player1);
		board[row1][col1] = 'O';
		printBoard(board);

		//Player 2
		row2 = getRow(Player2);
		col2 = getCol(Player2);
		board[row2][col2] = 'X';
		printBoard(board);
	}

	cout << endl;
	system("pause");
	return 0;
}

//Fills up the initial board with asterisks
void initializeBoard(char initBoard[MAXROW][MAXCOL])
{
	// Loop through rows
	for (int row = 0; row < MAXROW; row++)
	{
		// Loop through columns
		for (int column = 0; column < MAXCOL; column++)
			initBoard[row][column] = '*';
	}
}

//Display the board
void printBoard(char theBoard[MAXROW][MAXCOL])
{
	cout << "Welcome to Tic-Tac-Toe!" << endl;
	cout << endl;
	for (int row = 0; row < MAXROW; row++)
	{
		cout << " | ";
		for (int column = 0; column < MAXCOL; column++)
			cout << theBoard[row][column] << " | ";
		cout << endl;
	}
	cout << endl;
	cout << "Here is how to play:" << endl;
	cout << "Step 1: Player 1 enter an X in a desired location" << endl;
	cout << "Step 2: Player 2 enter an O in a desired location" << endl;
	cout << "Step 3: Repeat until there is a winner, loser, or a draw" << endl;
	cout << "Step 4: Have fun and play again!" << endl;
	cout << endl;
}


int getRow(int player)
{
	int row = 0;

	cout << "Player " << player << " enter row number: ";
	cin >> row;

	while (row < 0 || row >= MAXROW)
	{
		cout << "Invalid row number. Please enter 0, 1, 2 for tic tac toe row: ";
		cin >> row;
	}
	
	return row;
}

int getCol(int player)
{
	int col = 0;

	cout << "Player " << player << " enter column number: ";
	cin >> col;

	while (col < 0 || col >= MAXCOL)
	{
		cout << "Invalid column number. Please enter 0, 1, 2 for tic tac toe row: ";
		cin >> col;
	}

	return col;
}


You can simplify it dramatically further from this but it is a bit clearer this way.
Last edited on
Ok I have added to the and the only problem I am having is for the input to be between 1-9 because it is taking numbers like 10 and 111 as the first slot.

/*
Designing the Board
*/
#include <iostream>
#include <string.h>
#include <string>
using namespace std;
const int MAXROW = 3;
const int MAXCOL = 3;
void initializeBoard(char initializeBoard[MAXROW][MAXCOL]);
void display_board(char game1[MAXROW][MAXCOL]);
void printBoard(char theBoard[MAXROW][MAXCOL]);
int player1();
int player2();
int check(char);
char board[MAXROW][MAXCOL];
char game[MAXROW][MAXCOL] = { { '1','2','3' },{ '4','5','6' },{ '7','8','9' } };
void gameplay();

//Main function
int main(char initBoard[MAXROW][MAXCOL])
{
//TicTacToe
gameplay();
system("pause");
return 0;
}

//Checks for winner, loser or draw
int check(char input)
{
//Inititalize local variables
int i, j;
int draw = 1;

//Loops to enforce game rules
for (i = 0; i < 3; i++)
{
if ((board[i][0] == input) && (board[i][1] == input) && (board[i][2] == input))
return 1;
}


for (i = 0; i < 3; i++)
{
if ((board[0][i] == input) && (board[1][i] == input) && (board[2][i] == input))
return 1;
}

for (i = 0; i < 3; i++)
{
if ((board[0][0] == input) && (board[1][1] == input) && (board[2][2] == input))
return 1;
}

for (i = 0; i < 3; i++)
{
if ((board[0][2] == input) && (board[1][1] == input) && (board[2][0] == input))
return 1;
}

for (i = 0; i < 3; i++)
{
if ((board[0][0] == input) && (board[1][1] == input) && (board[2][2] == input))
return 1;
}


for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
if (board[i][j] == '*')
draw = 0;
if (draw)
return 2;
else
return 0;
}

//Fills up the initial board with asterisks
void initializeBoard(char initBoard[MAXROW][MAXCOL])
{
// Loop through rows
for (int row = 0; row < MAXROW; row++)
{
// Loop through columns
for (int column = 0; column < MAXCOL; column++)
initBoard[row][column] = '*';
}
}

//Display the initialized board
void printBoard(char theBoard[MAXROW][MAXCOL])
{
cout << "Welcome to Tic-Tac-Toe!" << endl;
cout << endl;
for (int row = 0; row < MAXROW; row++)
{
cout << " | ";
for (int column = 0; column < MAXCOL; column++)
cout << theBoard[row][column] << " | ";
cout << endl;
}
cout << endl;
cout << "Here is how to play:" << endl;
cout << "Step 1: Player 1 choose a desired location" << endl;
cout << "Step 2: Player 2 choose a desired location" << endl;
cout << "Step 3: Repeat until there is a winner, loser, or a draw" << endl;
cout << "Step 4: Have fun and play again!" << endl;
cout << endl;
}

//Display the gameboard
void display_board(char game[MAXROW][MAXCOL])
{
cout << "---------------------" << endl << endl;
cout << " | | " << endl;
cout << " " << game[0][0] << " | " << game[0][1] << " | " << game[0][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << game[1][0] << " | " << game[1][1] << " | " << game[1][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << game[2][0] << " | " << game[2][1] << " | " << game[2][2] << endl;
cout << " | | " << endl;
return;
}

//Calculates a winner, loser, or draw and displays the result
void gameplay()
{
//Declare local variables


int Player1;
int Player2;
char enter = 0;
int p1_wins = 0; //p1_wins = 1 if p1 wins, and p1_wins = 2 if it is a draw
int p2_wins = 0; //p2_wins = 1 if p2 wins, and p2_wins = 2 if it is a draw


cout << "Enter Y or y to play TIC TAC TOE. Enter N or n to exit." << endl;
cin >> enter;
if ((enter == 'Y') || (enter == 'y'))
{
initializeBoard(board);
printBoard(board);


do
{
display_board(game);
Player1 = player1();
p1_wins = check('X');
if (!p1_wins)
{
Player2 = player2();
p2_wins = check('O');
}
cout << endl;
} while (((enter == 'Y') || (enter == 'y')) && (p1_wins == 0) && (p2_wins == 0));

if (p1_wins == 1)
cout << "Player 1 wins!" << endl;
else if (p1_wins == 2)
cout << "Draw" << endl;
else if (p2_wins == 1)
cout << "Player 2 wins!" << endl;
}
}



int player1()
{
int row = 0;
int column = 0;
char choice;
bool position_filled = 1;
bool valid = 1;

while (position_filled || valid)
{

cout << "Player 1 enter your choice: ";
cin >> choice;

if ((isdigit(choice) == 0) || (int(choice) < 48) || (int(choice) > 57))
{
valid = 1;
cout << "Invalid entry! \n";
cout << "Please enter a number between 1-9 for your choice: ";

}

else
{
valid = 0;
switch (choice)
{
case '1': row = 0; column = 0; break;
case '2': row = 0; column = 1; break;
case '3': row = 0; column = 2; break;
case '4': row = 1; column = 0; break;
case '5': row = 1; column = 1; break;
case '6': row = 1; column = 2; break;
case '7': row = 2; column = 0; break;
case '8': row = 2; column = 1; break;
case '9': row = 2; column = 2; break;
default:
cout << "You didn't enter a correct number! Try again\n";
valid = 1;
}

if (board[row][column] != '*')
position_filled = 1;
else
position_filled = 0;
}
}

game[row][column] = 'X';
board[row][column] = 'X';
display_board(game);

return row, column;
}


int player2()
{
int row = 0;
int column = 0;
int selection;
char choice;
bool position_filled = 1;
bool valid = 1;

while (position_filled || valid)
{
cout << "Player 2 enter your choice: ";
cin >> choice;

if ((isdigit(choice) == 0) || (int(choice) < 48) || (int(choice) > 58))
{
valid = 1;
cout << "Invalid number! \n";
cout << "Please enter a number between 1-9 for your choice: ";
}
else
{
valid = 0;
switch (choice)
{
case '1': row = 0; column = 0; break;
case '2': row = 0; column = 1; break;
case '3': row = 0; column = 2; break;
case '4': row = 1; column = 0; break;
case '5': row = 1; column = 1; break;
case '6': row = 1; column = 2; break;
case '7': row = 2; column = 0; break;
case '8': row = 2; column = 1; break;
case '9': row = 2; column = 2; break;
default:
cout << "You didn't enter a correct number! Try again" << endl;
player2();
valid = 1;
}
if (board[row][column] != '*')
position_filled = 1;
else
position_filled = 0;
}
}

game[row][column] = 'O';
board[row][column] = 'O';
display_board(game);

return row, column;
}
closed account (48T7M4Gy)
:)
Topic archived. No new replies allowed.