chess_vwong 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,28 @@
1
+ module ChessVwong
2
+ class Queen < Piece
3
+
4
+ def character
5
+ color == "w" ? "\u{265B}" : "\u{2655}"
6
+ end
7
+
8
+ # Generate all possible Neighbouring Nodes
9
+ def generate_neighbours(current_space)
10
+ moves = []
11
+ # Diaganol
12
+ (1..8).each {|i| moves << [i, i]}
13
+ (1..8).each {|i| moves << [i, -i]}
14
+ (1..8).each {|i| moves << [-i, i]}
15
+ (1..8).each {|i| moves << [-i, -i]}
16
+ # Straight
17
+ (1..8).each {|i| moves << [i, 0]}
18
+ (1..8).each {|i| moves << [0, i]}
19
+ (1..8).each {|i| moves << [-i, 0]}
20
+ (1..8).each {|i| moves << [0, -i]}
21
+ moves.each do |move|
22
+ neigbour_helper(current_space, move[0], move[1])
23
+ end
24
+ end
25
+
26
+ end
27
+ end
28
+
@@ -0,0 +1,22 @@
1
+ module ChessVwong
2
+ class Rook < Piece
3
+
4
+ def character
5
+ color == "w" ? "\u{265C}" : "\u{2656}"
6
+ end
7
+
8
+ # Generate all possible Neighbouring Nodes
9
+ def generate_neighbours(current_space)
10
+ moves = []
11
+ (1..8).each {|i| moves << [i, 0]}
12
+ (1..8).each {|i| moves << [0, i]}
13
+ (1..8).each {|i| moves << [-i, 0]}
14
+ (1..8).each {|i| moves << [0, -i]}
15
+ moves.each do |move|
16
+ neigbour_helper(current_space, move[0], move[1])
17
+ end
18
+ end
19
+
20
+ end
21
+ end
22
+
@@ -0,0 +1,3 @@
1
+ module ChessVwong
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # require "chess_vwong/version"
2
+
3
+ module ChessVwong
4
+ # Your code goes here...
5
+ end
6
+
7
+
8
+ require_relative "./chess_vwong/node.rb"
9
+ require_relative "./chess_vwong/player.rb"
10
+ require_relative "./chess_vwong/piece.rb"
11
+ require_relative "./chess_vwong/knight.rb"
12
+ require_relative "./chess_vwong/rook.rb"
13
+ require_relative "./chess_vwong/king.rb"
14
+ require_relative "./chess_vwong/bishop.rb"
15
+ require_relative "./chess_vwong/queen.rb"
16
+ require_relative "./chess_vwong/pawn.rb"
17
+ require_relative "./chess_vwong/board.rb"
18
+ require_relative "./chess_vwong/preload.rb"
19
+ require_relative "./chess_vwong/game.rb"
20
+
21
+ # board = ChessVwong::Board.new
22
+ # board.preload_pieces
23
+ # board.formatted_grid
@@ -0,0 +1,50 @@
1
+ require "spec_helper"
2
+
3
+ module ChessVwong
4
+ describe Bishop do
5
+ let (:current_space) { [1,1]}
6
+ let (:bishop) { Bishop.new(current_space,"w")}
7
+ context "#initialize" do
8
+ it "should return Piece attributes: color" do
9
+ expect(bishop.color).to eq "w"
10
+ end
11
+
12
+ it "should return Piece attributes: current_space" do
13
+ expect(bishop.current_space).to eq current_space
14
+ end
15
+ end
16
+
17
+ context "#character" do
18
+ it "should return a white bishop character" do
19
+ expect(bishop.character).to eq "\u{265D}"
20
+ end
21
+
22
+ it "should return a black bishop character" do
23
+ bishop_2 = Bishop.new(current_space,"b")
24
+ expect(bishop_2.character).to eq "\u{2657}"
25
+ end
26
+ end
27
+
28
+ context "#generate_neighbours" do
29
+ it "should return only 7 neighbour when [1,1]" do
30
+ bishop.generate_neighbours(current_space)
31
+ expect(bishop.neighbours.count).to eq 7
32
+ end
33
+
34
+ it "should return only 7 neighbour when [8,8]" do
35
+ current_space = [8,8]
36
+ bishop.generate_neighbours(current_space)
37
+ expect(bishop.neighbours.count).to eq 7
38
+ end
39
+
40
+ it "should return only 13 neighbour when in the middle" do
41
+ current_space = [3,3]
42
+ bishop.generate_neighbours(current_space)
43
+ expect(bishop.neighbours.count).to eq 11
44
+ end
45
+
46
+ end
47
+
48
+
49
+ end
50
+ end
@@ -0,0 +1,293 @@
1
+ require "spec_helper"
2
+
3
+ module ChessVwong
4
+ describe Board do
5
+ let (:board) { Board.new}
6
+ let (:player_1) { Player.new("bob")}
7
+ let (:player_2) { Player.new("peter", "b")}
8
+ let (:knight) { Knight.new( [0,0], "w")}
9
+ let (:knight_2) { Knight.new( [0,0], "b")}
10
+ let (:rook) { Rook.new( [3,3], "w")}
11
+ let (:pawn) { Pawn.new( [6,6], "w")}
12
+ let (:pawn_2) { Pawn.new( [6,6], "b")}
13
+
14
+ context "#initialize" do
15
+ it "sets the grid with 8 rows" do
16
+ expect(board.grid.size).to eq 9
17
+ end
18
+
19
+ it "creates 8 things in each row" do
20
+ board.grid.each do |row|
21
+ expect(row.size).to eq 9
22
+ end
23
+ end
24
+ end
25
+
26
+ context "#process_input" do
27
+ it "should turn A1 to [1,8]" do
28
+ expect(board.process_input("A1")).to eq [1,8]
29
+ end
30
+
31
+ it "should turn a1 to [1,8]" do
32
+ expect(board.process_input("a1")).to eq [1,8]
33
+ end
34
+
35
+ it "should turn H7 to [8,2]" do
36
+ expect(board.process_input("H7")).to eq [8,2]
37
+ end
38
+
39
+ it "should turn H1 to [8,8]" do
40
+ expect(board.process_input("H1")).to eq [8,8]
41
+ end
42
+
43
+ it "should turn b2 to [2,7]" do
44
+ expect(board.process_input("B2")).to eq [2,7]
45
+ end
46
+ end
47
+
48
+ context "#get_piece" do
49
+ it "gets the piece from selected node" do
50
+ chosen_node = board.grid[3][2]
51
+ chosen_node.occupied << knight
52
+ expect(board.get_piece("B6", player_1)).to eq knight
53
+ end
54
+
55
+ it "it removes piece from selected node" do
56
+ chosen_node = board.grid[3][2]
57
+ chosen_node.occupied << knight
58
+ board.get_piece("B6", player_1)
59
+ expect(chosen_node.occupied).to eq []
60
+ end
61
+
62
+ it "it generates neighbours of selected piece" do
63
+ chosen_node = board.grid[1][1]
64
+ chosen_node.occupied << knight
65
+ piece = board.get_piece("A8", player_1)
66
+ expect(piece.neighbours.count).to eq 2
67
+ end
68
+
69
+
70
+ it "it generates neighbours of Pawn if enemies nearby" do
71
+ chosen_node = board.grid[7][7]
72
+ chosen_node.occupied << pawn
73
+ enemy_node1 = board.grid[6][6]
74
+ enemy_node1.occupied << knight_2
75
+ enemy_node2 = board.grid[6][8]
76
+ enemy_node2.occupied << knight_2
77
+ piece = board.get_piece("G2",player_1)
78
+ expect(piece.neighbours.count).to eq 4
79
+ end
80
+
81
+ it "it generates neighbours of Pawn if no enemies nearby" do
82
+ chosen_node = board.grid[7][7]
83
+ chosen_node.occupied << pawn
84
+ enemy_node1 = board.grid[6][6]
85
+ enemy_node1.occupied << knight
86
+ enemy_node2 = board.grid[6][8]
87
+ enemy_node2.occupied << knight
88
+ piece = board.get_piece("G2",player_1)
89
+ expect(piece.neighbours.count).to eq 2
90
+ end
91
+
92
+ it "it should return nil if no piece found" do
93
+ expect(board.get_piece("A1", player_1)).to eq nil
94
+ end
95
+
96
+ end
97
+
98
+ context "#set_piece" do
99
+ it "sets Knight to selected node" do
100
+ chosen_node = board.grid[3][3]
101
+ chosen_node.occupied << knight
102
+ board.get_piece("C6", player_1)
103
+ board.set_piece("D8", player_1)
104
+ new_node = board.grid[1][4]
105
+ expect(new_node.occupied).to eq [knight]
106
+ end
107
+
108
+ it "sets Pawn to selected node" do
109
+ chosen_node = board.grid[6][1]
110
+ chosen_node.occupied << pawn
111
+ board.get_piece("A3", player_1)
112
+ board.set_piece("A4", player_1)
113
+ new_node = board.grid[5][1]
114
+ expect(new_node.occupied).to eq [pawn]
115
+ end
116
+
117
+ it "returns nil if outside piece's range" do
118
+ chosen_node = board.grid[1][1]
119
+ chosen_node.occupied << knight
120
+ board.get_piece("A8", player_1)
121
+ expect(board.set_piece("A1", player_1)).to eq nil
122
+ end
123
+
124
+ it "should turn Pawn to Queen" do
125
+ chosen_node = board.grid[2][1]
126
+ chosen_node.occupied << pawn
127
+ board.get_piece("A7", player_1)
128
+ expect(board.set_piece("A8", player_1)).to_not eq [pawn]
129
+ end
130
+
131
+ it "adds piece to Player's kill list if piece is over taken" do
132
+ chosen_node = board.grid[1][1]
133
+ chosen_node.occupied << knight
134
+ new_node = board.grid[2][3]
135
+ new_node.occupied << knight_2
136
+ board.get_piece("A8", player_1)
137
+ board.set_piece("C7", player_1)
138
+ expect(player_1.kill_list).to eq [knight_2]
139
+ end
140
+ end
141
+
142
+
143
+
144
+ # ########DETERMINING CLEAR PATHS #################
145
+ context "#vertical_path" do
146
+ it "should return postive node_paths" do
147
+ expect(board.vertical_path([6,0], [6,6]).count).to eq 6
148
+ end
149
+
150
+ it "should return negative node_paths" do
151
+ expect(board.vertical_path([0,6], [0,0]).count).to eq 6
152
+ end
153
+ end
154
+
155
+ context "#horizontal_path" do
156
+ it "should return postive node_paths" do
157
+ expect(board.horizontal_path([0,0], [6,0]).count).to eq 6
158
+ end
159
+
160
+ it "should return negative node_paths" do
161
+ expect(board.horizontal_path([6,0], [0,0]).count).to eq 6
162
+ end
163
+ end
164
+
165
+ context "#rising_path" do
166
+ it "should return postive node_paths" do
167
+ expect(board.rising_path([3,3], [6,0]).count).to eq 3
168
+ end
169
+
170
+ it "should return negative node_paths" do
171
+ expect(board.rising_path([3,3], [0, 6]).count).to eq 3
172
+ end
173
+ end
174
+
175
+ context "#falling_path" do
176
+ it "should return postive node_paths" do
177
+ expect(board.falling_path([3,3], [0,0]).count).to eq 3
178
+ end
179
+
180
+ it "should return postive node_paths" do
181
+ expect(board.falling_path([3,3], [7,7]).count).to eq 4
182
+ end
183
+ end
184
+
185
+
186
+ context "#clear_path?" do
187
+ it "should return true if vertical_path is clear" do
188
+ expect(board.clear_path?([0,0], [0,4])).to_not eq false
189
+ end
190
+
191
+ it "should return true if vertical_path is clear, downwards" do
192
+ expect(board.clear_path?([0,6], [0,4])).to_not eq false
193
+ end
194
+
195
+ it "should return false if vertical_path is unclear" do
196
+ chosen_node = board.grid[3][0]
197
+ chosen_node.occupied << knight
198
+ expect(board.clear_path?([0,0], [0,4])).to eq false
199
+ end
200
+
201
+ it "should return true if horizontal_path is clear" do
202
+ expect(board.clear_path?([0,0], [6,0])).to_not eq false
203
+ end
204
+
205
+ it "should return false if horizontal_path is unclear" do
206
+ chosen_node = board.grid[0][3]
207
+ chosen_node.occupied << knight
208
+ expect(board.clear_path?([0,0], [6,0])).to eq false
209
+ end
210
+
211
+ it "should return true if rising_path is clear" do
212
+ expect(board.clear_path?([3,3], [0,6])).to_not eq false
213
+ end
214
+
215
+
216
+ it "should return false if rising_path is unclear" do
217
+ chosen_node = board.grid[2][4]
218
+ chosen_node.occupied << knight
219
+ expect(board.clear_path?([3,3], [5,1])).to eq false
220
+ end
221
+
222
+ it "should return true if falling_path is clear" do
223
+ expect(board.clear_path?([3,3], [0,0])).to_not eq false
224
+ end
225
+
226
+ it "should return false if horizontal_path is unclear" do
227
+ chosen_node = board.grid[4][4]
228
+ chosen_node.occupied << knight
229
+ expect(board.clear_path?([3,3], [7,7])).to eq false
230
+ end
231
+ end
232
+
233
+ context "valid_path" do
234
+ it "should be valid if path is clear" do
235
+ chosen_node = board.grid[3][3]
236
+ chosen_node.occupied << rook
237
+ board.get_piece("D3", player_1)
238
+ expect(board.valid_path?(board.get_value, [7,3])).to_not eq false
239
+ end
240
+
241
+ it "should be invalid if path is blocked" do
242
+ chosen_node = board.grid[3][3]
243
+ chosen_node.occupied << rook
244
+ blocked_node = board.grid[3][5]
245
+ blocked_node.occupied << knight
246
+ board.get_piece("C6", player_1)
247
+ expect(board.valid_path?(board.get_value, [7,3])).to eq false
248
+ end
249
+
250
+ it "should be invalid if path is blocked" do
251
+ chosen_node = board.grid[3][3]
252
+ chosen_node.occupied << rook
253
+ blocked_node = board.grid[4][3]
254
+ blocked_node.occupied << knight
255
+ board.get_piece("C6", player_1)
256
+ expect(board.valid_path?(board.get_value, [3,7])).to eq false
257
+ end
258
+
259
+ it "should be invalid if end_node is occupied with same color" do
260
+ chosen_node = board.grid[3][3]
261
+ chosen_node.occupied << rook
262
+ blocked_node = board.grid[3][7]
263
+ blocked_node.occupied << knight
264
+ board.get_piece("C6", player_1)
265
+ expect(board.valid_path?(board.get_value, [7,3])).to eq false
266
+ end
267
+
268
+
269
+ # it "should be valid if end_node is occupied with different color" do
270
+ # chosen_node = board.grid[3][3]
271
+ # chosen_node.occupied << rook
272
+ # blocked_node = board.grid[3][7]
273
+ # blocked_node.occupied << knight_2
274
+ # board.get_piece("C6", player_1)
275
+ # expect(board.valid_path?(board.get_value, [7,3])).to_not eq false
276
+ # end
277
+
278
+ it "should be valid if using Knight and path is blocked" do
279
+ chosen_node = board.grid[1][1]
280
+ chosen_node.occupied << knight
281
+ blocked_node = board.grid[1][2]
282
+ blocked_node.occupied << rook
283
+ blocked_node = board.grid[2][2]
284
+ blocked_node.occupied << rook
285
+ blocked_node = board.grid[2][1]
286
+ blocked_node.occupied << rook
287
+ board.get_piece("A8", player_1)
288
+ expect(board.valid_path?(board.get_value, [3,2])).to_not eq false
289
+ end
290
+ end
291
+
292
+ end
293
+ end
data/spec/game_spec.rb ADDED
@@ -0,0 +1,38 @@
1
+ require "spec_helper"
2
+ module ChessVwong
3
+ describe Game do
4
+
5
+ let (:bob) { Player.new("Bob", "w") }
6
+ let (:peter) { Player.new("Peter", "b") }
7
+ let (:king) { King.new([0,0], "b") }
8
+ let (:game) { Game.new([bob, peter]) }
9
+
10
+ context "#switch_players" do
11
+ it "will set @current_player to @other_player" do
12
+ current_player = bob
13
+ other_player = peter
14
+ game.switch_players
15
+ expect(game.current_player).to eq peter
16
+ end
17
+
18
+ it "will set @other_player to @current_player" do
19
+ current_player = bob
20
+ other_player = peter
21
+ game.switch_players
22
+ expect(game.other_player).to eq bob
23
+ end
24
+ end
25
+
26
+ context "#game_over" do
27
+ it "should return winner if king has just been killed" do
28
+ game.current_player.kill_list << king
29
+ expect(game.game_over).to eq game.current_player
30
+ end
31
+
32
+ it "should return false if no King" do
33
+ expect(game.game_over).to eq false
34
+ end
35
+ end
36
+
37
+ end
38
+ end
data/spec/king_spec.rb ADDED
@@ -0,0 +1,51 @@
1
+ require "spec_helper"
2
+
3
+ module ChessVwong
4
+ describe King do
5
+ let (:current_space) { [1,1]}
6
+ let (:king) { King.new(current_space,"w")}
7
+ context "#initialize" do
8
+ it "should return Piece attributes: color" do
9
+ expect(king.color).to eq "w"
10
+ end
11
+
12
+ it "should return Piece attributes: current_space" do
13
+ expect(king.current_space).to eq current_space
14
+ end
15
+ end
16
+
17
+ context "#character" do
18
+ it "should return a white king character" do
19
+ expect(king.character).to eq "\u{265A}"
20
+ end
21
+
22
+ it "should return a black king character" do
23
+ king_2 = King.new(current_space,"b")
24
+ expect(king_2.character).to eq "\u{2654}"
25
+ end
26
+ end
27
+
28
+ context "#generate_neighbours" do
29
+
30
+ it "should return only 16 neighbour when [1,1]" do
31
+ king.generate_neighbours(current_space)
32
+ expect(king.neighbours.count).to eq 3
33
+ end
34
+
35
+ it "should return only 14 neighbour when [8,8]" do
36
+ current_space = [8,8]
37
+ king.generate_neighbours(current_space)
38
+ expect(king.neighbours.count).to eq 3
39
+ end
40
+
41
+ it "should return only 14 neighbour when in the middle" do
42
+ current_space = [3,3]
43
+ king.generate_neighbours(current_space)
44
+ expect(king.neighbours.count).to eq 8
45
+ end
46
+
47
+ end
48
+
49
+
50
+ end
51
+ end
@@ -0,0 +1,73 @@
1
+ require "spec_helper"
2
+
3
+ module ChessVwong
4
+ describe Knight do
5
+ let (:current_space) {[1,1]}
6
+ let (:knight) { Knight.new(current_space, "w")}
7
+ context "#initialize" do
8
+ it "should return Piece attributes: color" do
9
+ expect(knight.color).to eq "w"
10
+ end
11
+
12
+ it "should return Piece attributes: current_space" do
13
+ expect(knight.current_space).to eq current_space
14
+ end
15
+ end
16
+
17
+ context "#character" do
18
+ it "should return a white knight character" do
19
+ expect(knight.character).to eq "\u{265E}"
20
+ end
21
+
22
+ it "should return a black knight character" do
23
+ knight_2 = Knight.new(current_space,"b")
24
+ expect(knight_2.character).to eq "\u{2658}"
25
+ end
26
+ end
27
+
28
+ context "#generate_neighbours" do
29
+ it "should return only 2 neighbour when [1,1]" do
30
+ knight.generate_neighbours(current_space)
31
+ expect(knight.neighbours.count).to eq 2
32
+ end
33
+
34
+ it "should return only 2 neighbour when [7,7]" do
35
+ current_space = [8,8]
36
+ knight.generate_neighbours(current_space)
37
+ expect(knight.neighbours.count).to eq 2
38
+ end
39
+
40
+ it "should return only 4 neighbour when on left edge" do
41
+ current_space = [1,4]
42
+ knight.generate_neighbours(current_space)
43
+ expect(knight.neighbours.count).to eq 4
44
+ end
45
+
46
+ it "should return only 4 neighbour when on right edge" do
47
+ current_space = [8,4]
48
+ knight.generate_neighbours(current_space)
49
+ expect(knight.neighbours.count).to eq 4
50
+ end
51
+
52
+ it "should return only 4 neighbour when on top edge" do
53
+ current_space = [4,1]
54
+ knight.generate_neighbours(current_space)
55
+ expect(knight.neighbours.count).to eq 4
56
+ end
57
+
58
+ it "should return only 4 neighbour when on right edge" do
59
+ current_space = [4,8]
60
+ knight.generate_neighbours(current_space)
61
+ expect(knight.neighbours.count).to eq 4
62
+ end
63
+
64
+ it "should return 8 neighbour when in middle" do
65
+ current_space = [4,4]
66
+ knight.generate_neighbours(current_space)
67
+ expect(knight.neighbours.count).to eq 8
68
+ end
69
+ end
70
+
71
+
72
+ end
73
+ end
data/spec/node_spec.rb ADDED
@@ -0,0 +1,24 @@
1
+ require "spec_helper"
2
+
3
+ module ChessVwong
4
+ describe Node do
5
+ let (:node) { Node.new}
6
+ let (:knight) { Knight.new(node, "w")}
7
+
8
+ context "#intialize" do
9
+ # it "is initialized with x,y coordinates for its value" do
10
+ # expect(node.value).to eq [0,0]
11
+ # end
12
+
13
+ it "defaults to unoccupied" do
14
+ expect(node.occupied).to eq []
15
+ end
16
+
17
+ it "can accept pieces as occupants" do
18
+ node.occupied << knight
19
+ expect(node.occupied).to eq [knight]
20
+ end
21
+ end
22
+
23
+ end
24
+ end