public class War
{
  public static Stats doWar(boolean verbose)
  {
    final int MAXMOVES = 10000;
    Stats retval = new Stats();
    retval.numMoves = 0;
    retval.numWars = 0;
    retval.winner = 0;
    Deck d = new Deck(true);
    CardHolder hand1 = new CardHolder();
    CardHolder hand2 = new CardHolder();
    CardHolder table = new CardHolder();
    Card c1, c2;

    d.shuffle();
    try
    {
      d.deal(26, hand1, hand2);
    }
    catch (CardHolderException e)
    {
      System.out.println("This should not happen " + e);
    }
    while(retval.numMoves < MAXMOVES)
    {
      retval.numMoves++;
      try
      {
        c1 = hand1.getCardFromCardHolder();
        if(verbose)
        {
          System.out.println("Player 1 puts down " + c1);
        }
      }
      catch (CardHolderException e)
      {
        retval.winner = 1; // player 2 wins
        return retval;
      }
      try
      {
        c2 = hand2.getCardFromCardHolder();
        if(verbose)
        {
          System.out.println("Player 2 puts down " + c2);
        }
      }
      catch (CardHolderException e)
      {
        retval.winner = -1; // player 1 wins
        return retval;
      }
      try
      {
        table.putCardIntoCardHolder(c1);
        table.putCardIntoCardHolder(c2);
      }
      catch (CardHolderException e)
      {
        System.out.println("This should not be possible. " + e);
      }
      while(c1.getValue() == c2.getValue())
      {
        if(verbose)
        {
          System.out.println("WAR!");
        }
        retval.numWars++;
        for(int i = 0; i < 3; i++)
        {
          try
          {
            table.putCardIntoCardHolder(hand1.getCardFromCardHolder());
          }
          catch (CardHolderException e)
          {
            retval.winner = 1; // player 2 wins
            return retval;
          }
        }
        if(verbose)
        {
          System.out.println("Player 1 puts three cards face down on the table.");
        }
        for(int i = 0; i < 3; i++)
        {
          try
          {
            table.putCardIntoCardHolder(hand1.getCardFromCardHolder());
          }
          catch (CardHolderException e)
          {
            retval.winner = -1; //player 1 wins
            return retval;
          }
        }
        if(verbose)
        {
          System.out.println("Player 2 puts three cards face down on the table.");
        }
        try
        {
          c1 = hand1.getCardFromCardHolder();
        }
        catch (CardHolderException e)
        {
          retval.winner = 1; // player 2 wins
          return retval;
        }
        if (verbose)
        {
          System.out.println("Player 1 plays " + c1);
        }
        try
        {
          c2 = hand2.getCardFromCardHolder();
        }
        catch (CardHolderException e)
        {
          retval.winner = -1; // player 1 wins
          return retval;
        }
        if (verbose)
        {
          System.out.println("Player 2 plays " + c2);
        } 
        try
        {
          table.putCardIntoCardHolder(c1);
          table.putCardIntoCardHolder(c2);
        }
        catch (CardHolderException e)
        {
          System.out.println("This should not  happen " + e);
        }
      }//end of while(c1.getValue == c2.getValue
      CardHolder winner;
      if(c1.getValue() > c2.getValue()) //c1 wins the move
      {
        winner = hand1;
        if(verbose)
        {
          System.out.println("Player 1 wins this move.");
        }
      }
      else // c2.getValue() > c1.getValue()
      {
        winner = hand2;
        if(verbose)
        {
          System.out.println("Player 2 wins this move.");
        }
      }
/*
hand1 |address of CardHolder representing hand1|
if I set winner = hand1, 
winner |address of CardHolder representing hand1|
similiarly, if I set winner = hand2
winner |address of CardHolder representing hand2|
*/ 
      while(!table.isEmpty())
      {
        try
        {
          winner.putCardIntoCardHolder(table.getCardFromCardHolder());
        }
        catch (CardHolderException e)
        {
          System.out.println("This should not happen. " + e);
        }
      }
    }// while(retval.numMoves < MAXMOVES
    return retval;
  }    

  public static void main(String args[]) throws CardHolderException
  {
    if(args.length != 1)
    {
      System.out.println("Usage: java War <howManyGames>");
      System.exit(1);
    }
    Stats s = null; 
    int totalMoves = 0;
    int totalWars = 0;
    int number1wins = 0;
    int number2wins = 0;
    int numberTies = 0;
    int n = Integer.parseInt(args[0]);

    for(int i = 0; i < n; i++)
    {
      s = doWar(false);
      totalMoves += s.numMoves;
      totalWars += s.numWars;
      if(s.winner == -1)
        number1wins++;
      else if(s.winner == 1)
        number2wins++;
      else
        numberTies++;
    }
    double aveMoves = (double)totalMoves / (double) n;
    double aveWars = (double)totalWars / (double) n;
    System.out.println("In " + n + " games of war:");
    System.out.println("Player 1 won " + number1wins + " games.");
    System.out.println("Player 2 won " + number2wins + " games.");
    System.out.println("There were " + numberTies + " ties.");
    System.out.println("The average number of moves was " + aveMoves);
    System.out.println("The average number of wars was " + aveWars);
  }
     
/*    Stats s = doWar(true);
    if(s.winner == -1)
      System.out.println("Player 1 wins.");
    else if (s.winner == 1)
      System.out.println("Player 2 wins.");
    else //s.winner == 0
      System.out.println("Tie");
    System.out.println("There were " + s.numMoves + " moves.");
    System.out.println("There were " + s.numWars + " wars.");
  }
*/
}

class Stats
{
  int winner; //-1 means hand1, 0 means tie, 1 means hand2
  int numMoves;
  int numWars;
}
