import java.util.Scanner;

/**
 * PA4: 7 Little Words Game.
 *
 * @author Dee A. B. Weikle
 * @version 08-01-2018
 */
public class SevenLittleWords {


    public static final int CLUE_NUM = 7;
    public static final int MIN_WORD_LENGTH = 2;
    
    /**
     * Driver to call different pieces of the game.
     *
     * @author Dee A. B. Weikle
     * @version 08-01-2018
     * @param args command line arguments
     */
    public static void main(String[] args) {
    
        Scanner in = new Scanner(System.in);
        
        String[] solutions; 
        String[] clues;
        
        String answer = "";
        
        System.out.println("Welcome to Seven Little Words - The Text Version");
        System.out.println(
            "What game would you like to play? - default or new?");
        answer = TerminalUI.promptForString(in, 
            "Type \"default\" or \"new\"", 1);
        
        if (answer.equals("new")) {
       
            solutions = new String[CLUE_NUM];
            clues = new String[CLUE_NUM];
           
            // fill solutions array from user
            for (int i = 0; i < solutions.length; i++) {
               
                solutions[i] = TerminalUI.promptForString(in, 
                    "Enter solution word " + (i + 1), MIN_WORD_LENGTH);
               
            }
            
            // fill clues array from user
            for (int i = 0; i < clues.length; i++) {
               
                clues[i] = TerminalUI.promptForString(in, 
                    "Enter clue " + (i + 1), MIN_WORD_LENGTH);
               
            }
                       
        } else {
            
            // Using default values
            solutions = new String[] {"tofutti", "magnetism", "edifice", 
                "grinder", "groundling", "thesaurus", "bull"};
                
        
            clues = new String[] {"soy-based frozen treat brand", 
                "charismatic power", 
                "monumental structure",
                "mincemeat maker", 
                "bottom-dwelling fish", 
                "synonym finder", 
                "upward-trending market"};
    
        }
        
        // Create the board and randomize it
        String[] board;
        
        board = Generator.wordSplicer(solutions);
        board = Generator.randomizer(board, 0);
        
        System.out.println(
            "Now that you have initialized a game, let's see it.\n");
        
        System.out.println(Generator.clueToString(clues));
        
        System.out.println(Generator.boardToString(board));
        
        // play the game
        
        for (int currentClue = 0; currentClue < CLUE_NUM; currentClue++) {
        
            boolean solved = false;
            int first;
            int second;      
            do {
            
                System.out.printf("Clue %02d: %s\n", 
                    currentClue + 1, clues[currentClue]);
                
                TerminalUI.promptNDisplayHints(in, 
                    solutions[currentClue]);                     
                
                first = TerminalUI.promptForInt(in, 
                    "Enter the number of the first slice", CLUE_NUM * 2);
                second = TerminalUI.promptForInt(in, 
                    "Enter the number of the second slice", CLUE_NUM * 2);
                
                if (solutions[currentClue].equals(
                    board[first - 1] + board[second - 1])) {
                    solved = true;
                    board[first - 1] = "-";
                    board[second - 1] = "-";
                } else {
                
                    System.out.println(
                        "Sorry you need to try this clue again.");
                }
                
                System.out.println(Generator.boardToString(board));
            
            }
            while (!solved);
            
            System.out.println(
                "Congratulations! You solved clue " 
                + (currentClue + 1) + "!\n");
            
        }
        
        System.out.println("Congratulations! You solved the puzzle!");
        
        System.out.println(Generator.clueToString(clues));
        // System.out.println();
        System.out.println(Generator.boardToString(board));
          
    }
}
