All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class com.monpetitcoin.java.PrisonerGame.PrisonerGame

java.lang.Object
   |
   +----java.awt.Component
           |
           +----java.awt.Container
                   |
                   +----java.awt.Panel
                           |
                           +----com.monpetitcoin.java.PrisonerGame.PrisonerGame

public class PrisonerGame
extends java.awt.Panel
implements java.awt.event.ActionListener, java.lang.Runnable
This class is the core of the Prisoner's Dilemma game application. It instantiates the game boards and all its cells and runs the game. It also manages all the options. Several inner classes are used for elements of the UI.

Version:
1.00, 14 August 2001
Author:
François Suter

Class Index

 o PrisonerGame.GameController
This inner class is used to display the game parameters:

This is all done in the paint() method.

 o PrisonerGame.OptionsDialog
This inner class displays a dialog box with the options of a Prisoner's Dilemma game that can be set by the user, i.e.

Variable Index

 o boardConstraints
 o buttonsWidth
 o cheatColor
 o controller
 o controllerHeight
 o DEFAULT_BOARDSIZE
 o DEFAULT_INCENTIVE
 o DEFAULT_REFRESH_FREQUENCY
 o DEFAULT_THRESHOLD
 o gameSize
 o gameStopped
 o gridbag
 o helpButton
 o honestColor
 o lightGray
 o optionsButton
 o refreshFrequency
 o restartButton
 o startButton
 o stepButton
 o theBoard
 o theDialog
 o theIncentive
 o theThread
 o theThreshold
 o turnCounter

Constructor Index

 o PrisonerGame(int, int)
 o PrisonerGame(int, int, double, double)
 o PrisonerGame(int, int, int, int)
 o PrisonerGame(int, int, int, int, double, double)

Method Index

 o actionPerformed(ActionEvent)
 o gameTurn()
This method performs a single game turn of the Prisoner's Dilemma.
 o getMinimumSize()
 o getPreferredSize()
 o isStopped()
This method returns the value of the gameStopped flag.
 o restartGame(int, int, double, double)
This method creates a new game with the parameters passed to it.
 o run()
 o setControls()
This method complements the constructors, by setting up all the necessary controls.
 o setRefreshFrequency(int)
This methods set the refresh frequency to a new value.
 o startGame()
This methods gets the Prisoner's Dilemma game running.
 o stopGame()
This method stops the game by setting the gameStopped flag to true.

Variables

 o DEFAULT_INCENTIVE
 public static final double DEFAULT_INCENTIVE
 o DEFAULT_THRESHOLD
 public static final double DEFAULT_THRESHOLD
 o DEFAULT_BOARDSIZE
 public static final int DEFAULT_BOARDSIZE
 o DEFAULT_REFRESH_FREQUENCY
 public static final int DEFAULT_REFRESH_FREQUENCY
 o theBoard
 private com.monpetitcoin.java.PrisonerGame.Square2DBoard theBoard
 o theThreshold
 private double theThreshold
 o theIncentive
 private double theIncentive
 o gameSize
 private java.awt.Dimension gameSize
 o gameStopped
 private boolean gameStopped
 o turnCounter
 private int turnCounter
 o refreshFrequency
 private int refreshFrequency
 o startButton
 private java.awt.Button startButton
 o stepButton
 private java.awt.Button stepButton
 o restartButton
 private java.awt.Button restartButton
 o optionsButton
 private java.awt.Button optionsButton
 o helpButton
 private java.awt.Button helpButton
 o controller
 private com.monpetitcoin.java.PrisonerGame.PrisonerGame.GameController controller
 o controllerHeight
 private int controllerHeight
 o buttonsWidth
 private int buttonsWidth
 o theDialog
 private com.monpetitcoin.java.PrisonerGame.PrisonerGame.OptionsDialog theDialog
 o lightGray
 private java.awt.Color lightGray
 o honestColor
 protected java.awt.Color honestColor
 o cheatColor
 protected java.awt.Color cheatColor
 o boardConstraints
 private java.awt.GridBagConstraints boardConstraints
 o gridbag
 private java.awt.GridBagLayout gridbag
 o theThread
 private java.lang.Thread theThread

Constructors

 o PrisonerGame
 public PrisonerGame(int width,
                     int height)
 o PrisonerGame
 public PrisonerGame(int width,
                     int height,
                     int columns,
                     int rows)
 o PrisonerGame
 public PrisonerGame(int width,
                     int height,
                     double incentive,
                     double threshold)
 o PrisonerGame
 public PrisonerGame(int width,
                     int height,
                     int columns,
                     int rows,
                     double incentive,
                     double threshold)

Methods

 o setControls
 private void setControls()
This method complements the constructors, by setting up all the necessary controls. Two gridbag layouts are used: one to contain the board, the game controller and the container that holds all the buttons. The latter has its own gridbag layout.

See Also:
com.monpetitcoin.java.PrisonerGame.PrisonerGame.GameController
 o startGame
 private void startGame()
This methods gets the Prisoner's Dilemma game running. It does so by setting the flag gameStopped to false and by starting a new thread with the PrisonerGame object itself as a parameter.

See Also:
stopGame
 o stopGame
 private synchronized void stopGame()
This method stops the game by setting the gameStopped flag to true. This causes to run() method to return, thereby killing the thread in which the game executes.

See Also:
startGame
 o restartGame
 private void restartGame(int newColumns,
                          int newRows,
                          double newIncentive,
                          double newThreshold)
This method creates a new game with the parameters passed to it. The current board is destroyed and a new one created, with a new population of prisoner, which means a new random distribution (even if the parameters haven't changed, as is the case when a user presses the "Restart" button.

See Also:
actionPerformed
 o gameTurn
 private void gameTurn()
This method performs a single game turn of the Prisoner's Dilemma. Using the GameBoard's enumeration capabilities, each prisoner is asked to bartner with its neighbors and then to evaluate its results. Since the board actually stores Pawns, it is necessary to cast them to Prisoners.

See Also:
com.monpetitcoin.java.GameBoard, com.monpetitcoin.java.Pawn, com.monpetitcoin.java.Prisoner
 o setRefreshFrequency
 public synchronized void setRefreshFrequency(int newFrequency)
This methods set the refresh frequency to a new value.

 o isStopped
 private synchronized boolean isStopped()
This method returns the value of the gameStopped flag. It is synchronized so as not to have a race condition with the run() method.

 o getPreferredSize
 public java.awt.Dimension getPreferredSize()
Overrides:
getPreferredSize in class java.awt.Container
 o getMinimumSize
 public java.awt.Dimension getMinimumSize()
Overrides:
getMinimumSize in class java.awt.Container
 o actionPerformed
 public void actionPerformed(java.awt.event.ActionEvent e)
 o run
 public void run()

All Packages  Class Hierarchy  This Package  Previous  Next  Index