This repository has been archived by the owner on May 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9.cpp
165 lines (150 loc) · 3.25 KB
/
9.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
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
#ifdef __linux__
const int OS = 0;
#elif defined(_WIN32)
const int OS = 1;
#endif
#include <iostream>
#include <string.h>
using namespace std;
int BOARD[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
void showBoard() {
// 显示棋盘以及棋子
for (int i = 0; i < 9; i++) {
cout << " __";
if (BOARD[i] == 0) {
cout << i;
} else {
if (BOARD[i] == 1) {
cout << 'x';
} else {
cout << 'A';
}
}
if ((i + 1) % 3 == 0) {
cout << "__" << endl;
} else {
cout << "__ |";
}
}
}
int checkWinner() {
// 检测赢家,返回值 -1(无赢家) or 1(玩家一) or 2(玩家二)
int winner = -1;
for (int i = 0; i < 3; i++) {
if (BOARD[i] == BOARD[i + 3] && BOARD[i + 3] == BOARD[i + 6] &&
BOARD[i] != 0) { // 竖
winner = BOARD[i];
break;
} else if (BOARD[i * 3] == BOARD[i * 3 + 1] &&
BOARD[i * 3 + 1] == BOARD[i * 3 + 2] &&
BOARD[i * 3] != 0) { // 横
winner = BOARD[i * 3];
break;
}
}
// 对角线
if (BOARD[0] == BOARD[4] && BOARD[4] == BOARD[8] && BOARD[4] != 0) {
winner = BOARD[4];
} else if (BOARD[2] == BOARD[4] && BOARD[4] == BOARD[6] && BOARD[4] != 0) {
winner = BOARD[4];
}
return winner;
}
// 获取用户的合法输入
int checkInput() {
int pos = -1;
while (pos < 0 || pos > 8) {
cin >> pos;
if (BOARD[pos] != 0) {
cout << "Position is not null, try again" << endl;
pos = -1;
}
}
return pos;
}
void clearScreen() {
// 清空控制台所有内容, linux 与 windows 有所不同
if (OS == 0)
system("clear");
else
system("clr");
}
// AI 输入
int AIInput() {
unsigned int i = 0;
bool found = false;
while (!found && i < 9) {
if (BOARD[i] == 0) {
BOARD[i] = 2; // 一下能赢的棋
if (checkWinner() == 2) {
found = true;
}
BOARD[i] = 0;
BOARD[i] = 1; // 不下会输的棋
if (checkWinner() == 1) {
found = true;
}
BOARD[i] = 0;
}
i++;
}
if (!found) { //第三种策略
const int next[9] = {4, 0, 2, 6, 8, 1, 3, 5, 7};
unsigned int k = 0;
while (!found && k < 9) {
if (BOARD[next[k]] == 0) {
found = true;
}
k++;
}
return k - 1;
} else {
return i - 1; // 前两中策略有效
}
}
int main() {
// 0 表示未有子,1、2分别表示两种棋子
bool AImodel = false;
char command[10];
cout << "Play with AI?yes or no" << endl;
cin.getline(command, 10);
if (!strcmp(command, "yes")) { // AI
AImodel = true;
}
clearScreen();
showBoard();
int winner = -1;
int count = 0, turn;
while (count < 9) {
int pos;
turn = count % 2 + 1;
if (AImodel && turn == 2) {
pos = AIInput();
// cout << "AI: " << pos << endl;
} else {
cout << "Player " << turn << " : ";
pos = checkInput();
}
BOARD[pos] = turn;
clearScreen();
showBoard();
winner = checkWinner();
if (winner != -1) {
break;
}
count++;
}
switch (winner) {
case 1:
cout << "YOU WIN!" << endl;
break;
case 2:
cout << "you lose!" << endl;
break;
case -1:
cout << "Nobody win" << endl;
break;
}
cout << "Game Over. Take a rest" << endl;
return 0;
}