/
Program.cs
470 lines (370 loc) · 13.5 KB
/
Program.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
using System;
using System.Collections.Generic;
namespace Battleships
{
public class CheatCodes
{
public static string REVEAL_GRID = "r";
public static string GIVE_UP = "x";
}
class Program
{
static void Main(string[] args)
{
Battleships battleships = new Battleships(10, 10);
battleships.Play();
}
}
class Battleships
{
private BattleshipsGrid grid;
public Battleships(int rows, int columns)
{
grid = new BattleshipsGrid(rows, columns);
}
public void Play()
{
char playAgainResponse;
do
{
// Setup grid
grid.SetUp();
// Place ships
grid.AddShip(new Battleship());
grid.AddShip(new Destroyer());
grid.AddShip(new Destroyer());
while (grid.ShipsRemaining > 0)
{
grid.PrintGridForPlayer();
Console.WriteLine("Enter grid reference to fire at: ");
string input = Console.ReadLine();
if (input == CheatCodes.REVEAL_GRID)
{
grid.PrintActualGrid();
continue;
}
if (input == CheatCodes.GIVE_UP)
{
break;
}
Point? chosenPoint = GridRef.GridRefToPoint(input);
if (chosenPoint == null)
{
Console.WriteLine("Please enter a valid point. It should be a letter directly followed by a number i.e. A1.");
continue;
}
Point point = (Point)chosenPoint;
if (!grid.IsPointValid(point))
{
Console.WriteLine("That point is outside the bounds of the grid");
continue;
}
string fireString = grid.Fire(point);
Console.WriteLine(fireString);
}
grid.PrintActualGrid();
Console.WriteLine("Game Over");
do
{
Console.WriteLine();
Console.WriteLine("Play again? [y/n]: ");
playAgainResponse = char.ToUpper(Console.ReadKey().KeyChar);
}
while (playAgainResponse != 'Y' && playAgainResponse != 'N');
} while (playAgainResponse == 'Y');
}
}
class BattleshipsGrid
{
public BattleshipsGrid(int rows, int cols)
{
this.rows = rows;
this.cols = cols;
}
private int rows;
private int cols;
public int Rows { get => rows; }
public int Columns { get => cols; }
public int ShipsRemaining { get => shipRegistry.Count - shipsSunk; }
public void SetUp()
{
grid = new int[rows, cols];
gridFiredAt = new bool[rows, cols];
shipRegistry.Clear();
shipsSunk = 0;
currentBuildCode = 1;
}
private int[,] grid; // Grid of numbers indicating 0 (no ship) or 1+ (a ship code)
private bool[,] gridFiredAt; // Bool grid indicating whether the player has fired at a spot or not
private Dictionary<int, Ship> shipRegistry = new Dictionary<int, Ship>(); // Stores registered ships
private int shipsSunk = 0; // Number of ships sunk
private int currentBuildCode = 1; // This determines the symbol representing the various ships
public Point GetRandomPoint()
{
Random random = new Random();
int row = random.Next(0, Rows);
int col = random.Next(0, Columns);
return new Point(row, col);
}
public bool IsPointValid(Point point)
{
if (point.Row < 0 || point.Col < 0) return false;
if (point.Row >= Rows || point.Col >= Columns) return false;
return true;
}
// This method will keep trying to place the ship and wont give up.
// Therefore it assumes that the ship placement is possible (i.e. the ship isn't longer than the grid space, there is available space, etc)
public void AddShip(Ship ship)
{
// Pick a random empty point to start building a ship
Point startPoint;
do { startPoint = GetRandomPoint(); }
while (!IsPointAvailable(startPoint));
// Pick a random orientation to start building (no orientation will get preference)
RandomOrientation randomOrientation = new RandomOrientation();
Orientation orientation = randomOrientation.PrimaryOrientation;
// "Scan" in that direction to ensure ample space
Point p1 = startPoint.Clone();
Point p2 = startPoint.Clone();
Scan(ref p1, ref p2, orientation);
int scannedSize = (int) Point.StraightLineDistance(p1, p2);
// Is the space too small for a ship?
if (scannedSize < ship.Size())
{
// If so, try other orientation
orientation = randomOrientation.SecondaryOrientation;
// "Scan" in this direction to ensure ample space
p1.Copy(startPoint);
p2.Copy(startPoint);
Scan(ref p1, ref p2, orientation);
scannedSize = (int) Point.StraightLineDistance(p1, p2);
// Is the space too small for a ship?
if (scannedSize < ship.Size())
{
// Try all over again with another random point
AddShip(ship);
return;
}
}
// If we made it this far, we have a line the ship can fit along (somewhere between p1 and p2).
// Now we determine (randomly) where we will offset the ship along this line.
int wiggleRoom = scannedSize - ship.Size();
Random random = new Random();
int offset = random.Next(0, wiggleRoom);
if (orientation == Orientation.horizontal){
p1.Right(offset);
}
else{
p1.Down(offset);
}
// Now we have the true start point, ship size and orientation. Time to build the ship.
int code = GetBuildCode();
Build(code, p1);
int buildProgress = 1;
while(buildProgress < ship.Size())
{
if (orientation == Orientation.horizontal) p1.Right(1);
else p1.Down(1);
Build(code, p1);
buildProgress++;
}
// Lastly, we register this ship
shipRegistry.Add(code, ship);
}
public string Fire(Point point)
{
if (gridFiredAt[point.Row, point.Col] == true) return "You've already fired here!";
// Toggle firedAt to true
gridFiredAt[point.Row, point.Col] = true;
if (grid[point.Row, point.Col] == 0) return "Miss.";
else
{
int shipCode = grid[point.Row, point.Col];
Ship ship = shipRegistry[shipCode];
ship.Hit();
if (ship.IsDestroyed())
{
shipsSunk++;
return "Hit! You sunk my " + ship.Name() + ".";
}
else return "Hit!";
}
}
public void PrintGridForPlayer()
{
PrintGrid(PRINTCODE_PLAYER_VIEW);
}
public void PrintActualGrid()
{
PrintGrid(PRINTCODE_ACTUAL_GRID);
}
private void Scan(ref Point p1, ref Point p2, Orientation orientation)
{
if (orientation == Orientation.horizontal)
{
do { p1.Left(1); } while (IsPointAvailable(p1)); // Edge leftwards
p1.Right(1); // Undo final step
do { p2.Right(1); } while (IsPointAvailable(p2)); // Edge rightwards
p2.Left(1); // Undo final step
}
else
{
do { p1.Up(1); } while (IsPointAvailable(p1)); // Edge upwards
p1.Down(1); // Undo final step
do { p2.Down(1); } while (IsPointAvailable(p2)); // Edge downwards
p2.Up(1); // Undo final step
}
}
private int GetBuildCode() => currentBuildCode++;
private void Build(int code, Point p)
{
grid[p.Row, p.Col] = code;
}
private bool IsPointAvailable(Point point)
{
if (!IsPointValid(point)) return false;
return grid[point.Row, point.Col] == 0;
}
static int PRINTCODE_ACTUAL_GRID = 0;
static int PRINTCODE_PLAYER_VIEW = 1;
private void PrintGrid(int printCode)
{
string leftIndent = " ";
Console.WriteLine();
Console.Write(leftIndent + " ");
for (int c = 0; c < Columns; c++)
{
Console.Write((c + 1) + " ");
}
Console.WriteLine();
Console.WriteLine();
for (int i = 0; i < Rows; i++)
{
Console.Write(MyStrings.alphabet[i] + leftIndent);
for (int j = 0; j < Columns; j++)
{
switch(printCode)
{
case 0:
Console.Write(grid[i, j]);
break;
case 1:
bool firedAt = gridFiredAt[i, j];
int shipCode = grid[i, j];
if (!firedAt) Console.Write("?");
else if (shipCode == 0) Console.Write("-");
else if (!shipRegistry[shipCode].IsDestroyed()) Console.Write("X");
else Console.Write(shipCode);
break;
}
Console.Write(" ");
}
Console.WriteLine();
}
Console.WriteLine();
}
}
struct Point
{
public Point(int row, int col)
{
this.row = row;
this.col = col;
}
private int row;
private int col;
public int Row { get => row; private set { row = value; } }
public int Col { get => col; private set { col = value; } }
public void Left(int by) { col -= by; }
public void Right(int by) { col += by; }
public void Up(int by) { row -= by; }
public void Down(int by) { row += by; }
public void Copy(Point toCopy)
{
this.row = toCopy.row;
this.col = toCopy.col;
}
public Point Clone() => new Point(row, col);
// Assumes points share a row or column. Returns null otherwise.
public static int? StraightLineDistance(Point p1, Point p2)
{
if (p1.row == p2.row) return Math.Abs(p1.col - p2.col);
if (p1.col == p2.col) return Math.Abs(p1.row - p2.row);
return null;
}
public override string ToString()
{
return "[" + row + "," + col + "]";
}
}
class GridRef
{
public static Point? GridRefToPoint(string entry)
{
char letterString = entry[0];
int letterIndex = MyStrings.alphabet.IndexOf(char.ToUpper(letterString));
if (letterIndex == -1) return null;
int num;
string numString = entry.Substring(1);
try
{
num = int.Parse(numString);
}
catch
{
return null;
}
return new Point(letterIndex, num - 1);
}
}
enum Orientation { horizontal, vertical }
abstract class RandomEnum
{
public RandomEnum() { Randomize(); }
public abstract void Randomize();
}
class RandomOrientation : RandomEnum
{
public override void Randomize()
{
Random random = new Random();
int random01 = random.Next(0, 2);
if (random01 == 0)
{
orientation1 = Orientation.horizontal;
orientation2 = Orientation.vertical;
}
else
{
orientation2 = Orientation.horizontal;
orientation1 = Orientation.vertical;
}
}
Orientation orientation1;
Orientation orientation2;
public Orientation PrimaryOrientation { get => orientation1; }
public Orientation SecondaryOrientation { get => orientation2; }
}
abstract class Ship
{
private int hits = 0;
public abstract string Name();
public abstract int Size();
public void Hit() { hits++; }
public bool IsDestroyed() => Size() <= hits;
}
class Battleship : Ship
{
public override string Name() => "Battleship";
public override int Size() => 5;
}
class Destroyer : Ship
{
public override string Name() => "Destroyer";
public override int Size() => 4;
}
class MyStrings
{
public static string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
}
}