de.htwdd.rosenkoenig.game
Class Game

java.lang.Object
  extended by de.htwdd.rosenkoenig.game.Game

public class Game
extends java.lang.Object

This class implements the game and all of its properties. As Domi calls jocosely it is the 'zentrale Obermopsklasse'. It does - of course - not contain anything that has something to do with GUI or whatsoever.


Field Summary
protected  Color activePlayer
           
protected  int availableStones
           
protected  Color[][] board
           
protected  Position currentPosition
           
private static double[][][] directions
          These are the constants for the coding of the directions of the cards.
private static double[][] distance
          These constants are used to code the distance of a card.
protected  java.util.List<Card> drawDeck
           
protected  java.util.List<GUI> guis
           
protected  java.util.List<Card> knightableCards
          List of knightable cards.
protected  boolean knightsSelected
          Wheter or not knights are enabled.
protected static Position maxDimension
          This variable give the maximum board dimensions.
protected  Card nextCard
          this property determines user's next move; if user can move, and this property is unequal to five then the selected card is played otherwise the user draws a card, if possible (i.e. lt 5 cards)
protected  java.util.List<GameObserver> observers
          List of game observers observing the game.
protected  Color passivePlayer
           
protected  java.util.List<Card> playableCards
          List of playable cards.
protected  java.util.List<Card> playedCardsDeck
           
protected  java.util.EnumMap<Color,Player> players
          This enumeration represents the players.
 
Constructor Summary
Game(Player playerOne, Player playerTwo)
          Ctor - randomly choses, which player is about to start.
Game(Player playerOne, Player playerTwo, GUI gui)
          Ctor - calls public
 
Method Summary
 void addGUI(GUI gui)
          Add a gui to the game.
 void addObserver(GameObserver observer)
          Add an observer to the game.
 int calcScore(Color color)
           
 boolean cardKnightable(Card card)
           
 boolean cardPlayable(Card card)
           
protected  void endGame()
          endGame updates the players highscore and notifies the observers that the game has ended.
 void endTurn()
          This function persists the move.
 Color getActivePlayer()
          Getter of the property activePlayer
 int getAvailableStones()
          Getter of the property stones
 Color[][] getBoard()
          Getter of the property board
private  int getCoherentArea(Color color, int score, boolean[][] tmpBoard, int x, int y)
           
 Position getCurrentPosition()
          Getter of the property currentPosition
 java.util.List<Card> getDrawDeck()
          Getter of the property drawDeck
 Card getNextCard()
          Getter of the property nextCard
 Color getPassivePlayer()
          Getter of the property passivePlayer
 java.util.List<Card> getPlayedCardsDeck()
          Getter of the property playedCardsDeck
 java.util.EnumMap<Color,Player> getPlayers()
          Getter of the property players
 double[][] getRotMatrix(double degree)
          Calculate the rotation matrix for a certain degree rotation.
 boolean isCardPlayable(Card card, boolean withKnight)
          Checks for active player if card is playable.
 boolean isCardPlayable(Color player, Card card, boolean withKnight)
          Checks for player if card is playable.
 Color[][] normalizeBoard()
          Normalize the board.
 java.util.ArrayList<Card> normalizeCards(Color player)
          Rotate (normalize) the cards for player depending on the position of the crown.
 int[] normalizeCrown()
          Put the crown in a position of the field that is saved.
 double[] prepareNetFeed()
          A highly sophisticated function that, much better than all other functions: creates a double array for the crown (1), player color (2), card direction, move width, playability of the card for all ten cards ((8+3+1)*10) and 10 values for the knights (four, three, two, one or no knights) For the crown there is only a small part of the board saved and rotated (normalized) to save input neurons of the net.
protected  void prepareTurn()
          Activates the current player's controls, updates the gui and notifies all observers.
 void removeGUI(GUI gui)
          Remove a game from the gui.
 void removeObserver(GameObserver observer)
          Remove an observer to the game.
 void selectCard(Card card)
           
 void selectDrawDeck()
          This function is called, when the player clicks on the drawDeck.
 void setActivePlayer(Color activePlayer)
          Setter of the property activePlayer
 void setAvailableStones(int availableStones)
          Setter of the property stones
 void setBoard(Color[][] board)
          Setter of the property board
 void setCurrentPosition(Position currentPosition)
          Setter of the property currentPosition
 void setDrawDeck(java.util.List<Card> drawDeck)
          Setter of the property drawDeck
 void setNextCard(Card nextCard)
          Setter of the property nextCard
 void setPassivePlayer(Color passivePlayer)
          Setter of the property passivePlayer
 void setPlayedCardsDeck(java.util.List<Card> playedCardsDeck)
          Setter of the property playedCardsDeck
 void setPlayers(java.util.EnumMap<Color,Player> players)
          Setter of the property players
 void start()
          Start a game.
 void toggleKnights()
          This function toggles the knight selection of the currently active player.
protected  void undoMove()
          This function deselects the card currently activated by the player and remove the shady stone from the board/resets the old stone to the board, in case the active player planned to use a knight.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

maxDimension

protected static final Position maxDimension
This variable give the maximum board dimensions.


directions

private static final double[][][] directions
These are the constants for the coding of the directions of the cards.


distance

private static final double[][] distance
These constants are used to code the distance of a card.


players

protected java.util.EnumMap<Color,Player> players
This enumeration represents the players.


availableStones

protected int availableStones

drawDeck

protected java.util.List<Card> drawDeck

playedCardsDeck

protected java.util.List<Card> playedCardsDeck

board

protected Color[][] board

activePlayer

protected Color activePlayer

passivePlayer

protected Color passivePlayer

currentPosition

protected Position currentPosition

nextCard

protected Card nextCard
this property determines user's next move; if user can move, and this property is unequal to five then the selected card is played otherwise the user draws a card, if possible (i.e. lt 5 cards)


guis

protected java.util.List<GUI> guis

observers

protected java.util.List<GameObserver> observers
List of game observers observing the game.


knightableCards

protected java.util.List<Card> knightableCards
List of knightable cards.


playableCards

protected java.util.List<Card> playableCards
List of playable cards.


knightsSelected

protected boolean knightsSelected
Wheter or not knights are enabled.

Constructor Detail

Game

public Game(Player playerOne,
            Player playerTwo)
Ctor - randomly choses, which player is about to start.

Parameters:
playerOne - player one
playerTwo - player two

Game

public Game(Player playerOne,
            Player playerTwo,
            GUI gui)
Ctor - calls public

Parameters:
playerOne - player one
playerTwo - player two
gui - the gui to be attached
Method Detail

getPlayers

public java.util.EnumMap<Color,Player> getPlayers()
Getter of the property players

Returns:
Returns the players.

getAvailableStones

public int getAvailableStones()
Getter of the property stones

Returns:
Returns the stones.

setAvailableStones

public void setAvailableStones(int availableStones)
Setter of the property stones

Parameters:
stones - The stones to set.

getDrawDeck

public java.util.List<Card> getDrawDeck()
Getter of the property drawDeck

Returns:
Returns the drawDeck.

setDrawDeck

public void setDrawDeck(java.util.List<Card> drawDeck)
Setter of the property drawDeck

Parameters:
drawDeck - The drawDeck to set.

getPlayedCardsDeck

public java.util.List<Card> getPlayedCardsDeck()
Getter of the property playedCardsDeck

Returns:
Returns the playedCardsDeck.

setPlayedCardsDeck

public void setPlayedCardsDeck(java.util.List<Card> playedCardsDeck)
Setter of the property playedCardsDeck

Parameters:
playedCardsDeck - The playedCardsDeck to set.

getBoard

public Color[][] getBoard()
Getter of the property board

Returns:
Returns the boards.

setBoard

public void setBoard(Color[][] board)
Setter of the property board

Parameters:
board - The boards to set.

getActivePlayer

public Color getActivePlayer()
Getter of the property activePlayer

Returns:
Returns the activePlayer.

setActivePlayer

public void setActivePlayer(Color activePlayer)
Setter of the property activePlayer

Parameters:
activePlayer - The activePlayer to set.

getPassivePlayer

public Color getPassivePlayer()
Getter of the property passivePlayer

Returns:
Returns the passivePlayer.

setPassivePlayer

public void setPassivePlayer(Color passivePlayer)
Setter of the property passivePlayer

Parameters:
activePlayer - The passivePlayer to set.

getCurrentPosition

public Position getCurrentPosition()
Getter of the property currentPosition

Returns:
Returns the currentPosition.

setCurrentPosition

public void setCurrentPosition(Position currentPosition)
Setter of the property currentPosition

Parameters:
currentPosition - The currentPosition to set.

selectDrawDeck

public void selectDrawDeck()
This function is called, when the player clicks on the drawDeck. The assumed move is undone, and the score change disappears. The draw deck is selected, if the user has less than five cards. Once the draw deck is empty the played cards deck will be recycled and used as a shuffled new draw deck. Oink!


undoMove

protected void undoMove()
This function deselects the card currently activated by the player and remove the shady stone from the board/resets the old stone to the board, in case the active player planned to use a knight.


selectCard

public void selectCard(Card card)
Parameters:
card - the card to play

endTurn

public void endTurn()
This function persists the move. It includes: decreasing stone count, knight count, updates the score sets the new stone or new card, disables the active player and calls - if the game is not over - prepareMove after switching the player.


endGame

protected void endGame()
endGame updates the players highscore and notifies the observers that the game has ended.


cardPlayable

public boolean cardPlayable(Card card)
Parameters:
card - card to be checked for playability
Returns:
true or false depending on the card Check if card is playable and return true if it is playable false otherwise.

cardKnightable

public boolean cardKnightable(Card card)
Parameters:
card - to be checked for knightability
Returns:
true if knight can be used on the card; false otherwise; Check if card is playable and return true if it is playable with a knight - false otherwise.

toggleKnights

public void toggleKnights()
This function toggles the knight selection of the currently active player. This includes the activation/deactivation of cards that are/were only playable with knights.


getNextCard

public Card getNextCard()
Getter of the property nextCard

Returns:
Returns the nextCard.

setNextCard

public void setNextCard(Card nextCard)
Setter of the property nextCard

Parameters:
nextCard - The nextCard to set.

calcScore

public int calcScore(Color color)
Parameters:
color - color of the player whose score is to be calculated
Returns:
returns the score of player color This function calulates the score of the player with color color

getCoherentArea

private int getCoherentArea(Color color,
                            int score,
                            boolean[][] tmpBoard,
                            int x,
                            int y)
Parameters:
color - the color of the user whose score is to be calculated
score - the score so far
tmpBoard - a temporary structure containing all fields already processed
Returns:
updated score Calculate the score of a coherent area by recursively calling this function for all upper, lower, left, and right fields not processed, yet.

start

public void start()
Start a game. Includes creation of cards, decks etc.


prepareTurn

protected void prepareTurn()
Activates the current player's controls, updates the gui and notifies all observers.


isCardPlayable

public boolean isCardPlayable(Card card,
                              boolean withKnight)
Checks for active player if card is playable.

Parameters:
card - card to be checked
withKnight - whether or not use a knight
Returns:
true if card is playable, false otherwise

isCardPlayable

public boolean isCardPlayable(Color player,
                              Card card,
                              boolean withKnight)
Checks for player if card is playable.

Parameters:
player -
card - card to be checked
withKnight - whether or not use a knight
Returns:
true if card is playable, false otherwise

addGUI

public void addGUI(GUI gui)
Add a gui to the game.

Parameters:
gui - guess!

removeGUI

public void removeGUI(GUI gui)
Remove a game from the gui.

Parameters:
gui - guess!

addObserver

public void addObserver(GameObserver observer)
Add an observer to the game.

Parameters:
observer - the observer to be added.

removeObserver

public void removeObserver(GameObserver observer)
Remove an observer to the game.

Parameters:
observer - the observer to be removed.

setPlayers

public void setPlayers(java.util.EnumMap<Color,Player> players)
Setter of the property players

Parameters:
players - The players to set.

prepareNetFeed

public double[] prepareNetFeed()
A highly sophisticated function that, much better than all other functions: creates a double array for the crown (1), player color (2), card direction, move width, playability of the card for all ten cards ((8+3+1)*10) and 10 values for the knights (four, three, two, one or no knights) For the crown there is only a small part of the board saved and rotated (normalized) to save input neurons of the net. This makes it necessary to also rotate (normalize) the board.

Returns:
the created array

normalizeCrown

public int[] normalizeCrown()
Put the crown in a position of the field that is saved. This way we only need 21 instead of 81 neurons.

Returns:

normalizeBoard

public Color[][] normalizeBoard()
Normalize the board. Depending on the position of the crown the board is rotated by 0, 90, 180 or 270 degrees. This reduces the number of positions on the board by 3/4.

Returns:
rotated (normalized) board

normalizeCards

public java.util.ArrayList<Card> normalizeCards(Color player)
Rotate (normalize) the cards for player depending on the position of the crown.

Parameters:
player - the cards must be rotated for
Returns:
the normalized cards.

getRotMatrix

public double[][] getRotMatrix(double degree)
Calculate the rotation matrix for a certain degree rotation.

Parameters:
degree - degree of rotation
Returns:
rotation matrix