public class War
{
  public static void main(String args[])
  {
    int numGames = 1000;
    int p1Count = 0;
    int p2Count = 0;
    int tieCount = 0;
    int totalMoves = 0;
    for(int i = 0; i < numGames; i++)
    {
      int moves = playGame(false);
      if(moves > 0)
        p1Count++;
      else if (moves == 0)
        tieCount++;
      else if (moves < 0)
      {
        moves = -moves;
        p2Count++;
      }
      totalMoves += moves;
    }
    System.out.println("After " + numGames + " games:");
    System.out.println("PLayer 1 won " + p1Count + "games,");
    System.out.println("PLayer 2 won " + p2Count + "games,");
    System.out.println("There were " + tieCount + "ties.");
    double aveMoves = (double)totalMoves / (double)(numGames - tieCount);
    System.out.println("The average number of moves per game was " + aveMoves);
        
  }

   
  public static int playGame(boolean verbose)
  {
/* play one game of war. Return number of moves made.  IF player 1 wins, return this as
   a positive number.  If player 2 wins, as a negative number.  A tie returns 0 moves
*/
    final int MAXMOVES = 10000;
    final int WARSIZE = 3;
    Deck d = new Deck();
    CardHolder hand1, hand2, table;
    hand1 = new CardHolder();
    hand2 = new CardHolder();
    table = new CardHolder();
    int numMoves = 0;
    int winner = 0;
    
    d.shuffle();
    int j = 0;
    while(!d.isEmpty())
    {
      j++;
      if(j % 2 == 0)
      {
        hand1.putCardIntoHolder(d.getCardFromDeck());
      }
      else
      {
        hand2.putCardIntoHolder(d.getCardFromDeck());
      }
    }
    
    Card play1, play2;
    boolean didbreak = false;
    while(winner == 0 && numMoves < MAXMOVES)
    {
      if(hand1.isEmpty())
      {
        winner = 2;
        break;
      }
      else
      {
        play1 = hand1.getCardFromHolder();
        table.putCardIntoHolder(play1);
        if(verbose)
          report(1, play1);
      } 
      if(hand2.isEmpty())
      { 
        winner = 1;
        break;
      }
      else
      {
        play2 = hand2.getCardFromHolder();
        table.putCardIntoHolder(play2);
        if(verbose)
          report(2, play2);
      } 
      while(play1.getValue() == play2.getValue())
      {//war
        if(verbose)
          System.out.println("War");
        didbreak = false;
        try
        {
          for(int i = 0; i < WARSIZE; i++)
          {
            table.putCardIntoHolder(hand1.getCardFromHolder());
          }
        }
        catch (CardHolderException e)
        {
          winner = 2;
          didbreak = true;
        }
        try
        {
          for(int i = 0; i < WARSIZE; i++)
          {
            table.putCardIntoHolder(hand2.getCardFromHolder());
          }
        }
        catch (CardHolderException e)
        {
          winner = 1;
          didbreak = true;
        }
        if(hand1.isEmpty())
        {
          winner = 2;
          didbreak =true;
        }
        else
        {
          play1 = hand1.getCardFromHolder();
          table.putCardIntoHolder(play1);
          if(verbose)
            report(1, play1);
        }
        if(hand2.isEmpty())
        {
          winner = 1;
          didbreak = true;
        }
        else
        {
          play2 = hand2.getCardFromHolder();
          table.putCardIntoHolder(play2);
          if(verbose)
            report(2, play2);
        }
      } //end while(play1.getValue() == play2.getValue())
      if(didbreak)
        break;
      if(play1.getValue() > play2.getValue())
      {
        while(!table.isEmpty())
        {
          hand1.putCardIntoHolder(table.getCardFromHolder());
        }
      }
      else
      {
        while(!table.isEmpty())
        {
          hand2.putCardIntoHolder(table.getCardFromHolder());
        }
      }
      numMoves++;
    }//main while loop
    if(winner == 2)
      numMoves = -numMoves; 
    return numMoves;
  }
  
  public static void report(int h, Card c)
  {
    if(h == 1)
      System.out.println("Player 1 places " + c + " on the table");
    else
      System.out.println("Player 2 places " + c + " on the table");      
  }
}
    
