Class JesonMor

java.lang.Object
castle.comp3021.assignment.protocol.Game
castle.comp3021.assignment.JesonMor
All Implemented Interfaces:
java.lang.Cloneable

public class JesonMor
extends Game
This class extends Game, implementing the game logic of JesonMor game. Student needs to implement methods in this class to make the game work. Hint: make good use of methods predefined in Game to get various information to facilitate your work.

Several sample tests are provided to test your implementation of each method in the test directory. Please make make sure all tests pass before submitting the assignment.

  • Constructor Details

  • Method Details

    • start

      public Player start()
      Start the game Players will take turns according to the order in Configuration.getPlayers() to make a move until a player wins.

      In the implementation, student should implement the loop letting two players take turns to move pieces. The order of the players should be consistent to the order in Configuration.getPlayers(). Player.nextMove(Game, Move[]) should be used to retrieve the player's choice of his next move. After each move, Game.refreshOutput() should be called to refresh the gameboard printed in the console.

      When a winner appears, set the local variable winner so that this method can return the winner.

      Specified by:
      start in class Game
      Returns:
      the winner
    • getWinner

      public Player getWinner​(Player lastPlayer, Piece lastPiece, Move lastMove)
      Get the winner of the game. If there is no winner yet, return null; This method will be called every time after a player makes a move and after updateScore(Player, Piece, Move) is called, in order to check whether any Player wins. If this method returns a player (the winner), then the game will exit with the winner. If this method returns null, next player will be asked to make a move.
      Specified by:
      getWinner in class Game
      Parameters:
      lastPlayer - the last player who makes a move
      lastMove - the last move made by lastPlayer
      lastPiece - the last piece that is moved by the player
      Returns:
      the winner if it exists, otherwise return null
    • updateScore

      public void updateScore​(Player player, Piece piece, Move move)
      Update the score of a player according to the Piece and corresponding move made by him just now. This method will be called every time after a player makes a move, in order to update the corresponding score of this player.

      The score of a player is the cumulative score of each move he makes. The score of each move is calculated with the Manhattan distance between the source and destination Place.

      Student can use Player.getScore() to get the current score of a player before updating. Player.setScore(int) can be used to update the score of a player.

      Attention: Student should make NO assumption that the Move is valid.

      Specified by:
      updateScore in class Game
      Parameters:
      player - the player who just makes a move
      piece - the piece that is just moved
      move - the move that is just made
    • movePiece

      public void movePiece​(@NotNull @NotNull Move move)
      Make a move. This method performs moving a Piece from source to destination Place according Move object. Note that after the move, there will be no Piece in source Place.

      Positions of all Pieces on the gameboard are stored in Game.board field as a 2-dimension array of Piece objects. The x and y coordinate of a Place on the gameboard are used as index in Game.board. E.g. board[place.x()][place.y()]. If one Place does not have a piece on it, it will be null in board[place.x()][place.y()]. Student may modify elements in Game.board to implement moving a Piece. The Move object can be considered valid on present gameboard.

      Specified by:
      movePiece in class Game
      Parameters:
      move - the move to make
    • getAvailableMoves

      @NotNull public @NotNull Move[] getAvailableMoves​(Player player)
      Get all available moves of one player. This method is called when it is the Player's turn to make a move. It will iterate all Pieces belonging to the Player on board and obtain available moves of each of the Pieces through method Piece.getAvailableMoves(Game, Place) of each Piece.

      Attention: Student should make sure all Moves returned are valid.

      Specified by:
      getAvailableMoves in class Game
      Parameters:
      player - the player whose available moves to get
      Returns:
      an array of available moves