-
Notifications
You must be signed in to change notification settings - Fork 0
/
CampoMinado.cs
277 lines (240 loc) · 8.8 KB
/
CampoMinado.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
class CampoMinado
{
static bool exit = false;
const string title = " _____ __ __ _ _ \n" +
" / ____| | \\/ (_) | | \n" +
" | | __ _ _ __ ___ _ __ ___ | \\ / |_ _ __ __ _ __| | ___ \n" +
" | | / _` | '_ ` _ \\| '_ \\ / _ \\ | |\\/| | | '_ \\ / _` |/ _` |/ _ \\ \n" +
" | |___| (_| | | | | | | |_) | (_) | | | | | | | | | (_| | (_| | (_) |\n" +
" \\_____\\__,_|_| |_| |_| .__/ \\___/ |_| |_|_|_| |_|\\__,_|\\__,_|\\___/ \n" +
" | | \n" +
" |_| \n\n";
static void PrintTitle()
{
for (int i = 0, j = 0; i < title.Length; i++)
{
Console.ForegroundColor = Table.colors[j % Table.colors.Length];
Console.Write(title[i]);
if (title[i] != ' ')
j++;
}
Console.ResetColor();
}
static void MainMenu()
{
int mode = 0;
Console.Write("Menu Principal\n0 - Sair\n1 - Um Jogador\n2 - Multiplayer\n: ");
try { mode = int.Parse(Console.ReadLine()); }
catch (Exception e)
{
MainMenu();
return;
}
switch (mode)
{
case 0:
exit = true;
return;
case 1:
PlayGameSP();
break;
case 2:
Multiplayer();
break;
}
Console.Write("Enter para continuar");
Console.ReadLine();
}
static void Multiplayer()
{
Console.Write("Insira o IP do servidor: ");
string ip = Console.ReadLine();
int port = 6778;
Console.Write("Insira a porta do servidor: ");
try { port = int.Parse(Console.ReadLine()); }
catch (Exception e)
{
Console.WriteLine("Valor invalido");
Multiplayer();
}
Console.Write("Insira como quer ser chamado: ");
string name = Console.ReadLine();
while (!Connector.Connect(ip, port, name))
{
Console.Write("Deseja tentar conectar novamente?(Y/n): ");
if (Console.ReadLine().ToUpper() != "Y")
return;
}
Console.WriteLine("Conexão bem sucedida");
while (Connector.IsConnected())
{
Console.Clear();
Console.WriteLine(Connector.GetRooms());
Console.WriteLine("0 - Desconectar");
Console.WriteLine("1 - Mostrar salas");
Console.WriteLine("2 - Criar sala");
Console.WriteLine("3 - Conectar a sala");
Console.Write(": ");
int input = -1;
string roomName = "";
try
{
input = int.Parse(Console.ReadLine());
roomName = "";
}
catch (Exception e)
{
Console.WriteLine("Valor invalido");
Console.WriteLine(e);
continue;
}
switch (input)
{
case 0:
Connector.Write("DISCONNECT");
Connector.Disconnect();
return;
case 1:
continue;
case 2:
Console.Write("Insira o nome da sala: ");
roomName = Console.ReadLine();
int maxPlayers = 2;
Console.Write("Insira o número máximo de jogadores: ");
try { maxPlayers = int.Parse(Console.ReadLine()); }
catch (Exception e)
{
Console.WriteLine("Valor invalido");
break;
}
if (Connector.CreateRoom(roomName, maxPlayers, InputHandler.GetDifficulty()) && Connector.EnterRoom(roomName))
WaitingRoom();
break;
case 3:
Console.Write("Insira o nome da sala: ");
if (Connector.EnterRoom(Console.ReadLine()))
WaitingRoom();
break;
}
}
Console.WriteLine("Conexão terminada");
}
static void WaitingRoom()
{
while (Connector.IsConnected())
{
lock (Connector.gamingLock)
{
Console.Clear();
Console.WriteLine(Connector.GetPlayers());
Console.WriteLine("0 - Sair da sala");
Console.WriteLine("1 - Mostrar jogadores");
Console.WriteLine("2 - Estou pronto");
Console.WriteLine("3 - Não estou pronto");
Console.Write(": ");
string sInput = "";
int input = -1;
try
{
sInput = Console.ReadLine();
input = int.Parse(sInput);
}
catch (Exception e)
{
Console.WriteLine("Valor invalido");
continue;
}
switch (input)
{
case 0:
Connector.Write("LEAVE_ROOM");
return;
case 1:
continue;
case 2:
Connector.Write("READY", true.ToString());
Thread.Sleep(1000);
break;
case 3:
Connector.Write("READY", false.ToString());
break;
}
}
}
}
static void PlayGameSP()
{
Table.Difficulty difficulty = InputHandler.GetDifficulty();
Table table = new Table(difficulty);
table.Draw();
Table.GameStatus gameStatus = GameLoop(table);
if (gameStatus == Table.GameStatus.WON)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Parabens! Você venceu!");
}
else if (gameStatus == Table.GameStatus.LOST)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Você perdeu :(");
}
Console.ResetColor();
Console.WriteLine($"O jogo durou {table.elapsedTime} segundos e você usou {table.flags} bandeiras de {table.maxFlags}");
}
public static void PlayGameMP(Table.Difficulty difficulty, int tableSeed)
{
Connector.Write("GAMESTATUS", ((int)Table.GameStatus.PLAYING).ToString());
Table table = new Table(difficulty, tableSeed);
table.Draw();
Table.GameStatus gameStatus = GameLoop(table);
if (gameStatus == Table.GameStatus.WON)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Parabens! Você venceu!");
}
else if (gameStatus == Table.GameStatus.LOST)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Você perdeu :(");
}
Connector.Write("GAMESTATUS", ((int)gameStatus).ToString(), $"Durou {table.elapsedTime} segundos e usou {table.flags} bandeiras de {table.maxFlags}");
Console.ResetColor();
Console.WriteLine($"O jogo durou {table.elapsedTime} segundos e você usou {table.flags} bandeiras de {table.maxFlags}");
Console.Write("Enter para continuar");
Console.ReadLine();
}
static Table.GameStatus GameLoop(Table table)
{
Table.GameStatus gameStatus = Table.GameStatus.PLAYING;
do
{
switch (InputHandler.GetGameInput(out string input))
{
case InputHandler.InputType.PLAY:
gameStatus = table.Play(InputHandler.GetInputCells(table, input));
break;
case InputHandler.InputType.FLAG:
table.PutFlags(InputHandler.GetInputCells(table, input));
break;
case InputHandler.InputType.HILIGHT:
table.HighlightLine(InputHandler.GetHighlightInputLine(table, input));
break;
}
table.Draw();
} while (gameStatus == Table.GameStatus.PLAYING);
return gameStatus;
}
static void Main(string[] args)
{
Console.Clear();
PrintTitle();
MainMenu();
if (!exit)
Main(args);
}
}