Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: JasBarrera en 2 Mayo 2022, 16:03 pm



Título: Me podrían ayudar con este código. :(
Publicado por: JasBarrera en 2 Mayo 2022, 16:03 pm
Se supone que es un juego del gato o tres en ralla, pero el jugador debe de responder unas preguntas y si responde bien puede tirar, el problema es que no se como insertar las preguntas.
Me podrían ayudar en eso, este es el código : )

Código
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. char matrix[3][3] = {
  4. ' ', ' ', ' ',
  5. ' ', ' ', ' ',
  6. ' ', ' ', ' ',
  7. };
  8. void get_player_move()
  9. {
  10. int x,y;
  11. int ok=0;
  12. printf("da las coordenadas para colocar tu X: \n");
  13. do
  14. {
  15. printf("renglon ==>");
  16. scanf("%d",&x);
  17. printf("columna ==>");
  18. scanf("%d",&y);
  19. x--;
  20. y--;
  21.  
  22. if(matrix[x][y]!=' ') printf("movimiento invalido,intenta de nuevo\n");
  23. else
  24. {
  25. matrix[x][y] = 'X';
  26. ok = 1;
  27. }
  28.  
  29. }while(ok!=1);
  30.  
  31. return;
  32. }
  33. void get_computer_move()
  34. {
  35. int t, i;
  36.  
  37. for(t=0; t<3; t++)
  38. {
  39. for(i=0; i<3; ++i)
  40. if(matrix[t][i]==' ') break;
  41. if(matrix[t][i]==' ') break;
  42. }
  43.  
  44. if(t*i==9)
  45. {
  46. printf("¡¡¡¡¡empate!!!!!\n");
  47. system("pause");
  48. exit(0);
  49. }
  50. else matrix[t][i] = 'O';
  51.  
  52. return;
  53. }
  54. void disp_matrix()
  55. {
  56. int t;
  57. for(t=0; t<3; t++)
  58. {
  59. printf(" %c | %c | %c",matrix[t][0],matrix[t][1],matrix [t][2]); ;
  60.  
  61. if(t!=2) printf("\n---|---|---\n");
  62. }
  63. printf("\n");
  64.  
  65. return;
  66. }
  67.  
  68. char check()
  69. {
  70. int t;
  71. for (t=0; t<3; t++)
  72. if((matrix[0][t]==matrix[1][t] &&
  73. matrix[1][t]==matrix[2][t]) && matrix[0][t]!=' ') return (matrix[0][t]);
  74. for(t=0; t<3; t++)
  75. if((matrix[t][0]==matrix[t][1] &&
  76. matrix[t][1]==matrix[t][2]) && matrix[t][0]!=' ') return (matrix[t][0]);
  77.  
  78.  
  79. if(matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2])
  80. return (matrix[0][0]);
  81. if(matrix[0][2]==matrix[1][1] && matrix[1][1]==matrix[2][0])
  82. return (matrix[0][2]);
  83. return(' ');
  84. int main()
  85. {
  86. char done;
  87.  
  88. printf("Este es el juego del Gato. \n");
  89. printf("jugaras contra la computadora \n");
  90. printf("alimenta un renglon(1..3) y una \n");
  91. printf("columna(1..3) para dar la posicion \n");
  92. printf("donde colocar tu X \n\n");
  93.  
  94. done = ' ';
  95. do
  96. {
  97. disp_matrix();
  98. get_player_move();
  99. done=check();
  100. printf("%c\n",done);
  101. if(done != ' ') break;
  102. get_computer_move();
  103. done=check(); //
  104. } while(done==' ');
  105. if (done == 'X') printf("\nTu ganaste!\n");
  106. else printf("\nYo gane!!!!\n");
  107.  
  108. disp_matrix();
  109. system("pause");
  110. return 0;
  111. }

MOD: El código debe ir entre etiquetas de Código GeSHi


Título: Re: Me podrían ayudar con este código. :(
Publicado por: K-YreX en 2 Mayo 2022, 16:48 pm
Hacer una pregunta no es más que:
1. Escribir por pantalla la pregunta
2. Recoger la respuesta del usuario
3. Comparar la respuesta del usuario con la respuesta correcta

Además dependerá de si es una pregunta abierta (el usuario escribe la respuesta que quiere -> en este caso tendrás que recoger una cadena de texto) o de selección (el usuario elige una de las opciones disponibles -> en este caso podrás hacerlo simplemente con números).

De todo lo anterior, no sé qué es lo que te ocasiona problemas...  :rolleyes: