-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4kyu_Connect_Four.cs
172 lines (145 loc) · 5.24 KB
/
4kyu_Connect_Four.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
using System;
using System.Collections.Generic;
using System.Linq;
namespace CodingChallenges;
[TestClass]
public class _4kyu_Connect_Four {
/*
Connect Four
Take a look at wiki description of Connect Four game:
Wiki Connect Four
The grid is 6 row by 7 columns, those being named from A to G.
You will receive a list of strings showing the order of the pieces which dropped in columns:
List<string> myList = new List<string>()
{
"A_Red",
"B_Yellow",
"A_Red",
"B_Yellow",
"A_Red",
"B_Yellow",
"G_Red",
"B_Yellow"
};
The list may contain up to 42 moves and shows the order the players are playing.
The first player who connects four items of the same color is the winner.
You should return "Yellow", "Red" or "Draw" accordingly.
*/
[TestMethod]
public void Main() {
List<string> myList = new List<string>()
{
"A_Red",
"B_Yellow",
"A_Red",
"B_Yellow",
"A_Red",
"B_Yellow",
"G_Red",
"B_Yellow"
};
Assert.AreEqual("Yellow", WhoIsWinner(myList), "it should return Yellow");
myList = new List<string>()
{
"A_Yellow",
"B_Red",
"B_Yellow",
"C_Red",
"G_Yellow",
"C_Red",
"C_Yellow",
"D_Red",
"G_Yellow",
"D_Red",
"G_Yellow",
"D_Red",
"F_Yellow",
"E_Red",
"D_Yellow"
};
Assert.AreEqual("Red", WhoIsWinner(myList), "it should return Red");
myList = new List<string>()
{
"C_Yellow",
"E_Red",
"G_Yellow",
"B_Red",
"D_Yellow",
"B_Red",
"B_Yellow",
"G_Red",
"C_Yellow",
"C_Red",
"D_Yellow",
"F_Red",
"E_Yellow",
"A_Red",
"A_Yellow",
"G_Red",
"A_Yellow",
"F_Red",
"F_Yellow",
"D_Red",
"B_Yellow",
"E_Red",
"D_Yellow",
"A_Red",
"G_Yellow",
"D_Red",
"D_Yellow",
"C_Red"
};
Assert.AreEqual("Yellow", WhoIsWinner(myList), "it should return Yellow");
myList = new List<string>() {
"C_Yellow", "B_Red", "B_Yellow", "E_Red", "D_Yellow", "G_Red", "B_Yellow", "G_Red", "E_Yellow", "A_Red", "G_Yellow", "C_Red", "A_Yellow", "A_Red", "D_Yellow", "B_Red", "G_Yellow", "A_Red", "F_Yellow", "B_Red", "D_Yellow", "A_Red", "F_Yellow", "F_Red", "B_Yellow", "F_Red", "F_Yellow", "G_Red", "A_Yellow", "F_Red", "C_Yellow", "C_Red", "G_Yellow", "C_Red", "D_Yellow", "D_Red", "E_Yellow", "D_Red", "E_Yellow", "C_Red", "E_Yellow", "E_Red"
};
Assert.AreEqual("Yellow", WhoIsWinner(myList), "it should return Yellow");
}
public static string WhoIsWinner(List<string> piecesPositionList) {
// Put the grid into a dictionary and check it
Dictionary<char, List<char>> rows = "ABCDEFG".ToDictionary(c => c, c => new List<char>());
foreach (var piece in piecesPositionList) {
char row = char.ToUpper(piece.First());
char color = char.ToUpper(piece.Split('_').Last().First());
if (rows.ContainsKey(row)) rows[row].Add(color);
bool win = CheckTheRows(rows);
if (win) return color == 'Y' ? "Yellow" : "Red";
}
return "Draw";
}
public static bool CheckTheRows(Dictionary<char, List<char>> rows) {
int maxLength = rows.Values.Max(r => r.Count());
//check each row
if (rows.Values.Any(r => CantainsWin(string.Concat(r)))) return true;
// check each line
string[] lines = new string[maxLength];
for (int x = 0; x < lines.Length; x++) {
foreach (List<char> row in rows.Values) {
lines[x] += x < row.Count ? row[x] : '*';
}
}
if (lines.Count() > 0 && lines.Any(CantainsWin)) return true;
// check queer 1: /
for (int x = 0; x < 7 + maxLength; x++) {
int xPtr = x;
string line = "";
foreach (List<char> row in rows.Values) {
line += xPtr >= 0 && xPtr < row.Count ? row[xPtr] : "*";
xPtr--;
}
if (CantainsWin(line)) return true;
}
// check queer 2: \
for (int x = -7 - maxLength; x < maxLength; x++) {
int xPtr = x;
string line = "";
foreach (List<char> row in rows.Values) {
line += xPtr >= 0 && xPtr < row.Count ? row[xPtr] : "*";
xPtr++;
}
if (CantainsWin(line)) return true;
}
return false;
}
public static bool CantainsWin(string line) => string.IsNullOrWhiteSpace(line) ? false : line.Contains("YYYY") || line.Contains("RRRR");
}