-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
168 lines (156 loc) · 5.83 KB
/
Copy pathProgram.cs
File metadata and controls
168 lines (156 loc) · 5.83 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
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
namespace rummikool;
public class Program {
private static readonly Random Rng = new();
private static readonly List<Tile> bag = [];
private static readonly List<List<Tile>> board = [];
private static Color ParseColor(char c) => c switch {
'R' => Color.RED,
'G' => Color.GREEN,
'B' => Color.BLUE,
'Y' => Color.YELLOW,
'J' => Color.JOKER,
_ => throw new FormatException($"Invalid color: {c}")
};
private static Tile ParseTile(string token) {
if (token == "J")
return new Tile(0, Color.JOKER);
if (token.Length < 2)
throw new FormatException($"Invalid tile token: {token}");
Color color = ParseColor(token[0]);
ushort value = ushort.Parse(token[1..]);
if (value < 1 || value > 13)
throw new FormatException($"Invalid tile value: {value}");
return new Tile(value, color);
}
private static Player[] ParseGameFile(string path, List<Tile> bag) {
if (!File.Exists(path))
throw new FileNotFoundException(path);
bag.Clear();
foreach (Color color in Enum.GetValues<Color>()) {
for (ushort v = 1; v <= 13; v++) {
bag.Add(new Tile(v, color));
bag.Add(new Tile(v, color));
}
}
for (int j = 0; j < 4; j++)
bag.Add(new Tile(0, Color.JOKER));
List<Player> players = [];
foreach (string line in File.ReadAllLines(path)) {
if (string.IsNullOrWhiteSpace(line))
continue;
string[] parts = line.Split(':', 2);
if (parts.Length != 2)
throw new FormatException($"Invalid line: {line}");
string name = parts[0];
string[] tiles = parts[1].Split(',', StringSplitOptions.RemoveEmptyEntries);
List<Tile> hand = [];
foreach (string token in tiles) {
Tile tile = ParseTile(token.Trim());
Tile? match = bag.FirstOrDefault(
t => t.Value == tile.Value && t.Color == tile.Color)
?? throw new InvalidOperationException($"Tile not available: {token}");
bag.Remove(match);
hand.Add(match);
}
players.Add(new Player(name, hand));
}
return [.. players];
}
private static void Shuffle(List<Tile> bag) {
for (int i = bag.Count - 1; i > 0; i--) {
int j = Rng.Next(i + 1);
(bag[i], bag[j]) = (bag[j], bag[i]);
}
}
private static List<Tile> TakeTiles(List<Tile> bag, int count = 14) {
if (bag.Count < count)
throw new InvalidOperationException("Not enough tiles in bag.");
List<Tile> hand = bag.GetRange(0, count);
bag.RemoveRange(0, count);
return hand;
}
private static Tile? TakeTile(List<Tile> bag) {
if (bag.Count == 0)
return null;
Tile taken = bag[0];
bag.RemoveAt(0);
return taken;
}
private static void PrintPlayers(Player[] players) {
foreach (Player player in players) {
Console.Write($"{player.Name}: ");
foreach (Tile tile in player.Hand)
Console.Write($"{tile} ");
Console.WriteLine();
}
}
private static void PrintBoard(List<List<Tile>> board) {
foreach (List<Tile> group in board) {
foreach (Tile tile in group)
Console.Write($"{tile} ");
Console.WriteLine();
}
}
public static void Main(string[] args) {
Player[] players;
if (args.Length > 1) {
Console.WriteLine("Error: Invalid arguments.");
return;
}
if (args.Length == 1) {
try {
players = ParseGameFile(args[0], bag);
} catch {
Console.WriteLine("Error: Failed to parse game file.");
return;
}
} else {
Console.Write("How many players? (2-4) ");
int playerCount = Convert.ToInt32(Console.ReadLine());
if (playerCount < 2 || playerCount > 4) {
Console.WriteLine("Error: Invalid player count.");
return;
}
foreach (Color color in new[] { Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW }) {
for (ushort j = 1; j <= 13; j++) {
bag.Add(new(j, color));
bag.Add(new(j, color));
}
}
for (int j = 0; j < 4; j++)
bag.Add(new Tile(0, Color.JOKER));
Shuffle(bag);
players = new Player[playerCount];
for (int p = 0; p < playerCount; p++)
players[p] = new($"P{p + 1}", TakeTiles(bag));
}
PrintPlayers(players);
Console.Write("(Start Game...)");
Console.ReadLine();
Player? winner = null;
int turn = 0;
while (winner == null) {
Console.WriteLine(Format.BoldText($"Turn {++turn}:"));
foreach (Player player in players) {
Console.WriteLine(Format.BoldText($"{player.Name}'s turn."));
bool played = player.Initiated
? player.TryPlay(board)
: player.TryInitiate(board);
if (!played) {
Tile? drawn = TakeTile(bag);
if (drawn != null)
player.Hand.Add(drawn);
}
PrintPlayers(players);
PrintBoard(board);
if (player.Hand.Count == 0) {
winner = player;
break;
}
Console.Write("(Next turn...)");
Console.ReadLine();
}
}
Console.WriteLine($"Winner: {winner!.Name}");
}
}