-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cs
More file actions
304 lines (285 loc) · 10.2 KB
/
Copy pathGame.cs
File metadata and controls
304 lines (285 loc) · 10.2 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
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarlosSeptica
{
public enum GameStatus
{
STATUS_NOT_STARTED,
STATUS_DISTRIBUTING_CARDS,
STATUS_YOUR_TURN,
STATUS_AI_TURN,
STATUS_WON,
STATUS_LOST,
STATUS_TIE
}
public class GameStatusHelper
{
public static string GetStatusMessage(GameStatus status)
{
switch (status)
{
case GameStatus.STATUS_NOT_STARTED:
return "Game not started yet!";
case GameStatus.STATUS_DISTRIBUTING_CARDS:
return "Distributing cards...";
case GameStatus.STATUS_YOUR_TURN:
return "Your turn";
case GameStatus.STATUS_AI_TURN:
return "Carlos is tinking...";
case GameStatus.STATUS_WON:
return "You won!";
case GameStatus.STATUS_LOST:
return "Carlos won!";
case GameStatus.STATUS_TIE:
return "Tie!";
}
// Should never reach this
return "Something unknown happened to the game!";
}
}
public class Game
{
public GameStatus Status
{
get;
set;
}
public GameState State
{
get;
}
private SepticaEngine septicaEngine;
private const int AI_DELAY = 5;
private int timeToWait = 0;
public Game()
{
Status = GameStatus.STATUS_NOT_STARTED;
State = new GameState();
Action onFinishGame = () =>
{
if (State.PlayerAI.Score > State.PlayerHuman.Score)
{
Status = GameStatus.STATUS_LOST;
}
else if (State.PlayerAI.Score < State.PlayerHuman.Score)
{
Status = GameStatus.STATUS_WON;
}
else
{
Status = GameStatus.STATUS_TIE;
}
};
Action onDistributeCards = () =>
{
Status = GameStatus.STATUS_DISTRIBUTING_CARDS;
};
Action onTurnChanged = () =>
{
bool ai = State.CurrentTurn.Type == PlayerType.PLAYER_AI;
Status = ai ? GameStatus.STATUS_AI_TURN : GameStatus.STATUS_YOUR_TURN;
if(Status == GameStatus.STATUS_AI_TURN)
{
timeToWait = AI_DELAY;
}
};
septicaEngine = new SepticaEngine(State, onFinishGame, onDistributeCards, onTurnChanged);
}
public void Start()
{
if(Status == GameStatus.STATUS_NOT_STARTED)
{
Status = GameStatus.STATUS_DISTRIBUTING_CARDS;
}
}
public void Update()
{
switch (Status)
{
case GameStatus.STATUS_NOT_STARTED:
{
break;
}
case GameStatus.STATUS_DISTRIBUTING_CARDS:
{
if (State.Dealer.IsEmpty())
{
Status = State.CurrentTurn.Type == PlayerType.PLAYER_AI ? GameStatus.STATUS_AI_TURN : GameStatus.STATUS_YOUR_TURN;
}
else
{
DistributeOneCard();
if (AreBothPlayersFullOfCards())
{
// First time after distributing cards select the first player
// Otherwise, the player is already selected by EndRound meth (also remember to do some meth)
if (State.CurrentTurn == null)
{
State.CurrentTurn = State.PlayerHuman;
}
Status = State.CurrentTurn.Type == PlayerType.PLAYER_AI ? GameStatus.STATUS_AI_TURN : GameStatus.STATUS_YOUR_TURN;
}
}
break;
}
case GameStatus.STATUS_YOUR_TURN:
{
break;
}
case GameStatus.STATUS_AI_TURN:
{
if(timeToWait == 0)
{
int aiTurn = MonteCarloEngine.GetAiNextMove(State);
if (aiTurn == -1)
{
Debug.WriteLine("Carlos selected to end round");
}
else
{
Debug.WriteLine("Carlos selected card " + aiTurn);
}
septicaEngine.PutCardDown(State.PlayerAI, aiTurn);
}
else
{
--timeToWait;
}
break;
}
case GameStatus.STATUS_WON:
{
break;
}
case GameStatus.STATUS_LOST:
{
break;
}
case GameStatus.STATUS_TIE:
{
break;
}
}
}
public void DistributeOneCard()
{
// Distribute cards to the round winner
// Or to human, in first round
bool firstRound = State.CurrentTurn == null;
Player first = firstRound ? State.PlayerHuman : State.CurrentTurn;
Player second = first.Type == PlayerType.PLAYER_AI ? State.PlayerHuman : State.PlayerAI;
if (second.CardsInHand >= first.CardsInHand)
{
if (!first.IsHandFull)
{
first.AddCardInHand(State.Dealer.GiveCard());
}
}
else
{
if (!second.IsHandFull)
{
second.AddCardInHand(State.Dealer.GiveCard());
}
}
}
public bool AreBothPlayersFullOfCards()
{
return State.PlayerHuman.IsHandFull && State.PlayerAI.IsHandFull;
}
public void OnPlayerClick(int x, int y)
{
if (Status == GameStatus.STATUS_YOUR_TURN)
{
int[] possibleMoves = septicaEngine.GetPossibleMoves(State.PlayerHuman);
if (possibleMoves.Length == 1 && possibleMoves[0] == -1) // If player chooses to finish the round without putting a card down
{
septicaEngine.PutCardDown(State.PlayerHuman, -1);
Debug.WriteLine("Player selected to end round");
}
else
{
int cardId = State.PlayerHuman.GetCardHandIndexAtCoords(x - 250, y - 450);
if(cardId != -1 && State.PlayerHuman.GetCardsInHand()[cardId] != null)
{
septicaEngine.PutCardDown(State.PlayerHuman, cardId);
Debug.WriteLine("Player selected card " + cardId);
}
else
{
if (possibleMoves.Contains(-1))
{
septicaEngine.PutCardDown(State.PlayerHuman, -1);
}
Debug.WriteLine("Player clicked on air!");
}
}
}
/*else if(Status == GameStatus.STATUS_AI_TURN)
{
// TODO: FOR TESTING ONLY, REMOVE FOR FUCK'S SAKE
int[] possibleMoves = septicaEngine.GetPossibleMoves(State.PlayerAI);
int cardId = State.PlayerAI.GetCardHandIndexAtCoords(x - 250, y - 50);
if (possibleMoves.Contains(cardId))
{
septicaEngine.PutCardDown(State.PlayerAI, cardId);
Debug.WriteLine("AI selected card " + cardId);
}
else
{
Debug.WriteLine("AI tried illegal move!");
}
}*/
}
public void Draw(Graphics g)
{
State.PlayerHuman.Draw(g, 250, 450);
State.PlayerAI.Draw(g, 250, 50);
State.Dealer.Draw(g, 50, 220);
State.Table.Draw(g, 300, 250);
// Draw status string
Font drawFont = new Font("Arial", 12, FontStyle.Bold);
SolidBrush drawBrush = new SolidBrush(Color.Black);
g.DrawString(GameStatusHelper.GetStatusMessage(Status), drawFont, drawBrush, 10, 630);
// Draw end round
if(Status == GameStatus.STATUS_YOUR_TURN)
{
int[] possibleMoves = septicaEngine.GetPossibleMoves(State.PlayerHuman);
Font statusFont = new Font("Arial", 18, FontStyle.Bold);
if (possibleMoves.Contains(-1))
{
if(possibleMoves.Length > 1)
{
g.DrawString("Click anywhere if", statusFont, drawBrush, 10, 490);
g.DrawString(" ya wanna end!", statusFont, drawBrush, 10, 520);
}
else
{
g.DrawString(" No moves left!", statusFont, drawBrush, 10, 490);
g.DrawString("Click to end round!", statusFont, drawBrush, 10, 520);
}
}
}
}
public static int GetSimulationsNumber()
{
int difficulty = CarlosSeptica.interfaceForm.GetDifficulty();
switch (difficulty){
case 0:
return 5;
case 1:
return 10;
case 2:
return 50;
case 3:
return 15000;
}
throw new Exception("Difficulty not selected");
}
}
}