/*----------------------------------------------------------------------+
|      Title:	Match.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 Match {

  private int payoff[][];
  private Player P[];  // array of players
  private Player Q[];  // array of quitters
  private int nQ;      // number of quitters


  public void simulate(Player PIn[], int lengthOfMatch, int payoffIn[][]) {
    P = PIn;
    payoff = payoffIn;
    // Put all the players in the quitters array
    nQ = 0;
    Q = new Player[P.length];
    for (int p=0; p<P.length; ++p)
      Q[nQ++] = P[p];
    // Clear the scores
    for (int p=0; p<P.length; ++p)
      P[p].score = 0;
    // now do all the plays
    for (int r=0; r<lengthOfMatch; ++r) {
      matchQuitters();
      // let each player have a turn
      for (int i=0; i<P.length; ++i)
        if (P[i].partner != -1 && i < P[i].partner)
          play(P[i],P[P[i].partner]);
    }
  }

  private void matchQuitters() {
    //Simply shuffle them
    for (int i=0; i<nQ-1; ++i) {
      int j = i + (int)((nQ-i)*Math.random());
      Player Temp = Q[i];
      Q[i] = Q[j];
      Q[j] = Temp;
    }
    // now adjacent pairs will pair off, starting at the end
    while (nQ>=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;
  }

} // Match.java

