import java.util.Scanner;
/** 
 *   This class is designed to play a game of PseudoWar
 *
 *
 *   @author:Elizabeth Adams
 *   @version 3 
 */

    public class PseudoWar_V3
	    {
      private int score1, score2;
      private String player1, player2;
      private int  numberRounds;
      private int roundNumber;
   
   /** This is the constructor for pseudoWar
    *  It sets the class attributes
    * 
    *   @param name1  user's choice of name for first player
    *   @param name2  user's choice of name for second player
    *   @param numRounds  number of rounds user want the game to contain
    */
       public PseudoWar_V3 (String name1, String name2, int numRounds)
      {
       		 // sets the name of the first player
         this.player1 = name1;
      	 
      		 // sets the name of the second player
         this.player2 = name2;  

      	    // initialize players' scores to 0
         this.score1 = 0;
         this.score2 = 0;

            // sets the number of rounds
         this.numberRounds = numRounds;
      	 
      	  // initialize round number 
         this.roundNumber = 0;

      }	// end constructor 
   	
   	   	
   /**
    *   This method returns the heading for the output
    *   and plays the game
    *     
   */
       public void  startGame ()
      {
         System.out.println ("\t\t " + player1 + "\t\t " + player2 + "\t\t  Winner ");
         System.out.println ("-----------------------------------------------------");
       
         playGame();
      } // end startGame
   
   /**
    *    This method runs the round and prints the
    *    data from each round along with the round results
	 *
    */
       public void playGame ()
      {
         String roundData;
      	
         for (int i = 1; i <= this.numberRounds; i++)
         {
            roundData = runRound ();
            System.out.println (roundData);
         }
      } // end playGame
    
    /** 
     *
     *   This method generates a random number in the
     *   range 1 to 52 and returns it.
     *
     *   @return random number in range 1 to 52
     *
     */
       public int generateNumber ()
      {
         double temp;
         int value;
      	 
			  // this gets a real random number between  0 and 1
			  // and turns it into an integer in range 1 to 52
         temp = Math.random();
         temp = temp * 52 + 1;
         value = (int) temp;

         return value;
      }  // end generateNumber
    
     /** 
      *  This method receives two random numbers, the
   	*  first belonging to player 1, the second to player 2
   	*  compares the numbers 
   	*  
   	*  @param num1  first player's number
   	*  @param num2  second player's number
   	*
   	*  @return  1  if player1's number is higher
   	*  		   2 if player2's number is higher
   	*           0 if the numbers of the two players are the same
   	*/
       public int determineWinner ( int num1, int num2)
      {
         int returnValue;
      	
         if (num1 > num2)
            returnValue = 1; // player 1 wins
         else if (num2 > num1)
            returnValue = 2;  // player 2 wins
         else
            returnValue = 0;  // it's a tie

         return returnValue;
      } // end determine winner	 
     
     /**  
      *   This method calls generateNumber and determineWinner
   	*   an updates the player's scores and returns a String
   	*   with the appropriate round String
   	*
   	*   @return result of the round to be put in output table
   	*/
       public  String runRound ()
      {
         int num1, num2, winner;
         String resultString, winnerName;

            // default winnerName
         winnerName = "\t\t\t\t\t\t\t ";

         this.roundNumber = this.roundNumber + 1;
         
			      	 // call generate number for player1
         num1 = generateNumber();
      	 // call generate number for player2
         num2 = generateNumber();
  
  	   // initialize result string
			resultString = this.roundNumber + "\t\t " + num1 + "\t\t " + num2;		   
  
          // call determineWinner  
         winner = determineWinner (num1, num2);

         if (winner == 1)
         {
            winnerName = this.player1;
            this.score1 = this.score1 + 1;
         }	
         else if (winner == 2)
         {
            winnerName = this.player2;
            this.score2 = this.score2 + 1;
         }	 

              // finalize resultString
         resultString = resultString + "\t\t " + winnerName;  
      	   
         return resultString;
      
      } // end runRound
   	
   	/**
   	 *
   	 *  this method returns a string with the final output
   	 *  including the final game results
   	 *
   	 *  @return final results 
   	 */
       public  String  getFinalResult ()
      {
         String finalResult;
      	
         finalResult = this.player1 + " won " + this.score1 + " rounds\n" +
                       this.player2 + " won " + this.score2 + " rounds\n" +
            			  "The game has ended ";
      		 
         return finalResult;
       
      } //end getFinalResult
       
   } // end PseudoWar_V3