Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- /**
- * BoardInterface represents the board state in the game of tabula (not including dice and players).
- *
- * Requires a constructor with no parameters which creates and initialises all of the locations for the startPosition of the game.
- *
- **/
- public class Board implements BoardInterface {
- private String name;
- private ArrayList<LocationInterface> board;
- private LocationInterface knockArr;
- private LocationInterface startArr;
- private LocationInterface endArr;
- private static final int PIECES_PER_PLAYER = 15;
- private static final int NUMBER_OF_LOCATIONS = 24;
- // Initialises board array
- public Board() {
- ArrayList<LocationInterface> board = new ArrayList<LocationInterface>();
- this.board=board;
- //Initialises Knocked Array
- //Add knocked as the first position in the board
- // Initilises startPosition array
- LocationInterface startPosition = new Location("startPosition");
- startPosition.setMixed(true);
- // Add startPosition to the board as the second position on the board
- board.add(startPosition);
- // Adds the rest of the positins on the board
- for (int i = 1; i < NUMBER_OF_LOCATIONS+1 ; i++) {
- LocationInterface InitLocation = new Location(Integer.toString(i));
- board.add(InitLocation);
- }
- //Adds a final position on the board
- LocationInterface end = new Location("Ended");
- end.setMixed(true);
- board.add(end);
- LocationInterface knocked = new Location("Knocked");
- knocked.setMixed(true);
- board.add(knocked);
- //Creates variables that point to key arrays for easy access
- knockArr=board.get(NUMBER_OF_LOCATIONS+2); //0
- startArr=board.get(0); //1
- endArr=board.get(NUMBER_OF_LOCATIONS+1); //25
- //Adds the starting amount of pieces to the starting position
- for (int i = 1; i <= PIECES_PER_PLAYER; i++) {
- try{
- startArr.addPieceGetKnocked(Colour.GREEN);
- }
- catch (Exception ex){
- }
- try{
- startArr.addPieceGetKnocked(Colour.BLUE);
- }
- catch (Exception ex){
- }
- }
- }
- public void setName(String name) {
- this.name = name;
- }
- /**
- * @return the Location off the board where all pieces startPosition the game. This will be a mixed location.
- **/
- public LocationInterface getStartLocation() {
- return startArr;
- }
- /**
- * @return the Location off the board where pieces get to when they have gone all the way round the board. This will be a mixed location.
- **/
- public LocationInterface getEndLocation() {
- return endArr;
- }
- /**
- * @return the Location where pieces go to when they are knocked off the board by an opposing piece. This will be a mixed location.
- **/
- public LocationInterface getKnockedLocation() {
- return knockArr;
- }
- /**
- * @return the Location corresponding to a numbered position on the board. This will not be a mixed location.
- *
- * @param locationNumber the number of the location going from 1-24
- *
- * @throws NoSuchLocationException when position is not in the range 1-24
- **/
- public LocationInterface getBoardLocation(int locationNumber) throws NoSuchLocationException
- { //Returns the position on the board associated with the location Number
- if ( !((0 <= locationNumber) && (locationNumber <= NUMBER_OF_LOCATIONS)) ) {
- throw new NoSuchLocationException("Invalid Location");
- }
- return board.get(locationNumber +1);
- }
- /**
- * @param colour the colour to move
- *
- * @param move the move to make
- *
- * @return true if and only if, from the current board state it would be legal for the given colour to make the given move.
- **/
- //Returns whether a move is allowed
- public boolean canMakeMove(Colour colour, MoveInterface move) {
- //What's the square the piece is trying to move to?
- int destinationLocation = move.getDiceValue() + move.getSourceLocation();
- //If this is greater than the 24 squares on the board, sanitise the input so stops the on the end array
- if(destinationLocation>NUMBER_OF_LOCATIONS+1)
- {
- destinationLocation=NUMBER_OF_LOCATIONS+1;
- }
- //flag for a valid move, starts off as true and is tested against conditions that will make it false
- boolean canMove=true;
- // Does the knocked list have pieces in it? If so, is the move from the knocked list?
- if ( knockArr.numberOfPieces(colour)>0) { // knocked off section has piecces in it
- if ( move.getSourceLocation() !=0 ) {
- return canMove=false;
- }
- }
- else {
- //Does the source location have pieces in it of that colour? If not, the move cannot be made
- try {
- if (board.get(move.getSourceLocation()).numberOfPieces(colour)==0) {
- return canMove=false;
- }
- }
- catch (Exception e) {}
- }
- // Can you add that piece to the destination location?
- if (!board.get(destinationLocation).canAddPiece(colour))
- {
- return canMove=false;
- }
- return canMove;
- }
- /**
- * Update the Board state by making the given move for the given colour, including any knocking off.
- *
- * @param colour the colour to move
- *
- * @param move the move to make
- *
- * @throws IllegalMoveException if and only if the move is not legal.
- **/
- public void makeMove(Colour colour, MoveInterface move) throws IllegalMoveException {
- // Calculates destination
- int destination = move.getDiceValue() + move.getSourceLocation(); // destination is the position in the array list
- //Check if you can make a move
- if (!canMakeMove(colour, move) ) {
- throw new IllegalMoveException("This move is illegal.");
- }
- //Sanitises the destination input, so if you complete the board, it stays in the end array
- if(destination>NUMBER_OF_LOCATIONS)
- {
- destination=NUMBER_OF_LOCATIONS;
- }
- //Sets the start location
- LocationInterface source=board.get(move.getSourceLocation());
- //Sets the destination location
- LocationInterface dest=board.get(destination);
- //Adds a piece to the destination. If a piece is knocked out, it feeds it into the knockArr input
- knockArr.addPieceGetKnocked(dest.addPieceGetKnocked(colour));
- //Takes a piece from the source
- source.removePiece(colour);
- }
- /**
- * Update the Board state by making the all of the moves in the given turn in order, including any knocking off, based on the given diceValues.
- *
- * @param colour the colour to move
- *
- * @param turn the turn to take
- *
- * @param diceValues the values of the dice available in no particular order. There will be repeated values in the list if a double is thrown
- *
- * @throws IllegalTurnException if and only if the turns in the move are not legal for the diceValues give. Each of the moves has to be legal, and the diceValues in the moves of the turn must match the diceValues parameter. The number of moves in the turn must be no less than the maximum possible number of legal moves: all available dice must be used. If IllegalTurnException is thrown then the board state remains unchanged.
- **/
- public void takeTurn(Colour colour, TurnInterface turn, List<Integer> diceValues) throws IllegalTurnException {
- List<MoveInterface> listOfMoves = new ArrayList<MoveInterface>();
- listOfMoves = turn.getMoves();
- for (MoveInterface currentMove : listOfMoves) {
- if ( !diceValues.contains(currentMove.getDiceValue()) || listOfMoves.size() > 4 ) {
- throw new IllegalTurnException("This move cannot be made");
- }
- canMakeMove(colour, currentMove); // to check
- }
- //implement the moves
- for (MoveInterface currentMove : listOfMoves) {
- try {
- makeMove(colour, currentMove);
- }
- catch (IllegalMoveException e) {}
- }
- }
- /**
- * @param colour the colour to check
- *
- * @return true if and only if the given colour has won
- **/
- public boolean isWinner(Colour colour) {
- if ( endArr.numberOfPieces(colour) >= PIECES_PER_PLAYER ) {
- return true;
- }
- return false;
- }
- /**
- * @return the colour of the winner if there is one, otherwise null
- **/
- public Colour winner() {
- if ( isWinner(Colour.GREEN) ) {
- return Colour.GREEN;
- }
- else if ( isWinner(Colour.BLUE) ) {
- return Colour.BLUE;
- }
- return null;
- }
- /**
- * @return true if and only if the Board is in a valid state (do not need to check whether or not it could be reached by a valid sequence of moves)
- **/
- public boolean isValid() {
- //Set flag for validity
- boolean valid=true;
- //Checks each position on the board
- for(LocationInterface Loc : board)
- {
- valid=Loc.isValid();
- //If it finds a single invalid position, it returns that the board is in an invalid state
- if(valid==false)
- {
- return false;
- }
- }
- return valid;
- }
- /**
- * @param colour the colour to move next
- *
- * @param diceValues the dice values available to use
- *
- * @return a list of moves that the given colour can make from the current board state with (any one of) the given diceValues
- **/
- public Set<MoveInterface> possibleMoves(Colour colour, List<Integer> diceValues) {
- Set<MoveInterface> possibleMoves = new HashSet<MoveInterface>();
- List<Integer> diceAvalaible = diceValues;
- //startPosition
- if ( startArr.numberOfPieces(colour) != 0 ) {
- for (Integer currentDice : diceAvalaible) {
- MoveInterface move = new Move();
- try {
- move.setSourceLocation(0);
- move.setDiceValue(currentDice);
- }
- catch ( NoSuchLocationException e1 ) {}
- catch ( IllegalMoveException e2 ) {
- //System.out.println("The dice value is invalid " + currentDice);
- }
- if ( canMakeMove(colour, move) && !moveAlreadyPresent(possibleMoves, move) ) {
- possibleMoves.add(move);
- }
- }
- }
- if ( isknockedEmpty(colour) ) {
- //board loop
- for (LocationInterface currentSpace : board) {
- if ( currentSpace.numberOfPieces(colour) != 0 ) {
- for (Integer currentDice : diceAvalaible) {
- MoveInterface move = new Move();
- try {
- move.setSourceLocation(Integer.parseInt(currentSpace.getName()));
- move.setDiceValue(currentDice);
- if ( canMakeMove(colour, move) && !possibleMoves.contains(move) ) {
- possibleMoves.add(move);
- }
- }
- catch ( NoSuchLocationException e1 ) {}
- catch( IllegalMoveException e2 ) {}
- }
- }
- }
- }
- return possibleMoves;
- }
- /**
- * @return a copy of the board that can be passed to players to work with
- */
- public BoardInterface clone() {
- BoardInterface boardClone = new Board();
- //startPosition
- while (boardClone.getStartLocation().numberOfPieces(Colour.values()[0]) > getStartLocation().numberOfPieces(Colour.values()[0])) {
- try {
- boardClone.getStartLocation().removePiece(Colour.values()[0]);
- }
- catch ( Exception e) {}
- }
- while (boardClone.getStartLocation().numberOfPieces(Colour.values()[1]) > getStartLocation().numberOfPieces(Colour.values()[1])) {
- try {
- boardClone.getStartLocation().removePiece(Colour.values()[1]);
- }
- catch ( Exception e) {}
- }
- //knocked
- while (boardClone.getKnockedLocation().numberOfPieces(Colour.values()[0]) < getKnockedLocation().numberOfPieces(Colour.values()[0])) {
- try {
- boardClone.getKnockedLocation().addPieceGetKnocked(Colour.values()[0]);
- }
- catch ( Exception e) {}
- }
- while (boardClone.getKnockedLocation().numberOfPieces(Colour.values()[1]) < getKnockedLocation().numberOfPieces(Colour.values()[1])) {
- try {
- boardClone.getKnockedLocation().addPieceGetKnocked(Colour.values()[1]);
- }
- catch ( Exception e) {}
- }
- //end
- while (boardClone.getEndLocation().numberOfPieces(Colour.values()[0]) < getEndLocation().numberOfPieces(Colour.values()[0])) {
- try {
- boardClone.getEndLocation().addPieceGetKnocked(Colour.values()[0]);
- }
- catch ( Exception e) {}
- }
- while (boardClone.getEndLocation().numberOfPieces(Colour.values()[1]) < getEndLocation().numberOfPieces(Colour.values()[1])) {
- try {
- boardClone.getEndLocation().addPieceGetKnocked(Colour.values()[1]);
- }
- catch ( Exception e) {}
- }
- //board
- try {
- int i = 1;
- for (LocationInterface currentSpace : board) {
- while (boardClone.getBoardLocation(i).numberOfPieces(Colour.values()[0]) < getBoardLocation(i).numberOfPieces(Colour.values()[0])) {
- try {
- boardClone.getBoardLocation(i).addPieceGetKnocked(Colour.values()[0]);
- }
- catch ( Exception e) {}
- }
- while (boardClone.getBoardLocation(i).numberOfPieces(Colour.values()[1]) < getBoardLocation(i).numberOfPieces(Colour.values()[1])) {
- try {
- boardClone.getBoardLocation(i).addPieceGetKnocked(Colour.values()[1]);
- }
- catch ( Exception e) {}
- }
- i++;
- }
- }
- catch (Exception e) {}
- return boardClone;
- }
- /**
- * Overrides toString() from Object with a suitable String representation of the board state for displaying via the console to a human
- **/
- public String toString() {
- String boardCondition = "";
- int dimensions = 20;
- //startPosition
- boardCondition += "+---------------+--------------------+--------------------+" + '\n';
- boardCondition += "| Location |" + makeCertainLength(Colour.values()[0].toString(), dimensions) + "|" + makeCertainLength(Colour.values()[1].toString(), dimensions) + "|" + '\n';
- boardCondition += "+---------------+--------------------+--------------------+" + '\n';
- boardCondition += "| startPosition |" + makeCertainLength( String.valueOf(startArr.numberOfPieces(Colour.values()[0])), dimensions) + "|" + makeCertainLength( String.valueOf(startArr.numberOfPieces(Colour.values()[1])), dimensions) + "|" + '\n';
- boardCondition += "+---------------+--------------------+--------------------+" + '\n';
- boardCondition += "| Knocked Off |" + makeCertainLength( String.valueOf(knockArr.numberOfPieces(Colour.values()[0])) , dimensions) + "|" + makeCertainLength( String.valueOf(knockArr.numberOfPieces(Colour.values()[1])), dimensions) + "|" + '\n';
- boardCondition += "+---------------+--------------------+--------------------+" + '\n';
- for (int i = 1; i <= NUMBER_OF_LOCATIONS ; i++) {
- LocationInterface currentLocation = null;
- try {
- currentLocation = getBoardLocation(i); // to avoid having to call a location of 0
- }
- catch (Exception e) {}
- boardCondition += "|" + makeCertainLength( String.valueOf(i) , 15) + "|" + makeCertainLength( String.valueOf(currentLocation.numberOfPieces(Colour.values()[0])), dimensions) + "|" + makeCertainLength(String.valueOf(currentLocation.numberOfPieces(Colour.values()[1])), dimensions) + "|" + '\n';
- boardCondition += "+---------------+--------------------+--------------------+" + '\n';
- }
- boardCondition += "| ended |" + makeCertainLength(String.valueOf(endArr.numberOfPieces(Colour.values()[0])), dimensions) + "|" + makeCertainLength(String.valueOf(endArr.numberOfPieces(Colour.values()[1])), dimensions) + "|" + '\n';
- boardCondition += "+---------------+--------------------+--------------------+" + '\n';
- return boardCondition;
- }
- // extra methods
- public boolean pieceHasended(int destination) {
- if ( destination > NUMBER_OF_LOCATIONS) {
- return true;
- }
- else {
- return false;
- }
- }
- /**
- * checks whethert the knocked section contains any pieces of the given colour
- */
- public boolean isknockedEmpty(Colour colour) {
- if (knockArr.numberOfPieces(colour) == 0) {
- return true;
- }
- else {
- return false;
- }
- }
- /**
- * colour is the colour that is being moved
- */
- public void sendRemovedToknocked(Colour colour, int destination) {
- if ( destination <= NUMBER_OF_LOCATIONS ) {
- int numberToMove = 0;
- try {
- numberToMove = getBoardLocation(destination).numberOfPieces(colour);
- }
- catch (Exception e) {}
- for (int i = 1; i <= numberToMove ; i++) {
- try {
- knockArr.addPieceGetKnocked(colour);
- }
- catch (Exception e) {}
- }
- }
- }
- public String makeCertainLength(String text, int length) {
- String textAltered = "";
- int boardOnEachSide = (length - text.length())/2;
- for (int i = 0; i < boardOnEachSide ; i++) {
- textAltered += " ";
- }
- textAltered += text;
- for (int i = 0; i < boardOnEachSide ; i++) {
- textAltered += " ";
- }
- if ( textAltered.length() != length ) {
- textAltered += " ";
- }
- return textAltered;
- }
- public boolean moveAlreadyPresent(Set<MoveInterface> possibleMoves, MoveInterface move) {
- int diceValue = move.getDiceValue();
- int sourceLocation = move.getSourceLocation();
- for (MoveInterface currentMove : possibleMoves ) {
- if ( currentMove.getDiceValue() == diceValue && currentMove.getSourceLocation() == sourceLocation ) {
- return true;
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement