public class GameBot extends java.lang.Thread implements Player
An automatic card game player - a robot - to plays all kinds of card games.
This class has the generic features of a bot. It relies on the Strategy design pattern to shift between different ways to pick the cards to play. Although a default strategy may be provided by this class, each game should have a least one strategy and may select among several different ones for its bots.
GameBot
implements the Player
interface,
hence it must provide a nick and handle events.
Nicks of bots are picked from a list, making sure the it is not already
being used in the game where bot will be added.
As GameBot
implements the GameObserver
interface, it
can receive events sent by its GameMaster
.
GameBot
extends Thread
and uses the run()
method to process process events. This way, event processing is
decoupled from event propagation. Threads in Java follow the
template method design pattern.
jpleal@fc.up.pt
Modifier and Type | Class and Description |
---|---|
(package private) class |
GameBot.PlayerCards |
Modifier and Type | Field and Description |
---|---|
(package private) boolean |
hasEventToProcess |
(package private) boolean |
hasToPlay |
Constructor and Description |
---|
GameBot(GameMaster gameMaster)
A
GameBot instance for a particular GameMaster instance. |
Modifier and Type | Method and Description |
---|---|
boolean |
gameEnded()
Has this game ended? This information is
notified by the
GameEndEvent . |
protected java.lang.String |
generateNick(GameMaster gameMaster)
Pick a name from a list of bot names, different from any of the
nicks already in game.
|
CardCollection |
getAllCardsOnTable()
Convenience method to retrieve all cards on the table.
|
CardComparator |
getCardComparator()
Card comparator for this game
|
Card |
getCardOnTable(java.lang.String nick)
Convenience method to retrieve the card on table
from player with given nick.
|
CardCollection |
getHand()
The collection of cards this player holds in her hand.
|
java.lang.String |
getMode()
Mode in which the game currently is.
|
java.lang.String |
getNick()
Players nick, it will used as an ID.
|
java.lang.String |
getNickWithTurn()
Nick of player currently with turn.
|
CardCollection |
getPlayedCards()
All cards already played in this game.
|
java.util.Map<java.lang.String,java.lang.Integer> |
getPoints()
Points received by each player in the game.
|
int |
getRoundsCompleted()
Number of rounds completed in this game.
|
GamePlayingStrategy |
getStrategy()
Current strategy for picking cards
|
CardSuit |
getSuitToFollow()
Convenience method to retrieve the trump suite .
|
java.lang.String |
getWinner()
Nick of the player that won the game:
null if the game was
not ended yet or ended in a tie. |
boolean |
noCardsOnTable()
Convenience method to check if there are no card is on the table.
|
void |
notify(GameEndEvent event)
Notify the observer that the game has ended.
|
void |
notify(RoundConclusionEvent event)
Notify the observer of the round conclusion.
|
void |
notify(RoundUpdateEvent event)
Notify the observer of updates in a round.
|
void |
notify(SendCardsEvent event)
Notify the observer and send her/him/it a list of cards.
|
(package private) void |
notifyEventArrived()
Method invoked when a new event in received by this bot.
|
void |
run()
The active part of the thread is responsible for processing events.
|
void |
setHand(CardCollection hand)
Change the cards this player holds in her hand
for testing purposes only.
|
void |
setMode(java.lang.String mode)
Change the mode the mode is currently testing purposes only.
|
void |
setStrategy(GamePlayingStrategy strategy)
Set the card picking strategy the the given one.
|
(package private) void |
waitForEventToProcess()
Make this thread wait for the arrival of event to process.
|
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
public GameBot(GameMaster gameMaster)
GameBot
instance for a particular GameMaster
instance.
The latter instance influences the initialization of some fields.
The nick must be different from those already in the game instance.
The strategy must be given by GameMaster
to ensure its adequacy.gameMaster
- here bot will be added.public void run()
GameEndEvent
is received.
It uses the waitForEventToProcess()
to passively wait for
a notification of an event to process. It must play (a) card(s) on
its GameMaster
when required, or terminate the thread when
the game ends. This method invokes a GamePlayingStrategy
to pick the cards to play.run
in interface java.lang.Runnable
run
in class java.lang.Thread
void waitForEventToProcess()
run()
. It does not block if there is
an event pending to be processed; otherwise, it blocks on Object.wait()
until the notifyEventArrived()
is executed.void notifyEventArrived()
Object.wait()
for an event to
process using Object.notifyAll()
.public GamePlayingStrategy getStrategy()
public void setStrategy(GamePlayingStrategy strategy)
strategy
- to setpublic java.lang.String getNick()
Player
public int getRoundsCompleted()
RoundUpdateEvent
public java.util.Map<java.lang.String,java.lang.Integer> getPoints()
public void notify(SendCardsEvent event)
GameObserver
notify
in interface GameObserver
event
- to send.public void notify(RoundUpdateEvent event)
GameObserver
notify
in interface GameObserver
event
- to send.public void notify(RoundConclusionEvent event)
GameObserver
notify
in interface GameObserver
event
- to send.public void notify(GameEndEvent event)
GameObserver
notify
in interface GameObserver
event
- to send.public boolean gameEnded()
GameEndEvent
.true
if game has ended; false
otherwise.public java.lang.String getWinner()
null
if the game was
not ended yet or ended in a tie.null
public CardCollection getHand()
public void setHand(CardCollection hand)
hand
- to set.public CardComparator getCardComparator()
public java.lang.String getNickWithTurn()
null
in games where players don't play in turns.null
public boolean noCardsOnTable()
true
if no cards are on the table; false
otherwise.public Card getCardOnTable(java.lang.String nick)
nick
- of playernull
public CardCollection getAllCardsOnTable()
public CardCollection getPlayedCards()
public java.lang.String getMode()
null
if
undefined.null
public void setMode(java.lang.String mode)
null
if
undefined.mode
- to setpublic CardSuit getSuitToFollow()
protected java.lang.String generateNick(GameMaster gameMaster)
gameMaster
- where nick will be used.