Advertisement
madegoff

ClassBoard

May 25th, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. import java.util.InputMismatchException;
  2. import java.util.Iterator;
  3. //import java.util.List;
  4. import java.util.Stack;
  5.  
  6. import static java.lang.Math.abs;
  7. /**
  8. * This class represents a generic TicTacToe game board.
  9. */
  10. public class Board {
  11. private int n;
  12. //TODO
  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 pos){
  59.  
  60. assert(pos != null); // pos existiert?
  61.  
  62. int x = pos.x;
  63. int y = pos.y;
  64. assert (x >= 0 && x < n && y >= 0 && y < n); //valid pos?
  65. }
  66.  
  67. public int getField(Position pos) throws InputMismatchException
  68. {
  69. // TODO
  70. is_valid(pos); //meine funktion, ueberprueft ob die position existiert und valid ist
  71.  
  72. int token = this.tokens[pos.x][pos.y];
  73. if (token != 0 && token != -1 && token != 1) //wenn ungueltiger value
  74. throw new InputMismatchException();
  75.  
  76. return token;
  77. }
  78.  
  79. /**
  80. * Sets the specified token at Position pos.
  81. */
  82. public void setField(Position pos, int token) throws InputMismatchException
  83. {
  84. // TODO
  85. if (token != 0 && token != -1 && token != 1) //wenn ungueltiger value
  86. throw new InputMismatchException();
  87.  
  88. is_valid(pos);
  89.  
  90. if (this.tokens[pos.x][pos.y] == 0) //wenn kaestchen unbesetzt
  91. this.free_fields--; // dann neuer zug gemacht
  92. this.tokens[pos.x][pos.y] = token; // ob da schon was stand oder nicht ist egal, koennen tauschen
  93. }
  94.  
  95. /**
  96. * Places the token of a player at Position pos.
  97. */
  98.  
  99. public void doMove(Position pos, int player)
  100. {
  101. // TODO
  102. // wenn es keinen moeglichen zug gibt assert(this.free_fields > 0); aber man kann ja tauschen
  103.  
  104. assert(player == 1 || player == -1); //spieler*in ist alzeptabel
  105.  
  106. setField(pos, player); // x (1) oder o (1) gestellt
  107. }
  108.  
  109. /**
  110. * Clears board at Position pos.
  111. */
  112. public void undoMove(Position pos)
  113. {
  114. // TODO
  115. is_valid(pos);
  116. setField(pos, 0); //nun frei
  117. this.free_fields++; //nun ein freies feld erzeugt
  118. }
  119.  
  120. /**
  121. * @return true if game is won, false if not
  122. */
  123. public boolean isGameWon() {
  124. // TODO
  125. //row 1
  126. //for (Position pos ???)
  127.  
  128. return false;
  129. }
  130.  
  131. /**
  132. * @return set of all free fields as some Iterable object
  133. */
  134. public Iterable<Position> validMoves() {
  135. // TODO
  136. Stack<Position> validMoves = new Stack<>();
  137. for (int i = 0; i < n; i++) {
  138. for (int j = 0; j < n; j++) {
  139. if (tokens[i][j] == 0) { // Unbesetzt
  140. validMoves.push(positions[i][j]);
  141. }
  142. }
  143. }
  144. return validMoves;
  145. }
  146.  
  147. /**
  148. * Outputs current state representation of the Board object.
  149. * Practical for debugging.
  150. */
  151. public void print()
  152. {
  153. // TODO
  154. for (int i = 0; i < n; i++) {
  155. for (int j = 0; j < n; j++) {
  156.  
  157. //fuer (x,y,value)
  158. //System.out.print("(" + positions[i][j].x +
  159. //"," + positions[i][j].y + "," + tokens[i][j] + ") ");
  160.  
  161. if(tokens[i][j] == -1) System.out.print("(o)");
  162. if(tokens[i][j] == 0) System.out.print("( )");
  163. if(tokens[i][j] == 1) System.out.print("(x)");
  164. }
  165. System.out.println();
  166. }
  167. }
  168.  
  169. public static void main(String[] args)
  170. {
  171. Board myBoard = new Board(3);
  172. //myBoard.print();
  173. System.out.println("free fields = " + myBoard.free_fields);
  174.  
  175. Position pos1 = new Position(0,0);
  176.  
  177. myBoard.setField(pos1, 1);
  178. myBoard.print();
  179.  
  180. System.out.println("(0,0) has the value = " + myBoard.getField((pos1)));
  181. System.out.println("free fields = " + myBoard.free_fields);
  182.  
  183. myBoard.undoMove(pos1);
  184. myBoard.print();
  185. System.out.println("free fields = " + myBoard.free_fields);
  186.  
  187. }
  188.  
  189. }
  190.  
  191.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement