-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cs
177 lines (146 loc) · 4.72 KB
/
Game.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
using System;
using Poker.Core;
using Poker.Player;
using Poker.UI;
namespace Poker;
public class Game
{
public int startingChips;
public Table table;
public TableUI tableUI;
public List<Core.Player> players;
public List<PlayerUI> playerUIs;
private readonly FoldButton foldButton;
private readonly CallButton callButton;
private readonly RaiseButton raiseButton;
private readonly Slider slider;
public Match match;
public const int matches = 50;
public Game(List<string> playerNames, int startingChips = 200, int smallBlind = 1, int bigBlind = 2)
{
this.startingChips = startingChips;
// Slider
slider = new Slider(Settings.Slider.PosX, Settings.Slider.PosY, Settings.Slider.Length, Settings.Slider.IsHorizontal);
// Create buttons
foldButton = (FoldButton) MakeChoiceButton("Fold");
callButton = (CallButton) MakeChoiceButton("Call");
raiseButton = (RaiseButton) MakeChoiceButton("Raise");
Pot pot = new();
players = new();
playerUIs = new();
// For now just stick with humans
CreatePlayer(playerNames[0], startingChips, pot, Core.Player.Type.Human);
CreatePlayer(playerNames[1], startingChips, pot, Core.Player.Type.RandomBot);
table = new Table(players, pot, smallBlind, bigBlind);
table.OnGameOver += HandleEndOfRound;
tableUI = new TableUI(table);
match = new(players, matches);
}
public void StartGame()
{
table.NewRound();
}
public void HandleEndOfRound()
{
// update the wins of the player in the match, and start a new match if there are still some left
for (int i = 0; i < table.NumOfPlayers; i++)
{
if (table.players[i].chips > 0)
{
match.wins[i]++;
}
}
if (match.matches > 1)
{
// Reset player chips
foreach (Core.Player player in players)
{
player.chips = startingChips;
}
match.matches--;
StartGame();
}
else
{
// End of game
match.PrintResults();
Environment.Exit(0);
}
}
public void Display()
{
tableUI.Display();
match.Display();
foreach (PlayerUI playerUI in playerUIs)
{
playerUI.Display();
}
foldButton.Render();
callButton.Render();
raiseButton.Render();
slider.Render();
}
public void Update()
{
foreach (Core.Player player in players)
{
player.Update();
}
Display();
}
private Button MakeChoiceButton(string choice)
{
int[][] bp = Settings.Button.ButtonPositions;
Button button = choice switch
{
"Fold" => new FoldButton(bp[0][0], bp[0][1]),
"Call" => new CallButton(bp[1][0], bp[1][1]),
"Raise" => new RaiseButton(bp[2][0], bp[2][1], slider),
_ => throw new Exception($"Invalid choice: {choice}"),
};
ButtonAction buttonAction = new ButtonAction(choice);
button.action = buttonAction;
button.OnClick += () => ExecuteButtonAction(buttonAction);
return button;
}
// This isn't strictly needed but is more convenient
private void MakeMove(Core.Action action)
{
table.MakeMove(action);
}
/// <summary>
/// Creates a player and UI and adds it to the game.
/// </summary>
private void CreatePlayer(string name, int chips, Pot pot, Core.Player.Type type)
{
Core.Player player = type switch
{
Core.Player.Type.Human => new Human(name, chips, pot, callButton, raiseButton),
Core.Player.Type.RandomBot => Bot.GetBotFromBotType(type, chips, pot),
_ => throw new Exception($"Invalid player type: {type}"),
};
player.PlayAction += MakeMove;
players.Add(player);
playerUIs.Add(new PlayerUI(player, playerUIs.Count));
}
private void ExecuteButtonAction(ButtonAction buttonAction)
{
if (players[table.playerToMove] is Human player)
{
switch (buttonAction.choice)
{
case "Fold":
player.Decided(new Fold());
break;
case "Call":
player.Decided(new Call(player.GetAmountToCall()));
break;
case "Raise":
player.Decided(new Raise(buttonAction.amount));
break;
default:
throw new Exception($"Invalid choice: {buttonAction.choice}");
}
}
}
}