class ScrabBot extends player
{
  public void setHand(char[] snd)
    {hand = snd;}

  public ScrabBot()
  {
    isHuman = false;
  }

  public String get_and_testWord(char letter,int location) 
    {

    boolean usedLetter = (letter == '!');
    boolean foundInHand = false;
    boolean[] usedHand = {false,false,false,false,false,false,false};


    //get word from file
    diction f = new diction();
    String word = f.words[location];   
    int len = word.length();

    for(int loop = 0;loop < len;loop++)    
      {
        if (!usedLetter & word.charAt(loop) == letter) 
          {usedLetter = true;}
         else 
          {
            foundInHand = false;
            int loop2 = 0;
            while (loop2 < 7 & !foundInHand)                     
              {
                if (word.charAt(loop) == hand[loop2] & !usedHand[loop2])
                  { foundInHand = true;
                    usedHand[loop2] = true;}
                loop2++;
              } //while
            if (!foundInHand) {return "???";}
          } //look through hand for letter segment          
      } 
      if (!usedLetter) {return "???";}
      return word;
    } 


/*
  doTurn executes a move by the bot.  The main bulk of the code is
  the looping structure, and rather than including comments within
  the code, here is the plan that the scrabbleBot follows:
 
  -Look at each space on the board
   -If there is a tile on that space than continue
    -Look at each word in the dictionary and detect if the bot can play
     that word, and detect if that word has the current tile that is
     being looked at.  For each word that is valid proceed
     -The word may contain more than one instance of the tile being played
      off of.  Example: playing of an "A", the word "PANDA" can be played
      multiple ways.  For each location of "A" test whether the word can be 
      "placed" vertically or horizontally (but not whether it is legal).  
      For each of these cases, if the play is placeable proceed.
      -Calculate points for that word.  If it is the best word yet then
       proceed
       -Finally detect whether the work is legal.  This is the last step
        since it is the step that takes the most time.  If it is legal, 
        than the current analysis is the best possible word.            
*/

  public char[][] doTurn(char[][] currentBoard)
    {    
      diction ff = new diction();
      final int wordInFile = 330;


      int bestScore = 0;
      int bestX = 0;
      int bestY = 0;
      String bestWord = "???";
      char bestDirection = '?';
      String bestPlayOff = "?"; //change later to a string

      //The following store data for the move being processed
      int possibleScore = 0;
      int possibleX;
      int possibleY;
      char possibleDirection;
      int numberOfLetterWithin;
      String possibleWord;

      Board botBoard = new Board();


      //The following block is if the bot goes first
      if (currentBoard[7][7] == '_')
        {for(int eachWord = 0; eachWord < wordInFile; eachWord++)
           {possibleWord = get_and_testWord('!',eachWord);
            if (possibleWord != "???") 
              {possibleScore = botBoard.calcPoints(possibleWord,6,7,'E');
               if (possibleScore > bestScore)
                 {bestX = 6; bestY = 7;
                  bestWord = possibleWord;
                  bestDirection = 'E';
                  bestScore = possibleScore;
                  bestPlayOff = "";}}}}


      for(int y = 0;y < 15;y++)
        { for(int x = 0;x < 15;x++)
          { if (currentBoard[x][y] >= 'a' & currentBoard[x][y] <= 'z')
            { for(int eachWord = 0; eachWord < wordInFile; eachWord++)
                { possibleWord = get_and_testWord(currentBoard[x][y],eachWord);           
                if (possibleWord != "???")
                  { numberOfLetterWithin = botBoard.moreThanOnce(possibleWord,currentBoard[x][y]);
                  for(int loop = 1; loop <= numberOfLetterWithin;loop++)
                    { for(int twoDirLoop = 0; twoDirLoop < 2; twoDirLoop++)
                      { if (twoDirLoop == 0) {possibleDirection = 'S';}
                        else {possibleDirection = 'E';}                       
                      possibleX = botBoard.findLetter(possibleWord,currentBoard[x][y],loop);
                      if (possibleDirection == 'E')
                        { possibleX = x - possibleX;
                          possibleY = y;} else
                        { possibleY = y - possibleX;
                          possibleX = x;}
                      if (botBoard.isValid(possibleX,possibleY,possibleWord,possibleDirection,currentBoard))
                        { possibleScore = botBoard.calcPoints(possibleWord,possibleX,possibleY,possibleDirection);
                          if (possibleScore > bestScore)
                          { if (botBoard.testBoard(botBoard.putWord(possibleX,possibleY,possibleWord,possibleDirection,currentBoard)))
                            { bestX = possibleX;
                              bestY = possibleY;
                              bestWord = possibleWord;
                              bestDirection = possibleDirection;
                              bestScore = possibleScore;
                              bestPlayOff = "" + currentBoard[x][y];
                            }
                          }
                        }    
                      }
                    }
                  }
                }
              }
            }
        }
        if (bestWord != "???")
        { noMoveInARow = 0; 
          takePlayedCharsFromHand(bestWord, bestPlayOff);
          getNewHand();
          addPoints(botBoard.calcScore(currentBoard, bestWord,bestX,bestY,bestDirection));
          return botBoard.putWord(bestX,bestY,bestWord,bestDirection,currentBoard);
        } 
        else
        { noMoveInARow++; 
          lastWord = "";
          lastScore = 0;
          putHand();
          getNewHand();
          return currentBoard;
        }


      } //method

} //class ScrabBot
