Tic Tac Toe

Okay guys so i need help big time. We are supposed to create a tic tac toe game vs smart AI. We are to use enums for game pieces and a single dimension array for our board. The player who goes first gets X's. Also the computer is supposed to block or go for the win, but it doesn't always have to win it can end in a tie. So here's my code and what i notice is wrong,
1 - Winner is never announced idk why. The game keeps going on forever. Its Probably my gameWin function.
2 - If the spot is taken and i pick that spot it doesn't give me an error, it just changes the symbol.
3 - if i answer no to me wanting to go first, it still makes me go first ?
4 - how do i check for a tie ? Should i make a separate function for that ?
Thanks in advance hopefully someone can help my teacher gets kind of mad when we ask for help.



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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
  #include <iostream>

using namespace std;

enum gamePiece { X_PIECE, O_PIECE, EMPTY_PIECE };
enum gameState { X_WINS, O_WINS, TIE };
char gamePieceChr[3] = { 'X', 'O', ' ' };
const int BEST_MOVE[] = { 4, 0, 2, 6, 8, 1, 3, 5, 7 };
gamePiece Board[] = { EMPTY_PIECE, EMPTY_PIECE, EMPTY_PIECE, EMPTY_PIECE, EMPTY_PIECE, EMPTY_PIECE, EMPTY_PIECE, EMPTY_PIECE, EMPTY_PIECE };
int const HUMAN = 1;
int const COMPUTER = 0;
gamePiece humanPiece;
gamePiece computerPiece;

void printInstructions();
void printBoard();
bool gameWin();
bool isLegal(gamePiece board[], int move);
int getHumanMove();
int getComputerMove();

int main()
{
	char yesNo = ' ';
	int playerTurn = HUMAN;
	char playerMarker = ' ';
	int playerMove = 0;

	printInstructions();
	
	do
	{
		cout << "Would you like to go first?(y/n): ";
		cin >> yesNo;
		yesNo = toupper(yesNo);
		if (yesNo != 'Y' && yesNo != 'N')
			cout << "Invalid Choice try again." << endl << endl;
	} while (yesNo != 'N' && yesNo != 'Y');
	
	switch (yesNo)
	{
	case 'Y':
		playerTurn = HUMAN;
		humanPiece = X_PIECE;
		computerPiece = O_PIECE;
	case 'N': 
		playerTurn = COMPUTER;
		humanPiece = O_PIECE;
		computerPiece = X_PIECE;
		break;
	default: cout << "Invalid Character" << endl;
		break;
	}

	do
	{
		Board[getHumanMove()] = X_PIECE;
		gameWin();
		printBoard();
	
		Board[getComputerMove()] = O_PIECE;
		gameWin();
		printBoard();
		
	}while(!gameWin());

		system("pause");
		return 0;
}
void printInstructions()
{
	cout << "Welcome to the game of Tic-Tac-Toe." << endl << endl;
	cout << "Make your move known by entering a number, 0-8. The number " << endl;
	cout << "corresponds to the desired board position as shown below." << endl << endl;

	cout << "       " << "0" << "  |  " << "1" << "  |  " << "2" << endl;
	cout << "     -----+-----+-----" << endl;
	cout << "       " << "3" << "  |  " << "4" << "  |  " << "5" << endl;
	cout << "     -----+-----+-----" << endl;
	cout << "       " << "6" << "  |  " << "7" << "  |  " << "8" << endl;

}
void printBoard()
{
	cout << endl;
	cout << "       " << gamePieceChr[Board[0]] << "  |  " << gamePieceChr[Board[1]] << "  |  " << gamePieceChr[Board[2]] << endl;
	cout << "     -----+-----+-----" << endl;
	cout << "       " << gamePieceChr[Board[3]] << "  |  " << gamePieceChr[Board[4]] << "  |  " << gamePieceChr[Board[5]] << endl;
	cout << "     -----+-----+-----" << endl;
	cout << "       " << gamePieceChr[Board[6]] << "  |  " << gamePieceChr[Board[7]] << "  |  " << gamePieceChr[Board[8]] << endl;
}
int getHumanMove()
{
	int move;

		cout << endl;
		cout << "Make your move." << endl;
		printBoard();
		cout << "Where will you move (0-8): ";
		cin >> move;
		if (move < 0 || move > 8)
			cout << "Invalid choice. Try again.";

	return move;
}
int getComputerMove()
{
	int move;

	cout << "Computer turn." << endl;

	// check if computer can win on next move
	for (int i = 0; i <= 8; i++)
	{
		if (Board[i] == EMPTY_PIECE)
		{
			Board[i] = computerPiece;
			if (gameWin())
			{
				cout << "computer has won";
				return i;
			}
			else {
				Board[i] = EMPTY_PIECE;
			}
		}
	}

	// check if human can win on next move
	for (int i = 0; i <= 8; i++)
	{
		if (Board[i] == EMPTY_PIECE)
		{
			Board[i] = humanPiece;
			if (gameWin())
			{
				Board[i] = computerPiece;
				return i;
			}
			else {
				Board[i] = EMPTY_PIECE;
			}
		}
	}

	//computer can't on next move; human can't win
	for (int i = 0; i <= 8; i++)
	{
		move = BEST_MOVE[i];
		if (Board[move] == EMPTY_PIECE)
			break;
	}

	return move;
}
bool isLegal(gamePiece board[], int move)
{
	if (move < 0 || move > 8 || board[move] != EMPTY_PIECE)
		return false;
	return true;
}
bool gameWin()
{

		bool winner = false;

		if (gamePieceChr[Board[0]] == X_PIECE && gamePieceChr[Board[1]] == X_PIECE && gamePieceChr[Board[2]] == X_PIECE)
			winner = true;
		if (gamePieceChr[Board[3]] == X_PIECE && gamePieceChr[Board[4]] == X_PIECE && gamePieceChr[Board[5]] == X_PIECE)
			winner = true;
		if (gamePieceChr[Board[6]] == X_PIECE && gamePieceChr[Board[7]] == X_PIECE && gamePieceChr[Board[8]] == X_PIECE)
			winner = true;
		if (gamePieceChr[Board[0]] == X_PIECE && gamePieceChr[Board[3]] == X_PIECE && gamePieceChr[Board[6]] == X_PIECE)
			winner = true;
		if (gamePieceChr[Board[1]] == X_PIECE && gamePieceChr[Board[4]] == X_PIECE && gamePieceChr[Board[7]] == X_PIECE)
			winner = true;
		if (gamePieceChr[Board[2]] == X_PIECE && gamePieceChr[Board[5]] == X_PIECE && gamePieceChr[Board[8]] == X_PIECE)
			winner = true;
	    if (gamePieceChr[Board[0]] == X_PIECE && gamePieceChr[Board[4]] == X_PIECE && gamePieceChr[Board[8]] == X_PIECE)
			winner = true;
		if (gamePieceChr[Board[2]] == X_PIECE && gamePieceChr[Board[4]] == X_PIECE && gamePieceChr[Board[6]] == X_PIECE)
			winner = true;

		if (gamePieceChr[Board[0]] == O_PIECE && gamePieceChr[Board[1]] == O_PIECE && gamePieceChr[Board[2]] == O_PIECE)
			winner = true;
		if (gamePieceChr[Board[3]] == O_PIECE && gamePieceChr[Board[4]] == O_PIECE && gamePieceChr[Board[5]] == O_PIECE)
			winner = true;
		if (gamePieceChr[Board[6]] == O_PIECE && gamePieceChr[Board[7]] == O_PIECE && gamePieceChr[Board[8]] == O_PIECE)
			winner = true;
		if (gamePieceChr[Board[0]] == O_PIECE && gamePieceChr[Board[3]] == O_PIECE && gamePieceChr[Board[6]] == O_PIECE)
			winner = true;
		if (gamePieceChr[Board[1]] == O_PIECE && gamePieceChr[Board[4]] == O_PIECE && gamePieceChr[Board[7]] == O_PIECE)
			winner = true;
		if (gamePieceChr[Board[2]] == O_PIECE && gamePieceChr[Board[5]] == O_PIECE && gamePieceChr[Board[8]] == O_PIECE)
			winner = true;
		if (gamePieceChr[Board[0]] == O_PIECE && gamePieceChr[Board[4]] == O_PIECE && gamePieceChr[Board[8]] == O_PIECE)
			winner = true;
		if (gamePieceChr[Board[2]] == O_PIECE && gamePieceChr[Board[4]] == O_PIECE && gamePieceChr[Board[6]] == O_PIECE)
			winner = true;	

		return winner;		
}
closed account (48T7M4Gy)
line 65: }while(gameWin() == false);
Topic archived. No new replies allowed.