import java.util.Scanner;

/**
 * Class that can be used to play an interactive game of Skip-3 Solitaire.
 * The player is repeatedly shown the row of piles and must then enter
 * a command, either "deal", "move RS", or "skip RS" (where RS represents 
 * the Rank & Suit of a card), or "quit".
 *   @author Dave Reed
 *   @version 8/20/15
 */
public class Skip3 {  
    public static void main(String[] args) {
        System.out.println("Welcome to Skip-3 Solitaire");
        System.out.println("(Enter \"help\" or \"?\" for help)");
        
        DeckOfCards deck = new DeckOfCards();
        deck.shuffle();
        RowOfCards row = new RowOfCards();
        Scanner input = new Scanner(System.in);
                
        boolean gameOver = false;
        while (!gameOver) {
            System.out.println("\n"+row);
            System.out.print("["+deck.cardsRemaining()+" cards left] Action? ");
            
            char response = input.next().toLowerCase().charAt(0);
            if (response == 'd') {
                if (deck.cardsRemaining() > 0) {
                    row.addCardAtEnd(deck.dealCard());
                }
                else {
                    System.out.println("The deck is empty.");
                }
            }
            else if (response == 'm') {
                String card = input.next();
                if (card.length() != 2 || !row.moveCard(new Card(card), 1)) {
                    System.out.println("Illegal move command");
                }
            }
            else if (response == 's') {
                String card = input.next();
                if (card.length() != 2 || !row.moveCard(new Card(card), 3)) {
                    System.out.println("Illegal skip comand");
                }
            }
            else if (response == 'q') {
                System.out.println("Final row size = " + row.rowSize());
                gameOver = true;
            }
            else if (response == 'h' || response == '?') {
                displayHelp();
            }
            else {
                System.out.println("Unknown command.");
            }
        }  
    }
    
    public static void displayHelp() {
        System.out.println("Your command options are:");
        System.out.println(" (d)eal    : Deal a card and add it to the end of the row");
        System.out.println(" (m)ove RS : Move a card with specified rank & suit (e.g., 'QH')");
        System.out.println("             one spot to the left");
        System.out.println(" (s)kip RS : Skip a card with specified rank & suit (e.g., 'QH')");
        System.out.println("             three spots to the left");
        System.out.println(" (h)elp    : DIsplay this help message");
        System.out.println(" (q)uit    : Quit the game");
    }
}

