Courses/CS 461/Winter 2006/Justin Wu/Week 5
From CSWiki
< Courses | CS 461 | Winter 2006 | Justin Wu
Not quite done...
/**
* Iterated Prisoner's Dilemma
* Agent model
*/
public class Prisoner
{
/**
* Member variable declarations
*/
private int strategy = -1; // Strategies ( as listed from 0-16 in http://www.cs.nyu.edu/courses/fall01/G22.3033-002/http/hw1.html )
private int score = 0;
private int[] my_history = new int[16];
private int[] opponents_history = new int[16];
private boolean active = false;
private int noise = 0;
private int games_played = 0;
private int wins = 0;
private int total_points = 0;
ResponseTable rt;
/**
* Class constructor
*/
public Prisoner ( int strategy, int noise )
{
this.strategy = strategy;
this.noise = noise;
rt = new ResponseTable ( strategy, noise );
}
/**
* Accessors
*/
public int getStrategy()
{
return strategy;
}
public int getScore()
{
return score;
}
public int getMove( Prisoner opponent )
{
logMove( rt.getMove( my_history[ opponent.getStrategy() ] , opponents_history[ opponent.getStrategy() ] ), opponent.getStrategy() );
return rt.getMove( my_history[ opponent.getStrategy() ] , opponents_history[ opponent.getStrategy() ] );
}
public int getOpponentsLastMove( Prisoner opponent )
{
return opponents_history[ opponent.getStrategy() ];
}
public boolean isActive()
{
return active;
}
public int getGamesPlayed()
{
return games_played;
}
public int getWins()
{
return wins;
}
public ResponseTable getResponseTable()
{
return rt;
}
public double getPercentage()
{
return ( double ) ( score / total_points );
}
/**
* Manipulators
*/
public void setNoise ( int noise )
{
this.noise = noise;
}
public void setActive ( boolean active )
{
this.active = active;
}
public void win ()
{
wins++;
}
public void gamePlayed ()
{
games_played++;
}
public void logMove( int move, int index )
{
my_history[ index ] = move;
}
public void logOpponentsMove( int move, int index )
{
opponents_history[ index ] = move;
}
public void setTotal_Points( int total_points )
{
this.total_points = total_points;
}
}
/**
* Payoff model
*/
class Payoff
{
private int r = 3; // both cooperate
private int t = 5; // you defect, opponent cooperates
private int p = 1; // both defect
private int s = 0; // you cooperate, opponent defects
/**
* Class constructor
*/
public Payoff ( int r, int t, int p, int s )
{
this.r = r;
this.t = t;
this.p = p;
this.s = s;
}
/**
* Accessors
*/
public int getR()
{
return r;
}
public int getT()
{
return t;
}
public int getP()
{
return p;
}
public int getS()
{
return s;
}
/**
* Manipulator
*/
public PayOff ( int r, int t, int p, int s )
{
this.r = r;
this.t = t;
this.p = p;
this.s = s;
}
}
/**
* Response model
*/

