that_one_nigga_you_k

codigo robo com velha

Mar 9th, 2024
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/socket.h>
  5. #include <arpa/inet.h>
  6. #include <unistd.h>
  7. #include <time.h>
  8.  
  9. #define SERVER_ADDRESS "127.0.0.1"
  10. #define SERVER_PORT 8888
  11.  
  12. #define OPEN 1
  13. #define CLOSE 0
  14.  
  15. // Função para imprimir o tabuleiro
  16. void imprimirTabuleiro(char tabuleiro[3][3]) {
  17.     printf("\n  1 | 2 | 3\n");
  18.     printf(" -----------\n");
  19.     printf("  4 | 5 | 6\n");
  20.     printf(" -----------\n");
  21.     printf("  7 | 8 | 9\n\n");
  22.  
  23.     printf("Tabuleiro atual:\n\n");
  24.  
  25.     printf("  %c | %c | %c\n", tabuleiro[0][0], tabuleiro[0][1], tabuleiro[0][2]);
  26.     printf(" -----------\n");
  27.     printf("  %c | %c | %c\n", tabuleiro[1][0], tabuleiro[1][1], tabuleiro[1][2]);
  28.     printf(" -----------\n");
  29.     printf("  %c | %c | %c\n\n", tabuleiro[2][0], tabuleiro[2][1], tabuleiro[2][2]);
  30. }
  31.  
  32. // Função para verificar se há um vencedor
  33. char verificarVencedor(char tabuleiro[3][3]) {
  34.     // Verificar linhas e colunas
  35.     for (int i = 0; i < 3; i++) {
  36.         if (tabuleiro[i][0] == tabuleiro[i][1] && tabuleiro[i][1] == tabuleiro[i][2] && tabuleiro[i][0] != ' ')
  37.             return tabuleiro[i][0];
  38.         if (tabuleiro[0][i] == tabuleiro[1][i] && tabuleiro[1][i] == tabuleiro[2][i] && tabuleiro[0][i] != ' ')
  39.             return tabuleiro[0][i];
  40.     }
  41.     // Verificar diagonais
  42.     if (tabuleiro[0][0] == tabuleiro[1][1] && tabuleiro[1][1] == tabuleiro[2][2] && tabuleiro[0][0] != ' ')
  43.         return tabuleiro[0][0];
  44.     if (tabuleiro[0][2] == tabuleiro[1][1] && tabuleiro[1][1] == tabuleiro[2][0] && tabuleiro[0][2] != ' ')
  45.         return tabuleiro[0][2];
  46.  
  47.     // Verificar empate
  48.     int empate = 1;
  49.     for (int i = 0; i < 3; i++) {
  50.         for (int j = 0; j < 3; j++) {
  51.             if (tabuleiro[i][j] == ' ') {
  52.                 empate = 0;
  53.                 break;
  54.             }
  55.         }
  56.         if (!empate)
  57.             break;
  58.     }
  59.     if (empate)
  60.         return 'D'; // Indica empate
  61.  
  62.     return ' '; // Se nenhum vencedor ainda
  63. }
  64.  
  65. // Função para verificar se a célula está vazia
  66. int celulaVazia(char tabuleiro[3][3], int posicao) {
  67.     int linha = (posicao - 1) / 3;
  68.     int coluna = (posicao - 1) % 3;
  69.     return tabuleiro[linha][coluna] == ' ';
  70. }
  71.  
  72. // Função para fazer a jogada do computador
  73. int jogadaComputador(char tabuleiro[3][3]) {
  74.     printf("Turno do computador (O):\n");
  75.     int posicao;
  76.  
  77.     // Primeiro, verifique se há uma jogada vencedora disponível
  78.     for (posicao = 1; posicao <= 9; posicao++) {
  79.         if (celulaVazia(tabuleiro, posicao)) {
  80.             int linha = (posicao - 1) / 3;
  81.             int coluna = (posicao - 1) % 3;
  82.             tabuleiro[linha][coluna] = 'O';
  83.             if (verificarVencedor(tabuleiro) == 'O')
  84.                 return posicao; // Jogada vencedora encontrada
  85.             tabuleiro[linha][coluna] = ' '; // Desfazer a jogada
  86.         }
  87.     }
  88.  
  89.     // Em seguida, verifique se há uma jogada do oponente para bloquear
  90.     for (posicao = 1; posicao <= 9; posicao++) {
  91.         if (celulaVazia(tabuleiro, posicao)) {
  92.             int linha = (posicao - 1) / 3;
  93.             int coluna = (posicao - 1) % 3;
  94.             tabuleiro[linha][coluna] = 'X';
  95.             if (verificarVencedor(tabuleiro) == 'X') {
  96.                 tabuleiro[linha][coluna] = 'O'; // Bloquear a jogada do oponente
  97.                 return posicao;
  98.             }
  99.             tabuleiro[linha][coluna] = ' '; // Desfazer a jogada
  100.         }
  101.     }
  102.  
  103.     // Se não houver jogadas vencedoras ou de bloqueio, escolha uma célula aleatória
  104.     srand(time(NULL)); // Semente do gerador de números aleatórios
  105.     do {
  106.         posicao = rand() % 9 + 1;
  107.     } while (!celulaVazia(tabuleiro, posicao));
  108.  
  109.     int linha = (posicao - 1) / 3;
  110.     int coluna = (posicao - 1) % 3;
  111.     tabuleiro[linha][coluna] = 'O';
  112.  
  113.     return posicao;
  114. }
  115.  
  116. void delay(int delayTime) {
  117.     usleep(delayTime * 1000);
  118. }
  119.  
  120. void initSocket(int* sock, char* buffer){
  121.  
  122.     struct sockaddr_in serv_addr;
  123.  
  124.     // Create a socket
  125.     if ((*sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  126.         printf("\n Socket creation error \n");
  127.         exit(1);
  128.     }
  129.  
  130.     // Set the server address and port
  131.     memset(&serv_addr, '0', sizeof(serv_addr));
  132.     serv_addr.sin_family = AF_INET;
  133.     serv_addr.sin_port = htons(SERVER_PORT);
  134.  
  135.     // Convert IPv4 and IPv6 addresses from text to binary form
  136.     if (inet_pton(AF_INET, SERVER_ADDRESS, &serv_addr.sin_addr) <= 0) {
  137.         printf("\n Invalid address/ Address not supported \n");
  138.         exit(1);
  139.     }
  140.  
  141.     // Connect to the server
  142.     if (connect(*sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
  143.         printf("\n Connection Failed \n");
  144.         exit(1);
  145.     }
  146. }
  147.  
  148. void movJoints(int sock, char* buffer, float j1, float j2, float j3, float j4, float j5,float j6, int state){
  149.     sprintf(buffer, "%f,%f,%f,%f,%f,%f,%d", j1, j2, j3, j4, j5, j6,state);
  150.     send(sock, buffer, strlen(buffer), 0);
  151.     int valread = read(sock, buffer, 2048);
  152.     printf("Result: %s\n", buffer);
  153. }
  154.  
  155. void meumovJoints(int sock, char* buffer, float* j, int state){
  156.     sprintf(buffer, "%f,%f,%f,%f,%f,%f,%d", j[0], j[1], j[2], j[3], j[4], j[5],state);
  157.     send(sock, buffer, strlen(buffer), 0);
  158.     int valread = read(sock, buffer, 2048);
  159.     printf("Result: %s\n", buffer);
  160. }
  161.  
  162. void pegarPeca(int sock){
  163.     //int sock = 0;
  164.     char buffer[2048] = {0};
  165.  
  166.     float PosPrePeca[6]= {-1.58,-0.26, -0.89,-0.07,0.18,0.08};
  167.     float PosPeca[6] = {-1.60,-0.50,-0.77,-0.07,0.18,0.09};
  168.     meumovJoints(sock, buffer, PosPrePeca, OPEN);
  169.     delay(1000);
  170.     meumovJoints(sock, buffer, PosPeca, OPEN);
  171.     delay(350);
  172.     meumovJoints(sock, buffer, PosPeca, CLOSE);
  173.     delay(350);
  174.     meumovJoints(sock, buffer, PosPrePeca, CLOSE);
  175.     delay(150);
  176. }
  177.  
  178. int main(int argc, char *argv[]) {
  179.    
  180.     //variaveis para conexcao com o robo NAO MEXER
  181.     int sock = 0;
  182.     char buffer[2048] = {0};
  183.  
  184.     //conexao com o robo NAO MEXER
  185.     initSocket(&sock, buffer);
  186.  
  187.     //Programe aqui ---------------------------------------------------
  188.     float Pos1[6] = {-0.25, -0.66, -0.46, -0.18, -0.43, -0.03};
  189.     float Pos2[6] = {-0.05,-0.63,-0.48,-0.03,-0.51,-0.18};
  190.     float Pos3[6] = {0.18,-0.62,-0.45,0.04,-0.49,-0.05};
  191.     float Pos4[6] = {-0.22,-0.81,-0.12,-0.10,-0.60,-0.16};
  192.     float Pos5[6] = {-0.05, -0.75, -0.23, 0.00, -0.49, -0.04};
  193.     float Pos6[6] = {0.13,-0.80,-0.14,-0.03,-0.60,-0.18};
  194.     float Pos7[6] = {-0.19, -0.90,0.06,-0.12,-0.49,-0.07};
  195.     float Pos8[6] = {-0.03, -0.93, 0.08, -0.03, -0.49, 0.06};
  196.     float Pos9[6] = {0.12, -0.93, 0.07, 0.11, -0.49, -0.04};
  197.     float PosPrePeca[6]= {-1.58,-0.26, -0.89,-0.07,0.18,0.08};
  198.     float PosPeca[6] = {-1.60,-0.50,-0.77,-0.07,0.18,0.09};
  199.  
  200.     float posicoes[11][6]= {{-0.25, -0.66, -0.46, -0.18, -0.43, -0.03},
  201.                       {-0.05,-0.63,-0.48,-0.03,-0.51,-0.18},
  202.                       {0.18,-0.62,-0.45,0.04,-0.49,-0.05},
  203.                       {-0.22,-0.81,-0.12,-0.10,-0.60,-0.16},
  204.                       {-0.05, -0.75, -0.23, 0.00, -0.49, -0.04},
  205.                       {0.13,-0.80,-0.14,-0.03,-0.60,-0.18},
  206.                       {-0.19, -0.90,0.06,-0.12,-0.49,-0.07},
  207.                       {-0.03, -0.93, 0.08, -0.03, -0.49, 0.06},
  208.                       {0.12, -0.93, 0.07, 0.11, -0.49, -0.04},
  209.                       {-1.58, -0.26, -0.89,-0.07,0.18,0.08},
  210.                       {-1.60,-0.50,-0.77,-0.07,0.18,0.09}}; //de 1 pra 9 e depois a pré-peça e peça
  211.  
  212.    
  213.    
  214.  
  215.     char tabuleiro[3][3] = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
  216.  
  217.     printf("Bem-vindo ao Jogo da Velha!\n");
  218.     printf("Você é o jogador X. Faça sua jogada digitando um número de 1 a 9 correspondente à posição no tabuleiro.\n");
  219.  
  220.     while (1) {
  221.         imprimirTabuleiro(tabuleiro);
  222.  
  223.         // Verificar se houve um vencedor ou empate
  224.         char resultado = verificarVencedor(tabuleiro);
  225.         if (resultado != ' ') {
  226.             if (resultado == 'D')
  227.                 printf("Empate!\n");
  228.             else if (resultado == 'X')
  229.                 printf("Parabéns! Você venceu!\n");
  230.             else
  231.                 printf("O computador venceu!\n");
  232.             break;
  233.         }
  234.  
  235.         // Obter a jogada do jogador
  236.         int posicao;
  237.         printf("Sua vez (X), digite sua jogada (1 a 9): ");
  238.         scanf("%d", &posicao);
  239.        
  240.  
  241.         // Verificar se a jogada é válida
  242.         if (posicao < 1 || posicao > 9 || !celulaVazia(tabuleiro, posicao)) {
  243.             printf("Jogada inválida! Tente novamente.\n");
  244.             continue;
  245.         }
  246.         pegarPeca(sock);
  247.         meumovJoints(sock, buffer, posicoes[posicao-1], OPEN);  //jogada do humano feita pelo braço robótico
  248.  
  249.         // Atualizar o tabuleiro com a jogada do jogador
  250.         int linha = (posicao - 1) / 3;
  251.         int coluna = (posicao - 1) % 3;
  252.         tabuleiro[linha][coluna] = 'X';
  253.  
  254.         // Verificar se houve um vencedor ou empate após a jogada do jogador
  255.         resultado = verificarVencedor(tabuleiro);
  256.         if (resultado != ' ')
  257.             continue;
  258.  
  259.         // Jogada do computador
  260.         int jogada_pc = jogadaComputador(tabuleiro);
  261.         printf("O computador jogou na posição: %d\n", jogada_pc);
  262.         pegarPeca(sock);
  263.         meumovJoints(sock, buffer, posicoes[jogada_pc-1], OPEN);
  264.     }
  265.  
  266.     imprimirTabuleiro(tabuleiro);
  267.     return 0;
  268.     //-----------------------------------------------------------------
  269.  
  270.     // Encerra a conexao NAO MEXER
  271.     close(sock);
  272.  
  273.     return 0;
  274. }
  275.  
Add Comment
Please, Sign In to add comment