-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cs
223 lines (179 loc) · 6.61 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
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
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
public abstract class DataLoader {
public static JsonSerializerSettings Settings = new JsonSerializerSettings();
static DataLoader()
{
Settings.Converters.Add(new Converter_CardRarity());
Settings.Converters.Add(new Converter_TupleFloatFloat());
Settings.Converters.Add(new Converter_TupleIntInt());
Settings.DefaultValueHandling = DefaultValueHandling.Ignore;
Settings.NullValueHandling = NullValueHandling.Ignore;
}
}
public class Game {
protected static Game m_Instance = null;
public static Game Instance {
get {
return m_Instance;
}
}
public static void DestroyInstance()
{
m_Instance = null;
}
protected List<Player> m_Players = new List<Player>();
protected int m_InitialPlayerIndex = 0;
public class Config {
[JsonProperty("maxMana")]
public int MaxMana;
[JsonProperty("maxHealth")]
public int MaxHealth;
[JsonProperty("initialHandSize")]
public int InitialHandSize;
[JsonProperty("manaIncrement")]
public int ManaIncrement;
[JsonProperty("cardDrawSize")]
public int CardDrawSize;
}
public int RoundCount {
get;
protected set;
}
public Config CurrentConfig {
get;
protected set;
}
public static void Create(string configPath, params string[] playerDefs)
{
m_Instance = new Game(configPath, playerDefs);
}
public static void Create( string gameDefPath )
{
string encodedData = File.ReadAllText(gameDefPath);
var gameDef = JsonConvert.DeserializeObject<Dictionary<string, object>>(encodedData, DataLoader.Settings);
List<string> playerDefs = JsonConvert.DeserializeObject<List<string>>(gameDef["players"].ToString());
Create(gameDef["config"].ToString(), playerDefs.ToArray());
}
protected Game(string configPath, params string[] playerDefs)
{
string encoded = File.ReadAllText(configPath);
CurrentConfig = JsonConvert.DeserializeObject<Game.Config>(encoded, DataLoader.Settings);
CreateNew(playerDefs);
}
protected Game( Config config, params string[] playerDefs )
{
CurrentConfig = config;
CreateNew(playerDefs);
}
public void CreateNew(params string[] playerDefs)
{
RoundCount = 0;
try
{
for (int ix = 0; ix < playerDefs.Length; ++ix)
{
string encodedPlayer = File.ReadAllText(playerDefs[ix]);
var pseudoPlayer = JsonConvert.DeserializeObject<Dictionary<string, object>>(encodedPlayer, DataLoader.Settings);
Deck deck = DeckLoader.LoadFrom(pseudoPlayer["deck"].ToString());
deck.Shuffle();
deck.Shuffle();
Player player = new Player(CurrentConfig.MaxHealth, CurrentConfig.MaxMana, deck);
player.Name = pseudoPlayer["name"].ToString();
m_Players.Add(player);
player.DrawCards(CurrentConfig.InitialHandSize);
}
using (var rngSlip = Neo.Utility.DataStructureLibrary<Random>.Instance.CheckOut(DateTime.UtcNow.Millisecond))
{
m_InitialPlayerIndex = rngSlip.Value.Next(0, playerDefs.Length);
}
}
catch( Exception ex )
{
Console.WriteLine(ex.Message);
}
}
protected void ResetGame()
{
}
public bool Execute()
{
try
{
for (int ix = m_InitialPlayerIndex; ix < m_Players.Count; ix = (ix + 1) % m_Players.Count)
{
if(ix == m_InitialPlayerIndex)// Basically show this everytime start player comes up
{
Console.WriteLine("\n\n***************** Round {0} *****************", ++RoundCount);
}
var player = m_Players[ix];
var otherPlayer = m_Players[(ix + 1) % m_Players.Count];
if (player.CurrentHealth <= 0)
{
Console.Clear();
Console.WriteLine("***************** Congratulations *****************\n");
Console.WriteLine(string.Format("{0} has one the game!!\n", otherPlayer.Name));
//TODO: Would like to have a way to play again.
Console.WriteLine("Press any key to quit...");
break;
}
Console.WriteLine(string.Format("\n\n***************** {0}'s turn ****************", player.Name));
player.ManaCrystals += CurrentConfig.ManaIncrement;
player.ManaCount = player.ManaCrystals;
player.DrawCards(CurrentConfig.CardDrawSize);
while (true)
{
Console.Write(player.ToString());
Console.ForegroundColor = player.ManaCount <= 0 || player.Hand.Count <= 0 ? ConsoleColor.Green : ConsoleColor.Yellow;
Console.WriteLine("d) Done with turn");
Console.ResetColor();
Console.WriteLine("q) Quit game");
Console.Write("Enter Selection: ");
//TODO: Feel like this should be in a better place
string line = Console.ReadLine();
if (line[0] >= '0' && line[0] <= '9')
{
player.PlayCard((line[0] - '0') - 1, otherPlayer);
}
else if (line[0] == 'd')
{
break;
}
else if (line[0] == 'q')
{
return false;
} else
{
Console.WriteLine("Unknown command! Please make a valid selection...");
System.Threading.Thread.Sleep(1000);
}
Console.WriteLine();
};
}
return true;
}
catch( Exception ex )
{
Console.WriteLine(ex.Message);
return false;
}
}
public bool IsOver {
get {
if( m_Players.Count <= 0 )
{
return true;
}
foreach( var player in m_Players )
{
if(player.CurrentHealth <= 0)
{
return true;
}
}
return false;
}
}
}