Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.InputMismatchException;
- import java.util.Iterator;
- //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 pos){
- assert(pos != null); // pos existiert?
- int x = pos.x;
- int y = pos.y;
- assert (x >= 0 && x < n && y >= 0 && y < n); //valid pos?
- }
- public int getField(Position pos) throws InputMismatchException
- {
- // TODO
- is_valid(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(pos);
- if (this.tokens[pos.x][pos.y] == 0) //wenn kaestchen unbesetzt
- this.free_fields--; // dann neuer zug gemacht
- this.tokens[pos.x][pos.y] = token; // ob da schon was stand oder nicht ist egal, koennen tauschen
- }
- /**
- * 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(pos);
- setField(pos, 0); //nun frei
- this.free_fields++; //nun ein freies feld erzeugt
- }
- /**
- * @return true if game is won, false if not
- */
- public boolean isGameWon() {
- // TODO
- //row 1
- //for (Position pos ???)
- return false;
- }
- /**
- * @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("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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement