package edu.marlboro.cs.prisoner; /** *
Implement one round of the iterated prisoner's dilemma. *
*The two possible strategies are COOPERATE and DEFECT. *
*The four possible outcomes (following the * definitions from the wikipedia's Prisononer's Dilemma article * as of Feb 2007) are (T, R, P, S) with the properites. *
** (1) T > R > P > S * (2) 2 R > T + S for cooperation to be optimal in repeated play ** * @see Wikipedia: Prisoner's Dilemma * @author Jim Mahoney * @version $Id: Game.java 12775 2007-04-17 15:23:12Z mahoney $ */ public class Game { /** * The first of the two possible strategies. */ public static int COOPERATE = 0; /** * The second of the two possible strategies. */ public static int DEFECT = 1; /** * Temptation to defect = 5 */ public static int T = 5; /** * Reward for mutual cooperation = 3 */ public static int R = 3; /** * Punishment for mutual defection = 1 */ public static int P = 1; /** * Sucker's payoff = 0 */ public static int S = 0; /** * payoff for player A is payoffMatrix[A's move][B's move] * = {{R,S},{T,P}} */ public static int[][] payoffMatrix = {{R, S}, {T, P}}; /** * playerA and playerB go at it in a prisoner's dilemma game, * and each of their internal scores is updated appropriately. */ public static void play(Player playerA, Player playerB){ int strategyA = playerA.getNextMove(playerB.getId()); int strategyB = playerB.getNextMove(playerA.getId()); int payoffA = payoffMatrix[strategyA][strategyB]; playerA.setScore(playerA.getScore() + payoffA); int payoffB = payoffMatrix[strategyB][strategyA]; playerB.setScore(playerB.getScore() + payoffB); } public static int getPayoff(int A_strategy, int B_strategy){ return payoffMatrix[A_strategy][B_strategy]; } }