-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.h
216 lines (184 loc) · 6.94 KB
/
main.h
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
#ifndef MAIN_C_SEA_BATTLE_H
#define MAIN_C_SEA_BATTLE_H
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#ifdef WIN32
#define CLEAR_SCREEN "cls"
#define SLEEP(milliseconds) \
Sleep(milliseconds)
#define GET_CHAR() \
getch()
#endif
#define MAX_LEN_OF_NAME 35
#define NOT_STARTED_GAME 0
#define RETURN_TO_MAIN_MENU (-1)
#define NO_PREVIOUS_VALUE (-1)
#define PLAYERS_FILENAME "Saved players.bin"
#define REPLAYS_FILENAME "Saved replays.bin"
#define GAMES_FILENAME "Saved games.bin"
extern int map_size, ships_count;
typedef struct {
int map_size;
int ships_count;
int lengths_of_ships[20];
} Settings;
typedef struct {
int row;
int col;
} Square;
typedef struct Ship {
int len;
Square bow;
Square stern;
struct Ship *next_ship;
} Ship;
typedef struct {
bool is_bot;
int score;
int remaining_ships;
Ship *ships;
char **revealed_map;
char **concealed_map;
char name[MAX_LEN_OF_NAME];
Settings settings;
} Player;
typedef struct {
int turn;
struct tm date;
char name[MAX_LEN_OF_NAME];
Player player1;
Player player2;
int shoots_counter;
Square shoots[150];
} Game;
enum MapCell {
WATER = 'W',
SHIP = 'S',
EXPLOSION = 'E',
CAPSIZED_SHIP = 'C',
NOT_REVEALED = '?'
};
enum Menu {
MAIN_MENU,
PAUSE_MENU,
SETTINGS_MENU
};
enum Direction {
UP,
RIGHT,
DOWN,
LEFT
};
enum Placeability {
NOT_PLACEABLE,
PLACEABLE
};
void say_goodbye();
char *get_player_name();
Player load_player(char *name, bool is_welcome_message_needed);
Player create_player(const char *name);
Settings set_defaults();
int seek_player_by_name(const char *name, FILE *saved_players);
void main_menu(Player player1);
Game *allocate_an_initial_game(Player player1);
int print_menu(int choice, enum Menu menu);
void free_game_pointer(Game* game);
void free_ships(Ship* ships);
void free_map(char** map);
void scoreboard();
void copy_players_file(FILE *dest, FILE *source);
void print_scoreboard(FILE *saved_players);
Player *find_player_with_max_score(FILE *saved_players);
Game create_game(Player player1, bool is_vs_bot);
struct tm get_date();
Ship *load_settings(Settings settings);
void set_map_size_and_ships_count(Settings settings);
Ship *create_list_of_ships(const int *lengths_of_ships);
Ship *copy_ships_list(Ship *source);
void add_ship(Ship **ships_head, int len_of_ship);
void do_the_maps_allocations(Player *player1, Player *player2);
char **allocate_map(char initial_value);
void display_screen_for_placing(Player player);
void display_screen_for_guessing(Player player1, Player player2, bool is_replay);
void gotoxy(int x, int y);
void print_header();
void print_date(struct tm date);
void display_map(char **map, char *name, int score);
void print_columns();
void put_colored_char(char c);
void set_text_color(int color);
void resume_previous_games(char *player_name);
Game *load_game(char *player1_name, bool is_replay);
int display_saved_games(char *player1_name, bool is_replay);
int get_chosen_game_num(int total_num_of_games);
Game *find_chosen_game(FILE *saved_games, char *player1_name, int choice, bool is_replay);
void fseek_to_next_game(Game *game, FILE *saved_games, bool is_replay);
void prepare_players_of_loaded_game(Game *game, FILE *saved_games, char *player1_name);
void prepare_players_of_replay(Game *game, FILE *saved_games);
void load_4_maps_of_game(Game *game, FILE *saved_games);
void fread_map(char **map, FILE *saved_games);
void restore_ships(Ship *ships_head, const char **map);
void restore_ships_with_equal_length(Ship **ship, const char **map);
Ship *load_ships_list(FILE *saved_games, int remaining_ships);
int load_score(char *player1_name, char *player_to_load_name);
void battle_log(char *player_name);
void replay(Game game);
void play_the_game(Game *game, bool is_a_loaded_game);
int game_loop(Game *game);
bool is_game_ended(Ship *player1_ships, Ship *player2_ships);
bool is_human_turn(int turn, bool player2_is_human);
int control_player_turn(Game *game);
void get_command(Player player1, Player player2, char command[], int turn);
void how_to_place_ships(Player *player);
void auto_arrange_map(Player *player);
void clear_map(char **map);
void fill_empty_squares_with_water(char **map);
bool is_refused();
void auto_place_ships(Player *player);
void auto_place_ship_longer_than_1(const Player *player, Ship *ship, int remained_squares);
Square rand_square(char **map, int remained_squares);
void reveal_ship(Ship ship, char **map, enum MapCell ship_sign);
void find_placeable_dirs(Square square, enum Placeability *directions, char **map, int len);
int count_placeable_dirs(const enum Placeability directions[]);
enum Direction rand_dir(const enum Placeability directions[], int placeable_dirs_count);
Square find_stern(Square bow, enum Direction chosen_direction, int len);
int count_char(char **map, char c);
void manually_place_ships(Player *player);
void get_bow_and_stern(char bow[], char stern[], int ship_len, int ship_num);
bool is_valid_ship_placement(Ship *ship, char **map, char *chosen_bow, char *chosen_stern);
bool is_placeable_square(char **map, char *chosen_square, Square *square_coordinates);
int square_compare(Square square1, Square square2);
void square_swap(Square *square1, Square *square2);
void play_for_bot(Game *game);
Square shoot_for_bot(char **opponent_map);
Square find_explosion(char **map, int previous_row, int previous_col);
Square follow_explosions(char **map);
int is_vertical(Square square1, Square square2);
void update_game(Game *game, Square shoot);
void apply_changes(Player *opponent, Square shoot, int *attacker_score, const int *lengths_of_ships);
bool is_ship_sunk(Ship attacked_ship, char **concealed_map);
Ship find_attacked_ship(Ship *opponents_ships, Square shoot);
void delete_ship(Ship **ships_head, Square bow);
int win_bonus(const int *lengths_of_ships);
int max_len_of_ships(const int *lengths_of_ships, bool is_the_game_ended);
void switch_turn(Game *game, Square shoot);
int get_choice_from_pause_menu(Game *game);
void ask_to_save_game(Game game);
void save_game(Game game, bool is_replay);
void save_ships_list(Game game, FILE *saved_games);
void save_4_maps_of_game(Game game, FILE *saved_games);
void fwrite_map(char **map, FILE *file_to_write_to);
void end_game(Game *game);
void update_players_in_file(Player *player1, Player *player2);
void game_over_message(Game game);
void game_over_menu(Game *game);
void settings_menu(Player *player);
void change_map_size(Player *player);
void change_ships_settings(Player *player);
int integer_compare_descending(const void *p_int1, const void *p_int2);
int get_map_size_or_ships_count(int choice_from_settings_menu);
void hide_cursor();
void show_cursor();
void handle_error();
#endif //MAIN_C_SEA_BATTLE_H