-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
386 lines (268 loc) · 10.2 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
namespace Max
{
public class Program
{
static List<Card> communityCards = new List<Card>(); // the five cards in the middle of the poker table
static Deck deck = new Deck(2);
static int Main()
{
Console.OutputEncoding = Encoding.UTF8;
Player[] players;
{
int temp = UI.inputI("How many players are joining?: ") ?? 0;
players = new Player[(temp < 2) ? 2 : temp];
int balance = UI.inputI("What should be the balance of the players?: ") ?? 1000;
////////////////////////////////////////////////////////////////////////Dealing cards
deck.shuffle(5);
for (int i = 0; i < players.Length; ++i)
{
players[i] = new Player(balance);
players[i].updateHand(deck.first());
}
}
UI.print("Done with dealing first cards");
for (int i = 0; i < players.Length; ++i)
{
players[i].updateHand(deck.first());
}
UI.print("Done with dealing second cards");
////////////////////////////////////////////////////////////////////////
UI.Reset();
UI.print($"{deck.cards.Count / 52} decks of cards in the deck - {deck.cards.Count} cards in the game - {players.Length} players online", 0, 0, true);
UI.print(" ------------------------------------------------------------------", 0, 15, true);
UI.print("| 'Space' for check | 'Z' for call | 'X' for raise | 'W' for fold |", 0, 16, true);
UI.print(" ------------------------------------------------------------------", 0, 17, true);
UI.print("What will you do?", 0, 18, true);
////////////////////////////////////////////////////////////////////////Game loop
ConsoleKey key;
for (int turn = 0; ; ++turn) //(int turn = 0; turn < 3; ++turn) //The game loop
{
for (int playerIndex = 0; playerIndex < players.Length; ++playerIndex)
{
int i = 0;
communityCards.ForEach(
x => {
UI.print(x.getCard(), 5 + i * 12, 8, true);
++i;
});
UI.print($"{players[playerIndex].balance}", 80, 5, true);
UI.print($"{players[playerIndex].bet}", 80, 6, true);
UI.print($"{players[playerIndex].hand[0].getCard()} {players[playerIndex].hand[1].getCard()}", 15, 20, true);
if (players[playerIndex].folded) continue;
key = Console.ReadKey(intercept: true).Key;
UI.Reset();
// check, call, raise, or fold
switch (key)
{
case ConsoleKey.Spacebar: //check
UI.print("Check ", 2, 4, true);
break;
case ConsoleKey.Z: //call
if (!players[playerIndex].newBet(players[(playerIndex - 1 < 0) ? (players.Length + playerIndex - 1) : (playerIndex - 1)].bet))
{
players[playerIndex].allIn();
}
UI.print($"Called for {players[playerIndex].bet}$", 2, 4, true);
break;
case ConsoleKey.X: //raise
if (!players[playerIndex].newBet(UI.inputI("What will be your new bet?: ", 0, 22) ?? players[playerIndex].bet))
{
players[playerIndex].allIn();
}
UI.print($"Raised to {players[playerIndex].bet}$ ", 2, 4, true);
break;
case ConsoleKey.W: //fold
UI.print($"{"Max"} folded ", 2, 4, true);
players[playerIndex].fold();
break;
case ConsoleKey.Escape:
return 0;
}
}
communityCardsAdd(turn);
}
//checking for the highest pair
for (int playerIndex = 0; playerIndex < players.Length; ++playerIndex)
{
}
return 0;
}
public static void communityCardsAdd(int turn)
{
if (turn > 2) return;// 5 -1
deck.first(); //burn
if (turn == 0) { communityCards.Add(deck.first()); communityCards.Add(deck.first()); }
communityCards.Add(deck.first());
}
}
public class Player
{
public Card[] hand { get; private set; } = new Card[2];
public int balance { get; private set; }
public int bet { get; private set; } = 0;
public bool folded { get; private set; } = false;
public Player(int balance)
{
this.balance = balance;
}
public void fold(bool activate = false)
{
folded = true;
if (activate) folded = false;
}
public void allIn()
{
bet = balance;
}
public bool newBet(int x)
{
if (x > balance) return false;
bet = x;
return true;
}
public void updateHand(Card card)
{
if (hand[0].Equals(new Card())) hand[0] = card;
else if (hand[1].Equals(new Card())) hand[1] = card;
}
}
public class Deck
{
public List<Card> cards { get; private set; } = new List<Card>();
public Deck(int decks)
{
for (int i = 0; i < decks; ++i)
{
for (int j = 2; j <= 14; ++j)
{
for (int k = 0; k < 4; ++k)
{
cards.Add(new Card(j, k));
}
}
}
}
public void shuffle(int times)
{
for (int i = 0; i < times; ++i)
{
for (int j = 0; j < cards.Count; ++j)
{
int index = Random.Shared.Next(cards.Count);
Card temp = cards[index];
cards[index] = cards[j];
cards[j] = temp;
}
}
}
public Card first()
{
Card c = cards[0];
cards.RemoveAt(0);
return c;
}
}
public struct Card
{
public int value { get; private set; }
public int suit { get; private set; }
public Card(int value, int suit)
{
this.value = value;
this.suit = suit;
}
public string getSuit() => new[] { "♥️", "♠️", "♦️", "♣️" }[suit]; //To get get suit of the card
public string getValue()
{
//Return for value or number of the card
if (value <= 10) return value.ToString();
return "JDKA"[value - 11] + "";
}
public string getCard() => $"{getValue()} {getSuit()}";
}
public static class UI
{
static List<(string, int, int)> buffer = new List<(string, int, int)>();
static UI()
{
Console.CursorVisible = false;
}
public static void print(object text)
{
Console.Write(text);
}
/// <summary>
///
/// </summary>
/// <param name="text"></param>
/// <param name="x"></param>
/// <param name="y"></param>
public static void print(object text, int x = 0, int y = 0, bool sticky = false ) // the name sticky comes from CSS, where it is used to make an element fixed or sticked to one position on the screen and not an the body
{
Console.SetCursorPosition(x, y);
Console.Write(text);
if (sticky && buffer.Contains(((string)text, x, y))) buffer.Remove( ((string)text, x, y) );
if (sticky) buffer.Add((text.ToString() ?? "", x, y));
}
public static string inputS(string text)
{
print(text);
return Console.ReadLine() ?? "";
}
/// <summary>
///
/// </summary>
/// <param name="text"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns>String form of the input using consol stream</returns>
public static string inputS(string text, int x, int y, int space = 1)
{
print(text, x, y);
return Console.ReadLine() ?? "";
}
public static int? inputI(string text)
{
print(text);
if (!int.TryParse(Console.ReadLine(), out int value)) return null;
return value;
}
/// <summary>
///
/// </summary>
/// <param name="text"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns>String form of the input using consol stream</returns>
public static int? inputI(string text, int x, int y)
{
print(text, x, y);
if (!int.TryParse(Console.ReadLine(), out int value)) return null;
return value;
}
public static void cursorLineReset()
{
Console.WriteLine();
}
public static void Reset(bool cleanBuffer = false)
{
Console.Clear();
if (cleanBuffer)
{
buffer.Clear();
}
else
{
foreach ((string, int, int) e in buffer.ToArray())
{
print(e.Item1, e.Item2, e.Item3);
}
}
}
}
}