Retrieved from the old wiki:
The Prisoner’s Dilemma source code has been released, a simple AWT based and multithreaded Java applet.
/**
The Prisoner's Dilemma
@version 0.20
@author Serge Helfrich 03-Aug-2002 (0.01 11-Feb-1998)
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Pris00 extends Applet
{ double b = 1.85;
double p = 0.1;
int delay = 100; // increased for green thread support
boolean paused;
PrisCanv cv;
PrisPan pn;
public void init()
{ paused = false;
Color bg = new Color(255,255,192);
setBackground(Color.black);
setLayout(new BorderLayout());
cv = new PrisCanv(this);
add("Center", cv);
pn = new PrisPan(this);
add("North", pn);
}
public void stop()
{ cv.stop();
cv.thr = null;
remove(cv);
remove(pn);
}
}
class PrisPan extends Panel
implements ActionListener, ItemListener
{ Pris00 ap;
TextField tfb, tfp, tfd;
Button bR,bB;
Checkbox cb;
Label lb;
public PrisPan(Pris00 ap)
{ this.ap=ap;
Color bg = new Color(255,255,192);
setBackground(bg);
Label lbb = new Label("b =",Label.RIGHT);
Label lbp = new Label("p =",Label.RIGHT);
Label lbd = new Label("delay:",Label.RIGHT);
tfb = new TextField(5);
tfp = new TextField(5);
tfd = new TextField(5);
tfb.setText(String.valueOf(ap.b));
tfp.setText(String.valueOf(ap.p));
tfd.setText(String.valueOf(ap.delay));
lb = new Label(" ");
bR = new Button("Run");
bR.addActionListener(this);
bB = new Button("Break");
bB.addActionListener(this);
bB.setEnabled(false);
cb = new Checkbox("pause");
cb.addItemListener(this);
cb.setEnabled(false);
add(lbb);
add(tfb);
add(lbp);
add(tfp);
add(lbd);
add(tfd);
add(lb);
add(bR);
add(bB);
add(cb);
}
public void actionPerformed(ActionEvent evt)
{ String tb = tfb.getText();
String tp = tfp.getText();
String td = tfd.getText();
ap.b = Double.valueOf(tb).floatValue();
ap.p = Double.valueOf(tp).floatValue();
ap.delay = Integer.valueOf(td).intValue();
if (evt.getSource() == bR)
{
if (ap.cv.thr == null)
{ ap.cv.start();
tfb.setEnabled(false);
tfp.setEnabled(false);
tfd.setEnabled(false);
bR.setEnabled(false);
bB.setEnabled(true);
cb.setState(false);
ap.paused = false;
cb.setEnabled(true);
}
}
if (evt.getSource() == bB)
{
if (ap.cv.thr != null)
{ ap.cv.stop();
ap.cv.repaint();
tfb.setEnabled(true);
tfp.setEnabled(true);
tfd.setEnabled(true);
bR.setEnabled(true);
bB.setEnabled(false);
cb.setState(false); // -> NA
ap.paused = false;
cb.setEnabled(false);
}
}
}
public void itemStateChanged(ItemEvent evt)
{ ap.paused = cb.getState();
}
}
class PrisCanv extends Canvas
implements Runnable
{ Pris00 ap;
Thread thr = null;
double p;
int n;
int[][] s;
int[][]sn;
int[] bc;
double[][] pm;
double[][] payoff;
double b;
Color c[][];
int ssn[][];
double h;
Image im;
Graphics gIm;
Dimension d;
int x,y;
boolean painted;
PrisCanv(Pris00 ap)
{ Color bg = new Color(255,255,192);
setBackground(bg);
this.ap = ap;
}
void start()
{
if (thr == null)
{ thr = new Thread(this);
thr.start();
}
}
void stop()
{
if (thr != null)
{ thr.stop();
thr = null;
}
}
public void run()
{ initialize();
for (;;)
{ if (!ap.paused)
{
calculate();
if (im == null)
{ im = createImage(5*n,5*n);
gIm = im.getGraphics();
}
{ for (int i=1; i<=n; i++)
{
for (int j=1; j<=n; j++)
{
gIm.setColor(c[sn[i][j]][s[i][j]]);
gIm.fillRect(j*5,i*5,5,5);
s[i][j] = sn[i][j];
}
}
}
painted = false;
repaint();
try {Thread.sleep(ap.delay);}
catch(InterruptedException e){}
while (!painted) {;}
}
}
}
void initialize()
{ b = ap.b;
p = ap.p;
n=60;
int bc[] = new int[n+1];
double pm[][] = new double[3][3];
pm[1][1]=1;
pm[1][2]=0;
pm[2][1]=b;
pm[2][2]=0;
this.pm=pm;
Color c[][] = new Color[3][3];
c[1][1] = new Color(0,0,255); // blue
c[2][2] = new Color(255,0,0); // red
c[1][2] = new Color(0,255,0); // green
c[2][1] = new Color(255,255,0); // yellow
this.c=c;
bc = setBoundary(n);
this.bc=bc;
s=generate(n,p);
sn=new int[n+1][n+1];
this.s=s;
this.sn=sn;
payoff = new double[61][61];
d = getSize();
this.d = d;
x = (int)(d.width/2 - 5*n / 2);
y = (int)(d.height/2 - 5*n / 2);
painted = false;
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{ if (thr == null)
{ if (d != null)
{ g.clearRect(0, 0, d.width, d.height);
}
return;
}
painted = g.drawImage(im, x, y, null);
}
int[][] generate(int n, double p)
{ int s[][] = new int[n+1][n+1];
for (int i=1; i<=n; i++)
{
for (int j=1; j<=n; j++)
{
s[i][j] = 1;
if (Math.random()<p) s[i][j] = 2;
}
}
return s;
}
int[] setBoundary(int n)
{ int bc[] = new int[n+2];
for (int i=1; i<=n; i++)
{ bc[i]=i;
}
bc[0]=n;
bc[n+1]=1;
return bc;
}
void calculate()
{ int i,j,k,l;
double pa,hp;
for (i=1; i<=n; i++)
{ for (j=1; j<=n; j++)
{ pa=0;
for (k=-1; k<=1; k++)
{ for (l=-1; l<=1; l++)
{ pa += pm[s[i][j]][s[bc[i+k]][bc[j+l]]];
}
}
payoff[i][j] = pa;
}
}
for (i=1; i<=n; i++)
{
for (j=1; j<=n; j++)
{ hp = payoff[i][j];
sn[i][j] = s[i][j];
for (k=-1; k<=1; k++)
{
for (l=-1; l<=1; l++)
{
if (payoff[bc[i+k]][bc[j+l]] > hp)
{ hp = payoff[bc[i+k]][bc[j+l]];
sn[i][j] = s[bc[i+k]][bc[j+l]];
}
}
}
}
}
}
}
Comments and meaningful variable names added by Vicki Allan: http://digital.cs.usu.edu/~allanv/cs7100/PrisonersApplet.java