-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessVarTests.py
More file actions
216 lines (196 loc) · 9.22 KB
/
ChessVarTests.py
File metadata and controls
216 lines (196 loc) · 9.22 KB
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
# Tests, notes, manual testing block for Chess Game
import unittest
from ChessVar import ChessVar
from unittest.mock import patch
from io import StringIO
class MyTestCase(unittest.TestCase):
# def test_something(self):
# self.assertEqual(True, False) # add assertion here
# TODO: does opening position print correctly?
# NOTE:
# This test was an exploratory attempt to snapshot-test the ASCII board output.
# It was ultimately not implemented because the output is large, formatting-heavy,
# and brittle to minor presentation changes. Preserved here as a development note.
# @patch('sys.stdout', new_callable=StringIO)
# def test_opening_position_print(self, mock_stdout):
# game = ChessVar()
# expected_output = ( """ a b c d e f g h
# ╭─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────╮
# 8 │ ♜ │ ♞ │ ♝ │ ♛ │ ♚ │ ♝ │ ♞ │ ♜ │ 8
# ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤
# 7 │ ♟ │ ♟ │ ♟ │ ♟ │ ♟ │ ♟ │ ♟ │ ♟ │ 7
# ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤
# 6 │ │ │ │ │ │ │ │ │ 6
# ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤
# 5 │ │ │ │ │ │ │ │ │ 5
# ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤
# 4 │ │ │ │ │ │ │ │ │ 4
# ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤
# 3 │ │ │ │ │ │ │ │ │ 3
# ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤
# 2 │ ♙ │ ♙ │ ♙ │ ♙ │ ♙ │ ♙ │ ♙ │ ♙ │ 2
# ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤
# 1 │ ♖ │ ♘ │ ♗ │ ♕ │ ♔ │ ♗ │ ♘ │ ♖ │ 1
# ╰─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────╯
# a b c d e f g h """
# )
# game.print_board()
# actual_output = mock_stdout.getvalue().strip()
#
# # Check if the actual output matches the expected output
# self.assertEqual(actual_output, expected_output)
# TODO: does each piece exist at the start of the game? (as an object)
# does the game initialize?
def test_initialization(self):
game = ChessVar()
starting_position = [['♖', '♘', '♗', '♕', '♔', '♗', '♘', '♖'],
['♙', '♙', '♙', '♙', '♙', '♙', '♙', '♙'],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
['♟', '♟', '♟', '♟', '♟', '♟', '♟', '♟'],
['♜', '♞', '♝', '♛', '♚', '♝', '♞', '♜']]
self.assertEqual(game._board, starting_position)
self.assertEqual(game._state, "UNFINISHED")
self.assertEqual(game._turn,"WHITE")
def test_is_white_pawn_move(self):
game = ChessVar()
# Ensure white pawn can move two at start of game
result = game._is_white_pawn_move(2, 0, 1, " ")
self.assertTrue(result)
# Ensure white pawn can't move diagonal without capture
move = game._is_white_pawn_move(1,1, 2, " ")
self.assertEqual(move, None)
def test_is_black_pawn_move(self):
game = ChessVar()
# Ensure black pawn can move by 2 at start of game
result = game._is_black_pawn_move(-2, 0, 6, " ")
self.assertTrue(result)
# Ensure black pawn can't move diagonal without capture
result2 = game._is_black_pawn_move(-1, 1, 6, " ")
self.assertEqual(result2, None)
# All further test ideas were addressed by the manual testing block
# def test_is_knight_move
# def test_is_rook_move
# def test_is_bishop_move
# def test_is_queen_move
# def test_is_king_move
#def test_basic_make_move
# does it have 8 black pawns in row 7, and 8 white pawns in row 2?
# is the black King in e8 and the white King in e1?
# does white move first?
# do all the pieces move following their respective rules
# can the knight successfully hop over pieces?
# does the game correctly prevent players from moving to same color pop sq
# do captures cause explosions?
# do pawns differentiate between first turn/move and afterwards?
# do captures remove the capturing piece?
# do explosions wipe out the 8 surrounding squares?
# are pawns immune from explosions?
# are pawns still able to be captured?
# does capturing pawns still trigger explosions?
# does the board accurately reflect changes over time?
# does turn taking consistently work across multiple moves?
# does capturing the king end the game?
# does exploding the king end the game?
# when game ends, is the winner correctly designated?
# does make move actually make said move?
# does make move reject out of bounds moves?
# does make move reject captures that
if __name__ == '__main__':
unittest.main()
"""This manual integration testing block can be pasted at the bottom of
ChessVar.py"""
# game = ChessVar()
# # test legal pawn opening 2 moves
# game.make_move("e2", "e4")
#
# # test black pawn legal opening
# game.make_move("b7", "b5")
# game.print_board()
# # test black pawn continues
# game.make_move("e4", "e5")
# game.print_board()
# # test black pawn moves to e5 where white pawn is currently
# game.make_move("e7", "e5") # successfully stops it
# game.print_board()
# # test black pawn move to diagonal square and white capture
# game.make_move("f7", "f6")
# game.make_move("e5", "f6")
# game.print_board()
# #test black pawn attack that wipes out non-pawns
# game.make_move("b5", "b4")
# game.make_move("g2", "g4")
# game.make_move("b4", "b3")
# game.make_move("g4","g5")
# game.make_move("b3","c2")
# # test king move
# game.make_move("e1", "d1")
# # test black king, diagonal
# game.make_move("e8", "f7")
# # test white king diagonal
#
# game.make_move("d1", "c2")
# # a advanced to attack black king
#
# game.make_move("f7", "f6")
# # bw diagonal white king
#
# game.make_move("c2", "b1")
#
# # b king x w pawn should fail
#
# print(game.make_move("f6", "g5"))
#
#
# game.make_move("c7", "c6")
#
# game.make_move("g5", "f6")
# #test white knight move
# game3 = ChessVar()
# game3.make_move("b1", "c3")
# game3.make_move("b8","d7")
# game3.make_move("b8", "c6")
# print(game3.make_move("c3","h4"))
#
# print(game3.make_move("c3","d5"))
# #test other knight shape
#
# print(game3.make_move("c6","a5"))
#
# #test knight endgame
# game3.make_move("d5","e7")
# # test a second game running
# game2 = ChessVar()
# # test a pawn led strike on king
# game2.make_move("d2","d4")
# game2.make_move("f7", "f5")
# game2.make_move("d4", "d5")
# game2.make_move("f5", "f4")
# game2.make_move("d5","d6")
# game2.make_move("f4","f3")
# game2.make_move("d6","e7")
# game2.print_board()
#
# # test queen, bishop, rook
# game4 = ChessVar()
# Progress notes
"""
6/2/24
basic turn control functionality working
and pawns move into blank spaces correctly, including first turn diff
next: get another non-knight piece moving within rules, maybe rook
after: differentiate between rook advance one place to capture, vs pawn can't
look into possible unnecessary boolean calls for if diagonal
watch out for remaining errors in direction labeling from rank_i_diff etc.
6/2 update - fixed alignment of rank math and rank index math 1 now maps
to 0 etc, 2 to 1. so e4-e2 will yield a positive value as will the rank
idx 3-1. the printboard is reversed now to compensate for chess vertically
showing the last row first, on down.
6/3/24, excellent progress, passes all pawn tests and knight test,
rook bishop queen and king are what remained. Wondering if the checks for vertical
for obstacles can live within the pieces themselves instead of as a separate
set of checks, the king is easy I could writer a now actually. DONE.
only bishop and rook remain. queen is a combo.
"""