-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cs
More file actions
140 lines (111 loc) · 3.3 KB
/
Game.cs
File metadata and controls
140 lines (111 loc) · 3.3 KB
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
// NOTE namespace is similar to export, but multiple definitions can be bundled into the same namespace
using pig_game.models;
namespace pig_game;
// ✅ each player takes turns rolling dice
// ✅ each player can roll the dice as many times as they want
// ✅ if you roll a 1, your turn is over and you get no points
// ✅ you must get a score of 20 to win the game
public class Game
{
private List<Player> Players { get; set; }
public Game()
{
Console.WriteLine("Starting game");
Players = [];
GetPlayerNames();
for (int i = 0; i < Players.Count; i++)
{
Player player = Players[i];
Console.WriteLine($"Player {i + 1} is {player.Name}");
}
for (int i = 0; i <= Players.Count; i++)
{
// NOTE starts loop over (only works if conditional is changed to <=)
if (i == Players.Count)
{
i = 0;
}
Player player = Players[i];
Console.Clear();
Console.WriteLine($"Turn for {player.Name}. Score is {player.Score}");
RollDice(player);
if (player.Score >= 20)
{
break; // ends loop
}
}
Player? winningPlayer = Players.Find(player => player.Score >= 20);
if (winningPlayer == null)
{
throw new Exception("UH OH CODE IS BAD");
}
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"{winningPlayer.Name.ToUpper()} WINS!");
Console.ResetColor();
}
private void GetPlayerNames()
{
Console.WriteLine("Please input a player name");
string? playerName = Console.ReadLine(); // pause the code and wait for the user to type into the terminal and hit enter
if (string.IsNullOrWhiteSpace(playerName))
{
Console.WriteLine("Player name must be at least 1 character");
// recursion
GetPlayerNames();
return;
}
Console.WriteLine("Player name is " + playerName);
Player player = new Player(playerName);
Players.Add(player);
// NOTE count is equivalent to length
if (Players.Count == 1)
{
GetPlayerNames();
return;
}
Console.WriteLine("Would you like to add another player? y/n");
var keyPressed = Console.ReadKey();
Console.WriteLine();
if (keyPressed.Key == ConsoleKey.Y) // if the user pressed the y key
{
GetPlayerNames();
return;
}
}
private void RollDice(Player player)
{
int diceRoll = new Random().Next(1, 7);
if (player.IsMick)
{
diceRoll = new Random().Next(1, 4);
}
Console.WriteLine($"{player.Name} rolled a {diceRoll}");
if (diceRoll == 1)
{
player.RunningScore = 0;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("YOU ROLLED A 1, TOO BAD IDIOT");
Console.ResetColor();
// NOTE pauses that application for 1 second
Thread.Sleep(1000);
return;
}
player.RunningScore += diceRoll;
if (player.RunningScore + player.Score >= 20)
{
player.Score += player.RunningScore;
return;
}
Console.WriteLine($"Your running score is {player.RunningScore}");
Console.WriteLine("Press space to roll again");
var keyPressed = Console.ReadKey();
Console.WriteLine();
if (keyPressed.Key == ConsoleKey.Spacebar)
{
RollDice(player);
return;
}
player.Score += player.RunningScore;
player.RunningScore = 0;
}
}