-
Notifications
You must be signed in to change notification settings - Fork 0
/
sea_battle.cpp
714 lines (650 loc) · 16.6 KB
/
sea_battle.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
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/*********************************************
* SEA BATTLE GAME 0.0.0
*
* *******************************************/
/*
* "New BSD License" or "Modified BSD License"
* Copyright 2019 (c) Igor Muravyov
* Redistribution and use in source and binary forms,
* with or without modification, are permitted provided that
* the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder
* nor the names of its contributors may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
//#define DEBUG_BATTLE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define AR_SIZE 10
#define CLEAR(X) memset(X, 0, sizeof X)
#define COPYING "Igor Muravyov (c) 2019"
#define INPUT_SIZE 10
/***************Platform dependent macro ***********************/
/* IO */
#if defined (__unix__)
/* main func linker name */
#define SEA_BATTLE_MAIN main
#include <ctime>
#include <cassert>
/* printf-like */
#define PRINT printf
/* put + go to newline */
#ifndef PUTSTR
#define PUTSTR puts
#endif
/* get one str from input */
#define GETS fgets(cli_line, sizeof cli_line, stdin)
#elif defined (__ba__)
/* main func linker name */
#define SEA_BATTLE_MAIN sea_battle_main
#include <jendefs.h>
#include <stdio_dbg.h>
#include <uart_stream.h>
#include <AppHardwareApi.h>
#include <device_conf.h>
/* printf-like */
#define PRINT pr_cont
/* put + go to newline */
#ifndef PUTSTR
#define PUTSTR(X) PUTS(X); NEWLINE
#endif
/* get one str from input */
#define GETS strncpy(cli_line, input_buffer, sizeof cli_line)
#elif defined (WIN32)
/* main func linker name */
#define SEA_BATTLE_MAIN main
#include <ctime>
#include <cassert>
/* printf-like */
#define PRINT printf
/* put + go to newline */
#ifndef PUTSTR
#define PUTSTR puts
#endif
/* get one str from input */
#define GETS fgets(cli_line, sizeof cli_line, stdin)
#endif /* platform */
/**************************************************************/
/********************** MAY-BE undefined data types ***********/
#ifdef DEBUG_BATTLE
#define __DEBUG__ PRINT
#else
#define __DEBUG__(...)
#endif
#ifndef UNUSED
#define UNUSED
#endif
#ifndef NEWLINE
#define NEWLINE PUTSTR("")
#endif
typedef char* string;
typedef bool state_t;
#ifndef PRIVATE
#define PRIVATE static
#endif
#ifndef GETS
#define GETS
#endif
typedef enum {
SEA = 0,
SEA_VERIFIED,
SHIP_PART,
SHIP_PART_DESTROYED,
} sea_attr;
/**************************************************************/
PRIVATE void platform_init(void);
/******************** CLASSES **************************/
class Battle {
private:
sea_attr area[AR_SIZE][AR_SIZE];
//float probability_area[AR_SIZE][AR_SIZE];
unsigned user_times;
public:
bool is_all_killed(void);
bool is_ship_killed(unsigned X, unsigned Y, unsigned X_busy, unsigned Y_busy);
void print_area(void);
void private_print_area(void);
bool is_near(unsigned X, unsigned Y);
bool is_same(unsigned X, unsigned Y);
bool is_near_destr(unsigned X, unsigned Y);
int set_ship(unsigned X, unsigned Y, bool is_vert, unsigned size);
void set_ships(void);
int explode(unsigned X, unsigned Y);
void reset(void);
unsigned get_tryes(void);
Battle();
};
#define CLI_WORDS 4
#define SIZE_OF_CMD INPUT_SIZE
#define CLI_IS(X) !strcmp(cli_word[0], X)
#define X_CONVERTED(X) strtol(X, NULL, 10) - 1
#define Y_CONVERTED(Y) Y - 'a'
class Cli {
private:
char cli_word[CLI_WORDS][SIZE_OF_CMD];
Battle* battle ;
string find_notspace(string str);
string find_space(string str);
void process(void);
void get_words(void);
public:
char cli_line[INPUT_SIZE];
void init(void);
void start(void);
void run_cmd(void);
Cli(Battle *b);
};
/********************************* RANDOM NUMBER GENERATOR (PLATFORM DEPENDENT) *************/
namespace RNG {
void init(void)
{
#if defined (__unix__) || defined (WIN32)
time_t UNIX_time;
srand((unsigned)time(&UNIX_time));
#elif defined (__ba__)
vAHI_StartRandomNumberGenerator(E_AHI_RND_CONTINUOUS, E_AHI_INTS_DISABLED);
#endif
}
int get(int from, int to)
{
#if defined (__unix__) || defined (WIN32)
return (from + rand() % (to + 1));
#elif defined (__ba__)
while(bAHI_RndNumPoll() == FALSE);
return (from + u16AHI_ReadRandomNumber() % (to + 1));
#else
return to - from;
#endif
}
float get( float from, float to )
{
float scale;
#if defined (__unix__) || defined (WIN32)
scale = rand() / (float) RAND_MAX; /* [0, 1.0] */
#elif defined (__ba__)
scale = u16AHI_ReadRandomNumber() / (float) (-1); /* [0, 1.0] */
#endif
return from + scale * ( to - from ); /* [from, to] */
}
};
/********************************************************************************************/
/******************** DATA **************************/
/* GLOBAL VARIABLES */
#ifdef __ba__
PRIVATE UNUSED char input_buffer[INPUT_SIZE];
#endif
Battle b1 = Battle();
Cli cli_main = Cli(&b1);
/********************/
/******************** *******************************/
/******************** EXPORTED FUNCTIONS ************/
/****************************************************/
/********************************* SEA Battle CLI *******************************************/
string Cli::find_notspace(string str)
{
if (str == NULL)
return NULL;
while (*(str++) == ' ') ;
return str - 1;
}
string Cli::find_space(string str)
{
if (str == NULL)
return NULL;
while (*(str++) != ' ') {
if (*str == '\0' || *str == '\n') {
return str;
}
}
return str - 1;
}
void Cli::get_words(void)
{
memset(cli_word, 0, sizeof cli_word);
int w_num = 0;
string w_n_start = find_notspace(cli_line);
string w_n_1_st;
for (w_num = 0; w_num < CLI_WORDS; w_num++) {
w_n_1_st = find_notspace(find_space(w_n_start));
if (find_space(w_n_start) != NULL) {
*find_space(w_n_start) = '\0';
strcpy(cli_word[w_num], w_n_start);
}
w_n_start = w_n_1_st;
}
}
void Cli::init(void)
{
CLEAR(cli_line);
CLEAR(cli_word);
PRINT(">>> ");
}
void Cli::process(void)
{
if (CLI_IS("kill")) {
unsigned X = X_CONVERTED(cli_word[1]);
unsigned Y = Y_CONVERTED(cli_word[2][0]);
if(!(X<AR_SIZE) || !(Y<AR_SIZE)) {
PUTSTR("Wrong direction");
return;
}
int result = battle->explode(X, Y);
#ifdef DEBUG_BATTLE
battle->print_area();
#else
battle->private_print_area();
#endif
PRINT("This is your %d try\n\r", battle->get_tryes());
if( result!= EXIT_SUCCESS) {
PUTSTR("Seems you missed");
return;
}
if(battle->is_ship_killed(X, Y, 100, 100) == true) {
PUTSTR("KILLED!");
if(battle->is_all_killed()) {
PRINT("YOU WON WITH SCORE %d !!!!!!!!!!!!!", battle->get_tryes());
}
}
else {
PUTSTR("WOUNDED!");
}
} else if(CLI_IS("exit")) {
PRINT("exiting...\n\r");
#if defined (__unix__) || defined (WIN32)
exit(0);
#elif defined (__ba__)
PRINT("resetting device\n\r");
CPU_RESET;
#endif
}
}
void Cli::start(void)
{
while (1) {
init();
GETS;
get_words();
process();
}
}
void Cli::run_cmd(void)
{
GETS;
get_words();
process();
}
Cli::Cli(Battle *b)
{
battle = b;
init();
}
/********************************************************************************************/
/********************************* SEA Battle GAME LOGIC ************************************/
void Battle::reset(void)
{
CLEAR(area);
//CLEAR(probability_area);
RNG::init();
}
void Battle::print_area(void)
{
PUTSTR("\n\r \t a b c d e f g h i j");
int i;
int j;
for (i = 0; i < AR_SIZE; i++) {
PRINT("\n\r%d\t", i + 1);
for (j = 0; j < AR_SIZE; j++) {
switch (area[i][j]) {
case SEA:
PRINT(" ~ ");
break;
case SEA_VERIFIED:
PRINT(" ~*");
break;
case SHIP_PART:
PRINT("[ ]");
break;
case SHIP_PART_DESTROYED:
PRINT("[X]");
break;
default:
PRINT(" ? ");
}
}
}
NEWLINE;
}
void Battle::private_print_area(void)
{
PUTSTR("\n\r \t a b c d e f g h i j");
int i;
int j;
for (i = 0; i < AR_SIZE; i++) {
PRINT("\n\r%d\t", i + 1);
for (j = 0; j < AR_SIZE; j++) {
switch (area[i][j]) {
case SEA:
PRINT(" ~ ");
break;
case SEA_VERIFIED:
PRINT(" ~*");
break;
case SHIP_PART:
PRINT(" ~ ");
break;
case SHIP_PART_DESTROYED:
PRINT("[X]");
break;
default:
PRINT(" ? ");
}
}
}
NEWLINE;
}
bool Battle::is_ship_killed(unsigned X, unsigned Y, unsigned X_busy, unsigned Y_busy)
{
__DEBUG__("X %d Y %d Xb %d Yb %d", X, Y, X_busy, Y_busy);
if(area[X][Y] != SHIP_PART_DESTROYED)
return false;
if(is_near(X, Y)) {
/* something near is alive */
return false;
}
__DEBUG__("main part is not alive");
if ((X+1) != X_busy && Y != Y_busy && (X < AR_SIZE) && area[X + 1][Y] == SHIP_PART_DESTROYED) {
if(!is_ship_killed(X+1, Y, X, Y)) {
return false;
}
}
if ((X-1) != X_busy && Y != Y_busy && (X > 0) && area[X - 1][Y] == SHIP_PART_DESTROYED) {
if(!is_ship_killed(X-1, Y, X, Y)) {
return false;
}
}
/*
if (X != X_busy && Y != Y_busy && area[X][Y] == SHIP_PART_DESTROYED) {
if(!is_ship_killed(X, Y, X, Y)) {
return false;
}
}*/
if (X - 1 != X_busy && Y + 1 != Y_busy && (X > 0) && (Y < AR_SIZE) && area[X - 1][Y + 1] == SHIP_PART_DESTROYED) {
if(!is_ship_killed(X-1, Y+1, X, Y)) {
return false;
}
}
if (X + 1 != X_busy && Y + 1 != Y_busy && (X < AR_SIZE) && (Y < AR_SIZE) && area[X + 1][Y + 1] == SHIP_PART_DESTROYED) {
if(!is_ship_killed(X+1, Y+1, X, Y)) {
return false;
}
}
if (X != X_busy && Y + 1 != Y_busy && (Y < AR_SIZE) && area[X][Y + 1] == SHIP_PART_DESTROYED) {
if(!is_ship_killed(X, Y+1, X, Y)) {
return false;
}
}
if (X - 1 != X_busy && Y - 1 != Y_busy && (X > 0) && (Y > 0) && area[X - 1][Y - 1] == SHIP_PART_DESTROYED) {
if(!is_ship_killed(X-1, Y-1, X, Y)) {
return false;
}
}
if (X + 1 != X_busy && Y - 1 != Y_busy && (Y > 0) && area[X + 1][Y - 1] == SHIP_PART_DESTROYED) {
if(!is_ship_killed(X+1, Y-1, X, Y)) {
return false;
}
}
if (X != X_busy && Y - 1 != Y_busy && (Y > 0) && area[X][Y - 1] == SHIP_PART_DESTROYED) {
if(!is_ship_killed(X, Y-1, X, Y)) {
return false;
}
}
return true;
}
bool Battle::is_same(unsigned X, unsigned Y)
{
if(is_near_destr(X, Y) || is_near(X, Y))
return true;
return false;
}
bool Battle::is_near_destr(unsigned X, unsigned Y)
{
if ((X < AR_SIZE) && area[X + 1][Y] == SHIP_PART_DESTROYED) {
return true;
}
if ((X > 0) && area[X - 1][Y] == SHIP_PART_DESTROYED) {
return true;
}
if (area[X][Y] == SHIP_PART_DESTROYED) {
return true;
}
if ((X > 0) && (Y < AR_SIZE) && area[X - 1][Y + 1] == SHIP_PART_DESTROYED) {
return true;
}
if ((X < AR_SIZE) && (Y < AR_SIZE) && area[X + 1][Y + 1] == SHIP_PART_DESTROYED) {
return true;
}
if ((Y < AR_SIZE) && area[X][Y + 1] == SHIP_PART_DESTROYED) {
return true;
}
if ((X > 0) && (Y > 0) && area[X - 1][Y - 1] == SHIP_PART_DESTROYED) {
return true;
}
if ((Y > 0) && area[X + 1][Y - 1] == SHIP_PART_DESTROYED) {
return true;
}
if ((Y > 0) && area[X][Y - 1] == SHIP_PART_DESTROYED) {
return true;
}
return false;
}
bool Battle::is_near(unsigned X, unsigned Y)
{
if ((X < AR_SIZE) && area[X + 1][Y] == SHIP_PART) {
return true;
}
if ((X > 0) && area[X - 1][Y] == SHIP_PART) {
return true;
}
if (area[X][Y] == SHIP_PART) {
return true;
}
if ((X > 0) && (Y < AR_SIZE) && area[X - 1][Y + 1] == SHIP_PART) {
return true;
}
if ((X < AR_SIZE) && (Y < AR_SIZE) && area[X + 1][Y + 1] == SHIP_PART) {
return true;
}
if ((Y < AR_SIZE) && area[X][Y + 1] == SHIP_PART) {
return true;
}
if ((X > 0) && (Y > 0) && area[X - 1][Y - 1] == SHIP_PART) {
return true;
}
if ((Y > 0) && area[X + 1][Y - 1] == SHIP_PART) {
return true;
}
if ((Y > 0) && area[X][Y - 1] == SHIP_PART) {
return true;
}
return false;
}
int Battle::set_ship(unsigned X, unsigned Y, bool is_vert, unsigned size)
{
assert(size <= 4);
assert(X < AR_SIZE);
assert(Y < AR_SIZE);
unsigned X_ver = X;
unsigned Y_ver = Y;
unsigned i;
if (is_vert) {
for (i = 0; i < size; i++) {
if (is_near(X_ver, Y_ver)) {
return EXIT_FAILURE;
}
Y_ver--;
}
} else {
for (i = 0; i < size; i++) {
if (is_near(X_ver, Y_ver)) {
return EXIT_FAILURE;
}
X_ver--;
}
}
__DEBUG__("X=%d, Y=%d, vert=%d, size=%d\n", X, Y, is_vert, size);
if (is_vert) {
for (i = 0; i < size; i++) {
area[X][Y--] = SHIP_PART;
}
} else {
for (i = 0; i < size; i++) {
area[X--][Y] = SHIP_PART;
}
}
return EXIT_SUCCESS;
}
void Battle::set_ships(void)
{
__DEBUG__("setting ships");
/* one 4 */
while (set_ship(RNG::get(3, AR_SIZE - 4),
RNG::get(3, AR_SIZE - 4),
RNG::get(0, 1), 4) == EXIT_FAILURE) ;
__DEBUG__("set 4 - layer");
/* two 3 */
while (set_ship(RNG::get(2, AR_SIZE - 3),
RNG::get(2, AR_SIZE - 3),
RNG::get(0, 1), 3) == EXIT_FAILURE) ;
while (set_ship(RNG::get(2, AR_SIZE - 3),
RNG::get(2, AR_SIZE - 3),
RNG::get(0, 1), 3) == EXIT_FAILURE) ;
__DEBUG__("set 3 - layer");
/* three 2 */
while (set_ship(RNG::get(1, AR_SIZE - 2),
RNG::get(1, AR_SIZE - 2),
RNG::get(0, 1), 2) == EXIT_FAILURE) ;
while (set_ship(RNG::get(1, AR_SIZE - 2),
RNG::get(1, AR_SIZE - 2),
RNG::get(0, 1), 2) == EXIT_FAILURE) ;
while (set_ship(RNG::get(1, AR_SIZE - 2),
RNG::get(1, AR_SIZE - 2),
RNG::get(0, 1), 2) == EXIT_FAILURE) ;
__DEBUG__("set 2 - layer");
/* four 1 */
while (set_ship(RNG::get(0, AR_SIZE - 1),
RNG::get(0, AR_SIZE - 1),
RNG::get(0, 1), 1) == EXIT_FAILURE) ;
while (set_ship(RNG::get(0, AR_SIZE - 1),
RNG::get(0, AR_SIZE - 1),
RNG::get(0, 1), 1) == EXIT_FAILURE) ;
while (set_ship(RNG::get(0, AR_SIZE - 1),
RNG::get(0, AR_SIZE - 1),
RNG::get(0, 1), 1) == EXIT_FAILURE) ;
while (set_ship(RNG::get(0, AR_SIZE - 1),
RNG::get(0, AR_SIZE - 1),
RNG::get(0, 1), 1) == EXIT_FAILURE) ;
__DEBUG__("set 1 - layer");
}
int Battle::explode(unsigned X, unsigned Y)
{
assert(X < AR_SIZE);
assert(Y < AR_SIZE);
user_times++;
if (area[X][Y] == SEA) {
area[X][Y] = SEA_VERIFIED;
return EXIT_FAILURE;
} else if (area[X][Y] == SHIP_PART) {
area[X][Y] = SHIP_PART_DESTROYED;
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
unsigned Battle::get_tryes(void)
{
return user_times;
}
Battle::Battle(void)
{
user_times = 0;
}
bool Battle::is_all_killed(void)
{
int i;
int j;
for(i=0; i<AR_SIZE; i++) {
for(j=0; j<AR_SIZE; j++) {
if(area[i][j] == SHIP_PART) {
return false;
}
}
}
return true;
}
/********************************************************************************************/
extern "C" {
int SEA_BATTLE_MAIN(void);
}
int SEA_BATTLE_MAIN(void)
{
/* platform dependent initialization */
platform_init();
/*************************************/
PUTSTR("generating ships via TRUE RNG... ");
b1.reset();
PUTSTR(COPYING);
PUTSTR("you are playing SEA Battle game... ");
b1.set_ships();
#if defined (__unix__) || defined (WIN32)
cli_main.start();
#endif
return EXIT_SUCCESS;
}
#ifdef __ba__
void getc_callback(char ch)
{
static unsigned i_pointer = 0;
if(i_pointer >= sizeof input_buffer) {
PUTSTR("WARN: out of bounds");
CLEAR(input_buffer);
i_pointer = 0;
return;
}
if(ch == '\r') {
Cli cli = Cli(&b1);
NEWLINE;
/* set flag */
input_buffer[i_pointer] = '\0';
i_pointer = 0;
cli.run_cmd();
return;
}
PRINT("%c", ch);
input_buffer[i_pointer++] = ch;
}
#endif /* BEYOND ARCHITECTURE */
PRIVATE void platform_init(void)
{
/* stop console input*/
#ifdef __ba__
uart1_callback = getc_callback;
#endif /* BA */
}
/********************************************************************************************/