import java.util.Scanner;
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 ScrabbleDriver
{
   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
      // Scanner to read file
      Scanner fileIn = new Scanner (new File("input3.dat"));
      Scanner scanIn = new Scanner (System.in);    // Keyboard scanner
      int letters[] = new int[LETTERS];            // Array for letters
      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 input left in the file
      while (fileIn.hasNext())
      {
         // get letter and its point value
         ch = fileIn.next();
         value = fileIn.nextInt();
         /*
          * 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 >");
      String wordOne = scanIn.next();
      String wordTwo = scanIn.next();
      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);
      }
   }
}
