-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
602 lines (551 loc) · 25.3 KB
/
Program.cs
File metadata and controls
602 lines (551 loc) · 25.3 KB
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace Broadsides
{
class Program
{
private static Field[][] playerBoard;
private static List<Ship> playerShips = new List<Ship>();
private static Coordinate playerPreviousCoords = new Coordinate(0, 0);
private static Field[][] computerBoard;
private static List<Ship> computerShips = new List<Ship>();
private static ArtificialIntelligence computer;
private static Dictionary<int, string> horizontalValueToLetter = new Dictionary<int, string>();
static void Main(string[] args)
{
horizontalValueToLetter.Add(0, "A");
horizontalValueToLetter.Add(1, "B");
horizontalValueToLetter.Add(2, "C");
horizontalValueToLetter.Add(3, "D");
horizontalValueToLetter.Add(4, "E");
horizontalValueToLetter.Add(5, "F");
horizontalValueToLetter.Add(6, "G");
horizontalValueToLetter.Add(7, "H");
horizontalValueToLetter.Add(8, "I");
horizontalValueToLetter.Add(9, "J");
bool quit = false;
char input;
while (!quit)
{
Console.Clear();
Console.WriteLine("Welcome to Broadsides!\n\nPlease select an option: 1. New Game | 2. Quit\n");
input = Console.ReadKey().KeyChar;
switch (input)
{
case '1':
GameSetup();
break;
case '2':
quit = true;
break;
}
}
}
/// <summary>
/// The Pre-Game setup where Player & Computer sets down their ships.
/// </summary>
public static void GameSetup()
{
Console.Clear();
// Resetting the Player.
playerPreviousCoords = new Coordinate(0, 0);
playerBoard = GenerateEmptyTenByTen();
playerShips.Clear();
playerShips.Add(new Ship("Aircraft Carrier", 5));
playerShips.Add(new Ship("Battleship", 4));
playerShips.Add(new Ship("Destroyer", 3));
playerShips.Add(new Ship("Uboat", 3));
playerShips.Add(new Ship("Patrolship", 2));
// Resetting the Computer.
computer = new ArtificialIntelligence();
computerBoard = GenerateEmptyTenByTen();
computerShips.Clear();
computerShips.Add(new Ship("Aircraft Carrier", 5));
computerShips.Add(new Ship("Battleship", 4));
computerShips.Add(new Ship("Destroyer", 3));
computerShips.Add(new Ship("Uboat", 3));
computerShips.Add(new Ship("Patrolship", 2));
// Time for the player to put down their ships.
foreach(Ship ship in playerShips)
{
Console.WriteLine("This is your board, place your ship!");
DrawBoard(playerBoard);
// First we want to know if the player wants to place the ship vertically or horizontally.
Console.WriteLine("\nYou are about to place down your " + ship.Type + ", it is " + ship.Length + " fields long.\nh. Horizontal | v. Vertical");
bool horizontal = true;
bool validSelection = false;
char input;
// While NOT validSelection of Horizontal or Vertical, keep asking.
while (!validSelection)
{
input = Console.ReadKey().KeyChar;
switch (input)
{
case 'h':
validSelection = true;
horizontal = true;
break;
case 'v':
validSelection = true;
horizontal = false;
break;
}
}
bool validPosition = false;
// While NOT a valid position.
// A Valid Position is one where:
// 1. The coordinates are within the board.
// 2. THe ship will not stick out of the board.
// 3. There are currently no other ships placed in the way of the new Ship.
while(!validPosition)
{
// Acquiring coordinates. X = Horizontal, Y = Vertical
// You might notice this: "9 - (horizontal ? Ship.Length : 1)" and the opposite for the Vertical (y) coordiante below.
// It means 9 minus either ship length or 0, depending on if it's horizontal or not.
// This way, the ship cannot stick outside of the board. So a horizontal carrier (length 5) can at most be placed at horizontal(x) coordinate 5. So it will be placed on 5, 6, 7, 8, 9 (5 total squares)
Coordinate coordinate = ManualCoordinates(ship, horizontal);
// These positions have Room for these ships, but-!
// .. We need to check if any other ships are blocking it
if (ShipInTheWay(playerBoard, ship, coordinate, horizontal))
{
Console.WriteLine("A ship is already placed there!");
}
else
{
// Since no ship is blocking it, we're going to place it down on the board!
for (int i = 0; i < ship.Length; i++)
{
if (horizontal)
{
playerBoard[coordinate.Y][coordinate.X + i]._Ship = ship;
}
else
{
playerBoard[coordinate.Y + i][coordinate.X]._Ship = ship;
}
}
validPosition = true;
Console.WriteLine(ship.Type + " successfully placed at " + (coordinate.Y+1) + ", " + horizontalValueToLetter[coordinate.X]);
}
}
Console.Clear();
}
// Computer ship placements
Random rnd = new Random();
// The computer is dumb, and will need to make randomized choices.
foreach (Ship ship in computerShips)
{
bool validPosition = false;
// While the computer does Not have a valid position for its ship..
while (!validPosition)
{
// Horizontal or not?
bool horizontal = rnd.Next(0, 2) == 1;
// If it is horizontal, than the x-value is adjusted to the length of the ship, thereby preventing the ship being placed out of bounds.
// If it is not hortizontal, then the y-value (Vertical coordinate) is adjusted to the length of the ship, -||-
Coordinate coordinate = new Coordinate(
rnd.Next(0, 10 - (!horizontal ? ship.Length : 0)),
rnd.Next(0, 10 - (horizontal ? ship.Length : 0))
);
// If there is no ship already on this board, along the length of the ship we're about to place, at x and y coordinate, horizontally or not...
if (!ShipInTheWay(computerBoard, ship, coordinate, horizontal))
{
// Then Place the ship on those Fields!
for (int i = 0; i < ship.Length; i++)
{
if (horizontal)
{
computerBoard[coordinate.Y][coordinate.X + i]._Ship = ship;
}
else
{
computerBoard[coordinate.Y + i][coordinate.X]._Ship = ship;
}
}
validPosition = true;
}
}
}
GameLoop();
}
/// <summary>
/// The game proper, where Computer and Player take turns trying to sink each other's ships.
/// </summary>
public static void GameLoop()
{
bool gameOver = false;
int round = 0;
while (!gameOver)
{
Console.Clear();
// Pre-Round test to see if User has lost.
if (AllShipsSunk(playerShips))
{
Console.Clear();
gameOver = true;
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.WriteLine("All of your ships have sunk! You LOSE!");
Console.ResetColor();
}
if (!gameOver)
{
round++;
Console.WriteLine("####### R O U N D " + round + " #######");
DrawBoard(playerBoard);
Console.WriteLine("\nYour ships and how they are doing:");
foreach (Ship ship in playerShips)
{
Console.WriteLine(ship.Type + " HP: " + (ship.Length - ship.Hits) + "/" + ship.Length);
}
Console.WriteLine("Press Any Key to Continue to Shooting");
Console.ReadKey();
Console.WriteLine();
// Player gets first shot!
bool validShot = false;
while (!validShot)
{
Console.Clear();
Console.WriteLine("Your turn to shoot!");
DrawTacticalDisplay(computerBoard);
Coordinate coordinate = ManualCoordinates();
Field field = computerBoard[coordinate.Y][coordinate.X];
if(!field.IsHit)
{
field.IsHit = true;
validShot = true;
Console.Clear();
DrawTacticalDisplay(computerBoard);
if (field._Ship != null)
{
Console.WriteLine("Hit confirmed! Enemy ship has taken damage!");
if (field._Ship.Sunk)
{
Console.WriteLine("An enemy vessel has been sunk!");
}
}
else
{
Console.WriteLine("Miss! No ship there!");
}
Console.WriteLine("Press Any Key to Continue");
Console.ReadKey();
Console.WriteLine();
}
else
{
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine("You have already fired at that position! It is against orders to waste ammunition!");
Console.ResetColor();
Console.ReadKey();
}
}
}
// Post-Player's turn, let's see if Computer has lost.
if (AllShipsSunk(computerShips))
{
Console.Clear();
gameOver = true;
Console.BackgroundColor = ConsoleColor.DarkYellow;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("You have sunk all of the Enemy's ships! You WIN!");
Console.ResetColor();
}
// Computer's turn to shoot!
if (!gameOver)
{
bool validShot = false;
Coordinate nextShot;
while (!validShot)
{
nextShot = computer.GetNextShot(playerBoard);
Field field = playerBoard[nextShot.Y][nextShot.X];
if(!field.IsHit)
{
Console.WriteLine("Computer: I SHOOT AT " + horizontalValueToLetter[nextShot.X] + ", " + (nextShot.Y + 1));
validShot = true;
field.IsHit = true;
if(field._Ship != null)
{
Console.WriteLine("Computer has hit your " + field._Ship.Type + "!");
if (!computer.LastShipHitSunk && computer.Direction.X == 0 && computer.Direction.Y == 0)
{
// The Computer is continueing on the ship it's trying to sink, and therefore knows the direction!
computer.Direction = new Coordinate(nextShot.Y - computer.LastShipHitCoordinate.Y, nextShot.X - computer.LastShipHitCoordinate.X);
}
computer.LastShipHitSunk = false;
computer.LastShipHitCoordinate = nextShot;
computer.Streak++;
if (field._Ship.Sunk)
{
Console.WriteLine("Computer has sunk your " + field._Ship.Type + "!");
computer.ResetTargetting();
}
}
else
{
Console.WriteLine("Computer has missed!");
}
Console.WriteLine("Press Any Key to Continue");
Console.ReadKey();
Console.WriteLine();
}
}
}
}
Console.WriteLine("This is yours:");
DrawBoard(playerBoard);
Console.WriteLine("This is Computer's:");
DrawBoard(computerBoard);
Console.ReadKey();
}
/// <summary>
/// Checks if all ships in the list have been sunk.
/// </summary>
/// <param name="ships">The list of ships to be checked.</param>
/// <returns>True if all ships in list are sunk.</returns>
public static bool AllShipsSunk(List<Ship> ships)
{
int sunkenShips = 0;
foreach (Ship ship in ships)
{
if (ship.Sunk)
{
sunkenShips++;
}
}
if (sunkenShips < ships.Count)
{
return false;
}
else
{
return true;
}
}
/// <summary>
/// Draw the "Tactical" Display of the enemy's board.
/// </summary>
/// <param name="board">The board of the opposing Player.</param>
public static void DrawTacticalDisplay(Field[][] board)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("y\\xA B C D E F G H I J");
for (int i = 0; i < board.Length; i++)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.BackgroundColor = ConsoleColor.Black;
Console.Write((i == 9 ? "" : " ") + (i + 1));
Console.ForegroundColor = ConsoleColor.White;
for (int j = 0; j < board[i].Length; j++)
{
Field field = board[i][j];
Console.ForegroundColor = ConsoleColor.White;
if((i+j) % 2 == 0)
{
Console.BackgroundColor = ConsoleColor.Green;
}
else
{
Console.BackgroundColor = ConsoleColor.DarkGreen;
}
if(field.IsHit)
{
if(field._Ship == null)
{
// IF there is no ship, draw mundane hit.
Console.ForegroundColor = ConsoleColor.Black;
Console.Write("()");
}
else
{
// If there is a hit ship, draw a Success hit.
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.ForegroundColor = ConsoleColor.Black;
Console.Write("{}");
}
}
else
{
// Unhit square. Might contain a ship. Might not.
Console.Write(" ");
}
}
Console.WriteLine();
}
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
}
/// <summary>
/// Allows user to manually input letters and numbers, corresponding to a coordinate position on the board.
/// </summary>
/// <param name="ship">Ship to be placed, if any.</param>
/// <param name="horizontal">Ship to be placed Horizontally or Vertically, if any ship.</param>
/// <returns>The coordinate of the final selection.</returns>
public static Coordinate ManualCoordinates(Ship ship = null, bool horizontal = true)
{
Coordinate coor = new Coordinate(0, 0);
int horizontalMax = 9 - (ship != null && horizontal ? ship.Length-1 : 0);
int verticalMax = 9 - (ship != null && !horizontal ? ship.Length-1 : 0);
Console.WriteLine("\nPlease choose a horizontal letter from A to " + horizontalValueToLetter[horizontalMax]);
string letterInput = "";
bool validHorizontalCoord = false;
while (!validHorizontalCoord)
{
letterInput = Console.ReadKey().Key.ToString().ToUpper();
for (int i = 0; i <= horizontalMax; i++)
{
if(letterInput == horizontalValueToLetter[i])
{
coor.X = i;
validHorizontalCoord = true;
}
}
}
Console.WriteLine("\nPlease choose a vertical position");
coor.Y = AcquireValidIntegerWithin(0, verticalMax);
return coor;
}
/// <summary>
/// Gets user to input an integer inclusively between minValue and maxValue.
/// </summary>
/// <param name="minValue">The Minimum value of Integer you want.</param>
/// <param name="maxValue">The Maximum value of Integer you want.</param>
/// <returns>A user-specified Integer inclusively between minValue and maxValue</returns>
public static int AcquireValidIntegerWithin(int minValue, int maxValue)
{
Console.WriteLine("Valid values are from " + (minValue+1) + " to " + (maxValue+1));
bool validNumber = false;
string input;
int output = 0;
while (!validNumber)
{
input = Console.ReadLine();
if(int.TryParse(input, out output) && output >= minValue+1 && output <= maxValue+1)
{
validNumber = true;
}
else
{
Console.WriteLine(" INCORRECT VALUE ");
}
}
return output-1;
}
/// <summary>
/// Makes a 10x10 grid of fields which has their isHit set to false.
/// </summary>
/// <returns>10x10 field grid, isShip & isHit are both false.</returns>
public static Field[][] GenerateEmptyTenByTen()
{
Field[][] newBoard = new Field[10][];
for (int i = 0; i < 10; i++)
{
newBoard[i] = new Field[10];
for (int j = 0; j < 10; j++)
{
newBoard[i][j] = new Field();
}
}
return newBoard;
}
/// <summary>
/// During GameSetup(), when placing down your ships, this function checks if another ship is placed on the X Y coordinates given, or anywhere along the length of the ship.
/// </summary>
/// <param name="board">The board the ship is going to be placed on.</param>
/// <param name="Ship">The ship that is soon to be placed. Really just need it for its Length.</param>
/// <param name="x">Horizontal Coordinate</param>
/// <param name="y">Vertical Coordinate</param>
/// <param name="horizontal">If the ship is to be placed horizontally, vertically if false</param>
/// <returns>If it at any point encountered a ship in the fields of the new Ship.</returns>
public static bool ShipInTheWay(Field[][] board, Ship ship, Coordinate coord, bool horizontal)
{
for (int i = 0; i < ship.Length; i++)
{
// If the board already has a Ship in any of the coordinates in the path of the new Ship, then return true that there is a Ship In The Way.
if (horizontal && board[coord.Y][coord.X + i]._Ship != null || !horizontal && board[coord.Y + i][coord.X]._Ship != null)
{
return true;
}
}
return false;
}
/// <summary>
/// Draws a player's board with boats on it. Boats are marked with the first letter of their type typed twice, unless it has been hit in that square, in which case it's XX. Water (Ship-less Field) is marked with ▓▓ or ▒▒.
/// </summary>
/// <param name="board">The boat-filled board to be displayed.</param>
public static void DrawBoard(Field[][] board)
{
Random rnd = new Random();
Console.WriteLine("######## Board ########");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("y\\xA B C D E F G H I J");
for (int i = 0; i < board.Length; i++)
{
Console.ForegroundColor = ConsoleColor.White;
Console.Write((i == 9 ? "" : " ") + (i+1));
for (int j = 0; j < board[i].Length; j++)
{
// Alternating color!
if ((i + j) % 2 == 0)
{
Console.ForegroundColor = ConsoleColor.DarkBlue;
}
else
{
Console.ForegroundColor = ConsoleColor.Blue;
}
Field field = board[i][j];
if (field._Ship != null)
{
if (field.IsHit)
{
Console.ForegroundColor = ConsoleColor.Black;
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.Write("XX");
}
else
{
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.Write(field._Ship.Type.Substring(0, 1) + field._Ship.Type.Substring(0, 1));
}
}
else if (field.IsHit)
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
switch (rnd.Next(0, 3))
{
case 0:
Console.Write("▒▒");
break;
default:
Console.Write("▓▓");
break;
}
}
else
{
switch(rnd.Next(0, 3))
{
case 0:
Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.Write("▒▒");
break;
default:
Console.Write("▓▓");
break;
}
}
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
}
Console.WriteLine();
}
}
}
}