Tic tac toe game issues

Good morning. I am writing a simple game of tic tac toe but I need some help getting the code below to be functional...at this point I am not sure what I need to change to get the code working. I also need to remove the few "break" commands in the code but am not sure what to use instead. Any input would be greatly appreciated.

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
#include <iostream>



// This program allows user(s) to play a game of tic-tac-toe.


using namespace std;

// constants
const char SIGIL[3] = { '.', 'X', 'O' };

// prototypes
int winner(int board[3][3]);
void drawBoard(int board[3][3]);
bool isMoveLegal(int board[3][3]);
bool isGameOver(int board[3][3]);



// return 1 if player 1 'X' has won
// return 2 if player 2 'O' has won
// return 0 if neither player has won
int winner(int board[3][3]){
	int win = 0;

	// Checks for:
	//     X X X
	for (int i = 0; i < 3; i++)
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2])
			win = board[i][0];

	// Checks for:
	// X
	// X
	// X
	for (int i = 0; i < 3; i++)
	if (board[0][i] == board[1][i] && board[1][i] == board[2][i])
		win = board[0][i];

	// Checks for:
	// X                 X
	//   X      or     X
	//     X         X 
	if ((board[0][0] == board[1][1] && board[1][1] == board[2][2]) ||
		(board[0][2] == board[1][1] && board[1][1] == board[2][0]))
		win = board[1][1];

	return win;
}


// using this board as a guide
// draw the board using "." for empty squares
// or 'X' or 'O' for player 1 or player 2
void drawBoard(int board[3][3]){

	cout << "   0  1  2" << endl;
	for (int i = 0; i < 3; i++){
		cout << i ;
		
		
		for (int j = 0; j < 3; j++){
			cout.width(3);
			switch (board[i][j])
			{
			case 0:{
					   cout << ".";
			}break; case 1:{
					   cout << "X";
			}break;
			case 2:{
					   cout << "0";
			}break;
			default:
				break;
			}
		} cout << endl;
	}
	
	
}


// return false if row or column are out of bounds
// or if that spot on the board is already taken 
// otherwise return true
bool isMoveLegal(int board[3][3], int row, int column){
	if (row >= 0 && row <= 3 && column <= 3 && column >= 0)		{
		if (board[row][column] == 0)
			return true;
		else return false;
	}
	return false;
}


// if any player has three in a row or if the board is full
// return true otherwise return false
bool isGameOver(int board[3][3]){
	if (winner(board) == 1 || winner(board) == 2) return true;
	for (int r = 0; r <= 2; r++)
	for (int c = 0; c <= 2; c++)
	if (board[r][c] == 0)
		return false;
	return true;
}


int main(){
	int board[3][3] = { { 0 } }; // 0 for empty square, 1 or 2 for taken squares
	int player = 1;
	int row, column, result;
	bool legalMove;

	// starting board
	drawBoard(board);
	while (!isGameOver(board)){
		cout << "Player " << player << "(" << SIGIL[player] << "), your move?";
		cin >> row >> column;
		legalMove = isMoveLegal(board, row, column);
		while (!legalMove){
			cout << "Player " << player << "(" << SIGIL[player] << "), your move?";
			cin >> row >> column;
			legalMove = isMoveLegal(board, row, column);
		}
		board[row][column] = player;
		drawBoard(board);
		player = 3 - player;
	}
	// game over
	result = winner(board);
	if (result == 0){
		cout << "Tie Game" << endl;
	}
	else {
		cout << "Player " << result << "(" << SIGIL[result] << ") wins!" << endl;
	}

	return 0;
}
Topic archived. No new replies allowed.