Tic-Tac-Toe

So I had to make a Tic-Tac-Toe final program for my C++ class and I was wondering if anyone could take a look at the code and see if there are anyways I could shorten it, maybe lose some if statements or anything. Thanks.
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
203
204
205
206
207
208
209
210
//Steve Osborne, Tic-Tac-Toe Program

#include<iostream>
#include<iomanip>
using namespace std;

int const ARRAYMAX=3;

void header();
void play();
void showBoard(char board[][ARRAYMAX]);
int checkWin(char board[][ARRAYMAX],char Player);
int draw(char board[][ARRAYMAX]);
int checkError(int row, int col,char board[][ARRAYMAX]);

int main(){
	int cont=1;
	header();
	while(cont==1){
		int choice=0;
		cout<<"\n\nPress 1 to play Tic-Tac-Toe or press 2 to quit: " ;
		cin>>choice;
		if(choice==1){
			play();
		}
		else{
			cout<<"\n\nGoodbye\n\n";
			cont=0;
		}
	}
	return 0;
}
void header(){
	int width=53;
	for(int i=0;i<width;i++){
		cout<<"*";
	}
	cout<<"\n";
	for(int i=0;i<width;i++){
		if(i==0 || i==width-1){
			cout<<"*";
		}
		else{
			cout<<" ";
		}
	}
	cout<<"\n"
		<<"*      Steve Osborne   Tic-Tac-Toe     ENGR 230     *"<<"\n";
	for(int i=0;i<width;i++){
		if(i==0 || i==width-1){
			cout<<"*";
		}
		else{
			cout<<" ";
		}
	}
	cout<<"\n";
	for(int i=0;i<width;i++){
		cout<<"*";
	}
	cout<<"\n";
}
void showBoard(char board[][ARRAYMAX]){
	cout<<"\n"
		<<setw(20)<<board[0][0]<<" | "<<board[0][1]<<" | "<<board[0][2]<<"\n"
		<<setw(28)<<"---------"<<"\n"
		<<setw(20)<<board[1][0]<<" | "<<board[1][1]<<" | "<<board[1][2]<<"\n"
		<<setw(28)<<"---------"<<"\n"
		<<setw(20)<<board[2][0]<<" | "<<board[2][1]<<" | "<<board[2][2]<<"\n"
		<<"\n";
}
int draw(char board[][ARRAYMAX]){
	for(int i=0;i<3;i++){
		for(int j=0;j<3;j++){
			if(board[i][j]==' '){
				return 0;
			}
		}
	}
	return 1;
}
int checkWin(char board[][ARRAYMAX],char Player){ 
	for(int i=0;i<3;i++){
		bool won=true;
		for(int j=0;j<3;j++){
			if(board[i][j]!=Player){
				won=false;
				break;
			}
		}
		if(won){
			return 1;
		}
	}
	for(int j=0;j<3;j++){
		bool won=true;
		for(int i=0;i<3;i++){
			if(board[i][j]!=Player){
				won=false;
				break;
			}
		}
		if(won){
			return 1;
		}
	}	
	bool won=true;
	for(int i=0;i<3;i++){
		if(board[i][i]!=Player){
			won=false;
			break;
		}
	}
	if(won){
		return 1;
	}
	won=true;
	for(int i=0;i<3;i++){
		if(board[i][2-i]!=Player){
			won=false;
			break;
		}
	}
	if(won){
		return 1;
	}
	return 0;
}
int checkError(int row,int col,char board[][ARRAYMAX]){
	if(1<=row&&row<=3&&1<=col&&col<=3){
		if(board[row-1][col-1]==' '){
			return 1;
		}
		else{
			return 0;
		}
	}
	else{
		return 0;
	}
}
void play(){
	char board[ARRAYMAX][ARRAYMAX];
	for(int i=0;i<3;i++){
		for(int j=0;j<3;j++){
			board[i][j]=' ';
		}
	}
	while(true){
		showBoard(board);
		int row,col;
		while(true){
			cout<<"Player One (X):Choose row & column separated\n"
				<<"               by a blank (0 0 to quit):";
			cin>>row>>col;
			if(row==0&&col==0){
				cout<<"\n\nTerminating Tic-Tac-Toe interface...\n\n";
				return;
			}
			if(checkError(row,col,board)){
				break;
			}
			else{
				cout<<"Your entered value is wrong.\n" 
					<<"Row & Column must between 1 and 3 inclusive and\n" 
					<<"the cell must be empty.\n\n";
			}
		}
		board[row-1][col-1] = 'X';
		if(checkWin(board,'X')){
			cout<<"\n"<<setw(36)<<"Player One (X) wins!\n";
			showBoard(board);
			break;
		}
		if(draw(board)){
			cout<<"\n"<<setw(27)<<"Draw!\n";
			showBoard(board);
			break;
		}
		showBoard(board);
		while(true){
			cout<<"Player Two (O):Choose row & column separated\n"
				<<"               by a blank (0 0 to quit):";
			cin>>row>>col;
			if(row==0&&col==0){ 
				cout<<"\n\nTerminating Tic-Tac-Toe interface...\n\n";
				return;
			}
			if(checkError(row,col,board)){
				break;
			}
			else{
				cout<<"\n\nYour entered value is wrong.\n"
					<<"Row & column must between 1 and 3 inclusive and"
					<<" the cell must be empty.\n\n"; 
			}
		}
		board[row-1][col-1]='O';
		if(checkWin(board,'O')){
			cout<<"\n"<<setw(36)<<"Player Two (O) wins!\n";
			showBoard(board);
			break;
		}
		if(draw(board)){
			cout<<"\n"<<setw(27)<<"Draw!\n";
			showBoard(board);
			break;
		}
	}
}

Topic archived. No new replies allowed.