-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
174 lines (156 loc) · 4.19 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.Collections.Generic;
namespace BlackJack
{
class Program
{
static void Main(string[] args)
{
var gamesWon = 0;
var gamesLost = 0;
var gamesTied = 0;
// Set up player and dealer.
Deck deck = new Deck();
deck.Shuffle();
Player dealer = new Player("dealer");
Player player = new Player("player");
System.Console.WriteLine("Welcome to Black Jack!");
// Game loop.
while(true)
{
System.Console.WriteLine("New Round has started...");
var isRoundOver = false;
var isWinner = false;
var bet = 0;
dealer.Draw(deck);
player.Draw(deck);
dealer.Draw(deck);
player.Draw(deck);
Console.WriteLine("Dealers visible card:");
dealer.ShowSecondCard();
Console.WriteLine("");
Console.WriteLine("Your cards are:");
player.ShowCards();
Console.WriteLine("");
//Ask for bet.
while(true)
{
Console.WriteLine($"Bet between 1 and {player.coins}");
string tryint = Console.ReadLine();
if (Int32.TryParse(tryint, out int intBet))
{
bet = intBet;
}
else
{
bet = 0;
}
if(bet >= 1 && bet <= player.coins)
{
player.PlaceBet(bet);
Console.WriteLine($"Player has {player.coins} coins remaining.");
break;
}
}
// Player draws.
while(!isRoundOver && player.getHandScore <= 21)
{
// Ask if player hits or stays.
Console.WriteLine("Hit? y/n");
string input = Console.ReadLine();
// If player hits, draw and show new card.
if (input == "y"){
Card dealtCard = player.Draw(deck);
player.ShowCardA(dealtCard.getStringVal, dealtCard.getSuit);
}
// If player stays, round ends and dealer may draw.
else
{
isRoundOver = true;
}
// Change Ace value to 1 if necessary.
player.CheckAce();
}
// Dealer draws.
while (dealer.getHandScore < 15)
{
dealer.Draw(deck);
Console.WriteLine($"Dealer draws card #{dealer.getCardCount}.");
// Change Ace value to 1 if necessary.
dealer.CheckAce();
}
//Round is over, display all cards.
Console.WriteLine("");
Console.WriteLine("~~~~~~Dealers cards are:~~~~~~");
dealer.ShowCards();
Console.WriteLine("");
Console.WriteLine("~~~~~~Your cards are:~~~~~~");
player.ShowCards();
Console.WriteLine($"Dealer total is {dealer.getHandScore}.");
Console.WriteLine($"Player total is {player.getHandScore}.");
// Check win condition.
if (player.getHandScore > 21)
{
gamesLost += 1;
player.PrintLose();
}
else if (dealer.getHandScore > 21)
{
gamesWon += 1;
isWinner = true;
player.PrintWin();
}
else if (dealer.getHandScore == player.getHandScore)
{
gamesTied += 1;
player.coins += bet;
player.PrintPush();
}
else if (dealer.getHandScore > player.getHandScore)
{
gamesLost += 1;
player.PrintLose();
}
else
{
gamesWon += 1;
isWinner = true;
player.PrintWin();
}
// Clear cards and reset deck.
player.DiscardAll();
dealer.DiscardAll();
deck.Reset();
deck.Shuffle();
//Update Coins (and reset bet).
player.BetResult(isWinner);
// Display winnings.
Console.WriteLine($"Player has {player.coins} coins.");
// Check if player can play again.
if(player.coins <= 0)
{
break;
}
// Play again or end game.
Console.WriteLine("Play again? y/n");
string again = Console.ReadLine();
if (again != "y")
{
break;
}
}
// Display game end condition result.
Console.WriteLine("#########################################");
Console.WriteLine($" Won: {gamesWon} Lost: {gamesLost} Tied: {gamesTied} ");
if (player.coins > 0 )
{
Console.WriteLine($" Player ended the game with {player.coins} coins. ");
}
else
{
Console.WriteLine(" You are broke. Goodbye! ");
}
Console.WriteLine("#########################################");
}
}
}