/
FiveCardDraw.cs
111 lines (93 loc) · 3.28 KB
/
FiveCardDraw.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
using ConsoleAppClasses;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lab10FiveCard
{
class FiveCardDraw
{
/// <summary>
/// Gets or sets the Cards in the Player's hand.
/// </summary>
public List<Card> PlayerHand { get; set; }
/// <summary>
/// Gets or sets the Cards in the Deck.
/// </summary>
public DeckOfCards deckOfCards { get; set; }
/// <summary>
/// Creates a new game of Five Card Draw with a deckof 52 cards.
/// </summary>
public FiveCardDraw()
{
deckOfCards = new DeckOfCards();
deckOfCards.Shuffle();
PlayerHand = new List<Card>();
}
private bool[] Discard;
/// <summary>
/// Play a hand of 5 card draw.
/// </summary>
public void PlayRound()
{
Discard = new bool[5] { false, false, false, false, false };
string userChoice;
int discardMe = 0;
//deal five cards to player
for (int n = 0; n < 5; n++)
{
PlayerHand.Add(deckOfCards.Deal());
}
//discard loop
do
{
ShowPlayerHand();
Console.WriteLine("Type the number of the card in your hand and hit Enter to toggle between Keep/Discard.");
Console.Write("Type 'D' to finalize dicard or 'exit' to quit game? ");
userChoice = Console.ReadLine();
if (userChoice.ToLower() == "exit") { System.Environment.Exit(0); }
//input validation loop
while (userChoice != "D" && !int.TryParse(userChoice, out discardMe))
{
Console.WriteLine("Invalid choice. Try Again: ");
userChoice = Console.ReadLine();
}
//toggle element using ternary operator.
if (userChoice != "D" && discardMe > 0 && discardMe < 6)
{
Discard[discardMe - 1] = Discard[discardMe - 1] == true ? false : true;
}
} while (userChoice != "D");
FinalizeDiscard();
ShowPlayerHand();
}//play Round
private void FinalizeDiscard()
{
for (int n = 0; n < PlayerHand.Count; n++)
{
//if card marked to be discarded, replace with card from deck.
if (Discard[n])
{
deckOfCards.Deck.Add(PlayerHand[n]);
PlayerHand[n] = deckOfCards.Deal();
}
}
//reset Discard bool array.
Discard = new bool[5] { false, false, false, false, false };
}
/// <summary>
/// Display each card in the Player's hand.
/// </summary>
public void ShowPlayerHand()
{
for (int n = 0; n < PlayerHand.Count; n++)
{
if (!Discard[n])
Console.WriteLine("Card #" + (n + 1) + ": " + PlayerHand[n]);
else
Console.WriteLine("Card #" + (n + 1) + ": " + PlayerHand[n] + "<---Discard");
}
}
}
}