forked from Mohamed-Taha-uni/swe-git
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathx_o.cpp
More file actions
184 lines (172 loc) · 3.89 KB
/
x_o.cpp
File metadata and controls
184 lines (172 loc) · 3.89 KB
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
#include<iostream>
using namespace std;
bool win_game_row(char arr[3][3]){
//Checks Horizontal Win
for(int i=0;i<3;i++){
bool res = true;
char same = arr[i][0];
if(same == '-'){
continue; //This Row has empty spaces
}
for(int j=1;j<3;j++){
if(arr[i][j] != same){
res = false;
break;
}
}
if(res == true){
return res;
}
}
return false;
}
bool win_game_col(char arr[3][3]){
//Checks Vertical Win
for(int i=0;i<3;i++){
bool res = true;
char same = arr[0][i];
if(same == '-'){
continue; //This Column has empty spaces
}
for(int j=1;j<3;j++){
if(arr[j][i] != same){
res = false;
break;
}
}
if(res == true){
return res;
}
}
return false;
}
bool win_game_diag1(char arr[3][3]){
//Checks Diagonal Win
char same = arr[0][0];
if(same == '-'){
return false;
}
for(int i=1;i<3;i++){
if(arr[i][i] != same){
return false;
}
}
return true;
}
bool win_game_diag2(char arr[3][3]){
//Checks Diagonal Win
char same = arr[0][2];
if(same == '-'){
return false;
}
for(int i=1;i<3;i++){
if(arr[i][2-i] != same){
return false;
}
}
return true;
}
void display(char arr[3][3]){
//Displays the complete board
cout<<"\n";
cout<<arr[0][0]<<" | "<<arr[0][1]<<" | "<<arr[0][2]<<endl;
cout<<"---------\n";
cout<<arr[1][0]<<" | "<<arr[1][1]<<" | "<<arr[1][2]<<endl;
cout<<"---------\n";
cout<<arr[2][0]<<" | "<<arr[2][1]<<" | "<<arr[2][2]<<endl;
}
bool completelyFilled(char arr[3][3]){
bool res = true;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(arr[i][j] == '-'){
res = false;
return res;
}
}
}
return res;
}
void change(char arr[3][3],int row,int col,int turn){
//Takes input from the user and changes the value at that position
if(turn == 0){
arr[row][col] = 'O';
}
else{
arr[row][col] = 'X';
}
}
bool filled(char arr[3][3],int row,int col){
//Check whether the given position is already occupied or not
return (arr[row][col] == 'X' || arr[row][col] == 'O');
}
int main(){
begin:
char arr[3][3];
fill_n(arr[0],3,'-');
fill_n(arr[1],3,'-');
fill_n(arr[2],3,'-');
int turn = 1;
string temp_turn = "a"; //Temporary variable to read input of turn from the user
//Creating this will enable us to read all the characters without any segmentation fault
cout<<"Player-1 will play with \'X\' \n"; //turn = 1
cout<<"Player-2 will play with \'O\' \n"; //turn = 0
cout<<"\n";
while(true){
cout<<"Which Player will turn first (1 or 2): ";
getline(cin,temp_turn);
if(temp_turn == "1"){
turn = 1; //0 represents Player-1 who plays with X
break;
}
else if(temp_turn == "2"){
turn = 0; //0 represents Player-2 who plays with O
break;
}
else{
cout<<"Wrong Input! Try Again\n\n";
}
}
int row,col;
while(true){
while(true){ //Checking whether the place is already filled or not
cout<<"\nEnter a row number (1 or 2 or 3): ";
cin>>row;
cout<<"Enter a column number (1 or 2 or 3): ";
cin>>col;
if(row<=0 || row>=4){
cout<<"Invalid Row Number Input! Try Again\n";
}
else if(col<=0 || col>=4){
cout<<"Invalid Column Number Input! Try Again\n";
}
else if(filled(arr,row-1,col-1)){
cout<<"Place Already Filled! Try Again\n";
}
else{
break;
}
}
change(arr,row-1,col-1,turn); //Passing Player Input to TIC-TAC-TOE Board
display(arr); //Displaying TIC-TAC-TOE Board
if(win_game_col(arr) || win_game_row(arr) || win_game_diag1(arr) || win_game_diag2(arr)){
cout<<(turn==0?"Player-2 won the game!\n":"Player-1 won the game!\n");
break;
}
if(completelyFilled(arr)){
cout<<"\n****It's a tie!!****\n";
cout<<"Board has been completely filled\n";
break;
}
turn = (turn+1)%2;
}
string choice;
cout<<"\n\nDo you want to play again? (y/n) ";
cout<<"\nPress any other key to exit: ";
getline(cin,choice);
getline(cin,choice);
if(choice=="y"){
cout<<endl;
goto begin;
}
}