-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.cpp
321 lines (312 loc) · 6.32 KB
/
input.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include<bits/stdc++.h>
using namespace std;
int dx[10] = {0, 1, 1, 1, 2, 2, 2, 3, 3, 3};
int dy[10] = {0, 1, 2, 3, 1, 2, 3, 1, 2, 3};
int board[900][4][4]={},cnt=1,ma=0, score[900];
void input();
int tree(void){
int i, temp = -1, s;
for(i = 1; i <= 9; i++){
if(score[i] >= 5000){
return i;
}
printf("%d ", score[i]);
}
printf("\n");
for(i = 91; i <= 819; i++){
if(score[i/9] == -1000000){
i += 8;
}
if(score[i] != -1000000){
score[i/9] = max(score[i/9], score[i]);
}
}
for(i = 10; i <= 90; i++){
if(score[i/9] == -1000000){
i += 8;
}
if(score[i] != -1000000){
score[i/9] = min(score[i/9], score[i]);
}
}
s = -1000001;
for(i = 1; i <= 9; i++){
if(score[i] != -1000000){
if(score[i] >= s){
temp = i;
s = score[i];
}
}
}
for(i = 1; i <= 9; i++){
printf("%d ", score[i]);
}
return temp;
}
int sc(int index){ //평가함수 플레이어 에게 유리할때는 음수, AI에게 유리할때는 양수
int i, j, s = 0,temp;
for(i = 1; i <= 3; i++){
temp = 0;
for(j = 1; j <= 3; j++){
if(board[index][i][j] == 2){
temp++;
}
}
if(temp == 3){
s += 10000;
}
else if(temp == 2){
s += 100;
}
else if(temp == 1){
s += 1;
}
temp = 0;
for(j = 1; j <= 3; j++){
if(board[index][j][i] == 2){
temp++;
}
}
if(temp == 3){
s += 10000;
}
else if(temp == 2){
s += 100;
}
else if(temp == 1){
s += 1;
}
}
for(i = 1; i <= 3; i++){
temp = 0;
for(j = 1; j <= 3; j++){
if(board[index][i][j] == 1){
temp++;
}
}
if(temp == 3){
s -= 10000;
}
else if(temp == 2){
s -= 100;
}
else if(temp == 1){
s -= 1;
}
temp = 0;
for(j = 1; j <= 3; j++){
if(board[index][j][i] == 1){
temp++;
}
}
if(temp == 3){
s -= 10000;
}
else if(temp == 2){
s -= 100;
}
else if(temp == 1){
s -= 1;
}
}
temp = 0;
for(i = 1; i <= 3; i++){
if(board[index][i][i] == 2){
temp ++;
}
}
if(temp == 3){
s += 10100;
}
else if(temp == 2){
s += 110;
}
else if(temp == 1){
s += 2;
}
temp = 0;
for(i = 1; i <= 3; i++){
if(board[index][i][4-i] == 2){
temp ++;
}
}
if(temp == 3){
s += 10100;
}
else if(temp == 2){
s += 110;
}
else if(temp == 1){
s += 2;
}
temp = 0;
for(i = 1; i <= 3; i++){
if(board[index][i][i] == 1){
temp ++;
}
}
if(temp == 3){
s -= 10300;
}
else if(temp == 2){
s -= 230;
}
else if(temp == 1){
s -= 2;
}
temp = 0;
for(i = 1; i <= 3; i++){
if(board[index][i][4-i] == 1){
temp ++;
}
}
if(temp == 3){
s -= 10300;
}
else if(temp == 2){
s -= 230;
}
else if(temp == 1){
s -= 2;
}
return s;
}
void copy(int a, int b){ //b 보드에 a보드를 복사함
int i, j;
for(i = 1; i <= 3; i++){
for(j = 1; j<= 3;j++){
board[b][i][j] = board[a][i][j];
}
}
}
void title(){ //타이틀
ma++;
system("cls");
puts("Tic-Tac-Toe");
int a;
cout << "1.Start 2.Exit : ";
while(1){
cin >> a;
if(a==1){
input();
}
else if(a==2) exit(0);
else cout << "잘못된 입력입니다. 다시 입력해주세요 : " ;
}
}
void re(){ //다시하기 입력받는 함수
char a;
while(1){
cin >> a;
if(a=='Y'||a=='y'){
system("cls");
puts("0 1 2 3"); //제일처음 판 출력
puts("1 - - -");
puts("2 - - -");
puts("3 - - -");
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
board[0][i][j]=0;
}
}
cnt=1;
return ;
}
else if(a=='N'||a=='n'){
exit(0);
}
else cout << "잘못된 입력입니다. 다시 입력해주세요: ";
}
}
void input(){
if(ma==0) title();
system("cls");
int k=0,t=1,cnt1=0,x,y;
puts("0 1 2 3");
puts("1 - - -");
puts("2 - - -");
puts("3 - - -");
while(1){
if(cnt==10){
cout << "무승부 입니다. 다시하시겠습니까? Y/N: ";
re();
}
if(cnt%2!=0) cout << "Player 1의 착수위치를 입력해주세요: ";
else cout << "Player 2의 착수위치를 입력해주세요: ";
while(1){
cin >> y >> x;
if(x<0||x>3||y<0||y>3||board[0][y][x]!=0) cout << "잘못된 입력입니다. 다시 입력해주세요: ";
else break;
}
if(cnt%2!=0){
board[0][y][x]=1; //player1 착수, board[0]은 루트노드
score[0] = sc(0);
for(int i = 1; i <= 9; i++){ //경우의 수 트리에 저장
copy(0, i);
if(board[i][dx[i]][dy[i]] != 0){ //이미 돌이 있는 경우
score[i] = -1000000; //나중에 점수 계산(미니맥스 알고리즘)을 사용할때 논외로 빼놓기 위함
for(int j = 1; j <= 9; j++){
score[i*9+j] = -1000000;
for(int k = 1; k <= 9; k++){
score[(i*9+j)*9+k] = -1000000;
}
}
}
else{
board[i][dx[i]][dy[i]] = 2;
score[i] = sc(i);
for(int j = 1; j <= 9; j++){
copy(i, i*9+j);
if(board[i*9+i][dx[j]][dy[j]] != 0){ //이미 돌이 있는 경우
score[i*9+j] = -1; //나중에 점수 계산(미니맥스 알고리즘)을 사용할때 논외로 빼놓기 위함
for(int k = 1; k <= 9; k++){
score[(i*9+j)*9+k] = -1000000;
}
}
else{
board[i*9+j][dx[j]][dy[j]] = 1;
score[i*9+j] = sc(i*9+j);
for(int k = 1; k <= 9; k++){
copy(i*9+j, (i*9+j)*9+k);
if(board[(i*9+j)*9+k][dx[j]][dy[j]] != 0){ //이미 돌이 있는 경우
score[(i*9+j)*9+k] = -1000000; //나중에 점수 계산(미니맥스 알고리즘)을 사용할때 논외로 빼놓기 위함
}
else{
board[(i*9+j)*9+k][dx[j]][dy[j]] = 2;
score[(i*9+j)*9+k] = sc((i*9+j)*9+k);
}
}
}
}
}
}
}
int temp = tree();
printf("temp : %d\n", temp);
board[0][dx[temp]][dy[temp]] = 2;
cnt++;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(i==0){ //가로줄 번호 출력
cout << k << " ";
k++;
continue;
}
if(j==0){ //세로줄 번호 출력
cout << t << " ";
t++;
continue;
}
if(board[0][i][j]==0) cout << "- ";
else if(board[0][i][j]==1) cout << "O ";
else if(board[0][i][j]==2) cout << "X ";
}
puts("");
}
t=1,k=0;
cnt ++;
}
}
int main(){
input();
}