Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ** EPITECH PROJECT, 2020
- ** navy
- ** File description:
- ** kill it
- */
- #include <stdio.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include "include/my.h"
- #include "include/struct.h"
- #include <signal.h>
- #include <string.h>
- #include <stdlib.h>
- void catch_bin(int signum)
- {
- if (signum == 10) {
- NAVY.bin_coord = my_strcat(NAVY.bin_coord, "1");
- }
- if (signum == 12) {
- NAVY.bin_coord = my_strcat(NAVY.bin_coord, "0");
- }
- }
- void connexion_player_one(int signum, siginfo_t *siginfo, void *oldact)
- {
- my_putstr("enemy connected\n\n");
- NAVY.player_pid = siginfo->si_pid;
- usleep(10000);
- kill(NAVY.player_pid, SIGUSR1);
- }
- void connexion_player_two(int signum, siginfo_t *siginfo, void *oldact)
- {
- my_putstr("\nsuccessfully connected\n\n");
- }
- char *nb_to_binary(int nb)
- {
- switch (nb)
- {
- case 1:
- return("0001");
- case 2:
- return("0010");
- case 3:
- return("0011");
- case 4:
- return("0100");
- case 5:
- return("0101");
- case 6:
- return("0110");
- case 7:
- return("0111");
- case 8:
- return("1000");
- }
- }
- int send_attack_coord(char *coord)
- {
- int colone = coord[0] - 'A' + 1;
- int line = coord[1] - '0';
- char *to_send = my_strcat(nb_to_binary(colone), nb_to_binary(line));
- coord[2] = '\0';
- NAVY.coord = coord;
- for (int i = 0; to_send[i] != '\0'; i++) {
- usleep(10000);
- if (to_send[i] == '0')
- kill(NAVY.player_pid, SIGUSR2);
- else if (to_send[i] == '1')
- kill(NAVY.player_pid, SIGUSR1);
- }
- free(to_send);
- return 1;
- }
- char *bin_to_coord(char *bin)
- {
- char *coord;
- int total = 0;
- if (bin == NULL)
- exit (84);
- if ((coord = malloc(sizeof(char) * 2 + 1)) == NULL)
- return NULL;
- for (int i = 0; i < 4; i++) {
- total *= 2;
- if (bin[i] == '1')
- total += 1;
- }
- coord[0] = total + 'A' - 1;
- total = 0;
- for (int i = 4; i < 8; i++) {
- total *= 2;
- if (bin[i] == '1')
- total += 1;
- }
- coord[1] = total + '0';
- coord[2] = '\0';
- return coord;
- }
- char *receive_attack_coord(void)
- {
- for (int i = 0; i < 8; i++) {
- signal(SIGUSR1, catch_bin);
- signal(SIGUSR2, catch_bin);
- pause();
- }
- return(bin_to_coord(NAVY.bin_coord));
- }
- void send_hit(void)
- {
- NAVY.life -= 1;
- usleep(10000);
- kill(NAVY.player_pid, SIGUSR1);
- }
- void send_miss(void)
- {
- usleep(10000);
- kill(NAVY.player_pid, SIGUSR2);
- }
- void hit_miss(int signum)
- {
- if (signum == 10) {
- NAVY.enemy_life -= 1;
- NAVY.enemy_position[NAVY.coord[1] - '1'][NAVY.coord[0] - 'A'] = 'x';
- NAVY.hit_miss = 1;
- }
- if (signum == 12) {
- if (!(NAVY.enemy_position[NAVY.coord[1] - '1'][NAVY.coord[0] - 'A']
- == 'x'))
- NAVY.enemy_position[NAVY.coord[1] - '1'][NAVY.coord[0] - 'A'] = 'o';
- NAVY.hit_miss = 0;
- }
- }
- void receive_hit_miss(void)
- {
- signal(SIGUSR1, hit_miss);
- signal(SIGUSR2, hit_miss);
- pause();
- }
- int error_handling_size_file(char *boat_position)
- {
- if (boat_position[31] != '\0')
- return -1;
- if (my_strlen(boat_position) != 31)
- return -1;
- }
- int error_handling_map_caracters(char *boat_position)
- {
- int count = 0;
- int j = 2;
- for (int i = 0; boat_position[i]; i++) {
- if (boat_position[i] == ':')
- count += 1;
- }
- if (count != 8)
- return -1;
- count = 0;
- for (int i = 0; boat_position[i]; i++) {
- if (boat_position[i] == '\n')
- count += 1;
- }
- if (count != 3)
- return -1;
- return 1;
- }
- int check_caracters(char **position)
- {
- int j = 2;
- for (int i = 0; position[i] != NULL; i++, j++) {
- if (position[i][0] != j + '0')
- return -1;
- if (position[i][2] > 'H' || position[i][2] < 'A')
- return -1;
- if (position[i][3] > '8' || position[i][2] < '1')
- return -1;
- if (position[i][5] > 'H' || position[i][2] < 'A')
- return -1;
- if (position[i][6] > '8' || position[i][2] < '1')
- return -1;
- }
- return 1;
- }
- int check_boat_possible(char **position)
- {
- for (int i = 0; position[i] != NULL; i++) {
- if (!(position[i][2] == position[i][5] ||
- position[i][3] == position[i][6])) {
- return -1;
- }
- }
- return 1;
- }
- int check_boat_lenght(char **position)
- {
- int j = 1;
- for (int i = 0; position[i] != NULL; i++, j++) {
- if (position[i][2] != position[i][5] &&
- position[i][5] - position[i][2] != j)
- return -1;
- else if (position[i][3] != position[i][6] &&
- position[i][6] - position[i][3] != j)
- return -1;
- }
- return 1;
- }
- int check_line_size(char *boat_position)
- {
- int count = 0;
- for (int i = 0; boat_position[i] != '\0'; i++) {
- if (boat_position[i] != '\n' || boat_position[i] != '\0') {
- count += 1;
- } else if (count != 7) {
- return -1;
- } else {
- count = 0;
- }
- }
- return 1;
- }
- char **create_empty_map(void)
- {
- int i = 0;
- int j = 0;
- char **empty_map;
- empty_map = malloc(sizeof(char*) * 9);
- for (; i < 8; i++) {
- empty_map[i] = malloc(sizeof(char) * 9);
- for (; j < 8; j++) {
- empty_map[i][j] = '.';
- }
- empty_map[i][j] = '\0';
- j = 0;
- }
- empty_map[i] = NULL;
- return (empty_map);
- }
- void add_boat2(char **map_with_boat, char **position)
- {
- int x = position[0][3] - '0' - 1;
- int y = position[0][2] - 'A';
- if (position[0][2] == position[0][5]) {
- map_with_boat[x][y] = '2';
- map_with_boat[x + 1][y] = '2';
- } else {
- map_with_boat[x][y] = '2';
- map_with_boat[x][y + 1] = '2';
- }
- }
- void add_boat3(char **map_with_boat, char **position)
- {
- int x = position[1][3] - '0' - 1;
- int y = position[1][2] - 'A';
- if (position[1][2] == position[1][5]) {
- map_with_boat[x][y] = '3';
- map_with_boat[x + 1][y] = '3';
- map_with_boat[x + 2][y] = '3';
- } else {
- map_with_boat[x][y] = '3';
- map_with_boat[x][y + 1] = '3';
- map_with_boat[x][y + 2] = '3';
- }
- }
- void add_boat4(char **map_with_boat, char **position)
- {
- int x = position[2][3] - '0' - 1;
- int y = position[2][2] - 'A';
- if (position[2][2] == position[2][5]) {
- map_with_boat[x][y] = '4';
- map_with_boat[x + 1][y] = '4';
- map_with_boat[x + 2][y] = '4';
- map_with_boat[x + 3][y] = '4';
- } else {
- map_with_boat[x][y] = '4';
- map_with_boat[x][y + 1] = '4';
- map_with_boat[x][y + 2] = '4';
- map_with_boat[x][y + 3] = '4';
- }
- }
- void add_boat5(char **map_with_boat, char **position)
- {
- int x = position[3][3] - '0' - 1;
- int y = position[3][2] - 'A';
- if (position[3][2] == position[3][5]) {
- map_with_boat[x][y] = '5';
- map_with_boat[x + 1][y] = '5';
- map_with_boat[x + 2][y] = '5';
- map_with_boat[x + 3][y] = '5';
- map_with_boat[x + 4][y] = '5';
- } else {
- map_with_boat[x][y] = '5';
- map_with_boat[x][y + 1] = '5';
- map_with_boat[x][y + 2] = '5';
- map_with_boat[x][y + 3] = '5';
- map_with_boat[x][y + 4] = '5';
- }
- }
- int add_boats_and_check(char **map_with_boat, char **position)
- {
- int boat_count = 0;
- add_boat2(map_with_boat, position);
- add_boat3(map_with_boat, position);
- add_boat4(map_with_boat, position);
- add_boat5(map_with_boat, position);
- for (int i = 0; map_with_boat[i] != NULL; i++) {
- for (int j = 0; map_with_boat[i][j] != '\0'; j++) {
- if (map_with_boat[i][j] >= '2' && map_with_boat[i][j] <= '5') {
- boat_count += 1;
- }
- }
- }
- if (boat_count != 14) {
- return 84;
- }
- return 1;
- }
- int create_map(char *filepath)
- {
- char *boat_position = fs_cat_x_bytes(filepath, 32);
- char **position;
- boat_position[31] = '\0';
- if (check_line_size(boat_position) == -1 ||
- error_handling_size_file(boat_position) == -1 ||
- error_handling_map_caracters(boat_position) == -1)
- return 84;
- position = my_str_to_word_array(boat_position, '\n');
- free(boat_position);
- if (check_caracters(position) == -1 || check_boat_lenght(position) == -1 ||
- check_boat_possible(position) == -1)
- return 84;
- NAVY.my_position = create_empty_map();
- NAVY.enemy_position = create_empty_map();
- if (add_boats_and_check(NAVY.my_position, position) == 84) {
- return 84;
- }
- return 0;
- }
- void print_my_positions(void)
- {
- my_putstr("\nmy positions:\n");
- my_putstr(" |A B C D E F G H\n");
- my_putstr("-+---------------\n");
- for (int i = 0; NAVY.my_position[i] != NULL; i++) {
- my_putnbr(i + 1);
- my_putchar('|');
- for (int j = 0; NAVY.my_position[i][j] != '\0'; j++) {
- my_putchar(NAVY.my_position[i][j]);
- my_putchar(' ');
- }
- my_putchar('\n');
- }
- my_putchar('\n');
- }
- void print_enemy_position(void)
- {
- my_putstr("enemy's positions:\n");
- my_putstr(" |A B C D E F G H\n");
- my_putstr("-+---------------\n");
- for (int i = 0; NAVY.enemy_position[i] != NULL; i++) {
- my_putnbr(i + 1);
- my_putchar('|');
- for (int j = 0; NAVY.enemy_position[i][j] != '\0'; j++) {
- my_putchar(NAVY.enemy_position[i][j]);
- my_putchar(' ');
- }
- my_putchar('\n');
- }
- }
- int check_if_hit(char *coord)
- {
- if (NAVY.my_position[coord[1] - '1'][coord[0] - 'A'] == '.' ||
- NAVY.my_position[coord[1] - '1'][coord[0] - 'A'] == 'x' ||
- NAVY.my_position[coord[1] - '1'][coord[0] - 'A'] == 'o') {
- if (NAVY.my_position[coord[1] - '1'][coord[0] - 'A'] != 'x')
- NAVY.my_position[coord[1] - '1'][coord[0] - 'A'] = 'o';
- my_putchar('\n');
- my_putstr(coord);
- my_putstr(": miss\n");
- NAVY.hit_miss = 0;
- return 0;
- } else {
- NAVY.my_position[coord[1] - '1'][coord[0] - 'A'] = 'x';
- my_putchar('\n');
- my_putstr(coord);
- my_putstr(": hit\n");
- NAVY.hit_miss = 1;
- return 1;
- }
- }
- int check_coord(char *coord)
- {
- if (my_strlen(coord) > 2)
- return -1;
- if (coord[0] > 'H' || coord[0] < 'A')
- return -1;
- if (coord[1] > '8' || coord[1] < '1')
- return -1;
- NAVY.wrong_position = 0;
- return 1;
- }
- int player_one(char *filepath)
- {
- char *coord = NULL;
- //Initialization
- if ((NAVY.bin_coord = malloc(sizeof(char))) == NULL)
- return 84;
- NAVY.bin_coord[0] = '\0';
- if (create_map(filepath) == 84) {
- my_puterr("ERROR");
- return 84;
- }
- NAVY.enemy_life = 14;
- NAVY.life = 14;
- struct sigaction sig = {0};
- sig.sa_sigaction = connexion_player_one;
- sig.sa_flags = SA_SIGINFO;
- //Connexion
- my_putstr("my_pid: ");
- my_putnbr(getpid());
- my_putstr("\nwaiting for enemy connexion...\n\n");
- sigaction(SIGUSR1, &sig, NULL);
- pause();
- while (1){
- //print
- print_my_positions();
- print_enemy_position();
- //attack
- coord = NULL;
- NAVY.wrong_position = 1;
- usleep(10000);
- while (NAVY.wrong_position == 1) {
- my_putstr("\nattack: ");
- if ((coord = input()) == NULL)
- return 0;
- if (check_coord(coord) == -1)
- my_putstr("wrong position");
- }
- send_attack_coord(coord);
- //Check hit/miss
- receive_hit_miss();
- if (NAVY.hit_miss == 1) {
- my_putstr(NAVY.coord);
- my_putstr(": hit\n");
- }
- if (NAVY.hit_miss == 0) {
- my_putstr(NAVY.coord);
- my_putstr(": missed\n");
- }
- free(NAVY.coord);
- if ((NAVY.bin_coord = malloc(sizeof(char))) == NULL)
- return 84;
- NAVY.bin_coord[0] = '\0';
- if (NAVY.enemy_life <= 0) {
- print_my_positions();
- print_enemy_position();
- my_putstr("\nI won\n\n");
- return 0;
- }
- //Reveive attack
- my_putstr("\nwaiting for enemy's attack...");
- if (check_if_hit(receive_attack_coord()) == 0) {
- send_miss();
- } else {
- send_hit();
- }
- if (NAVY.life <= 0) {
- print_my_positions();
- print_enemy_position();
- my_putstr("\nI lose\n\n");
- return 1;
- }
- }
- }
- int player_two(int pid, char *filepath)
- {
- char *coord = NULL;
- // Initialization data
- if ((NAVY.bin_coord = malloc(sizeof(char))) == NULL)
- return 84;
- NAVY.bin_coord[0] = '\0';
- if (create_map(filepath) == 84) {
- my_puterr("ERROR\n");
- return 84;
- }
- NAVY.player_pid = pid;
- NAVY.enemy_life = 14;
- NAVY.life = 14;
- //connexion
- struct sigaction sig = {0};
- sig.sa_sigaction = connexion_player_two;
- sig.sa_flags = SA_SIGINFO;
- kill(NAVY.player_pid, SIGUSR1);
- my_putstr("my_pid: ");
- my_putnbr(getpid());
- sigaction(SIGUSR1, &sig, NULL);
- pause();
- while (1) {
- //Print
- print_my_positions();
- print_enemy_position();
- //Reveive attack
- my_putstr("\nwaiting for enemy's attack...");
- if (check_if_hit(receive_attack_coord()) == 0) {
- send_miss();
- } else {
- send_hit();
- }
- if (NAVY.life <= 0) {
- print_my_positions();
- print_enemy_position();
- my_putstr("\nI lose\n\n");
- return 1;
- }
- //attack
- coord = NULL;
- NAVY.wrong_position = 1;
- while (NAVY.wrong_position == 1) {
- my_putstr("\nattack: ");
- if ((coord = input()) == NULL)
- return 0;
- if (check_coord(coord) == -1)
- my_putstr("wrong position");
- }
- send_attack_coord(coord);
- //Check hit/miss
- receive_hit_miss();
- if (NAVY.hit_miss == 1) {
- my_putstr(NAVY.coord);
- my_putstr(": hit\n");
- }
- if (NAVY.hit_miss == 0) {
- my_putstr(NAVY.coord);
- my_putstr(": missed\n");
- }
- free(NAVY.coord);
- if ((NAVY.bin_coord = malloc(sizeof(char))) == NULL)
- return 84;
- NAVY.bin_coord[0] = '\0';
- if (NAVY.enemy_life <= 0) {
- print_my_positions();
- print_enemy_position();
- my_putstr("\nI won\n\n");
- return 0;
- }
- }
- }
- int main(int argc, char *argv[])
- {
- if (argc == 2)
- return (player_one(argv[1]));
- if (argc == 3)
- return (player_two(my_getnbr(argv[1]), argv[2]));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement