Advertisement
Guest User

TicTacToe

a guest
Jul 23rd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1.  
  2. void printM(int[][] x) {
  3. for (int i = 0; i < x.length; i++) {
  4. for (int j = 0; j < x[i].length; j++) {
  5. System.out.print(x[i][j] + " ");
  6. }
  7. System.out.println();
  8. }
  9. }
  10.  
  11. boolean riga(int[][] x) {
  12. int i = 0;
  13. int j = 0;
  14. int variabile = x[j][0];
  15. int contatore = 1;
  16. while (i < x[0].length && j < x[0].length) {
  17. i++;
  18.  
  19. if (x[j][i] == variabile && x[j][i] != 0) {
  20. contatore++;
  21. if (contatore == x[0].length) {
  22. return true;
  23. }
  24. } else {
  25. j++;
  26. if (i < x[0].length && j < x[0].length) {
  27. i = 0;
  28. contatore = 1;
  29. variabile = x[j][0];
  30. } else {
  31. return false;
  32. }
  33. }
  34. }
  35. return false;
  36. }
  37.  
  38. boolean colonna(int[][] x) {
  39. int i = 0;
  40. int j = 0;
  41. int variabile = x[0][j];
  42. int contatore = 1;
  43. while (i < x[0].length && j < x[0].length) {
  44. i++;
  45.  
  46. if (x[i][j] == variabile && x[i][j] != 0) {
  47. contatore++;
  48. if (contatore == x[0].length) {
  49. return true;
  50. }
  51. } else {
  52. j++;
  53. if (i < x[0].length && j < x[0].length) {
  54. i = 0;
  55. contatore = 1;
  56. variabile = x[0][j];
  57. } else {
  58. return false;
  59. }
  60. }
  61. }
  62. return false;
  63. }
  64.  
  65. boolean diagonalePrima(int[][] x) {
  66. int i = 0;
  67. int j = 0;
  68. int variabile = x[i][j];
  69. int contatore = 1;
  70. while (i < x[0].length && j < x[0].length) {
  71. if (variabile != 0) {
  72. j++;
  73. i++;
  74. }
  75.  
  76. if (x[i][j] == variabile && x[i][j] != 0) {
  77. contatore++;
  78. if (contatore == x[0].length) {
  79. return true;
  80. }
  81. } else {
  82.  
  83. return false;
  84. }
  85. }
  86. return false;
  87. }
  88.  
  89.  
  90. boolean diagonaleSeconda(int[][] x) {
  91. int i = 0;
  92. int j = x[0].length -1 ;
  93. int variabile = x[i][j];
  94. int contatore = 1;
  95. while (i < x.length && j >= 0) {
  96. if (variabile != 0) {
  97. j--;
  98. i++;
  99. }
  100.  
  101. if (x[i][j] == variabile && x[i][j] != 0) {
  102. contatore++;
  103. if (contatore == x[0].length) {
  104. return true;
  105. }
  106. } else {
  107.  
  108. return false;
  109. }
  110. }
  111. return false;
  112. }
  113. boolean tictactoe(int[][] a) {
  114. if(a[0][0] == 0){return false;}
  115. if(a[0][0] == 1){return true;}
  116. if(a[0][0] == 2){return true;}
  117.  
  118. if (riga(a) == true) { return true;}
  119. if (colonna(a) == true) {return true;}
  120. if (diagonalePrima(a) == true) {return true;}
  121. if (diagonaleSeconda(a) == true) {return true;}
  122.  
  123. return false;
  124. }
  125. int[][] x = {{1,2,0}, {2,1,1}, {2,0,2}};
  126. printM(x);
  127. System.out.println(x[0].length);
  128. System.out.println(riga(x));
  129. System.out.println(colonna(x));
  130. System.out.println(diagonalePrima(x));
  131. System.out.println(diagonaleSeconda(x));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement