Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define COL 7
  4. #define LIN 6
  5.  
  6. int jogada_jogador1(){
  7. int col;
  8. printf("Insira a coluna onde pretende inserir a jogada do jogador1: \n");
  9. scanf(" %d",&col);
  10. if(col<0 || col>7){
  11. jogada_jogador1();
  12. }
  13. return col;
  14. }
  15. int jogada_jogador2(){
  16. int col;
  17. printf("Insira a coluna onde pretende inserir a jogada do jogador2: \n");
  18. scanf(" %d", &col );
  19. if(col<0 || col>7){
  20. jogada_jogador2();
  21. }
  22. return col;
  23. }
  24. void print_tabuleiro(int matrix[LIN][COL]){
  25. for(int i = 0 ; i < LIN; i++){
  26. for (int j = 0; j <COL ; ++j) {
  27. printf(" %d", matrix[i][j]);
  28. }
  29. printf(" \n");
  30. }
  31. }
  32.  
  33. int registar_jogada1(int col,int matrix[LIN][COL]){
  34. for(int i = LIN-1; i >= 0;i--){
  35. if(matrix[i][col] == 0 ){
  36. matrix[i][col] = 1;
  37. print_tabuleiro(matrix);
  38. return 1;
  39. }
  40. }
  41.  
  42. printf("Jogada invalida , por favor jogue de novo\n");
  43. return 0;
  44. }
  45.  
  46. int registar_jogada2(int col,int matrix[LIN][COL]){
  47. for(int i = LIN-1; i >= 0;i--){
  48. if(matrix[i][col] == 0 ){
  49. matrix[i][col] = 2;
  50. print_tabuleiro(matrix);
  51. return 1;
  52. }
  53. }
  54.  
  55. printf("Jogada invalida , por favor jogue de novo\n");
  56. return 0;
  57. }
  58.  
  59. int main() {
  60. int mat[LIN][COL];
  61.  
  62. // INICIALIZAÇAO DA MATRIX A 0 EM TODAS AS POSIÇOES
  63. for(int i = 0 ; i < LIN; i++){
  64. for (int j = 0; j <COL ; ++j) {
  65. mat[i][j]=0;
  66. }
  67. }
  68. print_tabuleiro(mat);
  69.  
  70. // 1 representa jogador 1 , 2 representa jogada jogador 2
  71. int contador=0;
  72. int jog1;
  73. int jog2;
  74. int val1,val2;
  75. while (contador != 42){
  76. jog1 = jogada_jogador1();
  77. val1 = registar_jogada1(jog1,mat);
  78. while(val1 == 0){
  79. jog1 = jogada_jogador1();
  80. val1 = registar_jogada1(jog1,mat);
  81. }
  82.  
  83. jog2 = jogada_jogador2();
  84. val2 = registar_jogada2(jog2,mat);
  85. while(val2 == 0){
  86. jog2 = jogada_jogador2();
  87. val2 = registar_jogada2(jog2,mat);
  88. }
  89. contador++;
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement