-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSepticaEngine.cs
232 lines (209 loc) · 7.17 KB
/
SepticaEngine.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarlosSeptica
{
public class SepticaEngine
{
public GameState State
{
get;
set;
}
private Action onFinishGame;
private Action onDistributeCards;
private Action onTurnChanged;
public SepticaEngine(GameState gameState, Action onFinishGame, Action onDistributeCards, Action onTurnChanged)
{
State = gameState;
this.onFinishGame = onFinishGame;
this.onDistributeCards = onDistributeCards;
this.onTurnChanged = onTurnChanged;
}
public SepticaEngine(GameState gameState)
{
State = gameState;
this.onFinishGame = () => { };
this.onDistributeCards = () => { };
this.onTurnChanged = () => { };
}
public void EndRound(Player player)
{
Player winner = State.Table.HandOwner;
foreach (Card c in State.Table.Cards)
{
winner.AddCardToStack(c);
}
State.Table.Cards.Clear();
if (AnyCardsLeft())
{
SetTurn(winner);
onDistributeCards();
}
else
{
onFinishGame();
}
}
public void PutCardDown(Player player, int index)
{
if(player == State.CurrentTurn)
{
if (index == -1)
{
if (CanEndRound(player))
{
EndRound(player);
// EndRound changes the turn to winner automatically
}
else
{
DebugMessage(player, "tried to end round prematurely!");
}
}
else
{
if (GetPossibleMoves(player).ToList().Contains(index))
{
Card card = player.GetCardsInHand()[index];
State.Table.Cards.Add(card);
player.GetCardsInHand()[index] = null;
if (card.Number == CardNumber.CARD_7 || card.Number == State.Table.Cards.First().Number)
{
State.Table.HandOwner = player;
}
FlipTurn();
// SHIT FUCK DICK THIS CHECK IS DONE IN UI AND YOU CAN ONLY END
// BECAUSE YOU CAN ONLY END, YOU LITTLE DICK
/*int[] opponentPossibleMoves = GetPossibleMoves(State.CurrentTurn);
if (opponentPossibleMoves.Length == 1 && opponentPossibleMoves[0] == -1)
{
EndRound(State.CurrentTurn);
}*/
}
else
{
DebugMessage(player, "tried to put down inexistent card or to do illegal move!");
}
}
}
else
{
DebugMessage(player, "tried to move when it's not his turn!");
}
}
public bool EvenCardsDown()
{
return State.Table.Cards.Count() % 2 == 0;
}
public bool CanEndRound(Player player)
{
if(State.CurrentTurn != player)
{
DebugMessage(player, "asked to end opponent's round!");
}
return State.Table.Cards.Count > 0 && EvenCardsDown();
}
public CardNumber[] GetPossibleCardsDown()
{
List<Card> cardsDown = State.Table.Cards;
if(cardsDown.Count == 0 || !EvenCardsDown())
{
return new CardNumber[] {
CardNumber.CARD_7,
CardNumber.CARD_8,
CardNumber.CARD_9,
CardNumber.CARD_10,
CardNumber.CARD_J,
CardNumber.CARD_Q,
CardNumber.CARD_K,
CardNumber.CARD_A,
};
}
else
{
// Even number of cards down
if (cardsDown.Last().Number == CardNumber.CARD_7 || cardsDown.Last().Number == cardsDown.First().Number)
{
if(cardsDown.First().Number != CardNumber.CARD_7)
{
// Something and 7 down
return new CardNumber[]
{
CardNumber.CARD_7,
cardsDown.First().Number
};
}
else
{
// Only 7s down
return new CardNumber[]
{
CardNumber.CARD_7,
};
}
}
else
{
return new CardNumber[] { };
}
}
}
public int[] GetPossibleMoves(Player player)
{
CardNumber[] possibleCardsDown = GetPossibleCardsDown();
List<int> possibleMoves = new List<int>();
foreach(CardNumber card in possibleCardsDown)
{
for(int i = 0; i < player.GetCardsInHand().Length; ++i)
{
// If player has a card in hand in this position and the card can be put down
if(player.GetCardsInHand()[i] != null && player.GetCardsInHand()[i].Number == card)
{
possibleMoves.Add(i);
}
}
}
if (CanEndRound(player))
{
possibleMoves.Add(-1);
}
return possibleMoves.ToArray();
}
public void FlipTurn()
{
bool ai = State.CurrentTurn.Type == PlayerType.PLAYER_AI;
State.CurrentTurn = ai ? State.PlayerHuman : State.PlayerAI;
onTurnChanged();
}
/**
* Sets next turn to player
* The player will be the next who puts down card
*/
public void SetTurn(Player player)
{
bool ai = player.Type == PlayerType.PLAYER_AI;
State.CurrentTurn = player;
onTurnChanged();
}
public bool AnyCardsLeft()
{
return !State.Dealer.IsEmpty() || State.PlayerHuman.CardsInHand > 0 || State.PlayerAI.CardsInHand > 0 || State.Table.Cards.Count > 0;
}
public bool IsGameDone()
{
return !AnyCardsLeft();
}
private static void DebugMessage(Player player, string message)
{
Debug.WriteLine(((player.Type == PlayerType.PLAYER_AI) ? "AI" : "Human") + " " + message);
}
private static void DebugMessage(string message)
{
Debug.WriteLine(message);
}
}
}