-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
118 lines (104 loc) · 3.08 KB
/
main.cpp
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
#include <iostream>
using namespace std;
int whoWon(char posNum){
int result = 0;
if(posNum == 'X'){
result = 1;
} else if(posNum == 'O'){
result = -1;
}
return result;
}
int isGameOver(char pos[]){
int result = 0;
if(pos[0] == pos[1] && pos[1] == pos[2]){
result = whoWon(pos[1]);
} else if(pos[3] == pos[4] and pos[4] == pos[5]){
result = whoWon(pos[4]);
} else if(pos[6] == pos[7] and pos[7] == pos[8]){
result = whoWon(pos[7]);
} else if(pos[0] == pos[3] and pos[3] == pos[6]){
result = whoWon(pos[3]);
} else if(pos[1] == pos[4] and pos[4] == pos[7]){
result = whoWon(pos[4]);
} else if(pos[2] == pos[5] and pos[5] == pos[8]){
result = whoWon(pos[5]);
} else if(pos[0] == pos[4] and pos[4] == pos[8]){
result = whoWon(pos[4]);
} else if(pos[2] == pos[4] and pos[4] == pos[6]){
result = whoWon(pos[4]);
}
return result;
}
int promptMovePosition(char pos[]){
int movePosition = 0;
while (true) { // checking loop
cin >> movePosition;
if (cin.fail() || movePosition > 9 || movePosition < 1) {
cout << "Invalid Input, please choose again" << endl;
cin.clear();
cin.ignore(10000, '\n');
}
else if (pos[(movePosition - 1)] != ' ') {
cout << "Please choose another spot" << endl;
}
else {
break;
}
}
return movePosition - 1;
}
int main()
{
char pos[9];
for(int i = 0; i < 9; i++){
pos[i] = ' ';
}
cout << " " << pos[0] <<" | " << pos[1] << " | " << pos[2] << " " << endl;
cout << "___|___|___" << endl;
cout << " " << pos[3] <<" | " << pos[4] << " | " << pos[5] << " " << endl;
cout << "___|___|___" << endl;
cout << " " << pos[6] <<" | " << pos[7] << " | " << pos[8] << " " << endl;
cout << " | | " << endl;
for(int i = 0; i < 9 and isGameOver(pos) == 0; i++){
cout << "Choose your move 1-9" << endl;
if (i % 2 == 0) {
pos[promptMovePosition(pos)] = 'X';
}
else if(i % 2 == 1){
pos[promptMovePosition(pos)] = 'O';
}
for(int i = 0; i < 100; i++){
cout << endl;
}
cout << " " << pos[0] <<" | " << pos[1] << " | " << pos[2] << " " << endl;
cout << "___|___|___" << endl;
cout << " " << pos[3] <<" | " << pos[4] << " | " << pos[5] << " " << endl;
cout << "___|___|___" << endl;
cout << " " << pos[6] <<" | " << pos[7] << " | " << pos[8] << " " << endl;
cout << " | | " << endl;
}
switch(isGameOver(pos)){
case -1:
cout << endl;
cout << "O wins" << endl;
break;
case 0:
cout << endl;
cout << "Draw" << endl;
break;
case 1:
cout << endl;
cout << "X wins" << endl;
break;
}
return 0;
}
/*
X | X | X
___|___|___
X | X | X
___|___|___
X | X | X
| |
*/