Advertisement
Guest User

reversia_end

a guest
Dec 16th, 2019
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 20.76 KB | None | 0 0
  1. /*
  2. * Projet REVERSIA - Prog C Avancées - ING4SE - Promo 2021
  3. *
  4. * Valentin Brunet & Arthur DESUTTER
  5. *
  6. *
  7. *
  8. */
  9.  
  10.  
  11. /*Les lignes suivantes doivent être supprimées avant la compilation,
  12. * elles permettent à mon éditeur de code de charger les bonnes librairies pour l'autocomplétion
  13. */
  14. #ifndef MINGW32
  15. #define MINGW32
  16. #endif
  17.  
  18. /*
  19. * Chargement des librairies
  20. */
  21. #include <stdlib.h>
  22. #include <sys/types.h>
  23.  
  24. #ifdef MINGW32
  25. // Libraries for Windows
  26. #include<winsock2.h>
  27. #include <windows.h>
  28. #else
  29. // Libraries for Linux
  30. #include <sys/socket.h>
  31. #include <netinet/in.h>
  32. #include <arpa/inet.h>
  33. #define INVALID_SOCKET -1
  34. #define SOCKET_ERROR -1
  35. #define closesocket(s) close(s)
  36. typedef int SOCKET;
  37. typedef struct sockaddr_in SOCKADDR_IN;
  38. typedef struct sockaddr SOCKADDR;
  39. typedef struct in_addr IN_ADDR;
  40.  
  41. #endif
  42.  
  43. #include <string.h>
  44. #include <stdio.h>
  45. #include <unistd.h>
  46. #include <stdbool.h>
  47. #include <string.h>
  48.  
  49.  
  50.  
  51. // messages constants
  52. #define BUFFER_SIZE 1024
  53. #define PORT 8888
  54. #define IP_SERVER 137.74.113.61
  55. #define MAX_CLIENT 1
  56. #define BLACK_PLAYER 0x01
  57. #define WHITE_PLAYER 0x02
  58. #define NOK_RESPONSE 0x00
  59. #define OK_RESPONSE 0x01
  60. #define SYNCRO 0x55
  61. #define FIELDSIZE 8
  62.  
  63.  
  64. #define OKNOK_MESSAGE 0x02
  65. #define CONNECT_MESSAGE 0x01
  66. #define PLAYEROK_MESSAGE 0x10
  67. #define NEWMOVE_MESSAGE 0x03
  68. #define END_MESSAGE 0x04
  69. #define NEXTTURN_MESSAGE 0x05
  70. #define STATUS1_MESSAGE 0x06
  71. #define STATUS2_MESSAGE 0x07
  72. #define CONTROL_MESSAGE 0x08
  73. #define PING_MESSAGE 0x11
  74.  
  75. //PERMET DE DEFINIR LES MESSAGES DE DEBUG QUE L'ON VEUT VOIR OU NON
  76. #define DEBUG_MESSAGE 1
  77. #define DEBUG_BUFFER 0
  78. #define DEBUG_SOCKET 0
  79. #define DEBUG_BOARD 0
  80. #define DEBUG_HEX_CONVERTER 0
  81. #define DEBUG_CONVERTER 0
  82.  
  83. int run = 1;
  84.  
  85.  
  86. typedef struct
  87. {
  88.     int port;
  89.     char name;
  90. }Client;
  91.  
  92. typedef struct
  93. {
  94.     SOCKET sock;
  95.     char name[BUFFER_SIZE];
  96. }Cliente;
  97.  
  98. //STRCUTURE DES MESSAGES
  99. typedef struct
  100. {
  101.     char type;
  102.     int length;
  103.     char *data;
  104.     char valid_message;
  105.  
  106. }Message;
  107.  
  108. //STRUCTURE DU JEU
  109. typedef struct
  110. {
  111.     char color;
  112.     char **tab;
  113.     int lig;
  114.     int col;
  115. }Game;
  116.  
  117. int eatPion(Game *game, int ligEmpty, int colEmpty, int ligPion, int colPion){
  118.    
  119.     int i = ligPion; int j = colPion;
  120.  
  121.     // Cas ou on est NOIR
  122.     if (game->color == 'b')
  123.     {
  124.         // On cherche dans la direction qui convient, un pion de notre couleur
  125.         // Si trouvé, on peut jouer en l(igEmpty ; colEmpty), sinon non
  126.         int runLocal =1;
  127.         while(runLocal){
  128.         // On se déplace selon l'axe formé par l'emplacement vide et le pion adverse
  129.        
  130.         i += ligPion - ligEmpty; j += colPion - colEmpty;
  131.         if(i >= 0 && j >= 0 && i < game->lig && j < game->col) {
  132.             if(game->tab[i][j] == 'w'){
  133.                
  134.             } else {
  135.                 runLocal = 0;
  136.             }
  137.         } else {
  138.             runLocal = 0;
  139.         }
  140.         }
  141.     }
  142.     // Cas ou on est BLANC
  143.     else{
  144.         int runLocal =1;
  145.         while(runLocal){
  146.         // On se déplace selon l'axe formé par l'emplacement vide et le pion adverse
  147.        
  148.         i += ligPion - ligEmpty; j += colPion - colEmpty;
  149.        
  150.         if(i >= 0 && j >= 0 && i < game->lig && j < game->col){
  151.             if(game->tab[i][j] == 'b'){
  152.  
  153.             } else {
  154.                 runLocal = 0;
  155.             }
  156.         } else {
  157.             runLocal = 0;
  158.         }
  159.         }
  160.     }
  161.    
  162.     if(i>=game->lig || j>=game->col || i<0 || j<0){
  163.         return 0;
  164.     }
  165.     if (game->tab[i][j] == game->color){
  166.         return 1; // On peut jouer ici
  167.     } else {
  168.         return 0; // On ne peut pas jouer ici
  169.     }
  170.  
  171.        
  172.    
  173.  
  174. }
  175.  
  176. void valid_case(Game *game,int ** coordValid){
  177.     for (int i = 0; i < game->col; ++i)
  178.     {
  179.         for (int j = 0; j < game->lig; ++j)
  180.         {  
  181.             // Si case vide
  182.             if(game->tab[i][j] == 'e'){
  183.  
  184.                 // Si on est NOIR
  185.                 if (game->color == 'b')
  186.                 {   // Parcout les 8 cases (max) autour, s'arrete si trouve un pion blanc
  187.                     for (int a = i-1; a < i+2; ++a) // on commence dans le coin en haut à gauche
  188.                     {  
  189.                         for (int b = j-1; b < j+2; ++b)
  190.                         {   // Test si on est pas hors de la grille
  191.                             if (a >= 0 && b >= 0 && a < game->lig && b < game->col)
  192.                             {  
  193.                                 if (game->tab[a][b] == 'w')
  194.                                 {
  195.                                     // fonction test si on peut manger des pions adverses (et donc jouer)
  196.                                     if (eatPion(game, i,j,a,b) == 1)
  197.                                     {  
  198.                                         coordValid[i][j] = 1;
  199.                                     }
  200.                                 }
  201.                             }
  202.                         }
  203.                     }  
  204.                 }
  205.  
  206.                 // Si on est BLANC
  207.                 else{
  208.                     // Parcout les 8 cases (max) autour, s'arrete si trouve un pion blanc
  209.                     for (int a = i-1; a < i+2; ++a) // on commence dans le coin en haut à gauche
  210.                     {  
  211.                         for (int b = j-1; b < j+2; ++b)
  212.                         {   // Test si on est pas hors de la grille
  213.                             if (a >= 0 && b >= 0 && a < game->lig && b < game->col)
  214.                             {  
  215.                                 if (game->tab[a][b] == 'b')
  216.                                 {
  217.                                     // fonction test si on peut manger des pions adverses (et donc jouer)
  218.                                     if (eatPion(game, i,j,a,b) == 1)
  219.                                     {  
  220.                                         coordValid[i][j] = 1;
  221.                                     }
  222.                                 }
  223.                             }
  224.                         }
  225.                     }
  226.                 }
  227.  
  228.             }
  229.         }
  230.     }
  231.    
  232. }
  233.  
  234. int getBestMove(Game *game, int * bestMove){
  235.     int ** coordValid = malloc(game->col*sizeof(*coordValid));
  236.  
  237.     for(int i = 0; i < game->col; ++i){
  238.         coordValid[i] = malloc(game->lig*sizeof(**coordValid));
  239.     }
  240.  
  241.     for (int a = 0; a < game->col; a++)
  242.     {
  243.         for (int b = 0; b < game->lig; b++)
  244.         {
  245.             coordValid[a][b] = 0;
  246.         }
  247.     }
  248.  
  249.     valid_case(game, coordValid);
  250.     if(DEBUG_BOARD){
  251.         printf("\n");
  252.         for (int a = 0; a < game->col; a++)
  253.         {
  254.             for (int b = 0; b < game->lig; b++)
  255.             {
  256.                 printf("%d", coordValid[a][b]);
  257.             }
  258.             printf("\n");
  259.         }  
  260.     }
  261.     for (int a = 0; a < game->col; a++)
  262.     {
  263.         for (int b = 0; b < game->lig; b++)
  264.         {
  265.             if(coordValid[a][b] == 1){
  266.                 if(DEBUG_BOARD){
  267.                     printf("x: %d\ny: %d\nval: %d\n", a, b, coordValid[a][b]);
  268.                 }
  269.                 bestMove[0] = a;
  270.                 bestMove[1] = b;
  271.                 return 1;
  272.             }
  273.         }
  274.     }
  275.     return 0;  
  276. }
  277.  
  278.  
  279. //FONCTION DONNEE DANS LE PROJET
  280. int init_connection(void){
  281.     SOCKET sock_client;
  282.  
  283.     SOCKADDR_IN server;
  284.     WSADATA wsa;
  285.    
  286. #ifdef MINGW32
  287.     // code for windows only
  288.     printf("\nInitialising Winsock...");
  289.     if(WSAStartup(MAKEWORD(2,2),&wsa) != 0)
  290.     {
  291.         printf("Failed. Error Code : %d",WSAGetLastError());
  292.         return 1;
  293.     }
  294. #endif
  295.  
  296.     //Create socket
  297.     sock_client = socket(AF_INET, SOCK_STREAM, 0);
  298.  
  299.     if (sock_client == INVALID_SOCKET) {
  300.         printf("Could not create socket");
  301.         exit (EXIT_FAILURE);
  302.     }
  303.     puts("Client Socket created");
  304.  
  305.     // Define the socket remote address : 192.168.0.111:8888
  306.     server.sin_addr.s_addr = inet_addr("127.0.0.1");
  307.     server.sin_family = AF_INET;
  308.     server.sin_port = htons(PORT);
  309.  
  310.  
  311.     //Connect to remote server
  312.     if (connect(sock_client, (SOCKADDR *) &server, sizeof(server)) < 0) {
  313.         perror("connect failed. Error");
  314.         exit (EXIT_FAILURE);
  315.     }
  316.     puts("Connected\n");
  317.  
  318.  
  319.     return sock_client;
  320. }
  321.  
  322. //PERMET DE NETTOYER LE BUFFER POUR N'AVOIR QUE DES 0 (USE ONLY FOR DEV - DEBUG SOCKET PROBLEM)
  323. void clearBuffer(char * buffer){
  324.     int i;
  325.     for(i=0; i<BUFFER_SIZE; i++){
  326.         buffer[i] = 0;
  327.     }
  328. }
  329.  
  330. //PERMET D'AFFICHER LE BUFFER DE FACON BRUT
  331. void displayBuffer(char * buffer, int length){
  332.     int i;
  333.     printf("\nSTART OF BUFFER\n");
  334.      for(i=0; i<length; i++){
  335.         printf(" %X ", buffer[i]);
  336.     }
  337.     printf("\nEND OF BUFFER\n");
  338. }
  339.  
  340. // function to convert
  341. // Hexadecimal to Binary Number
  342. void HexToBin(char hexNb, char* bin)
  343. {
  344.    
  345.     if(DEBUG_HEX_CONVERTER){
  346.         printf("START HEX TO BIN ********************\n");
  347.         printf("We are going to work with those value : \n");
  348.         printf("%02X ",  (unsigned int)(hexNb & 0xFF));
  349.     }
  350.    
  351.     int i = 0;
  352.     char hex[3];
  353.     if(sprintf(hex, "%02X",  (unsigned int)(hexNb & 0xFF)) > 0){
  354.        if(DEBUG_HEX_CONVERTER){printf("\nCopy well done\n");}
  355.     }else{
  356.         if(DEBUG_HEX_CONVERTER){printf("\nerror copy\n");}
  357.     }
  358.    
  359.     for(i=0; i<2; i++)
  360.     {
  361.          if(DEBUG_HEX_CONVERTER){printf("\nThe current conversion is on bit : %c \n", hex[i]);}
  362.  
  363.         if(hex[i] == '0'){
  364.             strcat(bin, "0000");
  365.         } else if (hex[i] == '1'){
  366.             strcat(bin, "0001");        
  367.         } else if (hex[i] == '2'){
  368.             strcat(bin, "0010");      
  369.         } else if (hex[i] == '3'){
  370.             strcat(bin, "0011");          
  371.         } else if (hex[i] == '4'){
  372.             strcat(bin, "0100");
  373.         } else if (hex[i] == '5'){
  374.              strcat(bin, "0101");
  375.         } else if (hex[i] == '6'){
  376.             strcat(bin, "0110");
  377.         } else if (hex[i] == '7'){
  378.              strcat(bin, "0111");
  379.         } else if (hex[i] == '8'){
  380.             strcat(bin, "1000");
  381.         } else if (hex[i] == '9'){
  382.             strcat(bin, "1001");
  383.         } else if (hex[i] == 'A'){
  384.             strcat(bin, "1010");
  385.         } else if (hex[i] == 'B'){
  386.             strcat(bin, "1011");
  387.         } else if (hex[i] == 'C'){
  388.             strcat(bin, "1100");
  389.         } else if (hex[i] == 'D'){
  390.             strcat(bin, "1101");
  391.         } else if (hex[i] == 'E'){
  392.            strcat(bin, "1110");
  393.         } else if (hex[i] == 'F'){
  394.             strcat(bin, "1111");
  395.         } else {
  396.             printf("Invalid hexadecimal input.\n");
  397.         }
  398.     }
  399.     if(DEBUG_HEX_CONVERTER){
  400.         puts(bin);
  401.         printf("END HEX TO BIN ********************\n\n\n\n\n\n");
  402.        
  403.     }
  404. }
  405.  
  406. char getContentFromDecodedHex(char a, char b){
  407.     if(a == '0' && b == '0'){
  408.         return 'e';
  409.     } else if (a == '1' && b == '0') {
  410.         return 'w';
  411.     } else if (a == '0' && b == '1') {
  412.         return 'b';
  413.     }
  414. }
  415.  
  416. //PERMET D'AFFICHER LE TYPE ET LE CONTENU DU MESSAGE
  417. void displayMessage(Message * recieved_message){
  418.    int i;
  419.     printf("\nSTART OF MESSAGE\n");
  420.     printf("The type is : %X \n", recieved_message->type);
  421.     printf("The data is : ");
  422.      for(i=0; i<recieved_message->length; i++){
  423.         printf(" %02X ",  (unsigned int)(recieved_message->data[i] & 0xFF));
  424.     }
  425.     printf("\nEND OF MESSAGE\n");
  426. }
  427.  
  428. void displayBoard(Game * game){
  429.     int i,j;
  430.     printf("\nThis is the game board :\n");
  431.     for(i=0; i<game->col; i++){
  432.         for(j=0; j<game->lig; j++){
  433.             printf("%c", game->tab[i][j]);
  434.         }
  435.         printf("\n");
  436.     }
  437.     printf("\nEnd of the game board.");
  438. }
  439.  
  440. //PERMET D'ENVOYER UN MESSAGE - DONNEE DANS LE PROJET
  441. void sendMessage(SOCKET sock, const char *ecrit, int nbOctet) {
  442.     //Variables locales
  443.     int l = 0;
  444.     int fin;
  445.  
  446.     //En cas d'erreur lors de l'envoi
  447.     if (send (sock, ecrit, nbOctet, 0) < 0)
  448.     {
  449.         printf ("Error : When sending new message\n");
  450.  
  451.     }
  452.  
  453.     //Affichage du message envoyé
  454.     printf("Message sent : ");
  455.     fin = (int) ecrit[1]+4;
  456.     for (l = 0; l < fin ; l++) {
  457.         printf(" %X ", ecrit[l]);
  458.     }
  459.     printf("\n");
  460.  
  461. }
  462.  
  463. void send_coord(SOCKET sock, int * bestMove){
  464.     //Variables locales
  465.     char message[100], server_reply[2000];
  466.     int e, i, playerColor;
  467.     char CRC = 0;
  468.  
  469.  
  470.  
  471.     // Create the "Connect Message"
  472.     message[0] = SYNCRO;  // Synchro
  473.     message[1] = 2; // Message Length
  474.     message[2] = NEWMOVE_MESSAGE;  // Connect message
  475.     message[3] = bestMove[1] & 0xff;  // x
  476.     message[4] = bestMove[0] & 0xff;  // y
  477.     message[5] = (NEWMOVE_MESSAGE + message[3] + message[4]) & 0xff;
  478.  
  479.     // Send "ok message" to the GM
  480.     sendMessage(sock, message, 6);
  481. }
  482.  
  483. void send_ok(SOCKET sock){
  484.     //Variables locales
  485.     char message[100], server_reply[2000];
  486.     int e, i, playerColor;
  487.     char CRC = 0;
  488.  
  489.  
  490.  
  491.     // Create the "Connect Message"
  492.     message[0] = SYNCRO;  // Synchro
  493.     message[1] = 1; // Message Length
  494.     message[2] = OKNOK_MESSAGE;  // Connect message
  495.     message[3] = OK_RESPONSE;  // OK info
  496.     message[4] = 0x03; //CRC
  497.  
  498.     // Send "ok message" to the GM
  499.     sendMessage(sock, message, 5);
  500. }
  501. //PERMET D'ENVOYER LE MESSAGE DE CONNECTION - DONNE DANS LE PROJET
  502. int connect_message(SOCKET sock, char *name){
  503.  
  504.     //Variables locales
  505.     char message[100], server_reply[2000];
  506.     int e, i, playerColor;
  507.     char CRC = 0;
  508.  
  509.  
  510.  
  511.     // Create the "Connect Message"
  512.     message[0] = SYNCRO;  // Synchro
  513.     message[1] = strlen(name)+1; // Message Length
  514.  
  515.  
  516.     message[2] = CONNECT_MESSAGE;  // Connect message
  517.     message[3] = strlen(name);  // Name Length
  518.  
  519.     //Récupération du nom
  520.     for(i=4; i < (strlen(name)+4); i++)
  521.         message[i] = name[i-4];
  522.  
  523.  
  524.     //Calcul du CRC lors du choix du nom
  525.     for (e = 2; e < (strlen(name)+4); e++)
  526.         CRC += message[e];
  527.     message[strlen(name)+4] = CRC;
  528.  
  529.     // Send "connect message" to the GM
  530.     sendMessage(sock, message, (strlen(name)+5));
  531. }
  532.  
  533. //PERMET DE RECEVOIR UN MESSAGE
  534. Message* readMessage(SOCKET sock) {
  535.     //on alloue un nouveau message
  536.     Message *received_message = (Message *) malloc(sizeof (Message));
  537.     received_message->valid_message = 0;
  538.     //on alloue le buffer pour recevoir le message
  539.     char buffer[BUFFER_SIZE];
  540.     if(DEBUG_SOCKET){
  541.         clearBuffer(buffer);
  542.     }
  543.     int status, i;
  544.    
  545.     status = recv (sock, buffer, BUFFER_SIZE, 0);
  546.    
  547.     if (status < 0) //En cas d'erreur lors de la reception
  548.     {
  549.         printf ("Error : When reading new message\n");
  550.         perror("read");
  551.  
  552.     } else if (status == 0){ //En cas de deconnection du serveur
  553.         printf ("Error : peer disconnected\n");
  554.         run = 0;
  555.     } else {
  556.         //Affichage du buffer
  557.         if(DEBUG_BUFFER){
  558.             displayBuffer(buffer, status);
  559.         }
  560.        
  561.         //Enregistrement du message reçu
  562.         received_message->length = buffer[1];
  563.         received_message->type = buffer[2];
  564.         received_message->data = (char *)malloc(received_message->length * sizeof(char));
  565.         for(i=3; i<received_message->length+3; i++){
  566.             received_message->data[i-3] = buffer[i];
  567.         }
  568.         received_message->valid_message = 1;
  569.        
  570.  
  571.     }
  572.  
  573.     return received_message; //on renvoi le pointeur du message
  574. }
  575.  
  576.  
  577.  
  578. //inet_addr
  579. int main(int argc, char *argv[]) {
  580.     //DECLARATION DES VARIABLES
  581.     char decoded_hex[9];
  582.     int i, j, indexData, resultOfModulo, ret;
  583.     int * bestMove = (int *)malloc(2*sizeof(int));
  584.     Game * game = (Game *)malloc(sizeof(Game));
  585.     Message *received_message;
  586.     SOCKET sock_client = init_connection();
  587.     bestMove[0] = 0;
  588.     bestMove[1] = 0;
  589.    
  590.     //ON SE CONNECTE AU SERVEUR VIA LA FONCTION DONNEE DANS LE SUJET
  591.     connect_message(sock_client, argv[1]);
  592.  
  593.     //ON AFFICHE NOTRE NOM
  594.     printf("You are : %s \n", argv[1]);
  595.  
  596.     //ON ENTRE DANS LA BOUCLE DE JEU
  597.     while(run){
  598.         //ON ATTEND UN NOUVEAU MESSAGE
  599.         received_message =  readMessage(sock_client);
  600.  
  601.         if(received_message->valid_message){ // ON S'ASSURE QUE LE MESSAGE EST BON
  602.  
  603.              //ON AFFICHE LE MESSAGE DU SERVEUR
  604.             if(DEBUG_MESSAGE){
  605.                 displayMessage(received_message);
  606.             }
  607.             /*
  608.             #define OKNOK_MESSAGE 0x02
  609.             #define CONNECT_MESSAGE 0x01
  610.             #define NEWMOVE_MESSAGE 0x03
  611.             #define END_MESSAGE 0x04
  612.  
  613.             */
  614.  
  615.             switch(received_message->type){
  616.                 case PLAYEROK_MESSAGE : //MESSAGE DE COULEUR
  617.                     printf("\nVous jouez avec la couleur :");
  618.                     if(received_message->data[0] == 0x01){
  619.                         game->color = 'b';
  620.                         printf(" Black\n");
  621.                     } else if(received_message->data[0] == 0x02){
  622.                         game->color = 'w';
  623.                         printf(" White\n");
  624.                     }
  625.                     break;
  626.                 case NEXTTURN_MESSAGE :
  627.                     printf("It's your turn now !\n");
  628.                    
  629.                     game->lig = received_message->data[2];
  630.                     game->col = received_message->data[3];
  631.                     game->tab = (char **)malloc(game->col * sizeof(char*));
  632.                     for(i=0; i<game->lig; i++){
  633.                         game->tab[i] = (char *)malloc(game->lig * sizeof(char));
  634.                     }
  635.                    
  636.                     indexData = -1;
  637.                     for(i = 0; i < game->col; i++){
  638.                         for(j = 0; j < game->lig; j++){
  639.                             //chaque case de data fait 4 case de tab donc on augmente la variable indexData tous les 4 tours
  640.                            
  641.                             if(j == 0 || j == 4){
  642.                                 indexData++;
  643.                                 strcpy(decoded_hex, "");
  644.                                 HexToBin(received_message->data[indexData+4], decoded_hex);
  645.                             }
  646.                            
  647.                             if(j == 0 || j == 4){
  648.                                 if(DEBUG_CONVERTER){
  649.                                     printf("i: %d  -  j:%d   -    %c%c    -    ", i, j, decoded_hex[0], decoded_hex[1]);
  650.                                     printf("%c\n", getContentFromDecodedHex(decoded_hex[0], decoded_hex[1]));
  651.                                 }
  652.                                
  653.                                     //on prend les 2 premiers bits
  654.                                     game->tab[i][j] = getContentFromDecodedHex(decoded_hex[0], decoded_hex[1]);
  655.                                  
  656.                             } else if(j == 1 || j == 5){
  657.                                 if(DEBUG_CONVERTER){
  658.                                 printf("i: %d  -  j:%d   -    %c%c    -    ", i, j, decoded_hex[2], decoded_hex[3]);
  659.                                     printf("%c\n", getContentFromDecodedHex(decoded_hex[2], decoded_hex[3]));
  660.                                 }
  661.                                     //on prend les 2  bits d'après
  662.                                     game->tab[i][j] = getContentFromDecodedHex(decoded_hex[2], decoded_hex[3]);
  663.                                    
  664.                             } else if(j == 2|| j == 6){
  665.                                 if(DEBUG_CONVERTER){
  666.                                     printf("i: %d  -  j:%d   -    %c%c    -    ", i, j, decoded_hex[4], decoded_hex[5]);
  667.                                     printf("%c\n", getContentFromDecodedHex(decoded_hex[4], decoded_hex[5]));
  668.                                 }
  669.                                     //on prend les 2 bits avant derniers
  670.                                     game->tab[i][j] = getContentFromDecodedHex(decoded_hex[4], decoded_hex[5]);
  671.                              
  672.                             } else if(j == 3 || j == 7){
  673.                                 if(DEBUG_CONVERTER){
  674.                                 printf("i: %d  -  j:%d   -    %c%c    -    ", i, j, decoded_hex[6], decoded_hex[7]);
  675.                                     printf("%c\n", getContentFromDecodedHex(decoded_hex[6], decoded_hex[7]));
  676.                                 }
  677.                                     //on prend les 2 dernier bits
  678.                                     game->tab[i][j] = getContentFromDecodedHex(decoded_hex[6], decoded_hex[7]);
  679.                                
  680.                             } else {
  681.                                 printf("ERROR WHEN DECODE DATA");
  682.                             }      
  683.                         }
  684.                         if(DEBUG_CONVERTER){
  685.                         printf("\n\n");
  686.                         }
  687.                     }
  688.                     displayBoard(game);
  689.                     printf("let's find a move\n");
  690.                     if(getBestMove(game, bestMove)){
  691.                         printf("We want to play this : {%d;%d}\n", bestMove[0], bestMove[1]);
  692.                         send_coord(sock_client, bestMove);
  693.                     } else {
  694.                         printf("We can't play !\n");
  695.                         bestMove[0] = -1;
  696.                         bestMove[1] = -1;
  697.                         send_coord(sock_client, bestMove);
  698.                     }
  699.                    
  700.                    
  701.                     break;
  702.                 case PING_MESSAGE:
  703.                     printf("SERVER ASK IF I'M HERE !");
  704.                     send_ok(sock_client);
  705.                     break;
  706.                 case END_MESSAGE : //MESSAGE END
  707.                     run = 0;
  708.                     printf("Server stop the game, go sleep !");
  709.                     break;
  710.                 default :
  711.                     //code
  712.                     break;
  713.             }
  714.            
  715.         }
  716.        
  717.         free(received_message); //le message ne nous servira plus, on libere de l'espace
  718.     }
  719.        
  720.    
  721.    
  722.  
  723.  
  724.     close(sock_client);
  725.     sleep(60);
  726.  
  727.     return 0;
  728. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement