import java.util.StringTokenizer;
import java.io.*;

/*
 * Program: ScrabbleDriver - reads in an input file names "input.dat"
 *                           and assigns a point value to each letter of the
 *                           alphabet. Then asks the user for two words they
 *                           would construct with scrabble tiles and scores
 *                           them to find the largest scoring word.
 * Author:  William Kreahling
 * Date:    Feb, 2006
 */
class ScrabbleDriverB
{
   public static void main(String[] args) throws Exception
   {
      final int LETTERS = 26;          // Number of possible letters
      final int OFFSET = (int)'a';     // offset, so I can index easily

      // Variable declarations - wow Scanner class is SO much easier
      FileInputStream fp = new FileInputStream("input.dat");
      InputStreamReader isr = new InputStreamReader(fp);
      BufferedReader fileIn = new BufferedReader(isr);
      BufferedReader scanIn = new BufferedReader(new 
                                                 InputStreamReader(System.in));
      int letters[] = new int[LETTERS];            // Array for letters
      String line;                                 // line of input
      String ch;                                     // single char
      int value;                                   // temp location
      int index;                                   // index into array

      // initialize my array, since letters not in the input file
      // have a score of zero
      for (int i = 0; i < LETTERS; i++)
         letters[i] = 0; 
      
      // while in input left in the file
      while ((line = fileIn.readLine()) != null)
      {
         // get letter and its point value
         StringTokenizer tokIn = new StringTokenizer(line);
         ch = tokIn.nextToken();
         value = Integer.parseInt(tokIn.nextToken());

         // make sure we read in a single character, since next() reads 
         // in Strings
         if (ch.length() < 1 || ch.length() > 1)
         {
            System.out.println("Input file is invalid");
            System.exit(1);
         }

         // convert the character into a value between 0 and 25
         index = ((int)ch.charAt(0)) - OFFSET ;

         if (value < 0)
         {
            System.out.println("Input file is invalid");
            System.out.println("Negative value "  + value +
                               " found for letter " + (char)(index + OFFSET));
            System.exit(1);
         }


         // Check to see if we have never seen a letter value before
         if (letters[index] == 0)
            letters[index] = value;
         else
         {
            System.out.println("Input file is invalid");
            System.out.println("Duplicate value found for letter " +
                               (char)(index + OFFSET));
            System.exit(1);
         }
      }
      
      System.out.println("Input file is valid");

      // Prompt user, read in two words, and convert to lower case, since 
      // we are ignoring case and our offset assumes lower case letters

      System.out.println("PLease enter two words to play in scrabble >");
      line = scanIn.readLine();
      StringTokenizer tokIn = new StringTokenizer(line);
      String wordOne = tokIn.nextToken();
      // in case they enter input on two lines
      if (!tokIn.hasMoreTokens())
      {
         line = scanIn.readLine();
         tokIn = new StringTokenizer(line);
      }
      String wordTwo = tokIn.nextToken();
      wordOne = wordOne.toLowerCase();
      wordTwo = wordTwo.toLowerCase();

      int sumOne = 0;
      int sumTwo = 0;
      
      /*
       * For both words read in, loop through each individual character and
       * add the values in the corresponding letters array
       */
      for (int i = 0; i < wordOne.length(); i++)
      {
         sumOne += letters[(int)(wordOne.charAt(i) - OFFSET)]; 
      }
      for (int i = 0; i < wordTwo.length(); i++)
      {
         sumTwo += letters[(int)(wordTwo.charAt(i) - OFFSET)]; 
      }

      // Print out the winning word
      if (sumOne > sumTwo)
      {
         System.out.println("\"" + wordOne + "\" with a score of " + sumOne +
                            " beats \"" + wordTwo + "\" with a score of " +
                            sumTwo);
      }
      else if (sumTwo > sumOne)
      {
         System.out.println("\"" + wordTwo + "\" with a score of " + sumTwo +
                            " beats \"" + wordOne + "\" with a score of " +
                            sumOne);
      }
      else
      {
         System.out.println("\"" + wordOne + "\" and \"" + wordTwo + 
                            "\" are tied " + "with a score of " + sumOne);
      }
   }
}
