cz.nipax.hippo.pexeso
Class Playground

java.lang.Object
  extended by cz.nipax.hippo.pexeso.Playground

public class Playground
extends java.lang.Object

Implementation of class - holds inner things about pexeso game state. Working with Playground and AI:

About the AI calls: Playground calls AI.onturn() if the AI is supposed to turn. The AI doesn't have to play right now. Then ai calls wantturn(cz.nipax.hippo.pexeso.AI, int, int). When a card is revealed, Playground calls AI.revealed(int, int, int). AI has to remember revealed cards if it wants to :-) [AI.revealed(int, int, int) is called even if the other player revealed card. Note that AI.onturn() is called once and it is expected that ai calls wantturn(cz.nipax.hippo.pexeso.AI, int, int) twice, because it can react on revealed card and alter the position of second card. When two same cards are revealed and geting out of game, AI.ivalidate(int, int) is called to AI. If AI will no longer be used (new game started and AI are set to null), AI.kill() is called. Note that if AI call wantturn(cz.nipax.hippo.pexeso.AI, int, int) the Playground may directly call back AI.onturn() withou any delay - so after succesfull wantturn(cz.nipax.hippo.pexeso.AI, int, int) call you should be carefull what you are changing.

If you call setPGC(cz.nipax.hippo.pexeso.PlaygroundComponent), then PlaygroundComponent.repaint_later(boolean) is called after every change of game state.


Nested Class Summary
(package private)  class Playground.Piece
          Info about one piece in game.
(package private)  class Playground.Player
          Info about one player.
 
Field Summary
private  int actual
          Currently playing player - 0 or 1.
(package private) static boolean DEBUG
          The debug mode.
(package private)  boolean game_started
          false after inicialization before start() called.
private  boolean m_beastmode
          Random switching cards after turns.
private  int m_cards
          Number of cards left in game. if == 0, the game ends.
private  Playground.Piece[][] m_field
          the desk with playground
(package private)  PlaygroundComponent m_pgc
          For notifying, that a refresh is needed.
private  int m_sleep_switch
          How long to sleep after switching cards at end of turn in switch mode.
private  int m_sleep_turn
          How long to sleep after end of turn.
private  boolean m_turnswitch
          Switches two cards when visible.
private  int m_x
          width of m_field
private  int m_y
          height of m_field
private  Playground.Player[] player
          Players in game - 2
 
Constructor Summary
Playground()
          Allocates objects for playground
 
Method Summary
 void add_AI(AI a1, AI a2)
          Plugs AIs into game.
protected  void beastswitch()
          Performs the beast action.
 java.lang.String getCPName()
          Gets string ID if current player.
 int getID(int x, int y)
          Returns ID of card on position x,y.
private  Playground.Piece getRandpiece()
          Get one valid Playground.Piece from m_field.
 int getScore(int playerID)
          Gets score of player.
 int getWinner()
          Gets ID of winner if game has ended.
 int getX()
          Gets width of m_field
 int getY()
          Gets height of m_field
 boolean checkDirty(int x, int y)
          Checks if piece need redrawing.
 void init(int x, int y)
          Inits new game on filed with dimensions x, y.
 boolean isEnd()
          Checks for end of game.
private  void nextturn()
          Ends current turn.
 void println(java.lang.String s)
          Inteligent debug message printinge.
 void setBeastmode(boolean beastmode)
          Change state of m_beastmode.
 void setPGC(PlaygroundComponent pgc)
          Set m_pgc variable.
 void setSwitchturn(boolean tswitch)
          Controls state of m_turnswitch.
 void start()
          Starts game - check if AI are allready added.
private  void switchplayer(boolean swp)
          Change current player.
private  void updateinvalidAI()
          Checks all cards in game and call AI.ivalidate(int, int) for every invalid card.
private  void updaterevealedAI()
          Checks all cards in game and call AI.revealed(int, int, int) for every visible card.
 boolean wantturn(AI ai, int x, int y)
          This should be called by AI when it wants to make turn.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEBUG

static final boolean DEBUG
The debug mode. Influents println(java.lang.String) and enables showing random switch in beast mode.

See Also:
Constant Field Values

player

private Playground.Player[] player
Players in game - 2


actual

private int actual
Currently playing player - 0 or 1. May be used as index to player


m_beastmode

private boolean m_beastmode
Random switching cards after turns. Value is set by setSwitchturn(boolean)


m_turnswitch

private boolean m_turnswitch
Switches two cards when visible. Value is set by setSwitchturn(boolean)


m_sleep_turn

private int m_sleep_turn
How long to sleep after end of turn. This is for human to have some time to remember positions of cards. In miliseconds.


m_sleep_switch

private int m_sleep_switch
How long to sleep after switching cards at end of turn in switch mode. This is for human to have some time to remember switched positions of cards. In miliseconds.


m_field

private Playground.Piece[][] m_field
the desk with playground


m_x

private int m_x
width of m_field


m_y

private int m_y
height of m_field


m_cards

private int m_cards
Number of cards left in game. if == 0, the game ends.


game_started

boolean game_started
false after inicialization before start() called.


m_pgc

PlaygroundComponent m_pgc
For notifying, that a refresh is needed. May be null so carefully.

Constructor Detail

Playground

public Playground()
Allocates objects for playground

Method Detail

println

public void println(java.lang.String s)
Inteligent debug message printinge. It is possible to disable all messages in one place - for some kind of final release


setPGC

public void setPGC(PlaygroundComponent pgc)
Set m_pgc variable. m_pgc is necessary for redrawing the screen. You don't need to call this method if you don't require callbecks from the game to diplay.


checkDirty

public boolean checkDirty(int x,
                          int y)
Checks if piece need redrawing. Returns current state and sets dirty to false.

Parameters:
x - coordinate of piece to check
y - coordinate of piece to check
Returns:
true, if place on x y was dirty.

init

public void init(int x,
                 int y)
Inits new game on filed with dimensions x, y. Fills the field with ints as ids of cards It leaves one last piece free, if x and y are odd. Them mixes all cards.

Parameters:
x - width of field
y - height of the field

add_AI

public void add_AI(AI a1,
                   AI a2)
Plugs AIs into game.

Parameters:
a1 - AI of first player - has first turn
a2 - AI of second player

start

public void start()
Starts game - check if AI are allready added. Checks for game_started to start game only once. Since this method is public, some nasty AI may try to cheat by starting game twice or something :-)


switchplayer

private void switchplayer(boolean swp)
Change current player.

Parameters:
swp - really switch player or just give another turn to current player

wantturn

public boolean wantturn(AI ai,
                        int x,
                        int y)
This should be called by AI when it wants to make turn. Reveales the picture and tells both AI that pictures were revealed.

Parameters:
ai - it should be "self" for the ai. This is needed to avoid AI cheating since AI doesn't have AI of opponent.
x - x-coordinate of move
y - y-coordinate of move
Returns:
false if invalid position and AI should try to play to different place

updaterevealedAI

private void updaterevealedAI()
Checks all cards in game and call AI.revealed(int, int, int) for every visible card.


updateinvalidAI

private void updateinvalidAI()
Checks all cards in game and call AI.ivalidate(int, int) for every invalid card.


nextturn

private void nextturn()
Ends current turn. Makes all cards invisible, calls switchplayer and updates for m_pgc. Also handles m_turnswitch and calls beastswitch().


getX

public int getX()
Gets width of m_field

Returns:
max x size of game field

getY

public int getY()
Gets height of m_field

Returns:
max y size of game field

getScore

public int getScore(int playerID)
Gets score of player.

Parameters:
playerID - 0 for first player and 1 for second
Returns:
the score

isEnd

public boolean isEnd()
Checks for end of game.

Returns:
true if game has ended

getWinner

public int getWinner()
Gets ID of winner if game has ended.

Returns:
ID of winner 0 - fisrt player, 1 - second player, 2 - both has same score or -1 if the game didn't end allready

getCPName

public java.lang.String getCPName()
Gets string ID if current player.

Returns:
string with current player name constructed from AI.getname() and actual

getID

public int getID(int x,
                 int y)
Returns ID of card on position x,y. This function is expected to be called by drawing function

Returns:
-1 for empty position, 0 for unrevealed card, > 0 as ID of a card

setBeastmode

public void setBeastmode(boolean beastmode)
Change state of m_beastmode.

Parameters:
beastmode - wanted mode

beastswitch

protected void beastswitch()
Performs the beast action. Checks if m_beastmode is true. Takes two cards and switches them. Takes two valid pieces and switch the IDs. It doesn't change dirty or revealed because all cards should be invisible now and the dirty is relevant to position.


getRandpiece

private Playground.Piece getRandpiece()
Get one valid Playground.Piece from m_field. The piece is chosen randomly.

Returns:
the random piece

setSwitchturn

public void setSwitchturn(boolean tswitch)
Controls state of m_turnswitch. Also alters m_sleep_switch and m_sleep_turn because of human memory :-).