/*----------------------------------------------------------------------+
|      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

  public void doSimulation (int nP, int numberOfMatchesIn,
        int lengthOfMatchIn, int payoffIn[][], TimeGraph statGraph) {
    Match match = new Match();
    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<S.length; ++s)
      S[s].reset();
    // initialize Player array P and
    P = new Player[nP];
    for (int p=0; p<nP; ++p)
      P[p] = new Player(p,S[p*S.length/nP]);
    // now do the simulation
    for (int m=0; m<numberOfMatches; ++m) {
      updateData(statGraph);
      match.simulate(P,lengthOfMatchIn, payoffIn);
      reassignPlayers();
    }
  }

  private void updateData (TimeGraph statGraph) {
    int data[] = new int[S.length];
    for (int s=0; s<S.length; ++s)
      data[s] = S[s].pop;
    statGraph.newData(data);
    //statGraph.repaint();
  }


  private void reassignPlayers() {
      // compute total scores for the strategies
      int total[] = new int[S.length];
      for (int p=0; p<P.length; ++p) {
        int s=P[p].S.strategyNumber;
        total[s] += P[p].score;
      }
      // compute the grand total
      int grandTotal = 0;
      for (int s=0; s<S.length; ++s) {
        grandTotal += total[s];
      }
      // determine how many players there should be for
      // each strategy and assign the strategies
      int p = 0;
      for (int s=0; s<S.length; ++s) {
        S[s].pop = total[s]*P.length/grandTotal;
        for (int j=0; j<S[s].pop; ++j)
          P[p++].S = S[s];
      }
      // Because of truncation, maybe not all players
      // have been assigned a strategy
      // Choose the remaining strategies randomly
      // weighted by the totals
      while (p<P.length) {
        int f = (int)(grandTotal*Math.random());
        int s = S.length;
        for (int i=grandTotal; i>f; 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

