Advertisement
madegoff

ClassBoard_verbessert

May 27th, 2024
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.21 KB | None | 0 0
  1. import java.util.InputMismatchException;
  2. //import java.util.List;
  3. import java.util.Stack;
  4.  
  5. import static java.lang.Math.abs;
  6. /**
  7. * This class represents a generic TicTacToe game board.
  8. */
  9. public class Board {
  10. private int n;
  11. //TODO
  12. //
  13.  
  14. Position[][] positions;
  15. int [][] tokens;
  16. int free_fields;
  17. /**
  18. * Creates Board object, am game board of size n * n with 1<=n<=10.
  19. */
  20. public Board(int n)
  21. {
  22. // TODO
  23. if (n < 1 || n > 10) throw new InputMismatchException();
  24.  
  25. this.n = n;
  26. this.positions = new Position[n][n];
  27. this.tokens = new int[n][n];
  28. this.free_fields = n*n;
  29.  
  30.  
  31. for (int i = 0; i < n; i++){
  32. for (int j = 0; j < n; j++){
  33. this.positions[i][j] = new Position(i, j);
  34. this.tokens[i][j] = 0;
  35. }
  36. }
  37.  
  38. }
  39.  
  40. /**
  41. * @return length/width of the Board object
  42. */
  43. public int getN() { return n; }
  44.  
  45. /**
  46. * @return number of currently free fields
  47. */
  48.  
  49. public int nFreeFields() {
  50. // TODO
  51. return this.free_fields;
  52.  
  53. }
  54.  
  55. /**
  56. * @return token at position pos
  57. */
  58. public void is_valid_position(Position pos){
  59.  
  60. if(pos == null)
  61. throw new InputMismatchException();
  62. // pos existiert?
  63.  
  64.  
  65. int x = pos.x;
  66. int y = pos.y;
  67. if (x < 0 || x > n || y < 0 || y > n)
  68. throw new InputMismatchException(); //valid pos?
  69. }
  70.  
  71. public int getField(Position pos) throws InputMismatchException
  72. {
  73. // TODO
  74. is_valid_position(pos); //meine funktion, ueberprueft ob die position existiert und valid ist
  75.  
  76. int token = this.tokens[pos.x][pos.y];
  77. if (token != 0 && token != -1 && token != 1) //wenn ungueltiger value
  78. throw new InputMismatchException();
  79.  
  80. return token;
  81. }
  82.  
  83. /**
  84. * Sets the specified token at Position pos.
  85. */
  86. public void setField(Position pos, int token) throws InputMismatchException
  87. {
  88. // TODO
  89. if (token != 0 && token != -1 && token != 1) //wenn ungueltiger value
  90. throw new InputMismatchException();
  91.  
  92. is_valid_position(pos);
  93.  
  94. this.free_fields -= abs(token) - abs(this.getField(pos));
  95. this.tokens[pos.x][pos.y] = token;
  96.  
  97.  
  98. }
  99.  
  100. /**
  101. * Places the token of a player at Position pos.
  102. */
  103.  
  104. public void doMove(Position pos, int player)
  105. {
  106. // TODO
  107. // wenn es keinen moeglichen zug gibt assert(this.free_fields > 0); aber man kann ja tauschen
  108.  
  109. assert(player == 1 || player == -1); //spieler*in ist alzeptabel
  110.  
  111. setField(pos, player); // x (1) oder o (1) gestellt
  112. }
  113.  
  114. /**
  115. * Clears board at Position pos.
  116. */
  117. public void undoMove(Position pos)
  118. {
  119. // TODO
  120. is_valid_position(pos);
  121. setField(pos, 0); //nun frei
  122. //nun ein freies feld erzeugt
  123.  
  124. }
  125.  
  126. /**
  127. * @return true if game is won, false if not
  128. */
  129. public boolean isGameWon() {
  130. // TODO
  131.  
  132. for (int i = 0; i < n; i++){
  133. // row
  134. if (same_stones(this.positions[0][i], 1, 0)) return true;
  135. //column
  136. if (same_stones(this.positions[i][0], 0, 1)) return true;
  137. }
  138.  
  139. //diagonalen,return false if nichts gefunden und true wenn zumindest auf diagonalen was gefunden
  140. return same_stones(this.positions[0][0], 1, 1)
  141. || same_stones(this.positions[0][n - 1], -1, -1);
  142. }
  143.  
  144. public boolean same_stones(Position start_pos, int next_row, int next_column){
  145. //tocken an anfangsposition
  146. int token = this.getField(start_pos);
  147.  
  148. if (token == 0) return false; //ubesetzt
  149.  
  150. for (int step = 1; step < n; step++){
  151. int pos_x = start_pos.x + step * next_row;
  152. int pos_y = start_pos.y + step * next_column;
  153.  
  154. Position new_pos = new Position(pos_x, pos_y);
  155. if (this.getField(new_pos) != token) return false; //anderer wert als erwartet
  156. }
  157. return true;
  158.  
  159. }
  160.  
  161. /**
  162. * @return set of all free fields as some Iterable object
  163. */
  164. public Iterable<Position> validMoves() {
  165. // TODO
  166. Stack<Position> validMoves = new Stack<>();
  167. for (int i = 0; i < n; i++) {
  168. for (int j = 0; j < n; j++) {
  169. if (tokens[i][j] == 0) { // Unbesetzt
  170. validMoves.push(positions[i][j]);
  171. }
  172. }
  173. }
  174. return validMoves;
  175. }
  176.  
  177. /**
  178. * Outputs current state representation of the Board object.
  179. * Practical for debugging.
  180. */
  181. public void print()
  182. {
  183. // TODO
  184. for (int i = 0; i < n; i++) {
  185. for (int j = 0; j < n; j++) {
  186.  
  187. //fuer (x,y,value)
  188. //System.out.print("(" + positions[i][j].x +
  189. //"," + positions[i][j].y + "," + tokens[i][j] + ") ");
  190.  
  191. if(tokens[i][j] == -1) System.out.print("(o)");
  192. if(tokens[i][j] == 0) System.out.print("( )");
  193. if(tokens[i][j] == 1) System.out.print("(x)");
  194. }
  195. System.out.println();
  196. }
  197. }
  198.  
  199. public static void main(String[] args)
  200. {
  201. /*Board myBoard = new Board(3);
  202. //myBoard.print();
  203. System.out.println("n = " + myBoard.getN() + ", free fields = " + myBoard.free_fields);
  204.  
  205. Position pos1 = new Position(0,0);
  206.  
  207. myBoard.setField(pos1, 1);
  208. myBoard.print();
  209.  
  210. System.out.println("(0,0) has the value = " + myBoard.getField((pos1)));
  211. System.out.println("free fields = " + myBoard.free_fields);
  212.  
  213. myBoard.undoMove(pos1);
  214. myBoard.print();
  215. System.out.println("free fields = " + myBoard.free_fields);
  216.  
  217. Position pos2 = new Position(0,1);
  218. Position pos3 = new Position(0,2);
  219. myBoard.doMove(pos1, -1);
  220. myBoard.doMove(pos2, -1);
  221. myBoard.doMove(pos3, -1);
  222. myBoard.print();
  223. System.out.println("won?" + myBoard.isGameWon());
  224.  
  225. */
  226.  
  227. Board myBoard2 = new Board(3);
  228. Position pos1 = new Position(0,0);
  229.  
  230. System.out.println("testen free fields Methode: Board leer");
  231. myBoard2.print();
  232. System.out.println("jetzt anzahl free fields = " + myBoard2.free_fields);
  233.  
  234. System.out.println("Setzen -1:");
  235.  
  236. myBoard2.setField(pos1, -1);
  237. myBoard2.print();
  238. System.out.println("anzahl free fields = " + myBoard2.free_fields);
  239.  
  240. System.out.println("jetzt undo Move");
  241. myBoard2.setField(pos1, 1);
  242. myBoard2.print();
  243. System.out.println("anzahl free fields = " + myBoard2.free_fields);
  244.  
  245. System.out.println("isGameWon() testen: setzen eine Win Position");
  246. Position pos2 = new Position(1, 1);
  247. myBoard2.doMove(pos2, 1);
  248. Position pos3 = new Position(2, 2);
  249. myBoard2.doMove(pos3, -1);
  250. myBoard2.print();
  251. System.out.println("Did Player 1 win the game?" + myBoard2.isGameWon() );
  252.  
  253.  
  254.  
  255. }
  256.  
  257. }
  258.  
  259.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement