Advertisement
Guest User

Untitled

a guest
Feb 14th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.13 KB | None | 0 0
  1. /*
  2. ** EPITECH PROJECT, 2020
  3. ** navy
  4. ** File description:
  5. ** kill it
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <sys/types.h>
  10. #include <unistd.h>
  11. #include "include/my.h"
  12. #include "include/struct.h"
  13. #include <signal.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16.  
  17. void catch_bin(int signum)
  18. {
  19.     if (signum == 10) {
  20.         NAVY.bin_coord = my_strcat(NAVY.bin_coord, "1");
  21.     }
  22.     if (signum == 12) {
  23.         NAVY.bin_coord = my_strcat(NAVY.bin_coord, "0");
  24.     }
  25. }
  26.  
  27. void connexion_player_one(int signum, siginfo_t *siginfo, void *oldact)
  28. {
  29.     my_putstr("enemy connected\n\n");
  30.     NAVY.player_pid = siginfo->si_pid;
  31.     usleep(10000);
  32.     kill(NAVY.player_pid, SIGUSR1);
  33. }
  34.  
  35. void connexion_player_two(int signum, siginfo_t *siginfo, void *oldact)
  36. {
  37.     my_putstr("\nsuccessfully connected\n\n");
  38. }
  39.  
  40. char *nb_to_binary(int nb)
  41. {
  42.     switch (nb)
  43.     {
  44.         case 1:
  45.             return("0001");
  46.         case 2:
  47.             return("0010");
  48.         case 3:
  49.             return("0011");
  50.         case 4:
  51.             return("0100");
  52.         case 5:
  53.             return("0101");
  54.         case 6:
  55.             return("0110");
  56.         case 7:
  57.             return("0111");
  58.         case 8:
  59.             return("1000");
  60.     }
  61. }
  62.  
  63. int send_attack_coord(char *coord)
  64. {
  65.     int colone = coord[0] - 'A' + 1;
  66.     int line = coord[1] - '0';
  67.     char *to_send = my_strcat(nb_to_binary(colone), nb_to_binary(line));
  68.     coord[2] = '\0';
  69.     NAVY.coord = coord;
  70.     for (int i = 0; to_send[i] != '\0'; i++) {
  71.         usleep(10000);
  72.         if (to_send[i] == '0')
  73.             kill(NAVY.player_pid, SIGUSR2);
  74.         else if (to_send[i] == '1')
  75.             kill(NAVY.player_pid, SIGUSR1);
  76.     }
  77.     free(to_send);
  78.     return 1;
  79. }
  80.  
  81. char *bin_to_coord(char *bin)
  82. {
  83.     char *coord;
  84.     int total = 0;
  85.     if (bin == NULL)
  86.         exit (84);
  87.     if ((coord = malloc(sizeof(char) * 2 + 1)) == NULL)
  88.         return NULL;
  89.     for (int i = 0; i < 4; i++) {
  90.         total *= 2;
  91.         if (bin[i] == '1')
  92.             total += 1;
  93.     }
  94.     coord[0] = total + 'A' - 1;
  95.     total = 0;
  96.     for (int i = 4; i < 8; i++) {
  97.         total *= 2;
  98.         if (bin[i] == '1')
  99.             total += 1;
  100.     }
  101.     coord[1] = total + '0';
  102.     coord[2] = '\0';
  103.     return coord;
  104. }
  105.  
  106. char *receive_attack_coord(void)
  107. {
  108.     for (int i = 0; i < 8; i++) {
  109.         signal(SIGUSR1, catch_bin);
  110.         signal(SIGUSR2, catch_bin);
  111.         pause();
  112.     }
  113.     return(bin_to_coord(NAVY.bin_coord));
  114. }
  115.  
  116. void send_hit(void)
  117. {
  118.     NAVY.life -= 1;
  119.     usleep(10000);
  120.     kill(NAVY.player_pid, SIGUSR1);
  121. }
  122.  
  123. void send_miss(void)
  124. {
  125.     usleep(10000);
  126.     kill(NAVY.player_pid, SIGUSR2);
  127. }
  128.  
  129. void hit_miss(int signum)
  130. {
  131.     if (signum == 10) {
  132.         NAVY.enemy_life -= 1;
  133.         NAVY.enemy_position[NAVY.coord[1] - '1'][NAVY.coord[0] - 'A'] = 'x';
  134.         NAVY.hit_miss = 1;
  135.     }
  136.     if (signum == 12) {
  137.         if (!(NAVY.enemy_position[NAVY.coord[1] - '1'][NAVY.coord[0] - 'A']
  138.         == 'x'))
  139.             NAVY.enemy_position[NAVY.coord[1] - '1'][NAVY.coord[0] - 'A'] = 'o';
  140.         NAVY.hit_miss = 0;
  141.     }
  142. }
  143.  
  144. void receive_hit_miss(void)
  145. {
  146.     signal(SIGUSR1, hit_miss);
  147.     signal(SIGUSR2, hit_miss);
  148.     pause();
  149. }
  150.  
  151.  
  152. int error_handling_size_file(char *boat_position)
  153. {
  154.     if (boat_position[31] != '\0')
  155.         return -1;
  156.     if (my_strlen(boat_position) != 31)
  157.         return -1;
  158. }
  159.  
  160. int error_handling_map_caracters(char *boat_position)
  161. {
  162.     int count = 0;
  163.     int j = 2;
  164.     for (int i = 0; boat_position[i]; i++) {
  165.         if (boat_position[i] == ':')
  166.             count += 1;
  167.     }
  168.     if (count != 8)
  169.         return -1;
  170.     count = 0;
  171.     for (int i = 0; boat_position[i]; i++) {
  172.         if (boat_position[i] == '\n')
  173.             count += 1;
  174.     }
  175.     if (count != 3)
  176.         return -1;
  177.     return 1;
  178. }
  179.  
  180. int check_caracters(char **position)
  181. {
  182.     int j = 2;
  183.     for (int i = 0; position[i] != NULL; i++, j++) {
  184.         if (position[i][0] != j + '0')
  185.             return -1;
  186.         if (position[i][2] > 'H' || position[i][2] < 'A')
  187.             return -1;
  188.         if (position[i][3] > '8' || position[i][2] < '1')
  189.             return -1;
  190.         if (position[i][5] > 'H' || position[i][2] < 'A')
  191.             return -1;
  192.         if (position[i][6] > '8' || position[i][2] < '1')
  193.             return -1;
  194.     }
  195.     return 1;
  196. }
  197.  
  198. int check_boat_possible(char **position)
  199. {
  200.     for (int i = 0; position[i] != NULL; i++) {
  201.         if (!(position[i][2] == position[i][5] ||
  202.         position[i][3] == position[i][6])) {
  203.             return -1;
  204.         }
  205.     }
  206.     return 1;
  207. }
  208.  
  209. int check_boat_lenght(char **position)
  210. {
  211.     int j = 1;
  212.     for (int i = 0; position[i] != NULL; i++, j++) {
  213.         if (position[i][2] != position[i][5] &&
  214.         position[i][5] - position[i][2] != j)
  215.             return -1;
  216.         else if (position[i][3] != position[i][6] &&
  217.         position[i][6] - position[i][3] != j)
  218.             return -1;
  219.     }
  220.     return 1;
  221. }
  222.  
  223. int check_line_size(char *boat_position)
  224. {
  225.     int count = 0;
  226.     for (int i = 0; boat_position[i] != '\0'; i++) {
  227.         if (boat_position[i] != '\n' || boat_position[i] != '\0') {
  228.             count += 1;
  229.         } else if (count != 7) {
  230.             return -1;
  231.         } else {
  232.             count = 0;
  233.         }
  234.     }
  235.     return 1;
  236. }
  237.  
  238. char **create_empty_map(void)
  239. {
  240.     int i = 0;
  241.     int j = 0;
  242.     char **empty_map;
  243.     empty_map = malloc(sizeof(char*) * 9);
  244.     for (; i < 8; i++) {
  245.         empty_map[i] = malloc(sizeof(char) * 9);
  246.         for (; j < 8; j++) {
  247.             empty_map[i][j] = '.';
  248.         }
  249.         empty_map[i][j] = '\0';
  250.         j = 0;
  251.     }
  252.     empty_map[i] = NULL;
  253.     return (empty_map);
  254. }
  255.  
  256. void add_boat2(char **map_with_boat, char **position)
  257. {
  258.     int x = position[0][3] - '0' - 1;
  259.     int y = position[0][2] - 'A';
  260.     if (position[0][2] == position[0][5]) {
  261.         map_with_boat[x][y] = '2';
  262.         map_with_boat[x + 1][y] = '2';
  263.     } else {
  264.         map_with_boat[x][y] = '2';
  265.         map_with_boat[x][y + 1] = '2';
  266.     }
  267. }
  268.  
  269. void add_boat3(char **map_with_boat, char **position)
  270. {
  271.     int x = position[1][3] - '0' - 1;
  272.     int y = position[1][2] - 'A';
  273.     if (position[1][2] == position[1][5]) {
  274.         map_with_boat[x][y] = '3';
  275.         map_with_boat[x + 1][y] = '3';
  276.         map_with_boat[x + 2][y] = '3';
  277.     } else {
  278.         map_with_boat[x][y] = '3';
  279.         map_with_boat[x][y + 1] = '3';
  280.         map_with_boat[x][y + 2] = '3';
  281.     }
  282. }
  283.  
  284. void add_boat4(char **map_with_boat, char **position)
  285. {
  286.     int x = position[2][3] - '0' - 1;
  287.     int y = position[2][2] - 'A';
  288.     if (position[2][2] == position[2][5]) {
  289.         map_with_boat[x][y] = '4';
  290.         map_with_boat[x + 1][y] = '4';
  291.         map_with_boat[x + 2][y] = '4';
  292.         map_with_boat[x + 3][y] = '4';
  293.     } else {
  294.         map_with_boat[x][y] = '4';
  295.         map_with_boat[x][y + 1] = '4';
  296.         map_with_boat[x][y + 2] = '4';
  297.         map_with_boat[x][y + 3] = '4';
  298.     }
  299. }
  300.  
  301. void add_boat5(char **map_with_boat, char **position)
  302. {
  303.     int x = position[3][3] - '0' - 1;
  304.     int y = position[3][2] - 'A';
  305.     if (position[3][2] == position[3][5]) {
  306.         map_with_boat[x][y] = '5';
  307.         map_with_boat[x + 1][y] = '5';
  308.         map_with_boat[x + 2][y] = '5';
  309.         map_with_boat[x + 3][y] = '5';
  310.         map_with_boat[x + 4][y] = '5';
  311.     } else {
  312.         map_with_boat[x][y] = '5';
  313.         map_with_boat[x][y + 1] = '5';
  314.         map_with_boat[x][y + 2] = '5';
  315.         map_with_boat[x][y + 3] = '5';
  316.         map_with_boat[x][y + 4] = '5';
  317.     }
  318. }
  319.  
  320. int add_boats_and_check(char **map_with_boat, char **position)
  321. {
  322.     int boat_count = 0;
  323.     add_boat2(map_with_boat, position);
  324.     add_boat3(map_with_boat, position);
  325.     add_boat4(map_with_boat, position);
  326.     add_boat5(map_with_boat, position);
  327.     for (int i = 0; map_with_boat[i] != NULL; i++) {
  328.         for (int j = 0; map_with_boat[i][j] != '\0'; j++) {
  329.             if (map_with_boat[i][j] >= '2' && map_with_boat[i][j] <= '5') {
  330.                 boat_count += 1;
  331.             }
  332.         }
  333.     }
  334.     if (boat_count != 14) {
  335.         return 84;
  336.     }
  337.     return 1;
  338. }
  339.  
  340. int create_map(char *filepath)
  341. {
  342.     char *boat_position = fs_cat_x_bytes(filepath, 32);
  343.     char **position;
  344.     boat_position[31] = '\0';
  345.     if (check_line_size(boat_position) == -1 ||
  346.     error_handling_size_file(boat_position) == -1 ||
  347.     error_handling_map_caracters(boat_position) == -1)
  348.         return 84;
  349.     position = my_str_to_word_array(boat_position, '\n');
  350.     free(boat_position);
  351.     if (check_caracters(position) == -1 || check_boat_lenght(position) == -1 ||
  352.     check_boat_possible(position) == -1)
  353.         return 84;
  354.     NAVY.my_position = create_empty_map();
  355.     NAVY.enemy_position = create_empty_map();
  356.     if (add_boats_and_check(NAVY.my_position, position) == 84) {
  357.         return 84;
  358.     }
  359.     return 0;
  360. }
  361.  
  362. void print_my_positions(void)
  363. {
  364.     my_putstr("\nmy positions:\n");
  365.     my_putstr(" |A B C D E F G H\n");
  366.     my_putstr("-+---------------\n");
  367.     for (int i = 0; NAVY.my_position[i] != NULL; i++) {
  368.         my_putnbr(i + 1);
  369.         my_putchar('|');
  370.         for (int j = 0; NAVY.my_position[i][j] != '\0'; j++) {
  371.             my_putchar(NAVY.my_position[i][j]);
  372.             my_putchar(' ');
  373.         }
  374.         my_putchar('\n');
  375.     }
  376.     my_putchar('\n');
  377. }
  378.  
  379. void print_enemy_position(void)
  380. {
  381.     my_putstr("enemy's positions:\n");
  382.     my_putstr(" |A B C D E F G H\n");
  383.     my_putstr("-+---------------\n");
  384.     for (int i = 0; NAVY.enemy_position[i] != NULL; i++) {
  385.         my_putnbr(i + 1);
  386.         my_putchar('|');
  387.         for (int j = 0; NAVY.enemy_position[i][j] != '\0'; j++) {
  388.             my_putchar(NAVY.enemy_position[i][j]);
  389.             my_putchar(' ');
  390.         }
  391.         my_putchar('\n');
  392.     }
  393. }
  394.  
  395. int check_if_hit(char *coord)
  396. {
  397.     if (NAVY.my_position[coord[1] - '1'][coord[0] - 'A'] == '.' ||
  398.     NAVY.my_position[coord[1] - '1'][coord[0] - 'A'] == 'x' ||
  399.     NAVY.my_position[coord[1] - '1'][coord[0] - 'A'] == 'o') {
  400.         if (NAVY.my_position[coord[1] - '1'][coord[0] - 'A'] != 'x')
  401.             NAVY.my_position[coord[1] - '1'][coord[0] - 'A'] = 'o';
  402.         my_putchar('\n');
  403.         my_putstr(coord);
  404.         my_putstr(":  miss\n");
  405.         NAVY.hit_miss = 0;
  406.         return 0;
  407.     } else {
  408.         NAVY.my_position[coord[1] - '1'][coord[0] - 'A'] = 'x';
  409.         my_putchar('\n');
  410.         my_putstr(coord);
  411.         my_putstr(":  hit\n");
  412.         NAVY.hit_miss = 1;
  413.         return 1;
  414.     }
  415. }
  416.  
  417. int check_coord(char *coord)
  418. {
  419.     if (my_strlen(coord) > 2)
  420.         return -1;
  421.     if (coord[0] > 'H' || coord[0] < 'A')
  422.             return -1;
  423.     if (coord[1] > '8' || coord[1] < '1')
  424.             return -1;
  425.     NAVY.wrong_position = 0;
  426.     return 1;
  427. }
  428.  
  429. int player_one(char *filepath)
  430. {
  431.     char *coord = NULL;
  432.     //Initialization
  433.     if ((NAVY.bin_coord = malloc(sizeof(char))) == NULL)
  434.         return 84;
  435.     NAVY.bin_coord[0] = '\0';
  436.     if (create_map(filepath) == 84) {
  437.         my_puterr("ERROR");
  438.         return 84;
  439.     }
  440.     NAVY.enemy_life = 14;
  441.     NAVY.life = 14;
  442.  
  443.     struct sigaction sig = {0};
  444.     sig.sa_sigaction = connexion_player_one;
  445.     sig.sa_flags = SA_SIGINFO;
  446.  
  447.     //Connexion
  448.     my_putstr("my_pid:  ");
  449.     my_putnbr(getpid());
  450.     my_putstr("\nwaiting for enemy connexion...\n\n");
  451.     sigaction(SIGUSR1, &sig, NULL);
  452.     pause();
  453.  
  454.     while (1){
  455.         //print
  456.         print_my_positions();
  457.         print_enemy_position();
  458.  
  459.         //attack
  460.         coord = NULL;
  461.         NAVY.wrong_position = 1;
  462.         usleep(10000);
  463.         while (NAVY.wrong_position == 1) {
  464.             my_putstr("\nattack:  ");
  465.             if ((coord = input()) == NULL)
  466.                 return 0;
  467.             if (check_coord(coord) == -1)
  468.                 my_putstr("wrong position");
  469.         }
  470.         send_attack_coord(coord);
  471.  
  472.         //Check hit/miss
  473.         receive_hit_miss();
  474.         if (NAVY.hit_miss == 1) {
  475.             my_putstr(NAVY.coord);
  476.             my_putstr(":  hit\n");
  477.         }
  478.         if (NAVY.hit_miss == 0) {
  479.             my_putstr(NAVY.coord);
  480.             my_putstr(":  missed\n");
  481.         }
  482.         free(NAVY.coord);
  483.         if ((NAVY.bin_coord = malloc(sizeof(char))) == NULL)
  484.             return 84;
  485.         NAVY.bin_coord[0] = '\0';
  486.         if (NAVY.enemy_life <= 0) {
  487.             print_my_positions();
  488.             print_enemy_position();
  489.             my_putstr("\nI won\n\n");
  490.             return 0;
  491.         }
  492.         //Reveive attack
  493.         my_putstr("\nwaiting for enemy's attack...");
  494.         if (check_if_hit(receive_attack_coord()) == 0) {
  495.             send_miss();
  496.         } else {
  497.             send_hit();
  498.         }
  499.         if (NAVY.life <= 0) {
  500.             print_my_positions();
  501.             print_enemy_position();
  502.             my_putstr("\nI lose\n\n");
  503.             return 1;
  504.         }
  505.     }
  506. }
  507.  
  508. int player_two(int pid, char *filepath)
  509. {
  510.     char *coord = NULL;
  511.     // Initialization data
  512.     if ((NAVY.bin_coord = malloc(sizeof(char))) == NULL)
  513.         return 84;
  514.     NAVY.bin_coord[0] = '\0';
  515.     if (create_map(filepath) == 84) {
  516.         my_puterr("ERROR\n");
  517.         return 84;
  518.     }
  519.     NAVY.player_pid = pid;
  520.     NAVY.enemy_life = 14;
  521.     NAVY.life = 14;
  522.  
  523.  
  524.     //connexion
  525.     struct sigaction sig = {0};
  526.     sig.sa_sigaction = connexion_player_two;
  527.     sig.sa_flags = SA_SIGINFO;
  528.     kill(NAVY.player_pid, SIGUSR1);
  529.     my_putstr("my_pid:  ");
  530.     my_putnbr(getpid());
  531.     sigaction(SIGUSR1, &sig, NULL);
  532.     pause();
  533.     while (1) {
  534.         //Print
  535.         print_my_positions();
  536.         print_enemy_position();
  537.  
  538.         //Reveive attack
  539.         my_putstr("\nwaiting for enemy's attack...");
  540.         if (check_if_hit(receive_attack_coord()) == 0) {
  541.             send_miss();
  542.         } else {
  543.             send_hit();
  544.         }
  545.         if (NAVY.life <= 0) {
  546.             print_my_positions();
  547.             print_enemy_position();
  548.             my_putstr("\nI lose\n\n");
  549.             return 1;
  550.         }
  551.         //attack
  552.         coord = NULL;
  553.         NAVY.wrong_position = 1;
  554.         while (NAVY.wrong_position == 1) {
  555.             my_putstr("\nattack:  ");
  556.             if ((coord = input()) == NULL)
  557.                 return 0;
  558.             if (check_coord(coord) == -1)
  559.                 my_putstr("wrong position");
  560.         }
  561.         send_attack_coord(coord);
  562.         //Check hit/miss
  563.         receive_hit_miss();
  564.         if (NAVY.hit_miss == 1) {
  565.             my_putstr(NAVY.coord);
  566.             my_putstr(":  hit\n");
  567.         }
  568.         if (NAVY.hit_miss == 0) {
  569.             my_putstr(NAVY.coord);
  570.             my_putstr(":  missed\n");
  571.         }
  572.         free(NAVY.coord);
  573.         if ((NAVY.bin_coord = malloc(sizeof(char))) == NULL)
  574.             return 84;
  575.         NAVY.bin_coord[0] = '\0';
  576.         if (NAVY.enemy_life <= 0) {
  577.             print_my_positions();
  578.             print_enemy_position();
  579.             my_putstr("\nI won\n\n");
  580.             return 0;
  581.         }
  582.     }
  583. }
  584.  
  585. int main(int argc, char *argv[])
  586. {
  587.     if (argc == 2)
  588.         return (player_one(argv[1]));
  589.     if (argc == 3)
  590.         return (player_two(my_getnbr(argv[1]), argv[2]));
  591. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement