Package org.swtpra1.amazongame
Class Game
- java.lang.Object
-
- org.swtpra1.amazongame.Game
-
- All Implemented Interfaces:
java.io.Serializable
public class Game extends java.lang.Object implements java.io.Serializable
Represents the state of the "Game of the Amazons" board. TODO: Ich hasse zweidimensionale Arrays, sind die Indizes überall richtig? Coordinates are 0-indexed where (0,0) is the top left and (GAME_SIZE_COLUMNS-1,GAME_SIZE_ROWS-1) the bottom right- Version:
- 1.0
- Author:
- Tim Storm
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description Game(int gameId, Player[] players, int rows, int columns, long maxTurnTime)
Represents a single instance of the "Game of the Amazons" between players.Game(int gameId, Player[] players, Board initialBoard, long maxTurnTime)
Represents a single instance of the "Game of the Amazons" between players.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
executeTurn(Turn turn)
Executes a given turn.void
forceExecuteTurn(Turn turn)
Forces the execution of a given turn regardless of circumstances.Board
getBoard()
Board
getBoard(int index)
Returns the state of the board after turn no.int
getGameId()
Board
getInitialBoard()
@Nullable Turn
getLatestTurn()
Gets the latest executed turn.long
getMaxTurnTime()
Player
getPlayerById(int playerId)
Returns the Player object corresponding to the given id if the Player is playing in this game.Player[]
getPlayers()
int
getRelativePlayerId(int playerId)
Takes an absolute player id and returns the relative one.int
getRelativePlayerId(Player player)
Takes a player object and returns the relative id in this game.java.util.ArrayList<Turn>
getTurns()
int
getWinner()
If no other Board is given we evaluate the latest Board.int
getWinner(Board board)
Checks for a winner and returns the absolute player id.int
getWinnerRelative()
Checks for a winner of the current board and returns the relative player id.int
getWinnerRelative(Board board)
Checks for a winner and returns the relative player id.void
initializeUnsetFields()
Sets field not covered by constructor.boolean
isValidTurn(Turn turn, Board board)
Convenience wrapper method to validate turns.void
setWinningPlayer(int playerId)
java.lang.String
toString()
-
-
-
Constructor Detail
-
Game
public Game(int gameId, Player[] players, Board initialBoard, long maxTurnTime)
Represents a single instance of the "Game of the Amazons" between players.- Parameters:
gameId
- the unique ID of the gameplayers
- the participating playersinitialBoard
- the initial state of the board on which is playedmaxTurnTime
- the maximum amount of time allowed per move
-
Game
public Game(int gameId, Player[] players, int rows, int columns, long maxTurnTime)
Represents a single instance of the "Game of the Amazons" between players.- Parameters:
gameId
- the unique ID of the gameplayers
- the participating playersrows
- the number of rows on the boardcolumns
- the number of columns on the boardmaxTurnTime
- the maximum amount of time allowed per move
-
-
Method Detail
-
executeTurn
public int executeTurn(Turn turn)
Executes a given turn.- Parameters:
turn
- the turn to be executed- Returns:
- the ID of the winning player; -1 if still running
-
forceExecuteTurn
public void forceExecuteTurn(Turn turn) throws java.lang.ArrayIndexOutOfBoundsException
Forces the execution of a given turn regardless of circumstances. May throw ArrayIndexOutOfBoundsException for invalid turns USE WITH CAUTION, mainly for debugging/testing purposes This method intentionally foregoes the "winner check", because in the process of setting up a position the game may be lost beforehand- Parameters:
turn
- the (possibly illegal) turn to be made- Throws:
java.lang.ArrayIndexOutOfBoundsException
-
getWinner
public int getWinner(Board board)
Checks for a winner and returns the absolute player id.- Parameters:
board
- the board which we are checking- Returns:
- the playerID of the winning player; -1 if game is still running
-
getWinner
public int getWinner()
If no other Board is given we evaluate the latest Board.- Returns:
- the playerID of the winning player; -1 if game is still running
-
getWinnerRelative
public int getWinnerRelative(Board board)
Checks for a winner and returns the relative player id.- Parameters:
board
- board- Returns:
- 0 or 1 if player won; else -1
-
getWinnerRelative
public int getWinnerRelative()
Checks for a winner of the current board and returns the relative player id.- Returns:
- 0 or 1 if player won; else -1
-
isValidTurn
public boolean isValidTurn(Turn turn, Board board)
Convenience wrapper method to validate turns.- Parameters:
turn
- turn to be validatedboard
- the Board on which we are operating- Returns:
- whether turn is a valid turn
-
getRelativePlayerId
public int getRelativePlayerId(Player player)
Takes a player object and returns the relative id in this game.- Parameters:
player
- The player of whom the ID is wanted.- Returns:
- the relative player id
-
getRelativePlayerId
public int getRelativePlayerId(int playerId)
Takes an absolute player id and returns the relative one.- Parameters:
playerId
- absolute player id- Returns:
- relative player id (0 or 1)
-
getLatestTurn
@Nullable public @Nullable Turn getLatestTurn()
Gets the latest executed turn.- Returns:
- latest turn; null if there are any turns so far
-
getBoard
public Board getBoard(int index)
Returns the state of the board after turn no. index has been executed. If index is out of range it will only go up to the maximum possible value.- Parameters:
index
- the index- Returns:
- the board
-
getBoard
public Board getBoard()
-
getInitialBoard
public Board getInitialBoard()
-
getMaxTurnTime
public long getMaxTurnTime()
-
getGameId
public int getGameId()
-
getTurns
public java.util.ArrayList<Turn> getTurns()
-
getPlayers
public Player[] getPlayers()
-
setWinningPlayer
public void setWinningPlayer(int playerId)
-
getPlayerById
public Player getPlayerById(int playerId)
Returns the Player object corresponding to the given id if the Player is playing in this game.- Parameters:
playerId
- the global id of the player we are looking for- Returns:
- Player object if player is playing, null otherwise
-
initializeUnsetFields
public void initializeUnsetFields()
Sets field not covered by constructor. GSON does not use the constructor which is why we have to manually set fields that would otherwise be covered by a constructor
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-