fork download
  1. /* Um exemplo de jogo-da-velha simples */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. char matrix[3][3]; /* A matriz do jogo */
  6.  
  7. char check(void);
  8. void int_matrix(void);
  9. void get_player_move(void);
  10. void get_computer_move(void);
  11. void disp_matrix(void);
  12.  
  13. void main(void)
  14. {
  15. char done;
  16.  
  17. printf("Este é o jogo-da-velha\n");
  18. printf("Você estará jogando contra o computador\n");
  19.  
  20. done = ' ';
  21. init_matrix();
  22. do{
  23. disp_matrix();
  24. get_player_move();
  25. done = check(); /* Verifica se há vencedor */
  26. if(done!=' ') break; /* Vencedor */
  27. get_computer_move();
  28. done = check(); /* Verifica se há vencedor */
  29. } while(done == ' ');
  30. if(done == 'X') printf("Voce ganhou!\n");
  31. else printf("Eu ganhei!\n");
  32. disp_matrix(); /* Mostra as posições finais */
  33. }
  34.  
  35. /* Inicializa a matriz */
  36.  
  37. void init_matrix(void)
  38. {
  39. int i, j;
  40.  
  41. for(i=0; i<3; i++)
  42. for(j=0; j<3; j++) matrix[i][j] = ' ';
  43. }
  44.  
  45. /* Obtém a sua jogada */
  46.  
  47. void get_player_move(void)
  48. {
  49. int x, y;
  50.  
  51. printf("Digite as coordenadas para o X: ");
  52. scanf("%d%d", &x, &y);
  53.  
  54. x--; y--;
  55.  
  56. if(matrix[x][y] != ' '){
  57. printf("Posicao invalida, tente novamente\n");
  58. get_player_move();
  59. }
  60. else matrix[x][y] = 'X';
  61. }
  62.  
  63. /* Obtém uma jogada do computador */
  64.  
  65. void get_computer_move(void)
  66. {
  67. int i, j;
  68. for(i=0; i<3; i++){
  69. for(j=0; j<3; j++)
  70. if(matrix[i][j] == ' ') break;
  71. if(matrix[i][j] == ' ') break;
  72. }
  73. if(i*j == 9){
  74. printf("Empate\n");
  75. exit(0);
  76. }
  77. else
  78. matrix[i][j] = 'O';
  79. }
  80.  
  81. /* Mostra a matriz na tela */
  82.  
  83. void disp_matrix(void)
  84. {
  85. int t;
  86.  
  87. for(t=0; t<3; t++){
  88. printf(" %c | %c | %c ", matrix[t][0], matrix [t][1], matrix[t][2]);
  89. if(t != 2) printf("\n---|---|---\n");
  90. }
  91. printf("\n");
  92. }
  93.  
  94. /* Verifica se há um vencedor */
  95.  
  96. char check(void)
  97. {
  98. int i;
  99.  
  100. for(i=0; i<3; i++) /* Verifica as linhas */
  101. if(matrix[i][0] == matrix[i][1] && matrix[i][0] == matriz[i][2]) return matrix[i][0];
  102.  
  103. for(i=0; i<3; i++) /* Verifica as colunas */
  104. if(matrix[0][i] == matrix[1][i] && matrix[0][i] == matrix[2][i]) return matrix[0][i];
  105.  
  106. /* Testa as diagonais */
  107.  
  108. if(matrix[0][0] == matrix[1][1] && matrix[1][1] == matriz[2][2]) return matrix[0][0];
  109. if(matrix[0][2] == matrix[1][1] && matrix[1][1] == matriz[2][0]) return matrix[0][2];
  110.  
  111. return ' ';
  112. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:13:6: error: return type of 'main' is not 'int' [-Werror=main]
 void main(void)
      ^
prog.c: In function 'main':
prog.c:21:1: error: implicit declaration of function 'init_matrix' [-Werror=implicit-function-declaration]
 init_matrix();
 ^
prog.c: At top level:
prog.c:37:6: error: conflicting types for 'init_matrix' [-Werror]
 void init_matrix(void)
      ^
prog.c:21:1: note: previous implicit declaration of 'init_matrix' was here
 init_matrix();
 ^
prog.c: In function 'check':
prog.c:101:56: error: 'matriz' undeclared (first use in this function)
     if(matrix[i][0] == matrix[i][1] && matrix[i][0] == matriz[i][2]) return matrix[i][0];
                                                        ^
prog.c:101:56: note: each undeclared identifier is reported only once for each function it appears in
cc1: all warnings being treated as errors
stdout
Standard output is empty