/**
 * CS1101 (AY2009/10 Semester 1)
 * MasterMind.java
 * This class implements the MasterMind game.
 * Duplicate colours are allowed.
 */

import java.util.*;
import java.text.*;

class MasterMind {

    private final static int MAX_ATTEMPTS = 10; // maximum number of attempts
  
    // Generate four pegs for the secret code
    private static MMCode generateSecretCode() {
        MMCode code = new MMCode();
        Random random = new Random();
 
        // fill in the code
       
        return code;
    }

    // Instruction
    private static void printInstruction() {
        System.out.println("Welcome to CS1101 MasterMind!");
        System.out.println("Enter a string consisting of 4 letters, which can be ");
        System.out.println("R (Red), B (Blue), G (Green), Y (Yellow), C (Cyan) or M (Magenta).");   
        System.out.println("Enjoy!");
    }
    
    //----------------------------------------------------------
    // main method 
    //----------------------------------------------------------
    public static void main (String [] args) {

        MasterMindGUI start = new MasterMindGUI(); // -- GUI enhancement
       
        Scanner scanner = new Scanner(System.in);
        DecimalFormat df = new DecimalFormat("00");

        MMCode secretPegs, guessPegs;
        String guess;
        int    attempt = 0;
        int    sinks = 0, hits = 0;

        printInstruction();

        // generate a secret code
        secretPegs = generateSecretCode();
 
        // The following two statements are used to display the secret code
        // so that we can verify the computation of hits and sinks
        // Once testing is done, we may remove these 2 statements.
        /*
           System.out.println("\t\t\tSecret code: " + secretPegs);
           start.addSecretCode(secretPegs); // -- GUI enhancement
        */

        do {
            attempt++;

            System.out.print("Enter your guess: ");
            guess = scanner.nextLine();

            while (guess.length() != 4) {
                System.out.print("Enter your guess: ");
                guess = scanner.nextLine();
            }
            guessPegs = new MMCode();

            System.out.print("\t\t\t  Guess #" + df.format(attempt) 
                              + ": " + guessPegs);
           
            start.addGuessCode(attempt, guessPegs); // -- GUI enhancement

            sinks = secretPegs.countSinks(guessPegs);
            hits  = secretPegs.countHits(guessPegs);

            System.out.println("\t  Sinks = " + sinks + "; Hits = " + hits);
           
            start.addSinksAndHits(attempt, sinks, hits); // -- GUI enhancement

        } while (attempt < MAX_ATTEMPTS && sinks < 4);

        System.out.println();
        if (sinks == 4)
            System.out.println("Congratulations!");
        else
            System.out.println("Sorry you didn't get it. The secret code is "
                               + secretPegs + ".");

        start.addSecretCode(secretPegs); // -- GUI enhancement
        System.out.println("Hope you have enjoyed the game. Bye!");
    }

}
