-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chessboard.kt
289 lines (254 loc) · 12.9 KB
/
Chessboard.kt
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
package chess
const val CHESS_COLUMN_NUMBER = 8
const val CHESS_ROWS_NUMBER = 8
const val WHITE_PAWNS_STARTING_RANK = 2
const val BLACK_PAWNS_STARTING_RANK = 7
const val WHITE_PAWNS_EN_PASSANT_RANK = 5
const val BLACK_PAWNS_EN_PASSANT_RANK = 4
enum class SquareType(val representation: String) {
WHITE(" W "), BLACK(" B "), EMPTY(" ")
}
enum class GameState {
PLAY, END
}
class Chessboard {
private var gameState = GameState.PLAY
private val chessboard = MutableList(CHESS_ROWS_NUMBER) { rank ->
MutableList(CHESS_COLUMN_NUMBER) {
when (rank) {
WHITE_PAWNS_STARTING_RANK - 1 -> SquareType.WHITE
BLACK_PAWNS_STARTING_RANK - 1 -> SquareType.BLACK
else -> SquareType.EMPTY
}
}
}
private var whiteFirstMove2Squares = false
private var blackFirstMove2Squares = false
fun readPlayerNames(): List<String> {
println("First Player's name:")
val player1name = readln().trim()
println("Second Player's name:")
val player2name = readln().trim()
return listOf(player1name, player2name)
}
private fun printChessboard() {
val verticalSide = "|"
val horizontalSide = "+---"
val indentation = " "
fun drawHorizontalLine() {
println(indentation + horizontalSide.repeat(CHESS_COLUMN_NUMBER) + "+")
}
fun drawFilesLabel() {
print(indentation)
('a'..'h').forEach {
print(" $it ")
}
}
fun drawSquare(squareType: SquareType) {
print(verticalSide + squareType.representation)
}
fun printRank(rank: Int) {
print("$rank ")
}
chessboard.reversed().forEachIndexed { index, rank ->
drawHorizontalLine()
printRank(CHESS_ROWS_NUMBER - index)
rank.forEach { cell ->
when (cell) {
SquareType.EMPTY -> drawSquare(SquareType.EMPTY)
SquareType.WHITE -> drawSquare(SquareType.WHITE)
SquareType.BLACK -> drawSquare(SquareType.BLACK)
}
}
println(verticalSide)
}
drawHorizontalLine()
drawFilesLabel()
println()
println()
}
fun play(player1name: String, player2name: String) {
printChessboard()
while (true) {
if (gameState == GameState.PLAY) {
if (isStalemateCondition(SquareType.WHITE)) {
println("Stalemate!")
println("Bye!")
return
}
playerTurn(player1name, SquareType.WHITE)
printChessboard()
if (isWinningCondition()) {
println("White Wins!")
println("Bye!")
return
}
} else break
if (gameState == GameState.PLAY) {
if (isStalemateCondition(SquareType.BLACK)) {
println("Stalemate!")
println("Bye!")
return
}
playerTurn(player2name, SquareType.BLACK)
printChessboard()
if (isWinningCondition()) {
println("Black Wins!")
println("Bye!")
return
}
} else break
}
}
private fun playerTurn(playerName: String, color: SquareType) {
while (true) {
println("$playerName's turn:")
when (val command = readln().trim()) {
"exit" -> {
println("Bye!")
gameState = GameState.END
break
}
else -> if (isValidMove(command, color)) break
}
}
}
private fun squareColor(rank: Int, file: Int): SquareType = chessboard[rank - 1][file]
private fun updateChessboard(playerInput: String, color: SquareType, isEnPassant: Boolean = false) {
val startingFile = playerInput[0] - 'a'
val startingRank = playerInput[1].digitToInt()
val endingFile = playerInput[2] - 'a'
val endingRank = playerInput[3].digitToInt()
chessboard[startingRank - 1][startingFile] = SquareType.EMPTY
chessboard[endingRank - 1][endingFile] = color
if (isEnPassant && color == SquareType.WHITE) chessboard[endingRank - 2][endingFile] = SquareType.EMPTY
else if (isEnPassant && color == SquareType.BLACK) chessboard[endingRank][endingFile] = SquareType.EMPTY
}
private fun isValidMove(playerInput: String, color: SquareType): Boolean {
val validSquareRegexp = "[a-hA-H][1-8][a-hA-H][1-8]".toRegex()
val startingFile = playerInput[0] - 'a'
val startingRank = playerInput[1].digitToInt()
val endingFile = playerInput[2] - 'a'
val endingRank = playerInput[3].digitToInt()
val sameFile = startingFile == endingFile
val emptyEndingSquare = squareColor(endingRank, endingFile) == SquareType.EMPTY
val whiteEndingSquare = squareColor(endingRank, endingFile) == SquareType.WHITE
val blackEndingSquare = squareColor(endingRank, endingFile) == SquareType.BLACK
val whiteFirstMove =
startingRank == WHITE_PAWNS_STARTING_RANK && (endingRank == WHITE_PAWNS_STARTING_RANK + 1 || endingRank == WHITE_PAWNS_STARTING_RANK + 2)
val whiteMove = startingRank != WHITE_PAWNS_STARTING_RANK && endingRank == startingRank + 1
val whiteStandardCapture =
if (startingRank == CHESS_ROWS_NUMBER - 1) false
else if (startingFile == 0) endingRank == startingRank + 1 && endingFile == startingFile + 1
else if (startingFile == CHESS_COLUMN_NUMBER - 1) endingRank == startingRank + 1 && endingFile == startingFile - 1
else endingRank == startingRank + 1 && (endingFile == startingFile + 1 || endingFile == startingFile - 1)
val whiteEnPassantCapture = blackFirstMove2Squares && startingRank == WHITE_PAWNS_EN_PASSANT_RANK &&
when (startingFile) {
0 -> squareColor(startingRank, startingFile + 1) == SquareType.BLACK &&
endingRank == startingRank + 1 && endingFile == startingFile + 1
CHESS_COLUMN_NUMBER - 1 -> squareColor(startingRank, startingFile - 1) == SquareType.BLACK &&
endingRank == startingRank + 1 && endingFile == startingFile - 1
else -> endingRank == startingRank + 1 &&
(squareColor(startingRank, startingFile + 1) == SquareType.BLACK &&
endingFile == startingFile + 1) ||
(squareColor(startingRank, startingFile - 1) == SquareType.BLACK &&
endingFile == startingFile - 1)
}
val whiteLegitMove = sameFile && emptyEndingSquare && (whiteFirstMove || whiteMove)
val whiteLegitCapture =
(blackEndingSquare && whiteStandardCapture) || (emptyEndingSquare && whiteEnPassantCapture)
val blackFirstMove =
startingRank == BLACK_PAWNS_STARTING_RANK && (endingRank == BLACK_PAWNS_STARTING_RANK - 1 || endingRank == BLACK_PAWNS_STARTING_RANK - 2)
val blackMove = startingRank != BLACK_PAWNS_STARTING_RANK && endingRank == startingRank - 1
val blackStandardCapture =
if (startingRank == 0) false
else if (startingFile == 0) endingRank == startingRank - 1 && endingFile == startingFile + 1
else if (startingFile == CHESS_COLUMN_NUMBER - 1) endingRank == startingRank - 1 && endingFile == startingFile - 1
else endingRank == startingRank - 1 && (endingFile == startingFile + 1 || endingFile == startingFile - 1)
val blackEnPassantCapture = whiteFirstMove2Squares && startingRank == BLACK_PAWNS_EN_PASSANT_RANK &&
when (startingFile) {
0 -> squareColor(startingRank, startingFile + 1) == SquareType.WHITE &&
endingRank == startingRank - 1 && endingFile == startingFile + 1
CHESS_COLUMN_NUMBER - 1 -> squareColor(startingRank, startingFile - 1) == SquareType.WHITE &&
endingRank == startingRank - 1 && endingFile == startingFile - 1
else -> endingRank == startingRank - 1 &&
(squareColor(startingRank, startingFile + 1) == SquareType.WHITE &&
endingFile == startingFile + 1) ||
(squareColor(startingRank, startingFile - 1) == SquareType.WHITE &&
endingFile == startingFile - 1)
}
val blackLegitMove = sameFile && emptyEndingSquare && (blackFirstMove || blackMove)
val blackLegitCapture =
(whiteEndingSquare && blackStandardCapture) || (emptyEndingSquare && blackEnPassantCapture)
if (!validSquareRegexp.matches(playerInput)) println("Invalid Input").also { return false }
when (color) {
SquareType.WHITE -> {
return if (chessboard[startingRank - 1][startingFile] != SquareType.WHITE) {
println("No white pawn at ${'a' + startingFile}$startingRank")
false
} else if (whiteLegitMove || whiteLegitCapture) {
whiteFirstMove2Squares =
startingRank == WHITE_PAWNS_STARTING_RANK && endingRank == WHITE_PAWNS_STARTING_RANK + 2 && sameFile
updateChessboard(playerInput, SquareType.WHITE, whiteEnPassantCapture)
true
} else {
println("Invalid Input")
false
}
}
SquareType.BLACK -> {
return if (chessboard[startingRank - 1][startingFile] != SquareType.BLACK) {
println("No black pawn at ${'a' + startingFile}$startingRank")
false
} else if (blackLegitMove || blackLegitCapture) {
blackFirstMove2Squares =
startingRank == BLACK_PAWNS_STARTING_RANK && endingRank == BLACK_PAWNS_STARTING_RANK - 2 && sameFile
updateChessboard(playerInput, SquareType.BLACK, blackEnPassantCapture)
true
} else {
println("Invalid Input")
false
}
}
else -> return false
}
}
private fun isWinningCondition(): Boolean {
var whitePawnsCount = 0
var blackPawnsCount = 0
chessboard[0].forEach { cell ->
if (cell == SquareType.BLACK) return true
}
chessboard[7].forEach { cell ->
if (cell == SquareType.WHITE) return true
}
chessboard.forEach { row ->
blackPawnsCount += row.count { it == SquareType.BLACK }
whitePawnsCount += row.count { it == SquareType.WHITE }
}
if (whitePawnsCount == 0 || blackPawnsCount == 0) return true
return false
}
private fun isStalemateCondition(playerColor: SquareType): Boolean {
chessboard.forEachIndexed { rowIndex, row ->
row.forEachIndexed { columnIndex, cell ->
when (playerColor) {
SquareType.WHITE -> {
if (cell == SquareType.WHITE && chessboard[rowIndex + 1][columnIndex] == SquareType.EMPTY) return false
if (cell == SquareType.WHITE && columnIndex == 0 && chessboard[rowIndex + 1][columnIndex + 1] == SquareType.BLACK) return false
if (cell == SquareType.WHITE && columnIndex == CHESS_COLUMN_NUMBER - 1 && chessboard[rowIndex + 1][columnIndex - 1] == SquareType.BLACK) return false
if (cell == SquareType.WHITE && columnIndex != 0 && columnIndex != CHESS_COLUMN_NUMBER - 1 && (chessboard[rowIndex + 1][columnIndex + 1] == SquareType.BLACK || chessboard[rowIndex + 1][columnIndex - 1] == SquareType.BLACK)) return false
}
SquareType.BLACK -> {
if (cell == SquareType.BLACK && chessboard[rowIndex - 1][columnIndex] == SquareType.EMPTY) return false
if (cell == SquareType.BLACK && columnIndex == 0 && chessboard[rowIndex - 1][columnIndex + 1] == SquareType.WHITE) return false
if (cell == SquareType.BLACK && columnIndex == CHESS_COLUMN_NUMBER - 1 && chessboard[rowIndex - 1][columnIndex - 1] == SquareType.WHITE) return false
if (cell == SquareType.BLACK && columnIndex != 0 && columnIndex != CHESS_COLUMN_NUMBER - 1 && (chessboard[rowIndex - 1][columnIndex + 1] == SquareType.WHITE || chessboard[rowIndex - 1][columnIndex - 1] == SquareType.WHITE)) return false
}
else -> return false
}
}
}
return true
}
}