forked from llvm/llvm-test-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhexxagon.cpp
434 lines (387 loc) · 11.4 KB
/
hexxagon.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
* Hexxagon board game.
* Copyright (C) 2001 Erik Jonsson.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Email erik@nesqi.homeip.net
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <libhexx.h>
void stripFromDblSpace(char *cpfrom)
{
char *cpto = cpfrom;
int space = 1;
while(*cpfrom)
{
if(!(space && (*cpfrom == ' ' || *cpfrom == '\t')))
{
*cpto = *cpfrom;
cpto++;
}
if(*cpfrom == ' ' || *cpfrom == '\t')
space = 1;
else
space = 0;
cpfrom++;
}
*cpto = 0;
}
void printCopy()
{
printf("\n");
printf("Hexxagon board game.\n");
printf("Copyright (C) 2001 Erik Jonsson.\n\n");
printf("The pieces was drawn by Stefan Påhlson.\n\n");
printf("This program is free software; you can redistribute it and/or\n");
printf("modify it under the terms of the GNU General Public License\n");
printf("as published by the Free Software Foundation; either version 2\n");
printf("of the License, or (at your option) any later version.\n\n");
printf("This program is distributed in the hope that it will be useful,\n");
printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
printf("GNU General Public License for more details.\n\n");
printf("You should have received a copy of the GNU General Public License\n");
printf("along with this program; if not, write to the Free Software\n");
printf("Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n\n");
printf("Email erik@nesqi.homeip.net\n\n");
};
void printHelp()
{
printf(" __ \n");
printf(" __/ \\__ \n");
printf(" __/ \\__/ \\__ Hexxagon v0.3.1 \n");
printf(" / \\__/ \\__/ \\ Copyright 2001 \n");
printf(" \\__/ \\__/ \\__/ Erik Jonsson \n");
printf(" / \\__/ \\__/ \\ \n");
printf(" \\__/ \\__/ \\__/ \n");
printf(" \\__/ \\__/ \n");
printf(" \\__/ \n\n");
printf(" erik@nesqi.homeip.net\n");
printf(" http://nesqi.homeip.net/hexxagon\n");
printf("\n");
printf(" Common commands:\n");
printf(" quit : Quit the program.\n");
printf(" help : Print this help.\n");
printf(" copyright : Prints copyright information.\n\n");
printf(" Play related commands:\n");
printf(" jump [FROM][TO] : Jump from eg. A4 to B4.\n");
printf(" j [FROM][TO] : Same as jump.\n");
printf(" clone [TO] : Clone to a position.\n");
printf(" c [TO] : Same as clone.\n");
printf(" hint : Let the computer move for you.\n");
printf(" board : Display the current playboard.\n");
printf(" undo : Go gack one half move. (Undo)\n");
printf(" redo : Goto the next half move. (If you have used \"undo\".)\n\n");
printf(" Game related commands:\n");
printf(" time [seconds] : Max time for the computer to think.\n");
printf(" level [level] : Max search depth for the computer the use.\n");
printf(" load [filename] : Load a game from a save-file.\n");
printf(" save [filename] : Save a game (with history) to a file.\n");
printf(" newgame [CC/HC/CH/HH] : Start a new game:\n");
printf(" HC for human against computer.\n");
printf(" CH for computer against human.\n");
printf(" Same as HC but computer starts.\n");
printf(" CC for computer against computer.\n");
printf(" HH for human against human.\n\n");
printf(" If you just hit enter your last command will be repeated.\n");
printf("\n");
}
enum
{
GAME_HC = 1,
GAME_HH
};
int parseCords(char *str)
{
if(((*str >= 'a' && *str <= 'i') ||
(*str >= 'A' && *str <= 'I')) && (str[1] >= '1' && str[1] <= '9'))
{
int x, y;
if(*str >= 'a' && *str <= 'i')
x = *str - 'a';
else if(*str >= 'A' && *str <= 'I')
x = *str - 'A';
else
return -1;
y = str[1] - '1';
return getHexxagonIndex(x + 1, y + 1);
}
return -1;
}
int main(int argc, char *argv[])
{
int quit = 0;
int mode = 1;
#ifdef SMALL_PROBLEM_SIZE
int level = 3;
#else
int level = 4;
#endif
int time = 12;
int llvm_index;
printf("Hexxagon board game.\n");
printf("Copyright 2001.\n");
printf("Erik Jonsson.\n");
printf("Type \"copyright\" to see the copyright notice.\n\n");
HexxagonGame *game = new HexxagonGame();
game->displayText();
char *input = 0;
for (llvm_index = 0; llvm_index < 1; llvm_index++)
{
char tmp[] = "newgame CC";
int len = strlen(tmp);
if(len && tmp[len - 1] == ' ')
tmp[len - 1] = 0;
if(*tmp == 0)
{
if(input)
printf("Repeating last command.\n");
}
else
{
input = tmp;
}
if(!input)
continue;
if(!strcasecmp("quit", input) || !strcasecmp("exit", input))
{
quit = 1;
}
else if(!strcasecmp("help", input))
printHelp();
else if(!strcasecmp("copyright", input))
printCopy();
else if(!strncasecmp("level ", input, 6))
{
if(!(isdigit(input[6]) && input[7] == 0))
printf("Invalid input to command \"level\".\nValid values are 0-9.\n");
else
level = input[6] - '0';
}
else if(!strcasecmp("level", input))
printf("The search level is set to: %i.\n", level);
else if(!strncasecmp("time ", input, 5))
{
char *cp = input + 5;
while(isdigit(*cp))
cp++;
if(cp == input + 5 || (*cp != 0 && !((*cp == ' ') && *(cp + 1) == 0)))
printf("Syntax: time [number]\n");
else
time = atoi(input + 5);
}
else if(!strcasecmp("time", input))
printf("The search time is set to %i sec.\n", time);
else if(!strcasecmp("board", input))
game->displayText();
else if(!strcasecmp("undo", input))
{
if(game->prev())
printf("No more moves left to undo!\n");
else
game->displayText();
}
else if(!strcasecmp("redo", input))
{
if(game->next())
printf("There is no move to redo!\n");
else
game->displayText();
}
else if(!strcasecmp("hint", input))
{
game->computerMove(level, 0, time * 1000);
game->displayText();
if(mode != GAME_HH)
{
game->computerMove(level, 0, time * 1000);
game->displayText();
}
}
else if(!strncasecmp("load ", input, 5))
{
int ret = game->loadGame(input + 5);
if(ret == 0)
{
printf("Game loaded.\n");
game->displayText();
}
else if(ret == -1)
printf("Error opening load-file!\n");
else if(ret == -3)
printf("Unknown file format.\n");
else
printf("Error reading from load-file!\n");
}
else if(!strcasecmp("load", input))
printf("The load command needs a filename as argument.\n");
else if(!strncasecmp("save ", input, 5))
{
int ret = game->saveGame(input + 5);
if(ret == 0)
printf("Game saved.\n");
else if(ret == -1)
printf("Error opening save-file!\n");
else
printf("Error writing to save-file!\n");
}
else if(!strcasecmp("save", input))
printf("The save command needs a filename as argument.\n");
else if(!strncasecmp("newgame ", input, 8))
{
if(((input[8] == 'c' || input[8] == 'C') || (input[8] == 'h' || input[8] == 'H')) &&
((input[9] == 'c' || input[9] == 'C') || (input[9] == 'h' || input[8] == '9')) &&
(input[10] == 0 || (input[10] == ' ' && input[11] == 0)))
{
delete game;
game = new HexxagonGame();
game->displayText();
if((input[8] == 'h' || input[8] == 'H') &&
(input[9] == 'c' || input[9] == 'C'))
mode = GAME_HC;
else if((input[8] == 'c' || input[8] == 'C') &&
(input[9] == 'h' || input[9] == 'H'))
{
game->computerMove(level, 0, time * 1000);
game->displayText();
mode = GAME_HC;
}
else if((input[8] == 'h' || input[8] == 'H') &&
(input[9] == 'h' || input[9] == 'H'))
mode = GAME_HH;
else if((input[8] == 'c' || input[8] == 'C') &&
(input[9] == 'c' || input[9] == 'C'))
{
while(!game->endOfGame())
{
game->computerMove(level, 0, time * 1000);
game->displayText();
}
mode = GAME_HC;
}
else
printf("You have found a bug in Hexxagon! This code should never be called!!\n");
}
else
printf("Syntax: newgame HC/CH/CC/HH\n");
}
else if(!strcasecmp("newgame", input))
{
printf("Syntax: newgame HC/CH/CC/HH\n");
}
else if(!strncasecmp("jump ", input, 5) ||
!strncasecmp("j ", input, 2))
{
char *cp = input;
if(!strncasecmp("j ", input, 2))
cp += 2;
else
cp += 5;
HexxagonMove move;
int len = strlen(cp);
if(len == 5)
{
move.from = parseCords(cp);
move.to = parseCords(cp + 3);
} else if(len == 4)
{
move.from = parseCords(cp);
move.to = parseCords(cp + 2);
}
else
move.from = -1;
if(move.from != -1 && move.to != -1)
{
if(game->isMoveValid(move))
{
game->applyMove(move);
game->displayText();
if(mode != GAME_HH)
{
game->computerMove(level, 0, time * 1000);
game->displayText();
}
}
else
printf("Illegal move!\n");
}
else
{
if(len == 5)
{
if(cp[2] == ' ')
printf("Bad cordinate.\n");
else
printf("Syntax: jump [FROM][TO]\nEg. \"jump a4b4\"\n");
}
else if(len == 4)
printf("Bad cordinate.\n");
else
printf("Syntax: jump [FROM][TO]\nEg. \"jump a4b4\"\n");
}
}
else if(!strcasecmp("jump", input) ||
!strcasecmp("j", input))
{
printf("Syntax: jump [FROM][TO]\nEg. \"jump a4b4\"\n");
}
else if(!strncasecmp("clone ", input, 6) ||
!strncasecmp("c ", input, 2))
{
char *cp = input;
if(!strncasecmp("c ", input, 2))
cp += 2;
else
cp += 6;
HexxagonMove move;
if(strlen(cp) == 2)
{
move.to = parseCords(cp);
move.from = move.to;
if(move.to != -1)
{
if(game->isMoveValid(move))
{
game->applyMove(move);
game->displayText();
if(mode != GAME_HH)
{
game->computerMove(level, 0, time * 1000);
game->displayText();
}
}
else
printf("Illegal move!\n");
}
else
printf("Invalid cordinate.\n");
}
else
printf("Syntax: clone [TO]\nEg. \"clone b4\"\n");
}
else if(!strcasecmp("clone", input) ||
!strcasecmp("c", input))
{
printf("Syntax: clone [TO]\nEg. \"clone b4\"\n");
}
else
printf("Unknown command, try \"help\" for a list of commands.\n");
}
}