Advertisement
Guest User

Untitled

a guest
May 30th, 2015
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. public class Field {
  2. char [][] gameField;
  3. boolean [][] booleansField;
  4.  
  5. public Field() {
  6. this.gameField = new char[][]{
  7. {'#', '1', ' ', '2', ' ', '3'},
  8. {'1', ' ', '|', ' ', '|', ' '},
  9. {' ', '-', ' ', '-', ' ', '-'},
  10. {'2', ' ', '|', ' ', '|', ' '},
  11. {' ', '-', ' ', '-', ' ', '-'},
  12. {'3', ' ', '|', ' ', '|', ' '},
  13. };
  14. }
  15. public Field(boolean truee){
  16.  
  17. this.booleansField=new boolean[3][3];
  18.  
  19. }
  20.  
  21.  
  22. public char[][] createGameField() {
  23. char[][] gameField = new char[][]{
  24. {'#', '1', ' ', '2', ' ', '3'},
  25. {'1', ' ', '|', ' ', '|', ' '},
  26. {' ', '-', ' ', '-', ' ', '-'},
  27. {'2', ' ', '|', ' ', '|', ' '},
  28. {' ', '-', ' ', '-', ' ', '-'},
  29. {'3', ' ', '|', ' ', '|', ' '},
  30. };
  31. return gameField;
  32. }
  33.  
  34. public boolean[][] createBooleansField() {
  35. boolean[][] booleansField = new boolean[3][3];
  36. return booleansField;
  37. }
  38.  
  39. public void printGameField(Field field){
  40. for (int i = 0; i < field.gameField.length; i++) {
  41. System.out.println();
  42. for (int j = 0; j < field.gameField.length; j++) {
  43. System.out.print(field.gameField[i][j] + " ");
  44. }
  45. }
  46.  
  47. }
  48.  
  49. public boolean checkIfTheCoordinateIsFree(Step step, Field gameField) {
  50.  
  51. int xGameFieldPosition = gameFieldCoordinateConverter(step.yPos);
  52. int yGameFieldPosition = gameFieldCoordinateConverter(step.xPos);
  53. if (gameField.gameField[xGameFieldPosition][yGameFieldPosition] == ' ') {
  54. return true;
  55. } else {
  56. return false;
  57. }
  58. }
  59.  
  60. public Field showTheXOOnTheGameField(Field gameField, Step step, Player cross) {
  61. if (cross.xOSign) {
  62. gameField.gameField[gameFieldCoordinateConverter(step.xPos)][gameFieldCoordinateConverter(step.yPos)] = 'X';
  63. } else {
  64. gameField.gameField[gameFieldCoordinateConverter(step.xPos)][gameFieldCoordinateConverter(step.yPos)] = '0';
  65. }
  66. printGameField(gameField);
  67. return gameField;
  68. }
  69.  
  70. public boolean[][] FixStepOnTheBooleansField(Player player, Step step) {
  71. player.booleansField[booleansFieldCoordinateConverter(step.xPos)][booleansFieldCoordinateConverter(step.yPos)] = true;
  72. return player.booleansField;
  73. }
  74.  
  75. public int gameFieldCoordinateConverter(int position) {
  76. if (position == 1) {
  77. return 1;
  78. } else if (position == 2) {
  79. return 3;
  80. } else return 5;
  81. }
  82.  
  83. public int booleansFieldCoordinateConverter(int position) {
  84. if (position == 1) {
  85. return 0;
  86. } else if (position == 2) {
  87. return 1;
  88. } else return 2;
  89. }
  90.  
  91. public boolean threeInARow(Step step, Player player) {
  92. int xx = booleansFieldCoordinateConverter(step.xPos);
  93. int yy = booleansFieldCoordinateConverter(step.yPos);
  94.  
  95.  
  96.  
  97. return checkHorisontal(xx, yy, player.booleansField) || checkVertical(xx, yy, player.booleansField) || checkDiagonals(xx, yy, player.booleansField);
  98.  
  99.  
  100. }
  101.  
  102. private boolean checkHorisontal(int x, int y, boolean[][] booleansField) {
  103. if (x == 0) {
  104. if (booleansField[x + 1][y] && booleansField[x + 2][y]) {
  105. return true;
  106. }
  107. } else if (x == 1) {
  108. if (booleansField[x - 1][y] && booleansField[x + 1][y]) {
  109. return true;
  110. }
  111.  
  112. } else if (x == 2) {
  113. if (booleansField[x - 1][y] && booleansField[x - 2][y]) {
  114. return true;
  115. }
  116. }
  117. return false;
  118. }
  119.  
  120.  
  121. private boolean checkVertical(int x, int y, boolean[][] booleansField) {
  122. for (int i = 0; i < booleansField.length; i++) {
  123. System.out.println();
  124. for (int j = 0; j < booleansField.length; j++) {
  125. // ? System.out.print(booleansField[i][j]);
  126. System.out.print(i+j);
  127. }
  128. }
  129. if (y == 0) {
  130. if (booleansField[x][y + 1] && booleansField[x][y + 2]) {
  131. return true;
  132. }
  133. } else if (y == 1) {
  134. if (booleansField[x][y - 1] && booleansField[x][y + 1]) {
  135. return true;
  136. }
  137. } else if (y == 2) {
  138. System.out.println(booleansField[x][y-1]);
  139. if (booleansField[x][y-1] && booleansField[x][y - 2]) {
  140. return true;
  141. }
  142. }
  143. return false;
  144. }
  145.  
  146. private boolean checkDiagonals(int x, int y, boolean[][] booleansField) {
  147. if ((x == 0 && y == 1) || (x == 1 && y == 0) || (x == 2 && y == 1) || (x == 1 && y == 2)) {
  148. return false;
  149. } else if (x == 0 && y == 0) {
  150. if (booleansField[x + 1][y + 1] && booleansField[x + 2][y + 2]) {
  151. return true;
  152. }
  153. } else if (x == 1 && y == 1) {
  154. if (booleansField[x + 1][y + 1] && booleansField[x - 1][y - 1]) {
  155. return true;
  156. }
  157. } else if (x == 2 && y == 2) {
  158. if (booleansField[x - 2][y - 2] && booleansField[x - 1][y - 1]) {
  159. return true;
  160. }
  161. } else if (x == 0 && y == 2) {
  162. if (booleansField[x + 1][y - 1] && booleansField[x + 2][y - 2]) {
  163. return true;
  164. }
  165. }
  166.  
  167. return false;
  168. }
  169.  
  170. }
  171.  
  172. /* for (int i = 0; i < booleansField.length; i++) {
  173. System.out.println();
  174. for (int j = 0; j < booleansField.length; j++) {
  175. System.out.print(booleansField[i][j] + " ");
  176. }
  177. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement