All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class com.monpetitcoin.java.PrisonerGame.Prisoner

java.lang.Object
   |
   +----com.monpetitcoin.java.PrisonerGame.Prisoner

public class Prisoner
extends java.lang.Object
implements com.monpetitcoin.java.PrisonerGame.Pawn
This class contains the Prisoner's Dilemma logic on an atomic level. Each Prisoner knows the rules of the games and can calculate its gains by comparing its behavior with its neighbors'. It then can decide whether to change its behavior or not.

The class implements the Pawn interface, so that the Prisoners can fit on a GameBoard. The GameBoard knows how it wants the Prisoner to draw itself. The Prisoner chooses dynamically a subclass of ShapeDrawer to fit the request of the board.

Version:
1.00, 22 August 2001
Author:
François Suter
See Also:
com.monpetitcoin.java.GameBoard, com.monpetitcoin.java.Pawn, prisoner.shapes.ShapeDrawer

Variable Index

 o board
 o DEFAULT_INCENTIVE
 o DEFAULT_THRESHOLD
 o gain
 o game
 o id
 o idCounter
 o incentive
 o maxNeighbors
 o neighbors
 o NO_STATE
 o oldState
 o renderer
 o state
= 1 for honest behavior
= 0 for cheating
 o threshold

Constructor Index

 o Prisoner(PrisonerGame, GameBoard)
 o Prisoner(PrisonerGame, GameBoard, double)
 o Prisoner(PrisonerGame, GameBoard, double, double)
 o Prisoner(PrisonerGame, GameBoard, double, double, int)
 o Prisoner(PrisonerGame, GameBoard, double, double, int, double)
This is the most complete constructor.

Method Index

 o calculateGain()
This methods is where the Prisoner deals with all its neighbors.
 o draw(Graphics, int, int, int, int)
 o drawConclusion()
This method is where the Prisoner decides whether to change its behavior or not.
 o getGain()
 o getID()
 o getIncentive()
 o getOldState()
 o getState()
 o getThreshold()
 o setNeighbors(Pawn[])

Variables

 o DEFAULT_INCENTIVE
 private static final double DEFAULT_INCENTIVE
 o DEFAULT_THRESHOLD
 private static final double DEFAULT_THRESHOLD
 o NO_STATE
 private static final int NO_STATE
 o idCounter
 private static int idCounter
 o state
 private int state
= 1 for honest behavior
= 0 for cheating

 o oldState
 private int oldState
 o id
 private int id
 o gain
 private double gain
 o incentive
 private double incentive
 o threshold
 private double threshold
 o board
 private com.monpetitcoin.java.PrisonerGame.GameBoard board
 o game
 private com.monpetitcoin.java.PrisonerGame.PrisonerGame game
 o neighbors
 private com.monpetitcoin.java.PrisonerGame.Prisoner neighbors[]
 o maxNeighbors
 private int maxNeighbors
 o renderer
 private prisoner.shapes.ShapeDrawer renderer

Constructors

 o Prisoner
 public Prisoner(com.monpetitcoin.java.PrisonerGame.PrisonerGame game,
                 com.monpetitcoin.java.PrisonerGame.GameBoard board)
 o Prisoner
 public Prisoner(com.monpetitcoin.java.PrisonerGame.PrisonerGame game,
                 com.monpetitcoin.java.PrisonerGame.GameBoard board,
                 double incentive)
 o Prisoner
 public Prisoner(com.monpetitcoin.java.PrisonerGame.PrisonerGame game,
                 com.monpetitcoin.java.PrisonerGame.GameBoard board,
                 double incentive,
                 double threshold)
 o Prisoner
 public Prisoner(com.monpetitcoin.java.PrisonerGame.PrisonerGame game,
                 com.monpetitcoin.java.PrisonerGame.GameBoard board,
                 double incentive,
                 double threshold,
                 int state)
 o Prisoner
 public Prisoner(com.monpetitcoin.java.PrisonerGame.PrisonerGame game,
                 com.monpetitcoin.java.PrisonerGame.GameBoard board,
                 double incentive,
                 double threshold,
                 int state,
                 double gain)
This is the most complete constructor. All other constructors refer it.

Parameters:
game - callback reference to the currently running game
board - callback reference to the board containing the Prisoner
incentive - variable gain when a dishonest Prisoner cheats an honest one
threshold - excess gain necessary for a Prisoner to change its behavior
state - initial state of the Prisoner (=1 honest, =0 dishonest)
gain - initial gain of the Prisoner

Methods

 o getState
 public int getState()
 o getOldState
 public int getOldState()
 o getGain
 public double getGain()
 o getIncentive
 public double getIncentive()
 o getThreshold
 public double getThreshold()
 o draw
 public void draw(java.awt.Graphics g,
                  int x,
                  int y,
                  int width,
                  int height)
 o getID
 public int getID()
 o setNeighbors
 public void setNeighbors(com.monpetitcoin.java.PrisonerGame.Pawn theNeighbors[])
 o calculateGain
 public void calculateGain()
This methods is where the Prisoner deals with all its neighbors. The Prisoner loops over its neighbors array and calculates its gain depending on its and its neighbor's behaviors.

At this point, it is important to preserve the state of the Prisoner, by storing it in the oldState member variable. Indeed, the game first asks all Prisoners to calculate their gain. Then each Prisoner in turn must decide whether to change its behavior or not. This decision is based on the neighbors' gains and behaviors. But since Prisoners are called one after the other, when Prisoner n must draw its conclusion, Prisoners 1 to n-1 have already drawn theirs and changed their state. Thus the comparison must be made with the previous game turn's state.

See Also:
drawConclusion
 o drawConclusion
 public void drawConclusion()
This method is where the Prisoner decides whether to change its behavior or not. This is achieved by comparing its gain and behavior with its neighbors' gains and behaviors (the behavior being the one from the previous game turn, as explained in calculateGain()).

The actual algorithm for decision calculates the average gain among all the honest neighbors and all the dishonest neighbors. A decision to change is then taken on two conditions:

  1. the other party (the one with the opposite behavior of the Prisoner) has gained significantly more than the Prisoner (i.e. the difference is higher than the threshold);
  2. the same party has gained less than the opposing party (note that the Prisoner itself isn't counted in the average of its own party)

This means that even if the opposing party has gained more than itself, the Prisoner will not change its behavior if its party has gained even more than the opposing party. In a sense, the Prisoner is reassured in its behavior by seeing its party come on top.

See Also:
calculateGain

All Packages  Class Hierarchy  This Package  Previous  Next  Index