public Game() { GameElements.Initialize(); GameElements.Reset(); Reset(); RoundNo = 1; }
void ShowAllCards <T>(T source, int i) { dynamic temp = null; if (source is Croupier) { temp = ((Croupier)(object)source); Console.WriteLine("Croupier's hand: "); } if (source is Player) { temp = ((Player)(object)source); if (temp.Hands.Count == 1) { Console.WriteLine("Cards in hand: "); } else { Console.WriteLine("Cards in hand no. {0}:", i); } } for (int j = 0; j < temp.Hands[i].Cards.Count; j++) { Console.Write(temp.Hands[i].Cards[j].Name); if (j != temp.Hands[i].Cards.Count - 1) { Console.Write(", "); } } Console.WriteLine(); GameElements.DrawSeparators("-"); }
void PlaceABet() { Console.WriteLine("Place a bet"); int temp; string input; do { temp = 1; input = Console.ReadLine(); try { temp = int.Parse(input); } catch { Console.WriteLine("We don't accept this currency"); } if (temp <= 0) { Console.WriteLine("The bet is too low"); } if (GameElements.CurrentBalance - temp < 0) { Console.WriteLine("You don't have this much money"); } } while (!int.TryParse(input, out temp) || temp <= 0 || GameElements.CurrentBalance - temp < 0); GameElements.Bet = temp; GameElements.CurrentBalance -= temp; GameElements.DrawSeparators("-"); }
public bool Split(int i) { try { if (String.Compare(Hands[i].Cards[0].Name, Hands[i].Cards[1].Name) == 0) { if (Hands[i].Cards.Count == 2) { if (DoubleTheBet()) { Hands.Add(new Hand()); Hands[i + 1].Cards.Add(Hands[i].Cards[1]); Hands[i].Cards.RemoveAt(1); Console.WriteLine("You've splitted your hand. Now you have {0} hands", Hands.Count); GameElements.DrawSeparators("-"); return(true); } return(false); } Console.WriteLine("It's not your first move"); return(false); } Console.WriteLine("Your first two cards don't have the same value"); return(false); } catch { Console.WriteLine("You don't even have two cards in this hand"); return(false); } }
public bool CroupiersMove() { int points = 0; foreach (Card card in Hands[0].Cards) { points += card.Value; } if (points <= 16) { Console.WriteLine("Croupier: Draw"); GameElements.DrawSeparators("-"); return(true); } else if (points == 21) { Console.WriteLine("Croupier has blackjack"); GameElements.DrawSeparators("-"); GameElements.CurrentBalance += GameElements.Insurance; return(false); } else { Console.WriteLine("Croupier: Stand"); GameElements.DrawSeparators("-"); return(false); } }
public bool DoubleTheBet() { if (GameElements.CurrentBalance - GameElements.Bet > 0) { GameElements.CurrentBalance -= GameElements.Bet; GameElements.Bet *= 2; GameElements.DrawSeparators("-"); Console.WriteLine("Current bet: {0}", GameElements.Bet); return(true); } Console.WriteLine("You don't have this much money"); return(false); }
public void Play() { while (GameElements.CurrentBalance > 0) { if (RoundNo != 1) { Console.WriteLine(); } GameElements.DrawSeparators("+"); Console.WriteLine("Round {0}", RoundNo); GameElements.DrawSeparators("+"); Round(); GameElements.Reset(); Reset(); RoundNo++; } Console.WriteLine("/nYou're out of money. Maybe next time Fortune will be in your favour"); }
public bool Insurance() { if (Hands[0].Cards[0].Name == "Ace") { if (GameElements.CurrentBalance - GameElements.Bet > 0) { GameElements.CurrentBalance -= GameElements.Bet; GameElements.Insurance = GameElements.Bet; GameElements.DrawSeparators("-"); Console.WriteLine("Current insurance: {0}", GameElements.Insurance); GameElements.DrawSeparators("-"); return(true); } Console.WriteLine("You don't have this much money"); return(false); } Console.WriteLine("Croupier's first card isn't an ace"); return(false); }
void PlayerBusted(int i) { if (Player.Hands.Count > 1) { GameElements.DrawSeparators("!"); Console.WriteLine("Hand no. {0} got busted", i); GameElements.DrawSeparators("!"); Player.Hands[i].Inactive = true; if (CheckIfAllInactive()) { RoundIsOver = true; } } else { GameElements.DrawSeparators("!"); Console.WriteLine("You busted"); GameElements.DrawSeparators("!"); RoundIsOver = true; } }
void Win(int i) { int playerPoints = 0; int croupierPoints = 0; foreach (Card card in Croupier.Hands[0].Cards) { croupierPoints += card.Value; } foreach (Card card in Player.Hands[i].Cards) { playerPoints += card.Value; } GameElements.DrawSeparators("!"); if (playerPoints > croupierPoints) { if (Player.Hands.Count == 1) { Console.WriteLine("You win"); } else { Console.WriteLine("Hand no. {i} wins", i); } GameElements.CurrentBalance += 2 * GameElements.Bet / Player.Hands.Count; } else { if (Player.Hands.Count == 1) { Console.WriteLine("You lose"); } else { Console.WriteLine("Hand no. {0} loses", i); } } GameElements.DrawSeparators("!"); RoundIsOver = true; }
void GetACard <T>(T receiver, bool show, int i) { try { if (receiver is Croupier) { ((Croupier)(object)receiver).Hands[i].Cards.Add(GameElements.Deck[GameElements.Deck.Count - 1]); } if (receiver is Player) { ((Player)(object)receiver).Hands[i].Cards.Add(GameElements.Deck[GameElements.Deck.Count - 1]); } } catch (IndexOutOfRangeException e) { GameElements.ReplaceDecks(); } GameElements.MoveToTheDiscardDeck(); if (show) { ShowAllCards(receiver, i); } }
void CheckIfBusted(List <Card> Hand, int i, bool player) { int points = 0; foreach (Card card in Hand) { points += card.Value; } if (points > 21) { for (int j = 0; j < Hand.Count; j++) { if (Hand[j].Name == "Ace" && Hand[j].Value == 11) { Hand[j].Value = 1; points -= 10; if (points <= 21) { return; } } } if (player) { PlayerBusted(i); } else { GameElements.DrawSeparators("!"); Console.WriteLine("Croupier busted"); GameElements.DrawSeparators("!"); GameElements.CurrentBalance += 2 * GameElements.Bet; RoundIsOver = true; } } }
void Round() { PlaceABet(); FirstDraw(); do { for (int i = 0; i < Player.Hands.Count; i++) { if (!Player.Hands[i].Inactive) { if (Player.Hands.Count > 1) { Console.WriteLine("Hand no. {0}|", i); Console.WriteLine("+++++++++++"); } PresentOptions(); GetInput: switch (GetInput()) { case 1: GameElements.DrawSeparators("-"); GetACard(Player, true, i); CheckIfBusted(Player.Hands[i].Cards, i, true); break; case 2: GameElements.DrawSeparators("-"); Console.WriteLine("Stand"); GameElements.DrawSeparators("-"); Player.Hands[i].Inactive = true; break; case 3: if (!Player.DoubleTheBet()) { goto GetInput; } GetACard(Player, true, i); CheckIfBusted(Player.Hands[i].Cards, i, true); Player.Hands[i].Inactive = true; break; case 4: if (!Player.Split(i)) { goto GetInput; } break; case 5: if (!Croupier.Insurance()) { goto GetInput; } break; } } if (CheckIfAllInactive()) { if (Croupier.CroupiersMove()) { GetACard(Croupier, true, 0); CheckIfBusted(Croupier.Hands[0].Cards, 0, false); } else { Win(i); } } } } while (!RoundIsOver); ShowAllCards(Croupier, 0); DisplayCurrentBlance(); }
public Player() { Hands = GameElements.ResetPlayerAndCroupier(); }