コード例 #1
0
        static void Main(string[] args)
        {
            Random rnd = new Random();
            int    UserCard1 = rnd.Next(1, 10);
            int    UserCard2 = rnd.Next(1, 10);
            int    DealerCard1 = rnd.Next(1, 10);
            int    DealerCard2 = rnd.Next(1, 10);
            int    HitCard1 = rnd.Next(1, 10);
            int    HitCard2 = rnd.Next(1, 10);
            int    UserSum1, DealerSum1;
            int    UserPot   = 0;
            int    DealerPot = 0;
            string _wager;
            int    wager;
            string PlayAgain;

            //System.Threading.Thread.sleep(20);
            try
            {
                do
                {
                    System.Console.WriteLine("How much would you like to wager?");
                    _wager = System.Console.ReadLine();

                    wager = Int32.Parse(_wager);
                    System.Console.WriteLine($"{wager} Wagered");

                    UserSum1 = UserCard1 + UserCard2;



                    System.Console.WriteLine($"Your Card Count: {UserSum1}");
                    DealerSum1 = DealerCard1 + DealerCard2;
                    System.Console.WriteLine($"The Dealers Card Count: {DealerSum1}");

                    System.Threading.Thread.Sleep(20);

                    /*do
                     *  {
                     *      DealerSum1 = DealerSum1 + HitCard1;
                     *      System.Console.WriteLine($"Dealer Card Count: {DealerSum1}");
                     *  } while (DealerSum1 <= 17);*/

                    System.Console.WriteLine("Hit?  (Y/N)");
                    string hit = System.Console.ReadLine();
                    if (hit.ToUpper() == "Y")
                    {
                        UserSum1 = UserSum1 + HitCard1;
                        System.Console.WriteLine($"Your Card Count: {UserSum1}");
                        if (DealerSum1 <= 17)
                        {
                            DealerSum1 = DealerSum1 + HitCard2;
                        }
                        //do
                        //{
                        //    DealerSum1 = DealerSum1 + HitCard2;
                        //} while (DealerSum1 <= 17);
                        System.Console.WriteLine($"Dealer Card Count: {DealerSum1}");

                        if ((DealerSum1 > UserSum1) && (DealerSum1 <= 21))
                        {
                            DealerPot = wager + DealerPot;
                        }
                        else
                        {
                            UserPot = wager + UserPot;
                        }
                        //System.Console.WriteLine($"Current User Pot: {UserPot}   Current Dealer Pot: {DealerPot}");
                    }
                    if (hit.ToUpper() == "N")
                    {
                        System.Console.WriteLine($"Your Card Count: {UserSum1}");
                        if (DealerSum1 <= 17)
                        {
                            DealerSum1 = DealerSum1 + HitCard2;
                        }
                        //do
                        //{
                        //    DealerSum1 = DealerSum1 + HitCard2;
                        //} while (DealerSum1 <= 17);
                        System.Console.WriteLine($"Dealer Card Count: {DealerSum1}");

                        if ((DealerSum1 > UserSum1) && (DealerSum1 <= 21))
                        {
                            DealerPot = wager + DealerPot;
                        }
                        else
                        {
                            UserPot = wager + UserPot;
                        }
                    }
                    System.Console.WriteLine($"Current User Pot: {UserPot}   Current Dealer Pot: {DealerPot}");

                    System.Console.WriteLine($"Would you like to play again?  (Y/N)");
                    PlayAgain = System.Console.ReadLine();
                } while (PlayAgain.ToUpper() == "Y");
            }
            catch
            {
            }
        }
コード例 #2
0
        static void Main(string[] args)
        {
            string PlayAgain;

            do
            {
                Console.WriteLine("Input 1 or 2 to Play: \n1: Player VS. Player \n2: Player VS. Computer");
                int MenuInput = Int32.Parse(Console.ReadLine());

                if (MenuInput == 1)
                {
                    Console.WriteLine("Please enter player one's name: ");
                    string PlayerOne = Console.ReadLine();
                    Console.WriteLine(PlayerOne + " You will be playing as 'X' \n\nPlease enter player two's name: ");
                    string PlayerTwo = Console.ReadLine();
                    Console.WriteLine("\n" + PlayerTwo + " You will be playing as 'O'");
                    Human NumOne = new Human('X', PlayerOne);
                    Human NumTwo = new Human('O', PlayerTwo);

                    Controller.PrintGameBoard();

                    for (int i = 1; i <= (Controller.Length * Controller.Width) / 2; i++) // total number of spaces on the game board (divided by two)...if no one wins in 42 turns the game is a tie
                    {
                        NumOne.TakeATurn();
                        if (Controller.CheckWin())
                        {
                            Console.WriteLine("Congratulations " + NumOne.Name + "! You Win!");
                            break;
                        }
                        NumTwo.TakeATurn();
                        if (Controller.CheckWin())
                        {
                            Console.WriteLine("Congratulations " + NumTwo.Name + "! You Win!");
                            break;
                        }
                        if (i == (Controller.Length * Controller.Width) / 2)
                        {
                            Console.WriteLine("This game is a tie!");
                        }
                    }
                }
                else if (MenuInput == 2)
                {
                    Console.WriteLine("Please enter your name: ");
                    string PlayerName = Console.ReadLine();
                    Console.WriteLine(PlayerName + " You will be playing as 'X'");
                    Human    Player = new Human('X', PlayerName);
                    Computer Comp   = new Computer('O');

                    Controller.PrintGameBoard();

                    for (int i = 1; i <= (Controller.Length * Controller.Width) / 2; i++) // total number of spaces on the game board (divided by two)...if no one wins in 42 turns the game is a tie
                    {
                        Player.TakeATurn();
                        if (Controller.CheckWin())
                        {
                            Console.WriteLine("Congratulations " + Player.Name + "! You Win!");
                            break;
                        }
                        Comp.TakeATurn();
                        if (Controller.CheckWin())
                        {
                            Console.WriteLine("The Computer is the winner!");
                            break;
                        }
                        if (i == (Controller.Length * Controller.Width) / 2)
                        {
                            Console.WriteLine("This game is a tie!");
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Invalid Input...Please enter 1 or 2");
                }

                Controller.ResetGameBoard();

                Console.WriteLine("Would you like to play again? (y/n)");
                PlayAgain = Console.ReadLine();
                PlayAgain = PlayAgain.ToUpper();
            } while (PlayAgain == "Y" || PlayAgain == "YES");



            Console.Read();
        }