inonit.domain.game.card.hearts.ai
Class AbstractIntelligence

java.lang.Object
  |
  +--inonit.domain.game.card.hearts.ai.AbstractIntelligence
All Implemented Interfaces:
Intelligence

public abstract class AbstractIntelligence
extends java.lang.Object
implements Intelligence

An implementation of Intelligence which includes a knowledge representation of the current Game.

Generally, AbstractIntelligence has final implementations of informational methods in Intelligence which update its knowledge representation and then invoke xxxEvent() methods to allow subclassers to update any additional knowledge representation that they have when events occur.

See the Intelligence class for specifications on Intelligence methods.


Inner classes inherited from class inonit.domain.game.card.hearts.Intelligence
Intelligence.Environment, Intelligence.Game, Intelligence.Hand, Intelligence.Kitty, Intelligence.Pass, Intelligence.Play, Intelligence.Trick
 
Fields inherited from interface inonit.domain.game.card.hearts.Intelligence
RANDOM
 
Constructor Summary
protected AbstractIntelligence()
          Constructor used by subclasses.
 
Method Summary
 void cardPlayed(Intelligence.Play play)
          Updates the Game object, and then invokes cardPlayedEvent.
protected  void cardPlayedEvent(int player, Card card)
          The default implementation of this method does nothing.
 void destroy()
          Invokes destroyEvent.
protected  void destroyEvent()
          The default implementation of this method does nothing.
 void exceptionThrown(java.lang.String stackTrace)
          The default implementation of this method does nothing.
 void gameEnded()
          Invokes gameEndedEvent, and then discards the Game object.
protected  void gameEndedEvent(Game game)
          The default implementation of this method does nothing.
 void gameStarting(Intelligence.Game game)
          Creates a Game object which is associated with the game argument, and then invokes gameStartingEvent.
protected  void gameStartingEvent(Intelligence.Game configuration)
          The default implementation of this method does nothing.
protected  CardArray getCards()
          Returns the Cards currently in this AbstractIntelligence's hand.
protected  Intelligence.Environment getEnvironment()
          Returns an object which contains services provided by, and information about, the environment in which this AbstractIntelligence is running.
protected  Game getGame()
          Returns an object representing the current Game.
protected  Hand getHand()
          Returns a object representing the current Hand.
protected  CardArray getLegalPlays()
          Returns a CardArray containing the Cards which would be legal to play if this AbstractIntelligence is playing next.
 CardArray getPass()
          Invoked to ask this Intelligence for Cards to pass to another player.
abstract  CardArray getPass(int numberToPass)
          Should return a CardArray containing the number of cards indicated by the numberToPass argument.
abstract  Card getPlay()
          Should return the Card this AbstractIntelligence wishes to play.
protected  Rules getRules()
          Returns an object representing the rules of the current game.
protected  int[] getScore()
          Returns the current score of the Game.
protected  Storage getStorage()
          Returns a Storage object associated with the Intelligence.Environment.Storage object from this AbstractIntelligence's Intelligence.Environment.
protected  Trick getTrick()
          Returns an object representing the current Trick.
protected  Trick[] getTricks()
          Returns an array of objects representing of the previous Tricks in this hand, not including the current Trick.
protected  int getValue(Card card)
          Returns the point (scoring) value (as determined by Rules.getValue(CardArray) of the given Card in the current Game.
protected  int getValue(CardArray cards)
          Returns the point (scoring) value (as determined by Rules.getValue of the given CardArray in the current Game.
protected  void handDealtEvent(CardArray hand)
          The default implementation of this method does nothing.
 void handEnded()
          Updates the Game object, and then invokes handScoredEvent.
protected  void handScoredEvent(int[] scores)
          The default implementation of this method does nothing.
 void handStarting(Intelligence.Hand hand)
          Updates the Game object, and then invokes handDealtEvent.
 void initialize(Intelligence.Environment environment)
          Informs this Intelligence that it has been instantiated, and provides it with an Environment from which it can obtain various services.
protected  void initializeEvent(Intelligence.Environment environment)
          The default implementation of this method does nothing.
protected  boolean isHigher(Card one, Card two)
          Returns whether the first Card is higher than the second, by Rank only.
protected  boolean isHigher(Card card, Rank rank)
          Returns whether the given Card is higher than the given Rank.
protected  boolean isLower(Card one, Card two)
          Returns whether the first Card is lower than the second, by Rank only.
protected  boolean isLower(Card card, Rank rank)
          Returns whether the given Card is lower than the given Rank.
protected  boolean isSameSuit(Card one, Card two)
          Returns whether the given two Cards are of the same suit.
 void kittyTaken(Intelligence.Kitty kitty)
          Updates the Game object, and then invokes tookKittyEvent.
protected  void log(java.lang.Object o)
          Write a log message to the game log; the log message will indicate the player responsible for writing it.
 void passIgnored(java.lang.String reason)
          The default implementation of this method does nothing.
protected  int players()
          Returns the number of players in the current Game.
 void playIgnored(java.lang.String reason)
          The default implementation of this method does nothing.
 void receivedPass(Intelligence.Pass pass)
          Updates the Game object, and then invokes receivedPassEvent.
protected  void receivedPassEvent(Pass pass)
          The default implementation of this method does nothing.
 void sentPass(Intelligence.Pass pass)
          Updates the Game object, and then invokes sentPassEvent.
protected  void sentPassEvent(Pass pass)
          The default implementation of this method does nothing.
protected  void tookKittyEvent(CardArray kitty)
          The default implementation of this method does nothing.
protected  void tookKittyEvent(int player, int size)
          The default implementation of this method does nothing.
 void trickEnded()
          Updates the Game object, and then invokes trickEndedEvent.
protected  void trickEndedEvent(int playerWhoTook)
          The default implementation of this method does nothing.
 void trickStarting(Intelligence.Trick trick)
          Updates the Game object, and then invokes trickStartingEvent.
protected  void trickStartingEvent(int playerToLead)
          The default implementation of this method does nothing.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractIntelligence

protected AbstractIntelligence()
Constructor used by subclasses.
Method Detail

log

protected void log(java.lang.Object o)
Write a log message to the game log; the log message will indicate the player responsible for writing it. May be useful for debugging. If a game is not in progress, this method has no effect.
Parameters:
o - The object to write to the log.

initialize

public final void initialize(Intelligence.Environment environment)
Description copied from interface: Intelligence
Informs this Intelligence that it has been instantiated, and provides it with an Environment from which it can obtain various services.
Specified by:
initialize in interface Intelligence
Following copied from interface: inonit.domain.game.card.hearts.Intelligence
Parameters:
environment - An Intelligence.Environment representing external information and services.

gameStarting

public final void gameStarting(Intelligence.Game game)
Creates a Game object which is associated with the game argument, and then invokes gameStartingEvent.
Specified by:
gameStarting in interface Intelligence
Following copied from interface: inonit.domain.game.card.hearts.Intelligence
Parameters:
game - An Intelligence.Game object containing information about the game which is starting.

handStarting

public final void handStarting(Intelligence.Hand hand)
Updates the Game object, and then invokes handDealtEvent.
Specified by:
handStarting in interface Intelligence
Following copied from interface: inonit.domain.game.card.hearts.Intelligence
Parameters:
hand - A Intelligence.Hand containing information about the current hand, including the cards dealt to this Intelligence.

sentPass

public final void sentPass(Intelligence.Pass pass)
Updates the Game object, and then invokes sentPassEvent.
Specified by:
sentPass in interface Intelligence
Following copied from interface: inonit.domain.game.card.hearts.Intelligence
Parameters:
pass - A Pass object containing information about what was passed, and to whom. Ordinarily, what was passed will already be known by this Intelligence, unless it timed out trying to pass or returned an invalid pass from getPass.

receivedPass

public final void receivedPass(Intelligence.Pass pass)
Updates the Game object, and then invokes receivedPassEvent.
Specified by:
receivedPass in interface Intelligence
Following copied from interface: inonit.domain.game.card.hearts.Intelligence
Parameters:
pass - A Pass object containing information about what was received, and from whom.

trickStarting

public final void trickStarting(Intelligence.Trick trick)
Updates the Game object, and then invokes trickStartingEvent.
Specified by:
trickStarting in interface Intelligence
Following copied from interface: inonit.domain.game.card.hearts.Intelligence
Parameters:
trick - A Trick object which can provide information about the current trick.

cardPlayed

public final void cardPlayed(Intelligence.Play play)
Updates the Game object, and then invokes cardPlayedEvent.
Specified by:
cardPlayed in interface Intelligence
Following copied from interface: inonit.domain.game.card.hearts.Intelligence
Parameters:
play - A Intelligence.Play object containing information about who played the card, and what was played.

trickEnded

public final void trickEnded()
Updates the Game object, and then invokes trickEndedEvent.
Specified by:
trickEnded in interface Intelligence

kittyTaken

public final void kittyTaken(Intelligence.Kitty kitty)
Updates the Game object, and then invokes tookKittyEvent.
Specified by:
kittyTaken in interface Intelligence
Following copied from interface: inonit.domain.game.card.hearts.Intelligence
Parameters:
kitty - A Kitty object containing information about the kitty.

handEnded

public final void handEnded()
Updates the Game object, and then invokes handScoredEvent.
Specified by:
handEnded in interface Intelligence

gameEnded

public final void gameEnded()
Invokes gameEndedEvent, and then discards the Game object.
Specified by:
gameEnded in interface Intelligence

destroy

public final void destroy()
Invokes destroyEvent.
Specified by:
destroy in interface Intelligence

getPass

public final CardArray getPass()
Description copied from interface: Intelligence
Invoked to ask this Intelligence for Cards to pass to another player. Should return a CardArray containing the Cards from this Intelligence's hand which it wishes to pass.

Whether this method is invoked in hands which are hold hands -- i.e., hands in which the Intelligence would be passing to itself -- is unspecified.

If this method does not successfully return a valid pass, the following steps will take place:

Specified by:
getPass in interface Intelligence
Following copied from interface: inonit.domain.game.card.hearts.Intelligence
Returns:
A CardArray containing the Cards which it wishes to pass. Must not be null. Must contain the number of Cards specified by size. Must contain Cards that were dealt to the Intelligence in handStarting.

getPass

public abstract CardArray getPass(int numberToPass)
Should return a CardArray containing the number of cards indicated by the numberToPass argument.

See Intelligence.getPass for further information about this method, including specifications about when it will be invoked and what it must return.

Parameters:
numberToPass - The number of Cards which should be in the CardArray returned from this method.
Returns:
A CardArray containing Cards to pass to the Intelligence(s) receiving this pass.

getPlay

public abstract Card getPlay()
Should return the Card this AbstractIntelligence wishes to play. Information about the current Game, Hand and Trick can be accessed via getGame, getHand, and getTrick, respectively.

See Intelligence.getPlay for additional details on this method, including specifications about when it will be invoked and what it must return.

Specified by:
getPlay in interface Intelligence
Returns:
A Card to play.

passIgnored

public void passIgnored(java.lang.String reason)
The default implementation of this method does nothing.
Specified by:
passIgnored in interface Intelligence
Following copied from interface: inonit.domain.game.card.hearts.Intelligence
Parameters:
reason - A String describing why getPass() was ignored.

playIgnored

public void playIgnored(java.lang.String reason)
The default implementation of this method does nothing.
Specified by:
playIgnored in interface Intelligence
Following copied from interface: inonit.domain.game.card.hearts.Intelligence
Parameters:
reason - A String describing why getPlay() was ignored.

exceptionThrown

public void exceptionThrown(java.lang.String stackTrace)
The default implementation of this method does nothing.
Specified by:
exceptionThrown in interface Intelligence
Following copied from interface: inonit.domain.game.card.hearts.Intelligence
Parameters:
stackTrace - A String containing the exception's stack trace.

initializeEvent

protected void initializeEvent(Intelligence.Environment environment)
The default implementation of this method does nothing.
Parameters:
environment - An Intelligence.Environment object containing information and services provided by the environment in which this AbstractIntelligence object is running.

gameStartingEvent

protected void gameStartingEvent(Intelligence.Game configuration)
The default implementation of this method does nothing.
Parameters:
configuration - An Intelligence.Game object containing information about the game which is starting.

handDealtEvent

protected void handDealtEvent(CardArray hand)
The default implementation of this method does nothing.
Parameters:
hand - A CardArray containing the Cards dealt to this AbstractIntelligence.

sentPassEvent

protected void sentPassEvent(Pass pass)
The default implementation of this method does nothing.
Parameters:
pass - A Pass containing information about cards passed to other players.

receivedPassEvent

protected void receivedPassEvent(Pass pass)
The default implementation of this method does nothing.
Parameters:
pass - A Pass containing information about cards received from other players.

trickStartingEvent

protected void trickStartingEvent(int playerToLead)
The default implementation of this method does nothing.
Parameters:
playerToLead - The player index of the Intelligence who will lead this trick.

cardPlayedEvent

protected void cardPlayedEvent(int player,
                               Card card)
The default implementation of this method does nothing.
Parameters:
player - The player index of the Intelligence which played a Card.
card - The Card which the Intelligence played.

trickEndedEvent

protected void trickEndedEvent(int playerWhoTook)
The default implementation of this method does nothing.
Parameters:
playerWhoTook - The player index of the Intelligence which took the trick.

tookKittyEvent

protected void tookKittyEvent(int player,
                              int size)
The default implementation of this method does nothing.
Parameters:
player - The player index of the Intelligence which took the kitty.
size - The number of cards in the kitty that player took.

tookKittyEvent

protected void tookKittyEvent(CardArray kitty)
The default implementation of this method does nothing.
Parameters:
kitty - A CardArray containing the Cards that were contained in the non-blind (see Rules.isKittyBlind) kitty.

handScoredEvent

protected void handScoredEvent(int[] scores)
The default implementation of this method does nothing.
Parameters:
scores - An array containing scores for the hand which just ended, by player index. scores[0] will contain the score for this AbstractIntelligence, scores[1] will contain the score for the Intelligence to this AbstractIntelligence's left, and so on. These are not the total scores for the game; total scores can be obtained via getScore.

gameEndedEvent

protected void gameEndedEvent(Game game)
The default implementation of this method does nothing.
Parameters:
game - The Game which just ended. Information can be extracted from the Game object in this method if there is information which this AbstractIntelligence wants to persist between Games.

destroyEvent

protected void destroyEvent()
The default implementation of this method does nothing.

isHigher

protected final boolean isHigher(Card one,
                                 Card two)
Returns whether the first Card is higher than the second, by Rank only.
Parameters:
one - A Card to compare. Must not be null.
two - Another Card to compare. Must not be null.
Returns:
true if one has a higher Rank than two; false if their Ranks are the same or if two's is higher.

isLower

protected final boolean isLower(Card one,
                                Card two)
Returns whether the first Card is lower than the second, by Rank only.
Parameters:
one - A Card to compare. Must not be null.
two - Another Card to compare. Must not be null.
Returns:
true if one has a lower Rank than two; false if their Ranks are the same or if two's is lower.

isHigher

protected final boolean isHigher(Card card,
                                 Rank rank)
Returns whether the given Card is higher than the given Rank.
Parameters:
card - A Card to compare. Must not be null.
rank - A Rank to which to compare it. Must not be null.
Returns:
true if card's Rank is higher than rank; false otherwise.

isLower

protected final boolean isLower(Card card,
                                Rank rank)
Returns whether the given Card is lower than the given Rank.
Parameters:
card - A Card to compare. Must not be null.
rank - A Rank to which to compare it. Must not be null.
Returns:
true if card's Rank is lower than rank; false otherwise.

isSameSuit

protected final boolean isSameSuit(Card one,
                                   Card two)
Returns whether the given two Cards are of the same suit.
Parameters:
one - A Card to compare. Must not be null.
two - Another Card to compare. Must not be null.
Returns:
true if one and two are of the same suit; false otherwise.

getCards

protected final CardArray getCards()
Returns the Cards currently in this AbstractIntelligence's hand.
Returns:
A CardArray containing the Cards currently in this AbstractIntelligence's hand, or null if no Hand is in progress.

getLegalPlays

protected final CardArray getLegalPlays()
Returns a CardArray containing the Cards which would be legal to play if this AbstractIntelligence is playing next. If this AbstractIntelligence is not playing next, returns the Cards that would be legal if this AbstractIntelligence were playing next.
Returns:
A CardArray containing the Cards in this AbstractIntelligence's hand which would be legal to play if this AbstractIntelligence were playing next, or null if no Hand is in progress.

getEnvironment

protected final Intelligence.Environment getEnvironment()
Returns an object which contains services provided by, and information about, the environment in which this AbstractIntelligence is running.
Returns:
An Intelligence.Environment object which contains information about, and services provided by, this AbstractIntelligence's environment.

getStorage

protected final Storage getStorage()
Returns a Storage object associated with the Intelligence.Environment.Storage object from this AbstractIntelligence's Intelligence.Environment.
Returns:
A Storage object which can be used for persistent storage by this AbstractIntelligence implementation.

getGame

protected final Game getGame()
Returns an object representing the current Game.
Returns:
The current Game, or null if no Game is in progress.

getRules

protected final Rules getRules()
Returns an object representing the rules of the current game.
Returns:
A Rules object containing information about the rules of the current Game.

getHand

protected final Hand getHand()
Returns a object representing the current Hand.
Returns:
The current Hand, or null if no Hand is in progress.

getTricks

protected final Trick[] getTricks()
Returns an array of objects representing of the previous Tricks in this hand, not including the current Trick.
Returns:
The previous Tricks in this Hand, or null if no Hand is in progress.

getTrick

protected final Trick getTrick()
Returns an object representing the current Trick.
Returns:
The current Trick, or null if no Trick is in progress.

players

protected final int players()
Returns the number of players in the current Game. A Game must be in progress.
Returns:
The number of players participating in the current Game.

getValue

protected final int getValue(Card card)
Returns the point (scoring) value (as determined by Rules.getValue(CardArray) of the given Card in the current Game. A Game must be in progress.
Parameters:
card - A Card. Must not be null.
Returns:
The point value of the given Card in this Game.

getValue

protected final int getValue(CardArray cards)
Returns the point (scoring) value (as determined by Rules.getValue of the given CardArray in the current Game. A Game must be in progress.
Parameters:
cards - A CardArray. Must not be null.
Returns:
The combined point value of the Cards in the given CardArray in this Game.

getScore

protected final int[] getScore()
Returns the current score of the Game.
Returns:
An int[] containing the current game score, with this AbstractIntelligence's score at index 0, the score of the Intelligence to this AbstractIntelligence's left at index 1, and so on. Returns null if no Game is in progress.