-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameBoard.cs
288 lines (199 loc) · 7.75 KB
/
GameBoard.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
278
279
280
281
282
283
284
285
286
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Net.Sockets;
using System.Numerics;
using System.Security.Cryptography.X509Certificates;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace CSharpBattleShip
{
internal class GameBoard
{
Player player1 = new("Player 1");
Player player2 = new("Player 2");
public void RunGame()
{
PrintWelcome();
AnnounceBoard(player1, player2);
int[,] player1Board = player1.CheckMatrices();
AnnounceBoard(player2, player1);
int[,] player2Board = player2.CheckMatrices();
int gameRound = 0;
int[,] turnHits1 = new int[21, 21];
int[,] turnHits2 = new int[21, 21];
int[,] oneToTwentyOne = player1.WriteBlankBoard();
//TESTING:
//int[,] testToPrint = player1.WriteBlankBoard();
//player1.PrintMatrix(testToPrint);
//player1.PrintMatrix(oneToTwentyOne);
//player1.PrintMatrix(turnHits1);
turnHits1 = player1.AddTwoBoards(turnHits1, oneToTwentyOne);
turnHits2 = player1.AddTwoBoards(turnHits2, oneToTwentyOne);
while (player1.health > 0 && player2.health > 0)
{
gameRound += 1;
Console.WriteLine(@$"Round: {gameRound}");
turnHits1 = PlayTurn(player1, player2, turnHits1, player2Board);
turnHits2 = PlayTurn(player2, player1, turnHits2, player1Board);
}
if (player1.health <= 0)
{
Console.WriteLine(@$"
{player2.name} is the winner! Thanks to both our players today and congratulations to {player2.name}!");
}
else if (player2.health <= 0)
{
Console.WriteLine(@$"
{player1.name} is the winner! Thanks to both our players today and congratulations to {player1.name}!");
}
else
{
Console.WriteLine(@$"
It's a tie! Thanks for playing!");
}
}
public void PrintWelcome()
{
Console.WriteLine(@"
Welcome to Battleship!!!
Before we get started, let's review the rules. Each player will receive a randomized game board with their ships placed in secret locations.
Players will guess coordinates(x, y), trying to identify the position(horizontal or vertical) of their opponent's ships.
The Destroyer is 2 spaces long.
The Submarine is 3.
The Battleship is 4.
And the Aircraft Carrier is 5.
The first player will make their guess, and if they hit any of the spaces containing an enemy ship,
that position will change from '0' to '2' If they miss, that position will change to a '1'
Be the first to sink all 4 ships to win!
Press 'space' when you are ready to continue.
");
player1.CheckSpaceBarPress();
}
public static void AnnounceBoard(Player playerA, Player playerB)
{
Console.WriteLine($@"
{playerA.name} your board is ready. {playerB.name} please look away.
{playerA.name} press 'space' when you are ready to see your board in secret.
");
playerA.CheckSpaceBarPress();
}
public int[,] PlayTurn(Player playerA, Player playerB, int[,] turnHits1, int[,] player2Board)
{
Console.WriteLine($@"
Here is your opponent's board as you know it, {playerA.name}:
");
player1.PrintMatrixTurns(turnHits1);
//int[,] turnBoard;
int[,] turnHitsToAdd1 = new int[21, 21];
var tuple = InputAndEvalGuess(out turnHitsToAdd1, player2Board);
if (turnHitsToAdd1[tuple.Item1, tuple.Item2] == turnHits1[tuple.Item1, tuple.Item2])
{
turnHitsToAdd1[tuple.Item1, tuple.Item2] = 0;
Console.WriteLine(@$"
{playerB.name} has {playerB.health} health left!");
}
else
{
turnHits1 = playerA.AddTwoBoards(turnHits1, turnHitsToAdd1);
if (playerB.destroyer.newBoard[tuple.Item1, tuple.Item2] == 2)
{
TakeDamage(playerA, playerB, playerB.destroyer);
}
else if (playerB.submarine.newBoard[tuple.Item1, tuple.Item2] == 2)
{
TakeDamage(playerA, playerB, playerB.submarine);
}
else if (playerB.battleship.newBoard[tuple.Item1, tuple.Item2] == 2)
{
TakeDamage(playerA, playerB, playerB.battleship);
}
else if (playerB.aircraftcarrier.newBoard[tuple.Item1, tuple.Item2] == 2)
{
TakeDamage(playerA, playerB, playerB.aircraftcarrier);
}
//add time.sleep(1);
}
return turnHits1;
}
public Tuple<int, int> InputAndEvalGuess(out int[,] turnBoard, int[,] player2Board)
{
//bool working;
//while (!working)
//{
Console.WriteLine(@$"
{player1.name}: choose your coordinate horizontally(1 - 20), then press 'enter':");
// add catch for if input is given...
string input = Console.ReadLine();
int guessX;
bool success = int.TryParse(input, out guessX);
while (!success)
{
if (success)
Console.WriteLine(@$"
{guessX}");
else
{
Console.WriteLine(@"
Invalid Input.");
}
}
//int guessX = Int32.Parse(Console.ReadLine());
Console.WriteLine(@$"
{player1.name}: choose your coordinate vertically(1 - 20), then press 'enter':");
//int guessY = Int32.Parse(Console.ReadLine());
string inputY = Console.ReadLine();
int guessY;
bool successY = int.TryParse(inputY, out guessY);
while (!successY)
{
if (successY)
Console.WriteLine(@$"
{guessY}");
else
{
Console.WriteLine(@"
Invalid Input.");
}
}
turnBoard = new int[21, 21];
if (player2Board[guessY, guessX] == 2)
{
turnBoard[guessY, guessX] = 2;
}
else
{
turnBoard[guessY, guessX] = 1;
Console.WriteLine(@"
It's a miss!");
}
//working = true;
return new Tuple<int, int>(guessY, guessX);
}
public static void TakeDamage(Player playerA, Player playerB, Ship ship)
{
if (ship.health > 0)
{
// add time.sleep(1) to the following CWL's.
Console.WriteLine(@"
It's a hit!");
ship.health -= 1;
playerB.health -= 1;
//is that line above necessary??
Console.WriteLine(@$"
{playerB.name}'s {ship.name} now has {ship.health} health left!");
if (ship.health == 0)
{
Console.WriteLine(@$"
{playerA.name} sank your {ship.name}!");
}
Console.WriteLine($@"
{playerB.name} has {playerB.health} health left!");
}
}
}
}