controller
Class Control
java.lang.Object
controller.Control
public class Control
- extends java.lang.Object
Control is the master of the game and controls all
of the operations executed
|
Constructor Summary |
Control()
constructor: Creates an instance of control and sets the game as
eligible to begin. |
|
Method Summary |
boolean |
canPlay()
accessor(selector): Checks whether one of the two Player(s) can
play in the game. |
Move |
createMove(Cell a,
Cell b)
Is responsible for creating a Move based on it's parameters which are
the source cell of the Move, and the destination cell of the Move,
and finally returning it. |
ChessBoard |
getBoard()
accessor(selector): Returns the current ChessBoard to the called upon
method. |
Player |
getFirstPlayer()
accessor(selector): Returns the first Player. |
Player |
getSecondPlayer()
accessor(selector): Returns the second Player. |
boolean |
hasNextMoves()
accessor: checks wether there are next moves done |
boolean |
hasPreviousMoves()
accessor: checks wether there are previous moves done |
boolean |
isCheck(boolean color)
accessor(observer): This method sees if the King with Color as parameter
passed by, is checked. |
boolean |
isCheckMate(boolean color)
accessor(observer): This method sees if King with Color as parameter
passed by is terminally checked. |
boolean |
isDraw(boolean color)
accessor(observer): This method checks whether the game has concluded
with a tie when the player with the given color plays. |
void |
loadGame()
transformer(mutative): Loads a file with the specific name and
creates a game by executing all the moves in the file. |
void |
newGame()
transformer(mutative): Creates a new game initializing the chess board
with the appropriate Pieces. |
boolean |
nextGame()
transformer(mutative): Takes the game by one move forward. |
boolean |
previousGame()
transformer(mutative): This method is responsible for taking the game
one move back. |
void |
saveGame()
accessor(selector): Creates a string representation of the current
game based on all previous moves. |
void |
setFirstPlayer(Player r)
transformer(mutative): Sets the first Player as the parameter
Player passed. |
void |
setSecondPlayer(Player r)
transformer(mutative): Sets the second Player as the parameter
Player passed. |
boolean |
updateGame(Move x)
transformer(mutative): This method is responsible for changing the
current game based on the Move passed as parameter, consequently it needs
to execute the argument Move. |
| Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Control
public Control()
- constructor: Creates an instance of control and sets the game as
eligible to begin.
postcondition: Is responsible for creating a new game and
initializing it. Also it permits the game to start.
hasPreviousMoves
public boolean hasPreviousMoves()
- accessor: checks wether there are previous moves done
- Returns:
- if previous moves are available true is returned,
false otherwiser
hasNextMoves
public boolean hasNextMoves()
- accessor: checks wether there are next moves done
- Returns:
- if next moves are available true is returned,
false otherwiser
createMove
public Move createMove(Cell a,
Cell b)
throws IllegalCellException
- Is responsible for creating a Move based on it's parameters which are
the source cell of the Move, and the destination cell of the Move,
and finally returning it. It creates the most specific type of Move
it can create.
precondition: Given Cell(s) are valid Cell(s) of a ChessBoard.
postcondition: Creates a Move based on the parameter Cell(s).
- Parameters:
a - Is the source cell.b - Is the destination cell.
- Returns:
- Move defined by cell arguments
- Throws:
java.lang.IllegalArgumentException - if Cell correspond to invalid ChessBoard Cell(s)
IllegalCellException- See Also:
Move
getFirstPlayer
public Player getFirstPlayer()
- accessor(selector): Returns the first Player.
postcondition: Returns the first Player of the game.
- Returns:
- Player one of the game.
getSecondPlayer
public Player getSecondPlayer()
- accessor(selector): Returns the second Player.
postcondition: Returns the second Player of the game.
- Returns:
- Player two of the game.
setFirstPlayer
public void setFirstPlayer(Player r)
throws java.lang.IllegalArgumentException
- transformer(mutative): Sets the first Player as the parameter
Player passed.
precondition: Parameter Player is not null.
postcondition: Sets the first Player to one passed as argument.
- Parameters:
r - is parameter to be assigned to Player one.
- Throws:
java.lang.IllegalArgumentException - if given Player is null
setSecondPlayer
public void setSecondPlayer(Player r)
- transformer(mutative): Sets the second Player as the parameter
Player passed.
precondition: Parameter Player is not null.
postcondition: Sets the second Player to the one passed as argument.
- Parameters:
r - is parameter to be assigned to Player two.
- Throws:
java.lang.IllegalArgumentException - if given Player is null
previousGame
public boolean previousGame()
- transformer(mutative): This method is responsible for taking the game
one move back. After executing that operation, Player is no longer eligible
to play until we've reached the current state again. Depending of the success
of the operation a boolean value is returned.
precondition: There is a previous move.
postcondition: This method takes us back one move, if the operation is successful, true is returned
if not, false is returned.
- Returns:
- true if going back was a success, false otherwise.
canPlay
public boolean canPlay()
- accessor(selector): Checks whether one of the two Player(s) can
play in the game. Returns true if so, false otherwise.
postcondition: Returns true if the game can be continued, Player(s)
can play.
- Returns:
- true if the game can continue, else false.
nextGame
public boolean nextGame()
- transformer(mutative): Takes the game by one move forward. After executing
the operation, Player is ineligible to Play, until reaching the current state
of the game.
precondition: There has to be a next Move. That means at least once
the previous operation must have been successfully executed.
postcondition: Takes the game one move forward. If operation is a success
returns true, false otherwise.
- Returns:
- true if successful, false otherwise.
isCheck
public boolean isCheck(boolean color)
- accessor(observer): This method sees if the King with Color as parameter
passed by, is checked. If so returns true. Else returns false.
postcondition: If the King with Color as argument passed is checked
returns true, else false.
- Parameters:
color - Defines the Player's color whose King we look if is checked.
- Returns:
- true if King is checked, false otherwise.
isCheckMate
public boolean isCheckMate(boolean color)
- accessor(observer): This method sees if King with Color as parameter
passed by is terminally checked. If so returns true, else false.
postcondition: Sees if King is definitively checked. If that is the
case, returns true, false otherwise.
- Parameters:
color - Defines the color of the Player we are checking for check mate.
- Returns:
- true if King is terminally checked, false otherwise.
isDraw
public boolean isDraw(boolean color)
- accessor(observer): This method checks whether the game has concluded
with a tie when the player with the given color plays.
If such is the case returns true, otherwise returns false.
postcondition: Checks whether the game is a draw, returns true if so,
false otherwise.
- Returns:
- true, if game is tied, false otherwise.
getBoard
public ChessBoard getBoard()
- accessor(selector): Returns the current ChessBoard to the called upon
method.
postcondition: Returns the ChessBoard
- Returns:
- The current ChessBoard
updateGame
public boolean updateGame(Move x)
throws PromotionException
- transformer(mutative): This method is responsible for changing the
current game based on the Move passed as parameter, consequently it needs
to execute the argument Move.
postcondition: This method updates the chess board with the Move
passed as argument and returns true if successfully executed, false else.
- Parameters:
x - The Move that the chess board will be updated.
- Returns:
- true if change is applied, false otherwise.
- Throws:
PromotionException - If the Move played concludes to a pawn
reaching the opposite side.
newGame
public void newGame()
- transformer(mutative): Creates a new game initializing the chess board
with the appropriate Pieces.
postcondition: Creates a new game by initializing all of Chess's
pieces' on the correct positions of the board.
saveGame
public void saveGame()
throws java.io.IOException
- accessor(selector): Creates a string representation of the current
game based on all previous moves. Saves this representation a file.
precondition: Program must be able to open file.
postcondition: Encrypts the moves played into real-game moves
and saves them in a file.
- Throws:
java.io.IOException - If file cannot be opened or written.
loadGame
public void loadGame()
throws java.io.IOException
- transformer(mutative): Loads a file with the specific name and
creates a game by executing all the moves in the file.
precondition: File cannot be readen.
postcondition: Decrypts moves in the file and creates a new board
based on those moves.
- Throws:
java.io.IOException - If file cannot be opened