-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrid.cs
165 lines (153 loc) · 3.28 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
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
class Grid
{
// 6 rows, 7 columns
const int rows = 6;
const int cols = 7;
private Token[,] grid = new Token[rows, cols];
public void PrintGrid(int errorMsg = 0)
{
Console.Clear();
switch (errorMsg)
{
case 1:
Console.WriteLine("That column is full\n");
break;
case 2:
Console.WriteLine("Enter a number between 1 and 7\n");
break;
}
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
{
Console.Write(" ");
Console.Write(grid[r, c] == null ? "#" : grid[r, c].ToString());
}
Console.WriteLine();
}
Console.Write(" ");
Console.WriteLine("1 2 3 4 5 6 7");
Console.WriteLine();
}
public void AddToken(Token token, int col)
{
int desiredRow = 0;
//this finds the lowest row that is empty
for (int r = rows - 1; r >= 0; r--)
{
if (grid[r, col] == null)
{
desiredRow = r;
break;
}
}
//this "moves" the token downwards from the top of the grid
//updating each time to animate it
for (int i = 0; i <= desiredRow; i++)
{
if (i != 0)
{
grid[i - 1, col] = null;
}
grid[i, col] = token;
PrintGrid();
Thread.Sleep(100);
}
}
public bool IsColumnFull(int col)
{
return grid[0, col - 1] != null;
}
public bool CheckWin(char player)
{
//check for horizontal win
//iterate each row
for (int r = 0; r < rows; r++)
{
int count = 0;
//iterate each column in that row
for (int c = 0; c < cols; c++)
{
if ((grid[r, c] == null ? '#' : char.Parse(grid[r, c].ToString())) == player)
{
count++;
}
else
{
count = 0;
}
if (count >= 4)
{
return true;
}
}
}
// check for vertical win
for (int c = 0; c < cols; c++)
{
int count = 0;
for (int r = 0; r < rows; r++)
{
if ((grid[r, c] == null ? '#' : char.Parse(grid[r, c].ToString())) == player)
{
count++;
}
else
{
count = 0;
}
if (count >= 4)
{
return true;
}
}
}
// check for diagonal win from top left to bottom right
for (int r = 0; r < rows - 3; r++)
{
for (int c = 0; c < cols - 3; c++)
{
int count = 0;
for (int i = 0; i < 4; i++)
{
if ((grid[r + i, c + i] == null ? '#' : char.Parse(grid[r + i, c + i].ToString())) == player)
{
count++;
}
else
{
count = 0;
}
if (count >= 4)
{
return true;
}
}
}
}
// check for diagonal win from top right to bottom left
for (int r = 0; r < rows - 3; r++)
{
for (int c = 3; c < cols; c++)
{
int count = 0;
for (int i = 0; i < 4; i++)
{
if ((grid[r + i, c - i] == null ? '#' : char.Parse(grid[r + i, c - i].ToString())) == player)
{
count++;
}
else
{
count = 0;
}
if (count >= 4)
{
return true;
}
}
}
}
return false;
}
}