From d0972c57cc23d7a6c913594e8166770fc1b28ff6 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Wed, 11 Mar 2015 10:50:52 +0100 Subject: Added framework --- Week6/src/SlidingGame.java | 94 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 Week6/src/SlidingGame.java (limited to 'Week6/src/SlidingGame.java') diff --git a/Week6/src/SlidingGame.java b/Week6/src/SlidingGame.java new file mode 100644 index 0000000..29e76fc --- /dev/null +++ b/Week6/src/SlidingGame.java @@ -0,0 +1,94 @@ +import java.util.Collection; + +/** + * @author Pieter Koopman, Sjaak Smetsers + * @version 1.2 + * @date 28-02-2015 + * A template implementation of a sliding game also + * implementing the Graph interface + */ +public class SlidingGame implements Configuration +{ + public static final int N = 3, SIZE = N * N, HOLE = SIZE; + /* + * The board is represented by a 2-dimensional array; + * the position of the hole is kept in 2 variables holeX and holeY + */ + private int [][] board; + private int holeX, holeY; + + /* + * A constructor that initializes the board with the specified array + * @param start: a one dimensional array containing the initial board. + * The elements of start are stored row-wise. + */ + public SlidingGame (int [] start) { + board = new int[N][N]; + + assert start.length == N*N: "Length of specified board incorrect"; + + for( int p = 0; p < start.length; p++) { + board[p % N][p / N] = start[p]; + if ( start[p] == HOLE ) { + holeX = p % N; + holeY = p / N; + } + } + } + + /* + * Converts a board into a printable representation. + * The hole is displayed as a space + * @return the string representation + */ + @Override + public String toString() { + StringBuilder buf = new StringBuilder(); + for (int row = 0; row < N; row++) { + for (int col = 0; col < N; col++) { + int puzzel = board[col][row]; + buf.append(puzzel == HOLE ? " " : puzzel + " "); + } + buf.append("\n"); + } + return buf.toString(); + } + + /* + * a standard implementation of equals checking + * whether 2 boards are filled in exactly the same way + * @return a boolean indicating equality + */ + @Override + public boolean equals(Object o) { + if (o == null || o.getClass() != getClass()) { + return false; + } else { + SlidingGame other_puzzle = (SlidingGame) o; + for (int row = 0; row < N; row++) { + for (int col = 0; col < N; col++) { + if (board[col][row] != other_puzzle.board[col][row]) { + return false; + } + } + } + return true; + } + } + + @Override + public boolean isSolution () { + throw new UnsupportedOperationException("isGoal : not supported yet."); + } + + @Override + public Collection successors () { + throw new UnsupportedOperationException("successors : not supported yet."); + } + + @Override + public int compareTo(Configuration g) { + throw new UnsupportedOperationException("compareTo : not supported yet."); + } + +} -- cgit v1.2.3