Russells Final Project
<< Matthew's Final Project | OldProjectsTrailIndex | Don and Nick's Violin Hero >>
Click here for a working demonstration
PrisonerGame.java
import java.util.Scanner; public class PrisonerGame { public static void main(String[] args) { boolean done = false; String input = ""; Prisoner prisoner1 = null; Prisoner prisoner2 = null; int iterations = 0; Scanner in = new Scanner(System.in); System.out.println("Hello! And Welcome to the Prisoner's Dilemma!"); System.out.println("You and a friend are in separate interrogation rooms, and are suspected of a crime."); System.out.println("You have two options: confess or keep your mouth shut."); System.out.println("Here's the catch: confess, etc."); while (!done) { System.out.print("Please Enter the Type of Prisoner 1 (Human, Jesus, Lucifer, Massive Strike, Tit for Tat, Random, Linear Regression): "); input = in.nextLine(); if (input.equalsIgnoreCase("Human")) { System.out.print("Please Enter the Name of Prisoner 1: "); String humanName = in.next(); prisoner1 = new Human(humanName); } else if (input.equalsIgnoreCase("Jesus")) { prisoner1 = new Jesus(); } else if (input.equalsIgnoreCase("Lucifer")) { prisoner1 = new Lucifer(); } else if (input.equalsIgnoreCase("Massive Strike")) { prisoner1 = new MassiveStrike(); } else if (input.equalsIgnoreCase("Tit for Tat")) { prisoner1 = new TitForTat(); } else if (input.equalsIgnoreCase("Random")) { prisoner1 = new RandomPrisoner(); } else if (input.equalsIgnoreCase("Linear Regression")) { prisoner1 = new LinRegStrategy(); } System.out.print("Please Enter the Type of Prisoner 2 (Human, Jesus, Lucifer, Massive Strike, Tit for Tat, Random, Linear Regression): "); input = in.nextLine(); if (input.equalsIgnoreCase("Human")) { String inputName = ""; System.out.print("Please Enter the Name of Prisoner 2: "); prisoner2 = new Human(inputName); } else if (input.equalsIgnoreCase("Jesus")) { prisoner2 = new Jesus(); } else if (input.equalsIgnoreCase("Lucifer")) { prisoner2 = new Lucifer(); } else if (input.equalsIgnoreCase("Massive Strike")) { prisoner2 = new MassiveStrike(); } else if (input.equalsIgnoreCase("Tit for Tat")) { prisoner2 = new TitForTat(); } else if (input.equalsIgnoreCase("Random")) { prisoner2 = new RandomPrisoner(); } else if (input.equalsIgnoreCase("Linear Regression")) { prisoner2 = new LinRegStrategy(); } System.out.print("Please Enter the Number of iterations: "); iterations = in.nextInt(); Judge a = new Judge(prisoner1, prisoner2); a.iterate(iterations); System.out.print(prisoner1.getName()+" (Prisoner 1): "); System.out.println(a.getScore1()); System.out.print(prisoner2.getName()+" (Prisoner 2): "); System.out.println(a.getScore2()); if (a.getScore1() > a.getScore2()) System.out.println(prisoner1.getName()+" Wins!!!"); if (a.getScore1() < a.getScore2()) System.out.println(prisoner2.getName()+" Wins!!!"); if (a.getScore1() == a.getScore2()) System.out.println("It's a Tie!!!"); System.out.print("Would you like to play again? (Y/N): "); if (in.next().equalsIgnoreCase("N")) done = true; } System.out.println("Thank You for Playing!"); } }
- Judge.java
public class Judge { private Prisoner p1; private Prisoner p2; private double score1, score2; private double reward; private double punishment; private double temptation; private double suckerReward; public Judge(Prisoner p1, Prisoner p2) { setP1(p1); setP2(p2); this.setReward(0.5); this.setPunishment(5.0); this.setTemptation(0.0); this.setSuckerReward(10.0); this.setScore1(0); this.setScore2(0); } public void judge() { boolean choice1 = p1.choose(); boolean choice2 = p2.choose(); if (choice1 == choice2) { if (choice1 == Prisoner.BETRAY) { p1.record(choice2); p2.record(choice1); score1 = score1 + punishment; score2 = score2 + punishment; } else { p1.record(choice2); p2.record(choice1); score1 = score1 + reward; score2 = score2 + reward; } } if (choice1 != choice2) { if (choice1 == Prisoner.BETRAY) { p1.record(choice2); p2.record(choice1); score1 = score1 + suckerReward; score2 = score2 + temptation; } else { p1.record(choice2); p2.record(choice1); score1 = score1 + suckerReward; score2 = score2 + temptation; } } } public String iterate(int times) { String result = ""; p1.reset(); p2.reset(); this.setScore1(0); this.setScore2(0); for (int i = 0; i < times; i++) judge(); if (score1 > score2) { result = p1.getName() + ": " + score1; result = result + " " + p2.getName() + ": " + score2; } else { result = p2.getName() + ": " + score2; result = result + " " + p1.getName() + ": " + score1; } return result; } public void setP1(Prisoner p1) { this.p1 = p1; } public Prisoner getP1() { return p1; } public void setP2(Prisoner p2) { this.p2 = p2; } public Prisoner getP2() { return p2; } public void setReward(double reward) { this.reward = reward; } public double getReward() { return reward; } public void setPunishment(double punishment) { this.punishment = punishment; } public double getPunishment() { return punishment; } public void setSuckerReward(double suckerReward) { this.suckerReward = suckerReward; } public double getSuckerReward() { return suckerReward; } public void setTemptation(double temptation) { this.temptation = temptation; } public double getTemptation() { return temptation; } public void setScore2(double score2) { this.score2 = score2; } public double getScore2() { return score2; } public void setScore1(double score1) { this.score1 = score1; } public double getScore1() { return score1; } }
- Prisoner.java
import java.util.ArrayList; public abstract class Prisoner { /** * Constants */ public static final boolean BETRAY=false; public static final boolean DEFECT=false; public static final boolean SILENT=true; public static final boolean COOP=true; /** * instance variables */ private String name; private ArrayList<Boolean> history; /** * Constructors */ public Prisoner(String name){ this.setName(name); this.history = new ArrayList<Boolean>(); } public Prisoner(){ this("No Name"); } /** * Abstract methods that subclasses need to implement */ public abstract boolean choose(); /** * Methods that will be inherited by all subclasses */ public void record(boolean opponentsChoice){ history.add(opponentsChoice); } public void reset(){ history=new ArrayList<Boolean>(); } public void setName(String name) { this.name = name; } public String getName() { return name; } public ArrayList<Boolean> getHistory() { return history; } public boolean lastTurn(){ if (history.size()==0) return Prisoner.COOP; return history.get(history.size()-1); } public boolean firstTurn(){ return history.size()==0; } }
- LinRegStrategy.java
import java.util.ArrayList; import java.util.Random; public class LinRegStrategy extends Prisoner { private Random generator; private double slope; private int numTrue; private int numFalse; private ArrayList<Integer> x; private ArrayList<Integer> y; public LinRegStrategy() { super("Linear Regression Forecasting"); generator = new Random(); slope = 0; numTrue = 0; numFalse = 0; x = new ArrayList<Integer>(); y = new ArrayList<Integer>(); } public boolean choose() { boolean result = false; if (getHistory().size() == 0) return false; updateLine(); linearRegression(); if (slope == 1) result = generator.nextBoolean(); if (slope > 1) { result = true; } if (slope < 1) { result = false; } return result; } public void linearRegression() { double sumx = 0; double sumy = 0; double sumx2 = 0; for (int i = 0; i < x.size(); i++) { sumx += x.get(i); sumx2 += x.get(i) * x.get(i); sumy += y.get(i); } double xbar = sumx / x.size(); double ybar = sumy / y.size(); double xxbar = 0; double yybar = 0; double xybar = 0; for (int i = 0; i < x.size(); i++) { xxbar += (x.get(i) - xbar) * (x.get(i) - xbar); yybar += (y.get(i) - ybar) * (y.get(i) - ybar); xybar += (x.get(i) - xbar) * (y.get(i) - ybar); } slope = xybar / xxbar; } public void updateLine() { if (getHistory().size() == 0) return; if (getHistory().size() > 0) { boolean next = getHistory().get(getHistory().size()-1); if (next == true) numTrue++; else numFalse++; } x.add(numTrue); y.add(numFalse); } }
- Human.java
import java.util.Scanner; public class Human extends Prisoner { private Scanner in = new Scanner(System.in); public Human(String Name) { super(Name); } public Human() { super("Unknown"); } public boolean choose() { boolean result = false; String input = ""; for (boolean a : getHistory()) { if (a == false) System.out.println("Betrayal "); else System.out.println("Cooperate "); } System.out.print("What would you like to do? (Betray or Cooperate): "); input = in.next(); if (input.equalsIgnoreCase("Betray")) result = Prisoner.BETRAY; else if (input.equalsIgnoreCase("Cooperate")) result = Prisoner.COOP; return result; } }
- RandomPrisoner.java
import java.util.Random; public class RandomPrisoner extends Prisoner { private Random generator; public RandomPrisoner() { super("Random"); generator = new Random(); } public boolean choose() { return generator.nextBoolean(); } }
- MassiveStrike.java
public class MassiveStrike extends Prisoner { public MassiveStrike() { super("Massive Strike"); } public boolean choose() { if (firstTurn()) return Prisoner.COOP; for (boolean a : getHistory()) if (a == false) return Prisoner.BETRAY; return Prisoner.COOP; } }
- TitForTat.java
public class TitForTat extends Prisoner { public TitForTat() { super("Tit for Tat"); } public boolean choose() { if (firstTurn()) return Prisoner.COOP; if (this.lastTurn() == Prisoner.BETRAY) return Prisoner.BETRAY; return Prisoner.COOP; } }
- Jesus.java
public class Jesus extends Prisoner { public Jesus() { super("Jesus"); } public boolean choose() { return Prisoner.COOP; } }
- Lucifer.java
public class Lucifer extends Prisoner { public Lucifer() { super("Lucifer"); } public boolean choose() { return Prisoner.BETRAY; } }