Class JesonMor
- All Implemented Interfaces:
java.lang.Cloneable
public class JesonMor extends Game
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.
-
Field Summary
Fields inherited from class castle.comp3021.assignment.protocol.Game
board, configuration, currentPlayer, numMoves
-
Constructor Summary
Constructors Constructor Description JesonMor(Configuration configuration)
-
Method Summary
Modifier and Type Method Description @NotNull Move[]
getAvailableMoves(Player player)
Get all available moves of one player.Player
getWinner(Player lastPlayer, Piece lastPiece, Move lastMove)
Get the winner of the game.void
movePiece(@NotNull Move move)
Make a move.Player
start()
Start the game Players will take turns according to the order inConfiguration.getPlayers()
to make a move until a player wins.void
updateScore(Player player, Piece piece, Move move)
Update the score of a player according to thePiece
and corresponding move made by him just now.Methods inherited from class castle.comp3021.assignment.protocol.Game
clone, getCentralPlace, getConfiguration, getCurrentPlayer, getNumMoves, getPiece, getPiece, getPlayers, refreshOutput
-
Constructor Details
-
Method Details
-
start
Start the game Players will take turns according to the order inConfiguration.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. -
getWinner
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 afterupdateScore(Player, Piece, Move)
is called, in order to check whether anyPlayer
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. -
updateScore
Update the score of a player according to thePiece
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 classGame
- Parameters:
player
- the player who just makes a movepiece
- the piece that is just movedmove
- the move that is just made
-
movePiece
Make a move. This method performs moving aPiece
from source to destinationPlace
accordingMove
object. Note that after the move, there will be noPiece
in sourcePlace
.Positions of all
Piece
s on the gameboard are stored inGame.board
field as a 2-dimension array ofPiece
objects. The x and y coordinate of aPlace
on the gameboard are used as index inGame.board
. E.g.board[place.x()][place.y()]
. If onePlace
does not have a piece on it, it will be null inboard[place.x()][place.y()]
. Student may modify elements inGame.board
to implement moving aPiece
. TheMove
object can be considered valid on present gameboard. -
getAvailableMoves
Get all available moves of one player. This method is called when it is thePlayer
's turn to make a move. It will iterate allPiece
s belonging to thePlayer
on board and obtain available moves of each of thePiece
s through methodPiece.getAvailableMoves(Game, Place)
of eachPiece
.Attention: Student should make sure all
Move
s returned are valid.- Specified by:
getAvailableMoves
in classGame
- Parameters:
player
- the player whose available moves to get- Returns:
- an array of available moves
-