-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid.cs
88 lines (78 loc) · 2.77 KB
/
Grid.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
using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
namespace Week6
{
public class Grid
{
private readonly GridEntry[,] _grid;
private readonly int _gridSize;
public Grid(int gridSize)
{
_gridSize = gridSize;
_grid = new GridEntry[gridSize,gridSize];
//Fill the grid with empty entries marked as not hit
for (int x = 0; x < gridSize; x++)
{
for (int y = 0; y < gridSize; y++)
{
_grid[x,y] = new GridEntry();
}
}
}
public void Add(Ships ships)
{
foreach (var ship in ships._ships)
{
if (ship.Positions == null)
{
throw new ArgumentException("A player has not set the ships positions");
}
foreach (var pos in ship.Positions)
{
if (pos.X< 0 || pos.X >_gridSize || pos.Y <0 || pos.Y >= _gridSize)
{
throw new ArgumentException("One of the ships is outside the grid");
}
if (pos.Hit)
{
throw new ArgumentException("One of the players is adding a hit ship to the game");
}
if (_grid[pos.X, pos.Y].Ship != null)
{
throw new ArgumentException("One of the players has an overlapping ship");
}
_grid[pos.X, pos.Y].Ship = ship;
}
}
}
public void Draw(int drawX, int drawY)
{
for (int x = 0; x < _gridSize; x++)
{
for (int y = 0; y < _gridSize; y++)
{
Console.SetCursorPosition(drawX + x, drawY + y);
Console.ForegroundColor = (_grid[x, y].Ship == null) ? ConsoleColor.Gray : _grid[x, y].Ship.Color;
//Find if this segment of the ship is hit
Console.BackgroundColor = (_grid[x,y].Hit)? ConsoleColor.Red : ConsoleColor.Black;
if (_grid[x, y].Ship == null)
{
Console.Write(".");
}
else
{
Console.Write(_grid[x, y].Ship.Character);
}
}
}
//Reset colors
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
}
public void Attack(Position pos)
{
_grid[pos.X, pos.Y].Hit = true;
}
}
}