-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·142 lines (108 loc) · 3.46 KB
/
main.cpp
File metadata and controls
executable file
·142 lines (108 loc) · 3.46 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
#include <iostream>
#include <stdlib.h>
using namespace std ;
void printBoard( int *array ) {
system("clear");
for ( int i = 0 ; i < 9 ; i++ ) {
cout << " " ;
if ( array[i] == 0 ) {
cout << i+1 ;
} else {
cout << (( array[i] == 1 ) ? 'x' : 'o' ) ;
}
if ( ((i+1)%3) == 0 ) {
cout << "\n" ;
if ( i != 8 ) {
for ( int i = 0 ; i < 11 ; i++ ) cout << "-" ;
cout << "\n" ;
}
} else {
cout << " |" ;
}
}
cout << "\n" ;
}
bool isGameOver( int *array , int *winner ) {
// horizontal check
for ( int i = 3 ; i < 10 ; i += 3 ) {
int s = i - 3 ;
bool emptySlot = false ;
for ( int j = s ; j < i && !emptySlot ; j++ ) emptySlot = array[j] == 0 ;
if ( emptySlot ) continue ;
bool match = true ;
s++ ;
for ( int j = s ; j < i && match ; j++ ) match = ( array[j] == array[j-1] ) ;
if ( match ) {
*winner = array[s] ;
return true ;
}
}
// vertical check
for ( int i = 0 ; i < 3 ; i++ ) {
bool emptySlot = false ;
for ( int j = i ; j < 9 && !emptySlot ; j += 3 ) emptySlot = array[j] == 0 ;
if ( emptySlot ) continue ;
bool match = true ;
for ( int j = i+3 ; j < 9 ; j += 3 ) match = array[j] == array[j-3] ;
if ( match ) {
*winner = array[i] ;
return true ;
}
}
// cross check
if ( array[4] != 0 && ((array[0] == array[4] && array[4] == array[8]) || ( array[2] == array[4] && array[4] == array[6] ))) {
*winner = array[4] ;
return true ;
}
// check for tie
bool slotFilled = true ;
for ( int i = 0 ; i < 9 && slotFilled ; i++ ) slotFilled = (array[i] != 0) ;
// check for tie
if ( slotFilled ) *winner = -1 ;
// result
return slotFilled ;
}
int main(int argc, char **argv) {
int array[9] ;
char op ;
cout << "with each turn character will be swaped\n" ;
cout << "between x and o\n" ;
cout << "to reset the game type 0\n" ;
cout << "to exit game type -1\n" ;
cout << "otherwise index from 1 to 9\n" ;
cout << "enter to y continue : " ;
cin >> op ;
if ( op != 'y' ) return 0 ;
while ( true ) {
for ( int i = 0 ; i < 9 ; i++ ) array[i] = 0 ;
int c = 1 ;
int t = 0 ;
char invalid[] = "Invalid Index Try Again\n" ;
char occupied[] = "Place is already occupied\n" ;
int winner ;
while ( !isGameOver( array , &winner ) ) {
printBoard( array ) ;
if ( t == 1 ) cout << invalid ;
else if ( t == 2 ) cout << occupied ;
t = 0 ;
cout << "Enter Index for " ;
cout << (( c == 1 ) ? 'x' : 'o') ;
cout << " : " ;
int index ;
cin >> index ;
if ( index == -1 ) return 0 ;
else if ( index < 0 || index > 9 ) t = 1 ;
else if ( index == 0 ) break ;
else if ( array[(index-1)] != 0 ) t = 2 ;
else array[(index-1)] = c ;
if ( t == 0 ) c = ( c == 1 ) ? 2 : 1 ;
}
system("clear");
cout << ( ( winner == 0 ) ? "It's a tie" : ( ( winner == 1 ) ? "x won" : "o won" ) ) ;
op = 'n' ;
cout << "\nEnter 'y' to restart game : " ;
cin >> op ;
if ( op != 'y' ) return 0 ;
}
return 0;
}