using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AceyDucey { class Program { private bool _Running; private bool _GameOver; private int _Wallet; private int[] _Cards = new int[3] { 0, 0, 0 }; private Random _Random = new Random(Environment.TickCount); private string AskQuestion(string prompt) { while(true) { Console.WriteLine($"{prompt}? "); string ans = Console.ReadLine().ToUpper(); if (String.IsNullOrWhiteSpace(ans)) Console.WriteLine("PLEASE ANSWER THE QUESTION."); else return ans; } } private int GetIntInput(string prompt) { while(true) { Console.Write($"{prompt} "); string ans = Console.ReadLine(); if (int.TryParse(ans, out int value)) return value; Console.WriteLine("YOU DID NOT ENTER AN INTEGER."); } } private int AskForBet() { while(true) { int bet = GetIntInput("WHAT IS YOUR BET?"); if (bet < 0) Console.WriteLine("PLEASE BET A NON-NEGATIVE AMOUNT."); else if (bet > _Wallet) { Console.WriteLine("SORRY MY FRIEND, BUT YOU BET TOO MUCH."); Console.WriteLine($"YOU HAVE ONLY {_Wallet} DOLLARS TO BET."); } else return bet; } } private bool AskToPlayAgain() { while(true) { string ans = AskQuestion("DO YOU WANT TO PLAY AGAIN"); if (ans == "YES" || ans == "Y") return true; if(ans == "NO" || ans == "N") return false; Console.WriteLine("PLEASE ANSWER \"YES\" OR \"NO\""); } } private bool AskToSeeTheRules() { while (true) { string ans = AskQuestion("DO YOU WANT TO SEE THE RULES"); if (ans == "YES" || ans == "Y") return true; if (ans == "NO" || ans == "N") return false; Console.WriteLine("PLEASE ANSWER \"YES\" OR \"NO\""); } } private void DisplayCard(int card) { if (card == 11) Console.WriteLine("JACK"); else if (card == 12) Console.WriteLine("QUEEN"); else if (card == 13) Console.WriteLine("KING"); else if (card == 14) Console.WriteLine("ACE"); else Console.WriteLine($"{card}"); } private void DisplayRules() { Console.WriteLine("ACEY DUCEY IS PLAYED IN THE FOLLOWING MANNER:"); Console.WriteLine("THE DEALER (COMPUTER) DEALS TWO CARDS FACE UP"); Console.WriteLine("YOU HAVE THE OPTION TO BET OR NOT BET DEPENDING"); Console.WriteLine("ON WHETHER OR NOT YOU FEEL THE NEXT CARD WILL"); Console.WriteLine("HAVE A VALUE BETWEEN THE FIRST TWO.\r\n"); Console.WriteLine("IF YOU DO NOT WANT TO BET, INPUT A 0 (ZERO)."); } private int GenerateRandomNumber(int minimum, int maximum, bool show) { int value = _Random.Next(minimum, maximum + 1); if (show) Console.WriteLine($"{value}"); return value; } private int SelectCard() { while(true) { int card = GenerateRandomNumber(2, 14, false); if (card != _Cards[0] && card != _Cards[1] && card != _Cards[2]) { DisplayCard(card); return card; } } } public void Run() { Console.WriteLine("*** ACEY DUCEY : C# ***"); _Running = true; if (AskToSeeTheRules()) DisplayRules(); while(_Running) { _Cards[0] = _Cards[1] = _Cards[2] = 0; _Wallet = 100; _GameOver = false; while(!_GameOver) { Console.WriteLine($"YOU NOW HAVE {_Wallet} DOLLARS."); _Cards[0] = SelectCard(); _Cards[1] = SelectCard(); int bet = AskForBet(); if (bet > 0) { _Cards[2] = SelectCard(); if ((_Cards[2] > _Cards[0] && _Cards[2] < _Cards[1]) || (_Cards[2] > _Cards[1] && _Cards[2] < _Cards[0])) { Console.WriteLine("YOU WIN!!!"); _Wallet += bet; } else { Console.WriteLine("SORRY, YOU LOSE."); _Wallet -= bet; } if (_Wallet < 1) { Console.WriteLine("SORRY FRIEND, YOU ARE BROKE."); _GameOver = true; } } else _GameOver = true; } Console.WriteLine("*** GAME OVER ***"); _Running = AskToPlayAgain(); } Console.WriteLine("OK, HOPE YOU HAD FUN."); Console.WriteLine("THANKS FOR PLAYING!"); } static void Main(string[] args) { Program P = new Program(); P.Run(); Console.ReadLine(); } } }