//Simple Patience Game - Uses PlayingCard & Deck Objects.
//by Neil Broadbent

import java.io.*;
import NeilClass.*;

public class Patience
{
    static Pack tPlayingPile = new PackImpl(); //Main Pack
    static Pack tSecondPile = new PackImpl();  //Second Pack
    static PlayingCard Ace = new PlayingCardImpl("A:C"); //An Ace to do comparisons with. The suit is irrelevant.
    
    
    public static void main(String[] pArgs) throws IOException
        {
            System.out.println("Simple Patience Game by Neil Broadbent");
            System.out.println();

            String tManualEntry = NeilClass.GetInputString("Do you want to type in the 13 cards yourself? ");

            if (tManualEntry.equals("y")){
                ManualCardEntry();    // Practical asks for manual card entry
            }
            else{
                System.out.println("Original Cards");
                InitCards();          // But automated card generation makes more sense 
            }

                //List pack before and after shuffle
            tPlayingPile.ListPack();
            System.out.println("Shuffleing Cards");
            tPlayingPile.Shuffle();
            tPlayingPile.ListPack();
            System.out.println();

            StartGame();
        }

    public static void InitCards()
        {
                //Automatically Generate the Cards to save time
           for (int i=2; i<11; i++){
                PlayingCard tCard1 = new PlayingCardImpl(i + ":H");
                tPlayingPile.addCardTop(tCard1);
            }
            PlayingCard tCard1 = new PlayingCardImpl("A:H");
            tPlayingPile.addCardBottom(tCard1);
            tCard1 = new PlayingCardImpl("J:H");
            tPlayingPile.addCardTop(tCard1);
            tCard1 = new PlayingCardImpl("Q:H");
            tPlayingPile.addCardTop(tCard1);
            tCard1 = new PlayingCardImpl("K:H");
            tPlayingPile.addCardTop(tCard1);

        }

    public static void ManualCardEntry() throws IOException
        {
                //Type the cards in manually
            for (int i=0; i<13; i++){
                String CardString = NeilClass.GetInputString("Type in a card ");
                PlayingCard tCard1 = new PlayingCardImpl(CardString);
                tPlayingPile.addCardTop(tCard1);
            }
        }
    

    public static void StartGame() throws IOException
        {
            boolean GAME_OVER=false;

                //First check the pack top for an Ace
            PlayingCard tCard1 = tPlayingPile.removeCardTop();
            if (tCard1.SameNumber(Ace))
                {
                        //If the top card is an Ace the game is over
                    System.out.println("Game Over - Ace On Top");
                    System.exit(1);
                }
            else{
                    //Put the card back on the top of the pile
                tPlayingPile.addCardTop(tCard1);
            }

                //Main game loop
            while (!GAME_OVER){
                int tQuantity = NeilClass.GetInputInt("How many cards do you want to move? 2-13 ");
                if (!MoveAndContinue(tQuantity)){
                        //If we can not contine the game due to an Ace; Game is over
                    GAME_OVER=true;
                }
            }
        }

    public static boolean MoveAndContinue(int tQuantity)
        {
                //Move tQuantity of cards to tSecondPile and then copy them back in
                //the reverse order
            
            PlayingCard tCard = new PlayingCardImpl();

                //Copy cards from main to second pile
            for (int i=0; i<tQuantity; i++){
                tCard = tPlayingPile.removeCardTop();
                tSecondPile.addCardTop(tCard);
            }

                //Copy them back the other way round
            for (int i=0; i<tQuantity; i++){
                tCard = tSecondPile.removeCardBottom();
                tPlayingPile.addCardTop(tCard);
            }
            tPlayingPile.ListPack();

                //Check for an Ace on top
            if (tCard.SameNumber(Ace))
                {
                    System.out.println("Game Over - Ace On Top");
                    return false;
                }
            else{
                return true;
            }
        }
}






