-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.cs
221 lines (205 loc) · 7.31 KB
/
Board.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
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConnectFour
{
public interface IBoard : IRenderable {
int PutCounter(int index, string counter);
bool CheckGame(int column, int row);
int Height { get; }
int Width { get; }
}
public class ConnectFourBoard: IBoard {
public List<List<string>> Board { get; set; }
public int Height { get; }
public int Width { get; }
private int winningNumber;
public ConnectFourBoard(int winningNumber = 4, int maxHeight = 6, int maxwidth = 7) {
this.winningNumber = winningNumber;
this.Height = maxHeight;
this.Width = maxwidth;
this.Board = new List<List<string>>();
for (var i = 0; i < maxwidth; i++) {
this.Board.Add(new List<string>());
}
}
/**
* Given a location on the board, check to see if the
* game has been won. Return true is the game is over else false.
*/
public bool CheckGame(int columm, int row) {
var lists = new List<String>[]{
this.BuildColumn(columm),
this.BuildRow(row),
this.BuildHorizontalLeftList(columm, row),
this.BuildHorizontalRightList(columm, row)
};
foreach(var hor in lists) {
var success = this.CheckList(hor);
if (success) {
return true;
}
}
return false;
}
/**
* Puts a 'counter' into a given column.
*/
public int PutCounter(int index, string counter) {
var column = this.Board[index];
if (column.Count >= this.Height) {
throw new ArgumentException("Column Full");
}
column.Add(counter);
return column.Count - 1;
}
/**
* Given a list of counters, derive whether they're four in a row.
*/
private bool CheckList(List<string> list) {
var inARow = 1;
string previousValue = string.Empty;
foreach(var counter in list) {
if (previousValue == counter & previousValue != string.Empty) {
inARow++;
} else {
inARow = 1;
}
if (inARow >= this.winningNumber) {
return true;
}
previousValue = counter;
}
return false;
}
/**
* Returns the column of the board.
*/
public List<string> BuildColumn(int columIndex) {
return this.Board[columIndex];
}
/**
* Check a particular column for four in a row.
**/
public bool CheckColumn(int columIndex) {
var column = this.BuildColumn(columIndex);
return CheckList(column);
}
/**
* Given an index, build a row of the board.
*/
public List<String> BuildRow(int rowIndex) {
var row = new List<string>();
foreach(var column in this.Board) {
try {
row.Add(column[rowIndex]);
} catch {
row.Add(string.Empty);
}
}
return row;
}
/**
* Check a particular row for four in a row.
**/
public bool CheckRow(int rowIndex) {
var row = BuildRow(rowIndex);
return CheckList(row);
}
/**
* Builds a list based on the horizontal upward direction
* starting that the bottom left of where the counter coordinates are.
* Always starts from the bottom row, and continues until there are no more columns.
**/
public List<string> BuildHorizontalRightList(int columnIndex, int rowIndex) {
var row = new List<string>();
var startColumn = columnIndex - rowIndex;
var rowContinue = true;
// the bottom row of the board
var computedRowIndex = 0;
while (rowContinue) {
string nextBlock;
if (startColumn > this.Board.Count -1) {
rowContinue = false;
break;
}
try {
nextBlock = this.Board[startColumn][computedRowIndex];
} catch {
nextBlock = string.Empty;
}
row.Add(nextBlock);
computedRowIndex++;
startColumn++;
}
return row;
}
/**
* Builds a list based on the horizontal upward direction
* starting that the bottom right of where the counter coordinates are.
* Always starts on the bottom row, may start off the gird and continues until
* There are no more columns from right to left.
**/
public List<string> BuildHorizontalLeftList(int columnIndex, int rowIndex) {
var row = new List<string>();
var computedColumn = columnIndex + rowIndex;
var rowContinue = true;
// the bottom row of the board
var computedRowIndex = 0;
while (rowContinue) {
string nextBlock;
// When you hit the 'left side' of the board stop
if (computedColumn < 0) {
rowContinue = false;
break;
}
try {
nextBlock = this.Board[computedColumn][computedRowIndex];
} catch {
nextBlock = string.Empty;
}
row.Add(nextBlock);
computedRowIndex++;
computedColumn--;
}
return row;
}
/**
* Given a board coordinate, check the horizontal row which
* the counter is apart from. The horizontal left is left and up,
* right and down.
**/
public bool CheckLeftHorizontal(int columIndex, int rowIndex) {
var horizontalLeft = this.BuildHorizontalLeftList(columIndex, rowIndex);
return CheckList(horizontalLeft);
}
/**
* Given a board coordinate, check the horizontal row which
* the counter is apart from. The horizontal right is right and up,
* left and down.
**/
public bool CheckRightHorizontal(int columIndex, int rowIndex) {
var horizontalright = this.BuildHorizontalRightList(columIndex, rowIndex);
return CheckList(horizontalright);
}
/**
* Returns the string representation of the board.
*/
public string Render() {
var render = string.Empty;
for (var i = this.Height -1; i >= 0; i --) {
var rowContent = this.BuildRow(i);
var renderedContent = rowContent.Select(x => {
var y = " ";
if (x == string.Empty) {
x = " ";
}
y += x + " ";
return y;
});
render += "||" + String.Join("||", renderedContent) + "|| \n";
}
return render;
}
}
}