Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.InputMismatchException;
- //import java.util.List;
- import java.util.Stack;
- import static java.lang.Math.abs;
- /**
- * This class represents a generic TicTacToe game board.
- */
- public class Board {
- private int n;
- //TODO
- //
- Position[][] positions;
- int [][] tokens;
- int free_fields;
- /**
- * Creates Board object, am game board of size n * n with 1<=n<=10.
- */
- public Board(int n)
- {
- // TODO
- if (n < 1 || n > 10) throw new InputMismatchException();
- this.n = n;
- this.positions = new Position[n][n];
- this.tokens = new int[n][n];
- this.free_fields = n*n;
- for (int i = 0; i < n; i++){
- for (int j = 0; j < n; j++){
- this.positions[i][j] = new Position(i, j);
- this.tokens[i][j] = 0;
- }
- }
- }
- /**
- * @return length/width of the Board object
- */
- public int getN() { return n; }
- /**
- * @return number of currently free fields
- */
- public int nFreeFields() {
- // TODO
- return this.free_fields;
- }
- /**
- * @return token at position pos
- */
- public void is_valid_position(Position pos){
- if(pos == null)
- throw new InputMismatchException();
- // pos existiert?
- int x = pos.x;
- int y = pos.y;
- if (x < 0 || x > n || y < 0 || y > n)
- throw new InputMismatchException(); //valid pos?
- }
- public int getField(Position pos) throws InputMismatchException
- {
- // TODO
- is_valid_position(pos); //meine funktion, ueberprueft ob die position existiert und valid ist
- int token = this.tokens[pos.x][pos.y];
- if (token != 0 && token != -1 && token != 1) //wenn ungueltiger value
- throw new InputMismatchException();
- return token;
- }
- /**
- * Sets the specified token at Position pos.
- */
- public void setField(Position pos, int token) throws InputMismatchException
- {
- // TODO
- if (token != 0 && token != -1 && token != 1) //wenn ungueltiger value
- throw new InputMismatchException();
- is_valid_position(pos);
- this.free_fields -= abs(token) - abs(this.getField(pos));
- this.tokens[pos.x][pos.y] = token;
- }
- /**
- * Places the token of a player at Position pos.
- */
- public void doMove(Position pos, int player)
- {
- // TODO
- // wenn es keinen moeglichen zug gibt assert(this.free_fields > 0); aber man kann ja tauschen
- assert(player == 1 || player == -1); //spieler*in ist alzeptabel
- setField(pos, player); // x (1) oder o (1) gestellt
- }
- /**
- * Clears board at Position pos.
- */
- public void undoMove(Position pos)
- {
- // TODO
- is_valid_position(pos);
- setField(pos, 0); //nun frei
- //nun ein freies feld erzeugt
- }
- /**
- * @return true if game is won, false if not
- */
- public boolean isGameWon() {
- // TODO
- for (int i = 0; i < n; i++){
- // row
- if (same_stones(this.positions[0][i], 1, 0)) return true;
- //column
- if (same_stones(this.positions[i][0], 0, 1)) return true;
- }
- //diagonalen,return false if nichts gefunden und true wenn zumindest auf diagonalen was gefunden
- return same_stones(this.positions[0][0], 1, 1)
- || same_stones(this.positions[0][n - 1], -1, -1);
- }
- public boolean same_stones(Position start_pos, int next_row, int next_column){
- //tocken an anfangsposition
- int token = this.getField(start_pos);
- if (token == 0) return false; //ubesetzt
- for (int step = 1; step < n; step++){
- int pos_x = start_pos.x + step * next_row;
- int pos_y = start_pos.y + step * next_column;
- Position new_pos = new Position(pos_x, pos_y);
- if (this.getField(new_pos) != token) return false; //anderer wert als erwartet
- }
- return true;
- }
- /**
- * @return set of all free fields as some Iterable object
- */
- public Iterable<Position> validMoves() {
- // TODO
- Stack<Position> validMoves = new Stack<>();
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- if (tokens[i][j] == 0) { // Unbesetzt
- validMoves.push(positions[i][j]);
- }
- }
- }
- return validMoves;
- }
- /**
- * Outputs current state representation of the Board object.
- * Practical for debugging.
- */
- public void print()
- {
- // TODO
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- //fuer (x,y,value)
- //System.out.print("(" + positions[i][j].x +
- //"," + positions[i][j].y + "," + tokens[i][j] + ") ");
- if(tokens[i][j] == -1) System.out.print("(o)");
- if(tokens[i][j] == 0) System.out.print("( )");
- if(tokens[i][j] == 1) System.out.print("(x)");
- }
- System.out.println();
- }
- }
- public static void main(String[] args)
- {
- /*Board myBoard = new Board(3);
- //myBoard.print();
- System.out.println("n = " + myBoard.getN() + ", free fields = " + myBoard.free_fields);
- Position pos1 = new Position(0,0);
- myBoard.setField(pos1, 1);
- myBoard.print();
- System.out.println("(0,0) has the value = " + myBoard.getField((pos1)));
- System.out.println("free fields = " + myBoard.free_fields);
- myBoard.undoMove(pos1);
- myBoard.print();
- System.out.println("free fields = " + myBoard.free_fields);
- Position pos2 = new Position(0,1);
- Position pos3 = new Position(0,2);
- myBoard.doMove(pos1, -1);
- myBoard.doMove(pos2, -1);
- myBoard.doMove(pos3, -1);
- myBoard.print();
- System.out.println("won?" + myBoard.isGameWon());
- */
- Board myBoard2 = new Board(3);
- Position pos1 = new Position(0,0);
- System.out.println("testen free fields Methode: Board leer");
- myBoard2.print();
- System.out.println("jetzt anzahl free fields = " + myBoard2.free_fields);
- System.out.println("Setzen -1:");
- myBoard2.setField(pos1, -1);
- myBoard2.print();
- System.out.println("anzahl free fields = " + myBoard2.free_fields);
- System.out.println("jetzt undo Move");
- myBoard2.setField(pos1, 1);
- myBoard2.print();
- System.out.println("anzahl free fields = " + myBoard2.free_fields);
- System.out.println("isGameWon() testen: setzen eine Win Position");
- Position pos2 = new Position(1, 1);
- myBoard2.doMove(pos2, 1);
- Position pos3 = new Position(2, 2);
- myBoard2.doMove(pos3, -1);
- myBoard2.print();
- System.out.println("Did Player 1 win the game?" + myBoard2.isGameWon() );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement