/*----------------------------------------------------------------------+ | Title: Battle.java | | | | Author: David E. Joyce | | Department of Mathematics and Computer Science | | Clark University | | Worcester, MA 01610-1477 | | U.S.A. | | | | http://aleph0.clarku.edu/~djoyce/ | | djoyce@clarku.edu | | | | Date: May, 2002. | +----------------------------------------------------------------------*/ public class Battle { private Strategy S[]; private int numberOfMatches, lengthOfMatch; private int payoff[][] = new int[2][2]; public Battle (Strategy[] SIn) { S = SIn; } private Player P[]; // array of players private Player Q[]; // array of quitters private int nQ; // number of quitters public void doSimulation (int nP, int numberOfMatchesIn, int lengthOfMatchIn, int payoffIn[][], TimeGraph statGraph) { numberOfMatches = numberOfMatchesIn; lengthOfMatch = lengthOfMatchIn; for (int i=0; i<=1; ++i) for (int j=0; j<=1; ++j) payoff[i][j] = payoffIn[i][j]; for (int s=0; s=2) { --nQ; Q[nQ].partner = Q[nQ-1].id; Q[nQ-1].partner = Q[nQ].id; Q[nQ].movenumber = 0; Q[nQ-1].movenumber = 0; --nQ; } } private void play(Player A, Player B) { if (A.movenumber == 0) { A.thisMove = A.S.firstMove(); B.thisMove = B.S.firstMove(); } else if (A.movenumber == 1) { A.thisMove = A.S.secondMove(A.previousMove,B.previousMove,A.score); B.thisMove = B.S.secondMove(B.previousMove,A.previousMove,B.score); } else { A.thisMove = A.S.thirdMove(A.lastMove,B.lastMove, A.previousMove,B.previousMove,A.score); B.thisMove = B.S.thirdMove(B.lastMove,A.lastMove, B.previousMove,A.previousMove,B.score); } A.score += payoff[A.thisMove][B.thisMove]; B.score += payoff[B.thisMove][A.thisMove]; if (A.S.leave(A.thisMove,B.thisMove,A.score) || B.S.leave(B.thisMove,A.thisMove,B.score)) { A.partner = -1; B.partner = -1; Q[nQ++] = A; Q[nQ++] = B; } else { A.previousMove = A.lastMove; B.previousMove = B.lastMove; A.lastMove = A.thisMove; B.lastMove = B.thisMove; ++A.movenumber; ++B.movenumber; } return; } private void reassignPlayers() { // compute total scores for the strategies int total[] = new int[S.length]; for (int p=0; pf; i-=total[--s]); //no body for loop if (s==S.length) // error System.out.println ("*** p="+p+" f = "+f+" s = "+s); S[s].pop++; P[p++].S = S[s]; } } } // Battle.java