-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack
More file actions
99 lines (95 loc) · 2.25 KB
/
stack
File metadata and controls
99 lines (95 loc) · 2.25 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
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <ctime>
using namespace std;
char checkwin(char pole[3][3]) {
char winresult;
for (int i = 0; i < 3; i++) {
int j = 0;
if (pole[i][j] == pole[i][j + 1] && pole[i][j + 1] == pole[i][j + 2]) {
if (pole[i][j] == 'X') return winresult = pole[i][j];
else if (pole[i][j] == 'O') return winresult = pole[i][j];
else continue;
}
}
for (int j = 0; j < 3; j++) {
int i = 0;
if (pole[i][j] == pole[i + 1][j] && pole[i + 1][j] == pole[i + 2][j]) {
if (pole[i][j] == 'X') return winresult = pole[i][j];
else if (pole[i][j] == 'O') return winresult = pole[i][j];
else continue;
}
}
if ((pole[0][0] == pole[1][1] && pole[1][1] == pole[2][2]) || (pole[0][2] == pole[1][1] && pole[1][1] == pole[2][0])) {
if (pole[1][1] == 'X') return winresult = pole[1][1];
else if (pole[1][1] == 'O') return winresult = pole[1][1];
}
int res = 1;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (pole[i][j] == '_') { res = 0; break; }
else res *= 1; break;
}
}
if (res == 1) return 'N';
else return 'Y';
}
void wheretoput(char pole[3][3], char turn) {
int count = 1;
int l[9], c[9];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (pole[i][j] == '_') {
char k;
int n;
if (i == 0) k = 'a';
if (i == 1) k = 'b';
if (i == 2) k = 'c';
if (j == 0) n = 1;
if (j == 1) n = 2;
if (j == 2) n = 3;
cout << count << '.' << " mark cell " << k << n << " as " << turn << endl;
l[count] = i;
c[count] = j;
count++;
}
}
}
cout << endl;
cin >> count;
pole[l[count]][c[count]] = 'X';
}
void write(char pole[3][3]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << pole[i][j] << " ";
}
cout << endl;
}
}
int main()
{
char pole[3][3], turn;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
pole[i][j] = '_';
}
}
turn = 'X';
write(pole);
for (; 1; ) {
wheretoput(pole, turn);
if (turn == 'X') turn = 'O';
if (turn == 'O') turn = 'X';
switch (checkwin(pole)) {
case 'O': {cout << "O won. Congrat" << endl;
exit(0); }
case 'X': {cout << "X won. Congrat" << endl;
exit(0); }
case 'N': {cout << "Draw. Congrat" << endl;
exit(0); }
case 'Y': {}
}
}
}