-
Notifications
You must be signed in to change notification settings - Fork 0
/
battle_ship test 31.c
1852 lines (1637 loc) · 41.8 KB
/
battle_ship test 31.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
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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <Windows.h>
//set colors:
#define RED "\x1b[31m"
#define GREEN "\x1b[32m"
#define YELLOW "\x1b[33m"
#define BLUE "\x1b[34m"
#define MAGENTA "\x1b[35m"
#define CYAN "\x1b[36m"
#define RESET "\x1b[0m"
struct ships {
int x1, y1, x2, y2, len, damage;
struct ships* next;
}__attribute__((packed));
struct player {
char name[100];
int score;
struct ships* ships_head;
char naghshe_nobat[10][10];
}__attribute__((packed));
struct acc {
char name[100];
int score;
}__attribute__((packed));
struct player_game_saver {
char name[100];
int score;
int ships_count;
int ships_x1[10], ships_x2[10], ships_y1[10], ships_y2[10] , ships_len[10] , ships_damage[10];
char naghshe_nobat_[10][10];
}__attribute__((packed));
struct string_s{
char str[200];
}__attribute__((packed));
struct point {
int x, y;
}__attribute__((packed));
//declaration:
int qadr(int);
int minn(int, int);
int maxx(int, int);
void put_ships(struct player*);
void menu(struct player* p1, struct player* p2, int* game_method_p);
struct ships* new_ship(int x1, int y1, int x2, int y2, int len);
struct ships * old_ship(int x1 , int y1 , int x2 , int y2 , int len , int damage);
void add_ship_end(struct player* p, struct ships* new_sh);
int is_ship_valid(struct player p1, int x1, int y1, int x2, int y2, int len , int print_error);// panic
int print_available_users_and_get_name_score(struct player* p);
int is_in_acc_list(char nam[100]);
void put_new_acc_on_acc_list(char nam[100]);
int find_score(char nam[100]);
void show_score_board(void);
void sort_acc_list(void);
void set_bot(struct player*);
void play_with_bot(struct player* p1 , struct player * p2);//p2 is the bot
void play_togather(struct player*, struct player*);
void set_maps_to_space(struct player* p);
void print_map(struct player*);
int is_point_valid_to_attack(struct player* p, int x, int y);
int ship_counter(struct player * p);
struct player_game_saver make_game_saver(struct player * p);
void save_game(struct player* p1, struct player* p2 , int game_method);
int is_in_saved_game_list(struct string_s game_name);
struct string_s show_saved_games_and_get_name(void);
void load_game(struct player * p1 , struct player * p2 , struct string_s game_name , int * game_method_p);
void set_player(struct player * p , struct player_game_saver p_s);
void attack(struct player* p, int x, int y, int* jayze, int* score);
void delete_ship(struct player* p, struct ships* target);
void complete_explosion(struct player* p, struct ships* destroyed_ship);
void update_map(struct player* p);
int is_point_valid_to_check(int x, int y);
void update_scores(struct player* p1, struct player* p2 , int game_method);
struct point bot_choice (struct player * p );
int number_of_saved_users(void);
void hitlers_Nuke(struct player * p1, struct player * p2);
void random_ships(struct player * p);
void empty_army(struct player * p);
void print_ships(struct player * p);
//details:
int main() {
struct player p1, p2, bot;
//set_bot(&bot);//ino baadan benevis
int i, j, game_method;
srand(time(0));
printf("!!! WELCOME TO RAD'S BATTLE SHIP GAME !!!\n");
while (1) {
menu(&p1, &p2, &game_method);
if (game_method == 1) {
play_with_bot(&p1 , &p2);
}
else if (game_method == 2) {
play_togather(&p1, &p2);//ino baadan benevis
}
}
}
int qadr(int a) {
if (a >= 0) {
return a;
}
return (-1 * a);
}
int minn(int a, int b) {
if (a < b) {
return a;
}
return b;
}
int maxx(int a, int b) {
if (a < b) {
return b;
}
return a;
}
void put_ships(struct player* p) {
int ans , ans2;
struct ships* curr;
int x1, y1, x2, y2, len, i;
p->ships_head = NULL;
printf("Enter 1 to put ships manually or 2 to do it automatically(randomly) .\n");
while(1){
scanf("%d" , &ans);
if((ans == 1)||(ans == 2)){
break;
}
}
if(ans == 2){
random_ships(p);
print_ships(p);
printf("is your army ok? (1 for yes and 0 for no)\n");
scanf("%d" , &ans2);
if(ans2 == 1)
{
return;
}
else
{
empty_army(p);
put_ships(p);
return;
}
}
printf("now you put your ships ; remember that you set two points for each ship and you enter the row then column of each point. (<the number of row> space <the number of column>)\n all inputs must be betwin 0 , 9 (including both) \n\n");
len = 5;
do {
printf("Enter position of ship 1 * 5 : \n");
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
} while (is_ship_valid(*p, x1, y1, x2, y2, len , 1) == 0);
p->ships_head = new_ship(x1, y1, x2, y2, len);
len = 3;
do
{
printf("Enter position of first ship 1 * 3 : \n");
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
} while (is_ship_valid(*p, x1, y1, x2, y2, len , 1) == 0);
add_ship_end(p, new_ship(x1, y1, x2, y2, len));
do
{
printf("Enter position of second ship 1 * 3 : \n");
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
} while (is_ship_valid(*p, x1, y1, x2, y2, len , 1) == 0);
add_ship_end(p, new_ship(x1, y1, x2, y2, len));
len = 2;
for (i = 1; i <= 3; i++) {
do
{
printf("Enter position of %dth ship 1 * 2 : \n", i);
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
} while (is_ship_valid(*p, x1, y1, x2, y2, len , 1) == 0);
add_ship_end(p, new_ship(x1, y1, x2, y2, len));
}
len = 1;
for (i = 1; i <= 4; i++) {
do
{
printf("Enter position of %dth ship 1 * 1 (only one point for this kind of ship): \n", i);
scanf("%d%d", &x1, &y1);
} while (is_ship_valid(*p, x1, y1, x1, y1, len , 1) == 0);
add_ship_end(p, new_ship(x1, y1, x1, y1, len));
}
print_ships(p);
Sleep(10000);
}
void menu(struct player* p1, struct player* p2, int* game_method_p) {
int ans1, ans2, temp;
char nam[100];
printf("1. Play with a Friend\n2. Play with bot\n3. Load game\n4. Load last game\n5. Settings\n6. Score Board\n7. Developer information\n8. Exit\n");
scanf("%d", &ans1);
if (ans1 == 1)
{
set_maps_to_space(p1);
set_maps_to_space(p2);
*game_method_p = 2;//means 2 player game
printf("for player 1 :\n1. Choose user\n1. choose from available users\n2. new user\n");
scanf("%d", &ans2);
if (ans2 == 1) {
temp = print_available_users_and_get_name_score(p1);
if (temp == -1 || number_of_saved_users() < 1) {
ans2 = 2;
}
}
if (ans2 == 2)
{
while (1) {
printf("please enter the name : \n");
//scanf("%s", nam);
fflush(stdin);
gets(nam);
if (is_in_acc_list(nam) == 1) {
printf("this name is in the account list . \n choose another name:\n");
}
else {
strcpy(p1->name, nam);
put_new_acc_on_acc_list(p1->name);
p1->score = 0;
break;
}
}
}
printf("now put your ships %s\n", p1->name);
put_ships(p1);
system("cls");
printf("for player 2 :\n1. Choose user\n1. choose from available users\n2. new user\n");
scanf("%d", &ans2);
if (ans2 == 1) {
if (temp == -1 || number_of_saved_users() < 2) {
printf("no available users yet .\n");
ans2 = 2;
}
else{
do{
print_available_users_and_get_name_score(p2);
if(strcmp(p2->name , p1->name) == 0){
printf("this account is already chosen\n pick another one .\n");
}
} while(strcmp(p1->name , p2->name) == 0);
}
}
if (ans2 == 2) {
while (1)
{
printf("please enter the name :\n");
//scanf("%s", nam);
fflush(stdin);
gets(nam);
if (is_in_acc_list(nam) == 1) {
printf("this name is in the account list . \n choose another name:\n");
}
else {
strcpy(p2->name, nam);
put_new_acc_on_acc_list(p2->name);
p2->score = 0;
break;
}
}
}
printf("now put your ships %s\n", p2->name);
put_ships(p2);
}
else if (ans1 == 2)
{
set_maps_to_space(p1);
set_maps_to_space(p2);
*game_method_p = 1;// 1 player game
printf("for player 1 :\n1. Choose user\n1. choose from available users\n2. new user\n");
scanf("%d", &ans2);
if (ans2 == 1)
{
temp =print_available_users_and_get_name_score(p1);
if((temp == -1) || number_of_saved_users() < 1){
ans2 = 2;
}
}
if (ans2 == 2)
{
while (1)
{
printf("please enter the name :\n");
//scanf("%s", nam);
fflush(stdin);
gets(nam);
if (is_in_acc_list(nam) == 1)
{
printf("this name is in the account list . \n choose another name:\n");
}
else {
strcpy(p1->name, nam);
put_new_acc_on_acc_list(p1->name);
p1->score = 0;
break;
}
}
}
printf("now put your ships %s\n",p1->name);
put_ships(p1);
set_bot(p2);
}
else if(ans1 == 3){
struct string_s game_name;
game_name = show_saved_games_and_get_name();
if(strcmp(game_name.str , "none")==0){
*game_method_p = 0;
return; //bazgasht be menu;
}
load_game(p1,p2,game_name , game_method_p);
}
else if (ans1 == 4)
{
struct string_s last_game_name;
strcpy(last_game_name.str , "last game");
load_game(p1, p2,last_game_name ,game_method_p);
}
else if (ans1 == 5)
{
printf("no settings yet :))))\n");
Sleep(2000);
system("cls");
menu(p1, p2, game_method_p);
}
else if (ans1 == 6)
{
sort_acc_list();
show_score_board();
//getch();
menu(p1, p2, game_method_p);
}
else if(ans1 == 7)
{
printf("CREATED BY :\n MOHAMMAD HEYDARI RAD \n 1399\n UNIVERSITY : AUT \n ID : 9931017\n\n");
menu(p1, p2 , game_method_p);
return;
}
else if (ans1 == 8) {
system("cls");
printf("bye bye\n");
Sleep(3000);
exit(0);
}
else {
printf("not valid input");
Sleep(2000);
system("cls");
menu(p1, p2, game_method_p);
}
return;
}
struct ships* new_ship(int x1, int y1, int x2, int y2, int len) {
struct ships* new_sh = (struct ships*)malloc(sizeof(struct ships));
new_sh->x1 = x1;
new_sh->x2 = x2;
new_sh->y1 = y1;
new_sh->y2 = y2;
new_sh->len = len;
new_sh->damage = 0;
new_sh->next = NULL;
return new_sh;
}
struct ships* old_ship(int x1 , int y1 , int x2 , int y2 , int len, int damage){
struct ships* old_sh = new_ship(x1,y1,x2,y2,len);
old_sh->damage = damage;
return old_sh;
}
void add_ship_end(struct player* p, struct ships* new_sh) {
struct ships* curr;
if (p->ships_head == NULL) {
p->ships_head = new_sh;
}
else {
for (curr = p->ships_head; curr->next != NULL; curr = curr->next);
curr->next = new_sh;
new_sh->next = NULL;
}
return;
}
int is_ship_valid(struct player p, int x1, int y1, int x2, int y2, int len , int print_errors) {
struct ships* curr;
int i, j, max_x, max_y;
int map_valid_test[10][10] = { 0 };
if ((x1 != x2) && (y1 != y2))
{
if(print_errors)
{
printf("invalid ship .\n");
printf("broken ship .\n");
}
return 0;
}
else if ((x1 > 9) || (x2 > 9) || (y1 > 9) || (y2 > 9) || (x1 < 0) || (x2 < 0) || (y1 < 0) || (y2 < 0))
{
if(print_errors)
{
printf("invalid ship .\n");
printf("not correctly fixed in the map .\n");
}
return 0;
}
else if((qadr(x1-x2) != len-1) && (qadr(y1-y2) != len-1)){
if(print_errors)
{
printf("invalid ship .\n");
printf("not correct ship length .\n");
}
return 0;
}
else if (p.ships_head == NULL)
{
return 1;
}
//else :
for (curr = p.ships_head; curr != NULL; curr = curr->next) {
if (curr->x1 == curr->x2) { //horizenal
for (j = minn(curr->y1, curr->y2); j <= maxx(curr->y1, curr->y2); j++)
{
map_valid_test[curr->x1][j] += 100;
if (curr->x1 < 9)
{
map_valid_test[curr->x1 + 1][j] += 1;
}
if (curr->x1 > 0)
{
map_valid_test[curr->x1 - 1][j] += 1;
}
}
if (maxx(curr->y1, curr->y2) < 9)
{
map_valid_test[curr->x1][maxx(curr->y1, curr->y2) + 1] += 1;
if (curr->x1 > 0)
{
map_valid_test[curr->x1 - 1][maxx(curr->y1, curr->y2) + 1] += 1;
}
if (curr->x1 < 9)
{
map_valid_test[curr->x1 + 1][maxx(curr->y1, curr->y2) + 1] += 1;
}
}
if (minn(curr->y1, curr->y2) > 0)
{
map_valid_test[curr->x1][minn(curr->y1, curr->y2) - 1] += 1;
if (curr->x1 > 0)
{
map_valid_test[curr->x1 - 1][minn(curr->y1, curr->y2) - 1] += 1;
}
if (curr->x1 < 9)
{
map_valid_test[curr->x1 + 1][minn(curr->y1, curr->y2) - 1] += 1;
}
}
}
else if (curr->y1 == curr->y2) //VERTICAL
{
for (j = minn(curr->x1, curr->x2); j <= maxx(curr->x1, curr->x2); j++)
{
map_valid_test[j][curr->y1] += 100;
if (curr->y1 < 9)
{
map_valid_test[j][curr->y1 + 1] += 1;
}
if (curr->y1 > 0)
{
map_valid_test[j][curr->y1 - 1] += 1;
}
}
if (maxx(curr->x1, curr->x2) < 9)
{
map_valid_test[maxx(curr->x1, curr->x2) + 1][curr->y1] += 1;
if (curr->y1 > 0)
{
map_valid_test[maxx(curr->x1, curr->x2) + 1][curr->y1 - 1] += 1;
}
if (curr->y1 < 9)
{
map_valid_test[maxx(curr->x1, curr->x2) + 1][curr->y1 + 1] += 1;
}
}
if (minn(curr->x1, curr->x2) > 0)
{
map_valid_test[minn(curr->x1, curr->x2) - 1][curr->y1] += 1;
if (curr->y1 > 0)
{
map_valid_test[minn(curr->x1, curr->x2) - 1][curr->y1 - 1] += 1;
}
if (curr->y1 < 9)
{
map_valid_test[minn(curr->x1, curr->x2) - 1][curr->y1 + 1] += 1;
}
}
}
}
if (x1 == x2)
{ //horizenal
if (qadr(y1 - y2) != len - 1)
{
return 0;
}
for (j = minn(y1, y2); j <= maxx(y1, y2); j++)
{
map_valid_test[x1][j] += 100;
if (x1 < 9)
{
map_valid_test[x1 + 1][j] += 1;
}
if (x1 > 0)
{
map_valid_test[x1 - 1][j] += 1;
}
}
if (maxx(y1, y2) < 9)
{
map_valid_test[x1][maxx(y1, y2) + 1] += 1;
if (x1 > 0)
{
map_valid_test[x1 - 1][maxx(y1, y2) + 1] += 1;
}
if (x1 < 9)
{
map_valid_test[x1 + 1][maxx(y1, y2) + 1] += 1;
}
}
if (minn(y1, y2) > 0)
{
map_valid_test[x1][minn(y1, y2) - 1] += 1;
if (x1 > 0)
{
map_valid_test[x1 - 1][minn(y1, y2) - 1] += 1;
}
if (x1 < 9)
{
map_valid_test[x1 + 1][minn(y1, y2) - 1] += 1;
}
}
}
else if (y1 == y2)//vertical
{
if (qadr(x1 - x2) != len - 1)
{
return 0;
}
for (j = minn(x1, x1); j <= maxx(x1, x2); j++)
{
map_valid_test[j][y1] += 100;
if (y1 < 9)
{
map_valid_test[j][y1 + 1] += 1;
}
if (y1 > 0)
{
map_valid_test[j][y1 - 1] += 1;
}
}
if (maxx(x1, x2) < 9)
{
map_valid_test[maxx(x1, x2) + 1][y1] += 1;
if (y1 > 0)
{
map_valid_test[maxx(x1, x2) + 1][y1 - 1] += 1;
}
if (y1 < 9)
{
map_valid_test[maxx(x1, x2) + 1][y1 + 1] += 1;
}
}
if (minn(x1, x2) > 0)
{
map_valid_test[minn(x1, x2) - 1][y1] += 1;
if (y1 > 0)
{
map_valid_test[minn(x1, x2) - 1][y1 - 1] += 1;
}
if (y1 < 9)
{
map_valid_test[minn(x1, x2) - 1][y1 + 1] += 1;
}
}
}
int test;
/*for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
printf("%4d", map_valid_test[i][j]);
}
printf("\n");
}*/
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
test = map_valid_test[i][j];
if (test > 100)
{
if(print_errors)
{
printf("invalid ship .\n");
printf("crashed ships .\n");
}
return 0;
}
}
}
return 1;
}
int print_available_users_and_get_name_score(struct player* p) {
char nam[100];
struct acc account;
FILE* fp = fopen("accounts.bin", "rb");
if (fp == NULL) {
printf("no available users to choose\n please enter new one\n");
return -1;
}
while (1) {
fread(&account, sizeof(struct acc), 1, fp);
if (feof(fp))
{
fclose(fp);
break;
}
else
{
strcpy(nam, account.name);
printf("%s\n", nam);
}
}
do {
printf("please enter the exact name :\n");
//scanf("%s", nam);
fflush(stdin);
gets(nam);
} while (is_in_acc_list(nam) == 0);
strcpy((p->name), nam);
//p->score = find_score(p->name);
p->score = 0;
return 1;
}
int is_in_acc_list(char nam[]) {
struct acc account;
FILE* fp = fopen("accounts.bin", "rb");
if (fp == NULL) {
return 0;
}
while (1) {
fread(&account, sizeof(struct acc), 1, fp);
if (feof(fp)) {
return 0;
}
else if (strcmp(account.name, nam) == 0) {
return 1;
}
}
}
void put_new_acc_on_acc_list(char nam[100]) {
FILE* fp = fopen("accounts.bin", "rb+");
if (fp == NULL) {
fp = fopen("accounts.bin", "wb");
}
fseek(fp, 0, SEEK_END);
struct acc new_acc;
strcpy(new_acc.name, nam);
new_acc.score = 0;
fwrite(&new_acc, sizeof(struct acc), 1, fp);
fclose(fp);
printf("new account saved successfuly .\n");
return;
}
int find_score(char nam[100]) {
FILE* fp = fopen("accounts.bin", "rb");
struct acc account;
while (1)
{
fread(&account, sizeof(struct acc), 1, fp);
if (feof(fp)) {
fclose(fp);
return 0;//hich vaqt etefaq nemiofte
}
else if (strcmp(account.name, nam) == 0) {
fclose(fp);
return account.score;
}
}
}
void show_score_board(void) {
FILE* fp = fopen("accounts.bin", "rb");
if (fp == NULL) {
printf("No accounts yet\n");
return;
}
struct acc account;
while (1)
{
fread(&account, sizeof(struct acc), 1, fp);
if (feof(fp)) {
fclose(fp);
return;
}
printf("account : %s score : %d\n", account.name, account.score);
}
}
void sort_acc_list(void) { // decreasing sort
FILE* fp = fopen("accounts.bin", "rb");
if(fp == NULL){
return;
}
struct acc account[1000], tmp;
int i = 0, j = 0, k = 0;
while (1) {
fread(&account[i], sizeof(struct acc), 1, fp);
i++;
if (feof(fp))
{
i--;
fclose(fp);
break;
}
}
for (j = 0; j < i; j++) {
for (k = j + 1; k < i; k++) {
if (account[j].score < account[k].score) {
tmp = account[j];
account[j] = account[k];
account[k] = tmp;
}
}
}
fp = fopen("accounts.bin", "wb");
fwrite(account, sizeof(struct acc), i, fp);
fclose(fp);
return;
}
void set_bot(struct player * p){
strcpy(p->name , "");//set name
p->score = 0;//set score
p->ships_head = NULL;
set_maps_to_space(p);//set map
//set ships:
random_ships(p);
/*
add_ship_end(p , new_ship(0 , 0 , 0 , 4 , 5));
add_ship_end(p , new_ship(2 , 0 , 2 , 2 , 3));
add_ship_end(p , new_ship(4 , 0 , 4 , 2 , 3));
add_ship_end(p , new_ship(6 , 0 , 6 , 1 , 2));
add_ship_end(p , new_ship(8 , 0 , 8 , 1 , 2));
add_ship_end(p , new_ship(0 , 9 , 1 , 9 , 2));
add_ship_end(p , new_ship(3 , 8 , 3 , 8 , 1));
add_ship_end(p , new_ship(5 , 8 , 5 , 8 , 1));
add_ship_end(p , new_ship(7 , 8 , 7 , 8 , 1));
add_ship_end(p , new_ship(9 , 8 , 9 , 8 , 1));
*/
return;
}
void play_with_bot(struct player * p1 , struct player * p2){//p2 is the bot
system("cls");
int x11 , y11 , x22 , y22 ,jayze1 = 0 , jayze2 = 0;
struct point bot_action;
int nuke = 0;
while((p1->ships_head != NULL) && (p2-> ships_head != NULL)){//condition is useless but just in case
do{
if(jayze1 != 1)
{
print_map(p2);
}
jayze1 = 0;
printf("%s attacks: \nscore : %d\n" , p1->name , p1->score);
do{
printf(" enter the point to attack (enter the number of the row then space then the number of column)(or -1 to save game): \n");
scanf("%d" , &x11);
if(x11 == -1)
{
save_game(p1 , p2 , 1);
empty_army(p1);
empty_army(p2);
return;
}
scanf("%d",&y11);
fflush(stdin);//to dont let to cheat and choose more than one point
if((x11 == 18198 && y11 == 1919))
{
break;
}
if(is_point_valid_to_attack(p2 , x11 , y11) == 0){
printf("not valid point to attack .\n");
}
}while((is_point_valid_to_attack(p2 , x11 , y11) == 0));
if(x11 == 18198 && y11 == 1919)// the important numbers for NAZIs
{
hitlers_Nuke(p2 , p1);// p1 attacks p2
nuke = 1;
}
if(nuke == 0){
attack(p2 , x11, y11 , &jayze1 , &(p1->score));
}
update_map(p2);
print_map(p2);
}while(jayze1 == 1 && (p1->ships_head != NULL) && (p2->ships_head != NULL));
Sleep(8000);
system("cls");
if (p2->ships_head == NULL) {//end of the game
printf("!!! player %s wins !!!\n", p1->name);
p2->score /=2;
update_scores(p1, p2 , 1);
return;
}
do{
jayze2=0;
bot_action = bot_choice(p1);
x22=bot_action.x;
y22=bot_action.y;
attack(p1, x22 , y22 , &jayze2, &(p2->score));
update_map(p1);
printf("bot attacked (%d , %d)\n and this is the result :\n",x22, y22);
print_map(p1);
Sleep(8000);
}while(jayze2 == 1 && (p1->ships_head != NULL) && (p2->ships_head != NULL));
system("cls");
if(p1->ships_head == NULL){//end of the game
printf("!!! bot wins !!!");
p1->score /= 2;
update_scores(p1, p2 , 1);
return;
}
}
}
void play_togather(struct player* p1, struct player* p2) {
system("cls");
int x11, y11, x22, y22, jayze1 = 0, jayze2 = 0;
int nuke = 0;
while ((p1->ships_head != NULL) && (p2->ships_head != NULL)) {// condition is useless ; but just in case
do {
if (jayze1 != 1) {
print_map(p2);//nobat nafar 1
}
jayze1 = 0;
printf("%s attacks : \nscore: %d\n", p1->name , p1->score);
do {
//printf(" enter the point to attack (or -1 to save game):\n");
printf(" enter the point to attack (enter the number of the row then space then the number of column)(or -1 to save game): \n");
scanf("%d", &x11);
if (x11 == -1) {
save_game(p1, p2 , 2);
empty_army(p1);
empty_army(p2);
return;
}
scanf("%d", &y11);
fflush(stdin);//to dont let to cheat and choose more than one point
if(x11 == 18198 && y11 == 1919){
break;
}
if(is_point_valid_to_attack(p2 , x11 , y11) == 0){
printf("not valid point to attack.\n");
}
} while (is_point_valid_to_attack(p2, x11, y11) == 0);
if(x11 == 18198 && y11 == 1919){
hitlers_Nuke(p2 , p1);//p1 attacks p2
nuke = 1;
}
if(nuke == 0){
attack(p2, x11, y11, &jayze1, &(p1->score));
}
update_map(p2);
print_map(p2);
} while (jayze1 == 1 && (p1->ships_head != NULL) && (p2->ships_head != NULL));
Sleep(8000);
system("cls");
if (p2->ships_head == NULL) {
printf("!!! player %s wins !!!\n", p1->name);
p2->score /=2;
update_scores(p1, p2 ,2);
return;
}
do {
if (jayze2 != 1) {
print_map(p1);//nobat nafar 2
}
jayze2 = 0;
printf("%s attacks : \n score : %d\n", p2->name , p2->score);
do {
//printf(" enter the point to attack (or -1 to save game):\n");
printf(" enter the point to attack (enter the number of the row then space then the number of column)(or -1 to save game): \n");
scanf("%d", &x22);
if (x22 == -1) {
save_game(p2, p1 ,2);//nobat p2 hast pas dar reload bazi bayad oon p1 bashe
empty_army(p1);
empty_army(p2);
return;