Esempio n. 1
0
        static GameState LoadSavedGame(string filename)
        {
            FileStream fs        = null;
            GameState  savedGame = new GameState();

            try
            {
                using (fs = new FileStream(filename, FileMode.Open))
                {
                    DataContractSerializer dcsGameState = new DataContractSerializer(typeof(GameState));
                    XmlDictionaryReader    reader       = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());
                    savedGame = (GameState)dcsGameState.ReadObject(reader);
                }
            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
                ScreenOperations.ClearGameScreen();
            }
            finally
            {
                fs?.Close();
            }
            return(savedGame);
        }
Esempio n. 2
0
 public void Lost()
 {
     ScreenOperations.ClearGameLine(0, 15, 39);
     Console.ForegroundColor = ConsoleColor.Red;
     Console.WriteLine("You lost the hand! You lost ${0}", Bet);
     Console.Write("                                 ");
     Console.SetCursorPosition(0, 16);
     Console.WriteLine("Funds: ${0}", Funds);
     Console.ForegroundColor = ConsoleColor.Gray;
 }
Esempio n. 3
0
 public void Tie()
 {
     ScreenOperations.ClearGameLine(0, 15, 39);
     Console.ForegroundColor = ConsoleColor.Yellow;
     Console.WriteLine("You tied the dealer! Got bet back!");
     Console.Write("                                    ");
     Console.SetCursorPosition(0, 16);
     Funds += Bet;
     Console.WriteLine("Funds: ${0}", Funds);
     Console.ForegroundColor = ConsoleColor.Gray;
 }
Esempio n. 4
0
 public void Win()
 {
     ScreenOperations.ClearGameLine(0, 15, 39);
     Console.ForegroundColor = ConsoleColor.Green;
     Console.WriteLine("You won the hand! You received ${0}!", Bet * 2);
     Funds += Bet * 2;
     Console.Write("                                 ");
     Console.SetCursorPosition(0, 16);
     Console.WriteLine("Funds: ${0}", Funds);
     Console.ForegroundColor = ConsoleColor.Gray;
 }
Esempio n. 5
0
        public int DealerInitialTurn()
        {
            HiddenString = Dealer.GetInitialCards(deck);
            ConsoleColor hiddenColor = Console.ForegroundColor;

            Dealer.Hidden = hiddenColor;
            if (Dealer.Sum == 21)
            {
                ScreenOperations.ClearGameLine(0, 4, 13);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("BLACKJACK!");
                Console.ForegroundColor = ConsoleColor.Gray;
            }
            return(Dealer.Sum);
        }
Esempio n. 6
0
        static void Main(string[] args)
        {
            Console.WriteLine("Load a saved game? (Y or N)");
            char      loadgame           = char.ToUpper(char.Parse(Console.ReadLine()));
            GameState savedGame          = new GameState();
            bool      successfulGameLoad = false;

            if (loadgame == 'Y')
            {
                Console.Write("Input filename to load: ");
                string filename = Console.ReadLine();
                savedGame = LoadSavedGame(filename);
                if (savedGame.Player != null)
                {
                    successfulGameLoad = true;
                }
            }
            Console.Title = "Blackjack";
            ScreenOperations.ClearGameScreen();
            Console.SetCursorPosition(0, 0);
            for (int i = 0; i < 35; i++)
            {
                Console.Write("-");
            }
            Console.Write("BLACKJACK");
            for (int i = 0; i < 36; i++)
            {
                Console.Write("-");
            }
            if (!successfulGameLoad)
            {
                Console.SetCursorPosition(0, 15);
                Console.Write("Enter Player Name: ");
                string name = Console.ReadLine();
                ScreenOperations.ClearGameLine(0, 15, 49);
                Player    player    = new Player(name);
                GameState blackjack = new GameState(player);
                while (blackjack.Turn())
                {
                    ;
                }
                if (blackjack.Player.Funds > 0)
                {
                    Console.WriteLine("If you wish to save, input save file name: ");
                    string filename = Console.ReadLine();
                    SaveGameState(filename, blackjack);
                }
            }
            else
            {
                while (savedGame.Turn())
                {
                    ;
                }
                if (savedGame.Player.Funds > 0)
                {
                    Console.WriteLine("If you wish to save, input save file name: ");
                    string filename = Console.ReadLine();
                    SaveGameState(filename, savedGame);
                }
            }
            Console.ReadLine();
        }
Esempio n. 7
0
        public int PlayerTurn(bool playerplay)
        {
            string s = Player.GetInitialCards(deck);

            Console.SetCursorPosition(Player.ColumnPosition, 3);
            Console.Write(s);
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.BackgroundColor = ConsoleColor.Black;
            int choice = 2;

            while (playerplay)
            {
                ScreenOperations.ClearGameLine(0, 15, 33);
                Console.WriteLine("Your current sum: {0}", player.Sum);
                Console.Write("1. Hit 2. Stay ");
                bool canDouble      = false;
                int  doubledBet     = player.Bet * 2;
                int  fundsBeforeBet = player.Bet + player.Funds;
                if (doubledBet <= fundsBeforeBet)
                {
                    Console.Write("3. Double");
                    canDouble = true;
                }
                Console.Write("               ");
                Console.SetCursorPosition(0, 17);
                bool choicenotmade = false;
                while (!choicenotmade)
                {
                    try
                    {
                        choice = Int32.Parse(Console.ReadLine());
                        if (choice == 3 && canDouble == false)
                        {
                            choicenotmade = false;
                        }
                        else
                        {
                            choicenotmade = true;
                        }
                    }
                    catch (FormatException)
                    {
                        ScreenOperations.ClearGameLine(0, 18, 33);
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write("Not digit 1, 2, or 3...");
                        Console.ForegroundColor = ConsoleColor.Gray;
                        ScreenOperations.ClearGameLine(0, 17, 28);
                    }
                }
                switch (choice)
                {
                case 1:
                    player.GetCard(deck);
                    if (Player.Sum > 21)
                    {
                        playerplay = false;
                        playerbust = true;
                    }
                    break;

                case 2:
                    playerplay = false;
                    break;

                case 3:
                    player.Funds -= player.Bet;
                    player.Bet   *= 2;
                    ScreenOperations.DebugView(player.ToString());
                    player.GetCard(deck);
                    playerplay = false;
                    if (Player.Sum > 21)
                    {
                        playerbust = true;
                    }
                    break;
                }
                ScreenOperations.DebugView(player.ToString());
                ScreenOperations.ClearGameLine(0, 18, 29);
            }
            return(Player.Sum);
        }
Esempio n. 8
0
        public bool Turn()
        {
            Dealer.ColumnPosition = 0;
            Console.SetCursorPosition(Dealer.ColumnPosition, 1);
            Console.Write(Dealer.Name);
            Player.ColumnPosition = 20;
            Console.SetCursorPosition(Player.ColumnPosition, 1);
            Console.Write(Player.Name);
            Player.PlayerBet();
            int  blackjack  = DealerInitialTurn();
            bool playerturn = true;

            if (blackjack == 21)
            {
                playerturn = false;
            }
            int playersum = PlayerTurn(playerturn);

            Console.SetCursorPosition(Dealer.ColumnPosition, 3);
            Console.ForegroundColor = Dealer.Hidden;
            Console.BackgroundColor = ConsoleColor.White;
            Console.Write(HiddenString);
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.BackgroundColor = ConsoleColor.Black;
            int dealersum = DealerTurn();

            ScreenOperations.ClearGameLine(0, 20, 34);
            Console.Write("{1}'s Card Sum is: {0}", playersum, Player.Name);
            ScreenOperations.ClearGameLine(0, 21, 34);
            Console.Write("{1}'s Card Sum is: {0}", dealersum, Dealer.Name);
            if ((dealersum > playersum && !dealerbust) || playerbust)
            {
                Player.Lost();
                if (playerbust)
                {
                    Console.WriteLine("You busted!");
                }
                Console.WriteLine();
            }
            else if (dealersum < playersum || dealerbust)
            {
                Player.Win();
            }
            else
            {
                Player.Tie();
            }
            Dealer.Sum             = 0;
            Player.Sum             = 0;
            Player.HighAce         = false;
            Dealer.HighAce         = false;
            dealerbust             = false;
            playerbust             = false;
            Dealer.CardRowPosition = 0;
            Player.CardRowPosition = 0;
            Deck.CardsNoLongerInPlay();
            bool correctinput     = false;
            int  choice           = 1;
            bool choiceNotSkipped = true;

            if (Player.Funds <= 0)
            {
                choice           = Player.LostTheGame();
                choiceNotSkipped = false;
            }
            else
            {
                ScreenOperations.ClearGameLine(0, 17, 48);
                Console.WriteLine("Continue playing? 1. Yes 2. No 3. NOOOOOO!!!");
                ScreenOperations.DebugView(player.ToString());
                while (!correctinput)
                {
                    try
                    {
                        ScreenOperations.ClearGameLine(0, 18, 31);
                        choice       = Int32.Parse(Console.ReadLine());
                        correctinput = true;
                    }
                    catch (FormatException)
                    {
                        ScreenOperations.ClearGameLine(0, 19, 34);
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("I asked for a number!");
                        Console.ForegroundColor = ConsoleColor.Gray;
                    }
                }
            }
            if (choiceNotSkipped)
            {
                ScreenOperations.ClearGameScreen();
            }
            switch (choice)
            {
            case 1:
                return(true);

            case 2:
                return(false);

            case 3:
                Console.WriteLine("Don't let it get to you...");
                return(false);

            default:
                Console.WriteLine("Provided Invalid Choice. Terminating...");
                return(false);
            }
        }
Esempio n. 9
0
        public void PlayerBet()
        {
            ScreenOperations.ClearGameLine(0, 15, 43);
            string betText = ($"You have ${Funds}. Bet: ");

            Console.Write(betText, Funds);
            OriginalFunds = Funds; // For debugging
            int  cursorPosition  = betText.Length;
            bool possibleBet     = false;
            bool formatexception = false;

            while (!possibleBet)
            {
                try
                {
                    Bet             = Int32.Parse(Console.ReadLine());
                    formatexception = false;
                }
                catch (FormatException)
                {
                    formatexception = true;
                    ScreenOperations.ClearGameLine(0, 16, 45);
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("You can only bet with digits!");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    ScreenOperations.ClearGameLine(cursorPosition, 15, 23);
                }
                catch (OverflowException)
                {
                    formatexception = true;
                    ScreenOperations.ClearGameLine(0, 16, 45);
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("This bet is too large (and you don't have those funds anyhow)!");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    ScreenOperations.ClearGameLine(cursorPosition, 15, 23);
                }
                if (Bet > Funds && !formatexception)
                {
                    Console.SetCursorPosition(0, 16);
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("You can't spend money you don't have!");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    ScreenOperations.ClearGameLine(cursorPosition, 15, 11);
                }
                else if (Bet < 0)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("You can't bet a negative amount!");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    ScreenOperations.ClearGameLine(cursorPosition, 15, 25);
                }
                else if (!formatexception)
                {
                    Console.SetCursorPosition(0, 16);
                    Console.WriteLine("                                     ");
                    possibleBet = true;
                }
            }
            OriginalBet = Bet; // For Debugging
            Funds      -= Bet;
            ScreenOperations.DebugView(ToString());
        }