import javax.swing.JOptionPane;
/** 
 *   This class is designed to play a game of PseudoWar
 *
 *
 *   @author:Elizabeth Adams
 *   @version 2 
 */

      public class PseudoWar_V2	    {
      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_V2 ()
      {
      	    // initialize players' scores to 0
         this.score1 = 0;
         this.score2 = 0;
      	 
      	  // initialize round number 
         this.roundNumber = 0;
     }	// end constructor 
   
	 /**
	 *   This method introduces the game to the user and gets
	 *   data required for the program from the user and
	 *   sets the attributes with those values 
	 */
     public void intro ()
	  {
	     String name1, name2;
		  String strRounds;
		  int numRounds;
		
		  JOptionPane.showMessageDialog (null, " This program will play a game of PseudoWar "); 
		
		       // gets and sets the name of the first player  
        name1 = JOptionPane.showInputDialog (" What is the name of the first player? ");
        this.player1 = name1;
      	 
	   		 // gets and sets the name of the second player
        name2 = JOptionPane.showInputDialog (" What is the name of the second player? ");     
		  this.player2 = name2;  

            // gets and sets the number of rounds
         strRounds = JOptionPane.showInputDialog (" How many rounds would you like to play ");
         this.numberRounds = Integer.parseInt(strRounds);
			
			JOptionPane.showMessageDialog(null, " your players are " + name1 + " and " + name2 
			  + " and they will play " + this.numberRounds + " rounds.");	
     }  // end intro 
 
   	   	
   /**
    *   This method prints 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
    *    output of each round and the echo of users
    *    input
    *
    */
       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;
      	 
			 // getting a random number in range 0 to 1, putting it 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;
         else if (num2 > num1)
            returnValue = 2;
         else
            returnValue = 0;
         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;
         winnerName = "\t\t\t\t\t\t\t ";
      	 
      	 // call generate number for player1
         num1 = generateNumber();
      	 // call generate number for player2
         num2 = generateNumber();

         this.roundNumber = this.roundNumber + 1;
			
              // initialise string to be returned
         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  string to be returned
         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_V2