-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbattleship.c
640 lines (615 loc) · 13.9 KB
/
battleship.c
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/*
_
|_)
|_)ATTLESHIP
Authored by abakh <[email protected]>
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include "common.h"
#define MISS -2
#define SEA -1
#define HIT 0
#define NOTHING -1
#define ALL 0x7c
#define RED 3
#define CYAN 2
#define ENGLISH_LETTERS 26
typedef unsigned char bitbox;
bool multiplayer;
byte py,px;//cursor
chtype colors[4]={0};
byte game[2][10][10];//main board
bool computer[2] = {0};
byte score[2] = {0};//set by header()
bitbox sunk[2]={0};
byte just_sunk[2]={0};//to be displayed for human players
byte firstinrowy , firstinrowx ;
byte lastinrowy ,lastinrowx;
byte goindirection;
byte shotinvain;
void sigint_handler(int x){
endwin();
puts("Quit.");
exit(x);
}
void mouseinput(bool ingame){
#ifndef NO_MOUSE
MEVENT minput;
#ifdef PDCURSES
nc_getmouse(&minput);
#else
getmouse(&minput);
#endif
if(minput.bstate & (BUTTON1_CLICKED|BUTTON1_RELEASED)){
if( minput.y-4 < 10){
if( (ingame && minput.x-23<20 && minput.x-23>=0 ) || (!ingame && minput.x-1<20) ){//it most be on the trackboard if ingame is true
py=minput.y-4;
px=(minput.x-1-(ingame*2)) /2;
}
}
else
return;
}
if(minput.bstate & (BUTTON1_CLICKED|BUTTON1_RELEASED))
ungetch('\n');
if(minput.bstate & (BUTTON2_CLICKED|BUTTON2_RELEASED|BUTTON3_CLICKED|BUTTON3_RELEASED) )
ungetch('r');
#endif
}
void rectangle(byte sy,byte sx){
for(byte y=0;y<=10+1;++y){
mvaddch(sy+y,sx,ACS_VLINE);
mvaddch(sy+y,sx+10*2,ACS_VLINE);
}
for(byte x=0;x<=10*2;++x){
mvaddch(sy,sx+x,ACS_HLINE);
mvaddch(sy+10+1,sx+x,ACS_HLINE);
}
mvaddch(sy,sx,ACS_ULCORNER);
mvaddch(sy+10+1,sx,ACS_LLCORNER);
mvaddch(sy,sx+10*2,ACS_URCORNER);
mvaddch(sy+10+1,sx+10*2,ACS_LRCORNER);
}
void print_type(byte type){
switch(type){
case(2):
addstr("patrol boat");
break;
case(3):
addstr("destroyer");
break;
case(4):
addstr("battleship");
break;
case(5):
addstr("carrier");
break;
case(6):
addstr("submarine");
break;
}
}
void MID(byte *y , byte *x, byte direction){
switch(direction){
case(0):
*x=*x-1;
break;
case(1):
*y=*y-1;
break;
case(2):
*x=*x+1;
break;
case(3):
*y=*y+1;
break;
}
}
void genocide(bool side , byte type){
byte y,x;
for(y=0;y<10;++y){
for(x=0;x<10;++x){
if(game[side][y][x] == type)
game[side][y][x] = SEA;
}
}
}
void header(bool side){
score[0]=score[1]=0;
byte y,x;
for(y=0;y<10;++y){
for(x=0;x<10;++x){
if(game[!side][y][x] == HIT)
score[side]++;
if(game[side][y][x] == HIT)
score[!side]++;
}
}
mvaddch(0,1, '_');
mvprintw(1,0,"|_) %2d:%2d",score[side],score[!side]);
mvprintw(2,0,"|_)ATTLESHIP ");
if(multiplayer){
attron(colors[side]);
if(side)
printw("Yellow's turn");
else
printw("Green's turn");
attroff(colors[side]);
}
}
void draw(bool side,byte sy,byte sx,bool regular){//the game's board
rectangle(sy,sx);
chtype ch ;
byte y,x;
for(y=0;y<10;++y){
for(x=0;x<10;++x){
ch =A_NORMAL;
if(y==py && x==px)
ch |= A_STANDOUT;
if(game[side][y][x] == HIT)
ch |= 'X'|colors[RED];
else if(game[side][y][x] > 0 && !(multiplayer&®ular) )
ch |= ACS_BLOCK|colors[side];
else if(game[side][y][x]== MISS)
ch |= 'O'|colors[CYAN];
else if(!(multiplayer&®ular))
ch |= '~'|colors[CYAN];
else
ch |=' ';
mvaddch(sy+1+y,sx+x*2+1,ch);
}
}
}
void draw_trackboard(bool side,byte sy,byte sx){
rectangle(sy,sx);
chtype ch ;
byte y,x;
for(y=0;y<10;++y){
for(x=0;x<10;++x){
ch =A_NORMAL;
if(y==py && x==px-10)
ch |= A_STANDOUT;
if(game[!side][y][x] == HIT)
ch |= '*'|colors[RED];
else if(game[!side][y][x]== MISS)
ch |= '~'|colors[CYAN];
else
ch |= '.';
mvaddch(sy+1+y,sx+x*2+1,ch);
}
}
refresh();
}
void autoset(bool side){
byte y=0,x=0,direction=0, invain=0;
byte realy,realx;
byte l;
for(byte type=2;type<7;++type){
SetLocation:
realy=rand()%10;
realx=rand()%10;
invain=0;
SetDirection:
y=realy;
x=realx;
direction=rand()%4;
for(l=0;(type != 6 && l<type) || (type==6 && l<3) ; ++l){//there are two kinds of ship sized 3 tiles
if( y<0 || x<0 || y>=10 || x>=10 || game[side][y][x] != SEA ){
genocide(side,type);
++invain;
direction= (direction+1)%4;
if(invain<4)
goto SetDirection;
else
goto SetLocation;//endless loop
}
else{
game[side][y][x]=type;
MID(&y,&x,direction);
}
}
}
}
void set_the_board(bool side){
if( computer[side] ){
autoset(side);
return;
}
erase();
mvaddch(0,1, '_');
mvaddstr(1,0,"|_) Set your board");
mvaddstr(2,0,"|_)ATTLESHIP");
mvaddstr(16,0,"Press RETURN to specify the location and press R to rotate the ship.");
int input;
byte y=0,x=0,direction=0, invain=0;
byte realy,realx;
byte l;
py=px=0;
for(byte type=2;type<7;++type){
mvaddstr(15,0,"Put your ");
print_type(type);
addstr(" in its position: ");
SetLocation:
while(1){
draw(side,3,0,false);
refresh();
input = getch();
if( input == KEY_MOUSE )
mouseinput(0);
if( (input=='k' || (input==KEY_UP||input=='w')) && py>0)
--py;
if( (input=='j' || (input==KEY_DOWN||input=='s')) && py<9)
++py;
if( (input=='h' || (input==KEY_LEFT||input=='a')) && px>0)
--px;
if( (input=='l' || (input==KEY_RIGHT||input=='d')) && px<9)
++px;
if( input=='\n'||input==KEY_ENTER )
break;
if( (input=='q'||input==27) )
sigint_handler(EXIT_SUCCESS);
}
realy=y=py;
realx=x=px;
invain=0;
SetDirection:
y=realy;
x=realx;
for(l=0;(type != 6 && l<type) || (type==6 && l<3) ; ++l){//there are two kinds of ship sized 3 tiles
if( y<0 || x<0 || y>=10 || x>=10 || game[side][y][x] != SEA ){
genocide(side,type);
++invain;
direction= (direction+1)%4;
if(invain<4)
goto SetDirection;
else
goto SetLocation;//endless loop
}
else{
game[side][y][x]=type;
MID(&y,&x,direction);
}
}
while(1){
invain=0;
draw(side,3,0,false);
input=getch();
if( input== 'r' || input == 'R' ){
genocide(side,type);
direction= (direction+1)%4;
goto SetDirection;
}
else if(input == KEY_MOUSE)
mouseinput(0);
else
break;
}
}
}
void turn_shift(void){
if(!multiplayer)
return;
char key = 'a'+(rand()%ENGLISH_LETTERS);
int input1,input2,input3;
input1=input2=input3=0;
erase();
beep();
mvaddch(0,1, '_');
mvaddstr(1,0,"|_) Anti-cheater");
mvaddstr(2,0,"|_)ATTLESHIP");
mvaddstr(4,0,"********************");
mvprintw(5,0," Type '%c' 3 times ",key);
mvaddstr(6,0," before ");
mvaddstr(7,0," proceeding ");
mvaddstr(8,0," to the game ");
mvaddstr(10,0,"********************");
refresh();
while(1){
input3=input2;
input2=input1;
input1=getch();
if( (input1==input2) && (input2==input3) && (input3==key) )
break;
}
erase();
}
byte shoot(bool turn, byte y , byte x){
if( y<0 || x<0 || y>9 || x>9 ){ //didn't shoot at all
return NOTHING;
}
byte s = game[!turn][y][x];
if(s==HIT || s==MISS)
return NOTHING;
if(s>0){
game[!turn][y][x]=HIT;
return 1;
}
else{
game[!turn][y][x]=MISS;
return 0;
}
}
void sink_announce(bool side){
byte type,y,x;
for(type=2;type<7;++type){
for(y=0;y<10;++y){
for(x=0;x<10;++x){
if( game[!side][y][x] == type )
goto Next;
}
}
//there is no instance of 'type' in the opponent's board
if( ( (1 << type) | sunk[!side] ) != sunk[!side] ){//if it is not yet announced as sunk
sunk[!side] |= (1 << type);
if(computer[side]){
lastinrowy=lastinrowx=firstinrowy=firstinrowx=-1;
shotinvain=0;
}
else{
just_sunk[!side]=type;//leave to be displayed by you_sunk
}
return;
}
Next:
continue;
}
}
void you_sunk(bool side){
if( just_sunk[!side] == 3)
mvaddstr(15,0,"You have destroyed my destroyer!!");
else if( just_sunk[!side]){
mvaddstr(15,0,"You have sunk my ");
print_type(just_sunk[!side]);
addstr("!!");
}
just_sunk[!side]=0;
}
void cheat(bool side){
/* its actually an anti-cheat, the player can place all their ships adjacent to one another and in the same direction,
and the algorithm will often play in a way that it will be left with one or two isolated tiles being unshot (with their respective ships being shot before).
in a such a situation a human will *very easily* find the tiles with logical thinking, but the computer shoots randomly and it will take such a long time for it
that it will often lose the winning game.
this function still doesn't make a win,it's randomly executed.
if i implemented the logical thinking thing, it would become a difficult, unenjoyable game.*/
byte y,x;
for(y=0;y<10;++y){
for(x=0;x<10;++x){
if(game[!side][y][x]>0){
shoot(side,y,x);
firstinrowy=y;
firstinrowx=x;
return;
}
}
}
}
void decide(bool side){// sink_announce is responsible for unsetting the global variables involved
byte y,x,r;
Again:
if( firstinrowy == NOTHING ){
if( score[side] > 14 && score[side]<score[!side] && rand()%2 ){
cheat(side);
return;
}
while(1){
y = rand()%10;
x = rand()%10;
r = shoot(side,y,x);
if(r == 1){
firstinrowy=y;
firstinrowx=x;
}
if(r != NOTHING)
return;
}
}
else if( lastinrowy ==NOTHING ){
if(goindirection == NOTHING)
goindirection = rand()%4;
while(1){
y= firstinrowy;//we know there is hit already
x= firstinrowx;
MID(&y,&x,goindirection);
r= shoot(side,y,x);
if( r != 1 ){
goindirection = (goindirection+1)%4;//the ship is oriented in another way then
++shotinvain;
if(shotinvain==4){ // this only occurs in case of a ship being shot before but not sunk ( e.g. in exprimenting for the direction)
shotinvain=0;
y=firstinrowy;
x=firstinrowx;
goindirection = (goindirection+1)%4;
while(game[!side][y][x]==HIT){//go till you reach an unshot tile
MID(&y,&x,goindirection);
if( (y<0 || x<0 || y>9 || x>9) && r==NOTHING){
goto Again;
}
}
r= shoot(side,y,x);//(y,x) may be MISS, but its impossible for it to be empty water, as executing this means it has tested every direction before
if(r==1){
lastinrowy=y;//continue from the imaginary firstinrow
lastinrowx=x;
}
if(r==NOTHING)
goto Again;
}
}
else{
lastinrowy= y;
lastinrowx= x;
}
if( r != NOTHING )
return;
}
}
else{
y=lastinrowy;
x=lastinrowx;
MID(&y,&x,goindirection);
r=shoot(side,y,x);
if( r == 1 ){
lastinrowy=y;
lastinrowx=x;
}
else{
lastinrowy=lastinrowx=NOTHING;
goindirection=(goindirection+2)%4;
}
if( r != NOTHING )
return;
else{
goto Again;
}
}
}
void help(bool side){//side is only there to feed header()
erase();
header(side);
attron(A_BOLD);
mvprintw(3,0," **** THE CONTROLS ****");
mvprintw(9,0,"YOU CAN ALSO USE THE MOUSE!");
attroff(A_BOLD);
mvprintw(4,0,"RETURN/ENTER : Shoot");
mvprintw(5,0,"R : Rotate");
mvprintw(6,0,"hjkl/ARROW KEYS : Move cursor");
mvprintw(7,0,"q : Quit");
mvprintw(8,0,"F1 & F2 : Help on controls & gameplay");
mvprintw(11,0,"Press a key to continue");
getch();
erase();
}
void gameplay(bool side){//side is only there to feed header()
erase();
header(side);
attron(A_BOLD);
mvprintw(3,0," **** THE GAMEPLAY ****");
attroff(A_BOLD);
move(4,0);
printw("Guess the location of your opponent's\n");
printw("ships and sink them! The player\n");
printw("who sinks all the opponent's ships wins.");
getch();
erase();
}
int main(int argc,char** argv){
if(argc>1){
printf("This game doesn't take arguments");
}
initscr();
#ifndef NO_MOUSE
mousemask(ALL_MOUSE_EVENTS,NULL);
#endif
curs_set(0);
noecho();
cbreak();
keypad(stdscr,1);
if( has_colors() ){
start_color();
use_default_colors();
init_pair(1,COLOR_GREEN,-1);
init_pair(2,COLOR_YELLOW,-1);
init_pair(3,COLOR_CYAN,-1);
init_pair(4,COLOR_RED,-1);
for(byte b=0;b<4;++b)
colors[b]=COLOR_PAIR(b+1);
}
int input;
printw("Choose type of the game:\n");
printw("1 : Single Player*\n");
printw("2 : Multi Player\n");
refresh();
input=getch();
if(input == '2'){
multiplayer=1;
computer[1]=computer[0]=0;
}
else{
multiplayer=0;
computer[1]=1;
computer[0]=0;
}
Start:
firstinrowy=firstinrowx=lastinrowy=lastinrowx=goindirection=NOTHING;
shotinvain=0;
sunk[0]=sunk[1]=0;
memset(game,SEA,200);
srand(time(NULL)%UINT_MAX);
erase();
set_the_board(0);
turn_shift();
set_the_board(1);
bool won;
bool turn=1;
Turn:
px=10;
py=0;
sink_announce(turn);
if( sunk[0]==ALL ){
won=1;
goto End;
}
else if( sunk[1]==ALL ){
won=0;
goto End;
}
//the turn starts HERE
turn=!turn;
//turn_shift();
if( computer[turn] ){
decide(turn);
goto Turn;
}
else{
erase();
you_sunk(turn);
while(1){
header(turn);
draw(turn,3,0,true);
draw_trackboard(turn,3,22);
refresh();
input=getch();
if(input == KEY_F(1) || input=='?' )
help(turn);
if((input==KEY_F(2)||input=='!') )
gameplay(turn);
if(input == KEY_MOUSE)
mouseinput(1);
if( (input=='k' || (input==KEY_UP||input=='w')) && py>0)
--py;
if( (input=='j' || (input==KEY_DOWN||input=='s')) && py<9)
++py;
if( (input=='h' || (input==KEY_LEFT||input=='a')) && px>10)
--px;
if( (input=='l' || (input==KEY_RIGHT||input=='d')) && px<19)
++px;
if( (input=='q'||input==27))
sigint_handler(EXIT_SUCCESS);
if( input=='\n' || input==KEY_ENTER){
byte r=shoot(turn,py,px-10);
if(r != NOTHING){
goto Turn;
}
}
}
}
End:
erase();
header(won);
draw(won,3,0,false);
draw_trackboard(won,3,22);
if( computer[won] )
mvaddstr(15,0,"Hahaha! I won! ");
else
mvprintw(15,0,"Player %d won the game.",won+1);
addstr(" Wanna play again? (y/n)");
refresh();
curs_set(1);
input=getch();
if( input!='n' && input !='N' && input!='q' ){
curs_set(0);
goto Start;
}
endwin();
return 0;
}