chess_game 0.0.11
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.
- checksums.yaml +7 -0
- data/.rspec +1 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +58 -0
- data/README.md +24 -0
- data/Rakefile +3 -0
- data/bin/chess +3 -0
- data/chess-0.0.11.gem +0 -0
- data/chess.gemspec +29 -0
- data/game.dat +0 -0
- data/lib/chess/bishop.rb +28 -0
- data/lib/chess/board.rb +170 -0
- data/lib/chess/game.rb +73 -0
- data/lib/chess/king.rb +101 -0
- data/lib/chess/knight.rb +28 -0
- data/lib/chess/move_helper.rb +185 -0
- data/lib/chess/pawn.rb +58 -0
- data/lib/chess/piece.rb +134 -0
- data/lib/chess/player.rb +36 -0
- data/lib/chess/queen.rb +30 -0
- data/lib/chess/rook.rb +30 -0
- data/lib/chess/version.rb +3 -0
- data/lib/chess.rb +71 -0
- data/save/test +0 -0
- data/spec/bishop_spec.rb +32 -0
- data/spec/board_spec.rb +206 -0
- data/spec/game_spec.rb +32 -0
- data/spec/king_spec.rb +167 -0
- data/spec/knight_spec.rb +57 -0
- data/spec/pawn_spec.rb +144 -0
- data/spec/piece_spec.rb +43 -0
- data/spec/player_spec.rb +47 -0
- data/spec/queen_spec.rb +35 -0
- data/spec/rook_spec.rb +31 -0
- data/spec/spec_helper.rb +3 -0
- metadata +147 -0
data/spec/board_spec.rb
ADDED
@@ -0,0 +1,206 @@
|
|
1
|
+
require_relative '../lib/chess/board.rb'
|
2
|
+
|
3
|
+
describe Board do
|
4
|
+
|
5
|
+
before { @board = Board.new }
|
6
|
+
|
7
|
+
it { should respond_to :board }
|
8
|
+
it { should respond_to :piece_between? }
|
9
|
+
|
10
|
+
describe "algebraic notation" do
|
11
|
+
|
12
|
+
it "should respond to algebraic notation" do
|
13
|
+
@board.board["A1"] = "Hello"
|
14
|
+
expect(@board.board["A1"]).to eq("Hello")
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return nil if the hash is out of bounds" do
|
18
|
+
expect(@board.board["Z8"]).to be nil
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should not be nil if the hash is in bounds" do
|
22
|
+
expect(@board.board["A1"]).to_not be nil
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "setting up the game" do
|
28
|
+
|
29
|
+
it "should have a pawn on A2" do
|
30
|
+
expect(@board.board["A2"]).to be_a(Pawn)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should have a queen on D8" do
|
34
|
+
expect(@board.board["D8"]).to be_a(Queen)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should be empty on D4" do
|
38
|
+
expect(@board.board["D4"]).to eq(" ")
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should be able to set pieces on the board" do
|
44
|
+
@white_pawn = Pawn.new("white", "D4")
|
45
|
+
@board.set_piece(@white_pawn)
|
46
|
+
expect(@board.board["D4"]).to eq(@white_pawn)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should be able to remove pieces from the board" do
|
50
|
+
@board.rv_piece("E5")
|
51
|
+
expect(@board.board["E5"]).to eq(" ")
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "making moves" do
|
55
|
+
|
56
|
+
it "should know when a notation refers to a spot off the board" do
|
57
|
+
expect(@board.on_board?("Z1")).to be_falsey
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should know when a notation refers to a spot on the board" do
|
61
|
+
expect(@board.on_board?("A1")).to be_truthy
|
62
|
+
end
|
63
|
+
|
64
|
+
describe "knowing which squares are between two locations" do
|
65
|
+
|
66
|
+
it "should be able to see the squares between on a veritcal move" do
|
67
|
+
expect(squares_between("A1", "A5")).to eq(["A2", "A3", "A4"])
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should be able to see the squares between on a horizontal move" do
|
71
|
+
expect(squares_between("H8", "C8")).to eq(["G8", "F8", "E8", "D8"])
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should be albe to see the squares between on a diagonal move" do
|
75
|
+
expect(squares_between("H8", "C3")).to eq(["G7", "F6", "E5", "D4"])
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "return value" do
|
81
|
+
it "should return the square a piece moved to" do
|
82
|
+
expect(@board.move("B8", "A6")).to eq("A6")
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should not return the square if the move is bad" do
|
86
|
+
expect(@board.move("B8", "B6")).to_not eq("B6")
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe "piece between" do
|
91
|
+
it "should know when pieces are in between two locations" do
|
92
|
+
expect(@board.piece_between?("A1", "A3")).to be_truthy
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should know when there are no pieces in between two locations" do
|
96
|
+
expect(@board.piece_between?("A2", "A4")).to be_falsey
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe "for white pawn" do
|
101
|
+
|
102
|
+
before { @board.move("D2", "D4") }
|
103
|
+
|
104
|
+
it "should be able to move a white pawn two spaces forward" do
|
105
|
+
expect(@board.board["D4"]).to be_a(Pawn)
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should not be able to move a white pawn two spaces on the next turn" do
|
109
|
+
@board.move("D4", "D6")
|
110
|
+
expect(@board.board["D6"]).to eq(" ")
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should be able to move a white pawn one space on the next turn" do
|
114
|
+
@board.move("D4", "D5")
|
115
|
+
expect(@board.board["D5"]).to be_a(Pawn)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe "for a white knight" do
|
120
|
+
|
121
|
+
it "should be able to jump the front row of pawns" do
|
122
|
+
@board.move("B1", "A3")
|
123
|
+
expect(@board.board["A3"]).to be_a(Knight)
|
124
|
+
end
|
125
|
+
|
126
|
+
it "should not be able to take its own color" do
|
127
|
+
@board.move("B1", "D2")
|
128
|
+
expect(@board.board["D2"]).to be_a(Pawn)
|
129
|
+
end
|
130
|
+
|
131
|
+
describe "taking an opposing color" do
|
132
|
+
before do
|
133
|
+
@board.move("B1", "A3")
|
134
|
+
@board.move("A3", "B5")
|
135
|
+
@board.move("B5", "C7")
|
136
|
+
@knight = @board.board["C7"]
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should have a white piece on the square" do
|
140
|
+
expect(@knight.color).to eq("white")
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should be be a knight" do
|
144
|
+
expect(@knight).to be_a(Knight)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
describe "undoing moves" do
|
150
|
+
|
151
|
+
it "should be able to undo moves" do
|
152
|
+
@board.move("D2", "D4")
|
153
|
+
@board.undo_last_move
|
154
|
+
expect(@board["D2"]).to be_a(Pawn)
|
155
|
+
expect(@board["D2"].moves).to eq(0)
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
describe "pawn promotion" do
|
162
|
+
|
163
|
+
before do
|
164
|
+
@board = Board.new
|
165
|
+
@promote = Pawn.new("white", "A8")
|
166
|
+
@board.set_piece(@promote)
|
167
|
+
end
|
168
|
+
|
169
|
+
it "should be able to promote to a queen" do
|
170
|
+
@board.promote("A8", :queen)
|
171
|
+
expect(@board["A8"]).to be_a(Queen)
|
172
|
+
end
|
173
|
+
|
174
|
+
it "should be able to promote to a Knight" do
|
175
|
+
@board.promote("A8", :knight)
|
176
|
+
expect(@board["A8"]).to be_a(Knight)
|
177
|
+
end
|
178
|
+
|
179
|
+
it "should be able to promote to a Bishop" do
|
180
|
+
@board.promote("A8", :bishop)
|
181
|
+
expect(@board["A8"]).to be_a(Bishop)
|
182
|
+
end
|
183
|
+
|
184
|
+
it "should be able to promote to a Rook" do
|
185
|
+
@board.promote("A8", :rook)
|
186
|
+
expect(@board["A8"]).to be_a(Rook)
|
187
|
+
end
|
188
|
+
|
189
|
+
it "should not be able to promote to a King" do
|
190
|
+
@board.promote("A8", :king)
|
191
|
+
expect(@board["A8"]).to_not be_a(King)
|
192
|
+
end
|
193
|
+
|
194
|
+
it "should know when a piece can be promoted" do
|
195
|
+
expect(@board.piece_to_promote?).to eq(@promote)
|
196
|
+
end
|
197
|
+
|
198
|
+
end
|
199
|
+
|
200
|
+
it "should be able to clear the board" do
|
201
|
+
@board.clear
|
202
|
+
pieces = @board.board.select{ |square, piece| piece.is_a?(Piece)}
|
203
|
+
expect(pieces.empty?).to be_truthy
|
204
|
+
end
|
205
|
+
|
206
|
+
end
|
data/spec/game_spec.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require_relative "../lib/chess/game.rb"
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Game do
|
5
|
+
|
6
|
+
before { @game = Game.new }
|
7
|
+
|
8
|
+
it "should be able to save and load the game" do
|
9
|
+
@game.save("game.dat")
|
10
|
+
@same_game = Game.load("game.dat")
|
11
|
+
expect(@game.board.to_a).to eq(@same_game.board.to_a)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should keep a record of moves made" do
|
15
|
+
@game.move("D2", "D4")
|
16
|
+
expect(@game.movelist.first == "#{Pawn::WHITE_PAWN_ICON} D2 D4")
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should allow moves of the player whose turn it is" do
|
20
|
+
expect(@game.move("D2", "D4")).to be_truthy
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should not allow moves of the wrong player" do
|
24
|
+
expect(@game.move("D7", "D5")).to be_falsey
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should swap the player and opponent" do
|
28
|
+
@game.move("D2", "D4")
|
29
|
+
expect(@game.turn).to eq(@game.black_player)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
data/spec/king_spec.rb
ADDED
@@ -0,0 +1,167 @@
|
|
1
|
+
require_relative '../lib/chess/king.rb'
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe King do
|
5
|
+
|
6
|
+
it { should be_a_kind_of(Piece) }
|
7
|
+
it { should respond_to :color }
|
8
|
+
it { should respond_to :location }
|
9
|
+
|
10
|
+
it "should know what its character is" do
|
11
|
+
@king = King.new("White", "A1")
|
12
|
+
expect(@king.icon).to eq("\u2654")
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "valid moves" do
|
16
|
+
before { @king = King.new("white", "D4") }
|
17
|
+
before { @board = Board.new }
|
18
|
+
|
19
|
+
it "should be able to move vertically one position" do
|
20
|
+
expect(@king.valid_move?("D3", @board)).to be_truthy
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should be able to move horizontally one position" do
|
24
|
+
expect(@king.valid_move?("C4", @board)).to be_truthy
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should be able to move diagonally one position" do
|
28
|
+
expect(@king.valid_move?("C3", @board)).to be_truthy
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should not be able to move more than one position" do
|
32
|
+
expect(@king.valid_move?("B2", @board)).to be_falsey
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "castling" do
|
36
|
+
|
37
|
+
before do
|
38
|
+
@board = Board.new
|
39
|
+
@king = @board["E1"]
|
40
|
+
@op_king = @board["E8"]
|
41
|
+
@board.rv_piece("F1")
|
42
|
+
@board.rv_piece("G1")
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should be valid to move twice horizontally on the first move\
|
46
|
+
when there is a rook on that side and no other pieces between" do
|
47
|
+
expect(@king.valid_move?("G1", @board))
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should move the king to the correct location" do
|
51
|
+
@board.move("E1", "G1")
|
52
|
+
expect(@board["G1"]).to be_a(King)
|
53
|
+
expect(@king.location).to eq("G1")
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should move the rook to the correct location" do
|
57
|
+
@board.move("E1", "G1")
|
58
|
+
expect(@board["F1"]).to be_a(Rook)
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should not be able to castle if it is in check" do
|
62
|
+
@board.move("E7", "E6")
|
63
|
+
@board.move("D2", "D3")
|
64
|
+
@board.move("F8", "B4")
|
65
|
+
# king is now in check
|
66
|
+
expect(@king.valid_move?("G1", @board)).to be_falsey
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should not be able to castle into check" do
|
70
|
+
@board.move("E7", "E6")
|
71
|
+
@board.move("D8", "G5")
|
72
|
+
@board.move("G5", "G2")
|
73
|
+
expect(@king.valid_move?("G1", @board)).to be_falsey
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should not be able to pass through a square being attacked" do
|
77
|
+
@board.move("E7", "E6")
|
78
|
+
@board.move("D8", "F6")
|
79
|
+
@board.move("F2", "F3")
|
80
|
+
@board.move("F6", "F3")
|
81
|
+
expect(@king.check?(@board)).to be_falsey
|
82
|
+
expect(@king.valid_move?("G1", @board)).to be_falsey
|
83
|
+
expect(@board["F1"]).to eq(" ")
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should be able to move through empty squares" do
|
87
|
+
@board.move("D2", "D4")
|
88
|
+
@board.move("D7", "D5")
|
89
|
+
@board.move("C1", "E3")
|
90
|
+
@board.move("C8", "E6")
|
91
|
+
@board.move("B1", "C3")
|
92
|
+
@board.move("B8", "C6")
|
93
|
+
@board.move("D1", "D3")
|
94
|
+
@board.move("D8", "D6")
|
95
|
+
expect(@king.valid_move?("C1", @board)).to be_truthy
|
96
|
+
expect(@op_king.valid_move?("C8", @board)).to be_truthy
|
97
|
+
expect(@op_king.valid_move?("C1", @board)).to be_falsey
|
98
|
+
expect(@king.will_be_in_check?(@king.location, "C1", @board)).to be_falsey
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe "in_check?" do
|
105
|
+
|
106
|
+
before do
|
107
|
+
@board = Board.new
|
108
|
+
@black_king = @board["E8"]
|
109
|
+
@white_king = @board["E1"]
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should know when it is in check" do
|
113
|
+
@board.move("C2", "C3")
|
114
|
+
@board.move("D7", "D6")
|
115
|
+
@board.move("D1", "A4")
|
116
|
+
expect(@black_king.check?(@board)).to be_truthy
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should know when it is not in check" do
|
120
|
+
expect(@black_king.check?(@board)).to be_falsey
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should know when it will be in check" do
|
124
|
+
@board.move("E2", "E3")
|
125
|
+
@board.move("F1", "B5")
|
126
|
+
expect(@black_king.will_be_in_check?("D7", "D6", @board)).to be_truthy
|
127
|
+
end
|
128
|
+
|
129
|
+
it "should not be in checkmate if the king is not in check" do
|
130
|
+
expect(@white_king.checkmate?(@board)).to be_falsey
|
131
|
+
end
|
132
|
+
|
133
|
+
it "should know when a check is a checkmate" do
|
134
|
+
@board.move("F2", "F3")
|
135
|
+
@board.move("E7", "E5")
|
136
|
+
@board.move("G2", "G4")
|
137
|
+
@board.move("D8", "H4")
|
138
|
+
expect(@white_king.checkmate?(@board)).to be_truthy
|
139
|
+
end
|
140
|
+
|
141
|
+
it "should know when a check is not a checkmate" do
|
142
|
+
@board.move("F2", "F3")
|
143
|
+
@board.move("E7", "E5")
|
144
|
+
@board.move("G1", "H3")
|
145
|
+
@board.move("D8", "H4")
|
146
|
+
expect(@white_king.checkmate?(@board)).to be_falsey
|
147
|
+
end
|
148
|
+
|
149
|
+
it "should know when a game is a stalemate" do
|
150
|
+
@board.clear
|
151
|
+
@white_king.location = "H1"
|
152
|
+
@white_pawn = Pawn.new("white", "H2")
|
153
|
+
@black_pawn = Pawn.new("black", "H3")
|
154
|
+
@black_rook = Rook.new("black", "G4")
|
155
|
+
@board.set_piece(@black_king)
|
156
|
+
@board.set_piece(@white_king)
|
157
|
+
@board.set_piece(@black_rook)
|
158
|
+
@board.set_piece(@white_pawn)
|
159
|
+
@board.set_piece(@black_pawn)
|
160
|
+
expect(@white_king.checkmate?(@board)).to be_falsey
|
161
|
+
expect(@white_king.stalemate?(@board)).to be_truthy
|
162
|
+
end
|
163
|
+
|
164
|
+
|
165
|
+
end
|
166
|
+
|
167
|
+
end
|
data/spec/knight_spec.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require_relative '../lib/chess/knight.rb'
|
2
|
+
require_relative '../lib/chess/board.rb'
|
3
|
+
|
4
|
+
describe Knight do
|
5
|
+
|
6
|
+
it { should be_a_kind_of(Piece) }
|
7
|
+
it { should respond_to :color }
|
8
|
+
it { should respond_to :location }
|
9
|
+
|
10
|
+
it "should know what its character is" do
|
11
|
+
@knight = Knight.new("White", "A1")
|
12
|
+
expect(@knight.icon).to eq("\u2658")
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "valid moves" do
|
16
|
+
before { @knight = Knight.new("white", "D5") }
|
17
|
+
before { @board = Board.new }
|
18
|
+
|
19
|
+
it "should be able to move in an 'L'" do
|
20
|
+
expect(@knight.valid_move?("F6", @board)).to be_truthy
|
21
|
+
expect(@knight.valid_move?("F4", @board)).to be_truthy
|
22
|
+
expect(@knight.valid_move?("E3", @board)).to be_truthy
|
23
|
+
expect(@knight.valid_move?("E7", @board)).to be_truthy
|
24
|
+
expect(@knight.valid_move?("C7", @board)).to be_truthy
|
25
|
+
expect(@knight.valid_move?("B6", @board)).to be_truthy
|
26
|
+
expect(@knight.valid_move?("B4", @board)).to be_truthy
|
27
|
+
expect(@knight.valid_move?("C3", @board)).to be_truthy
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should not be able to move horizontally to any position" do
|
31
|
+
expect(@knight.valid_move?("H4", @board)).to be_falsey
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should not be able to move diagonally" do
|
35
|
+
expect(@knight.valid_move?("B2", @board)).to be_falsey
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should be able to jump pieces" do
|
39
|
+
expect(@board.board["B1"].valid_move?("C3", @board)).to be_truthy
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should be able to block a checkmate" do
|
43
|
+
@board.clear
|
44
|
+
@board.set_piece(King.new("white", "E1"))
|
45
|
+
@board.set_piece(King.new("black", "E8"))
|
46
|
+
@board.set_piece(Queen.new("black", "E7"))
|
47
|
+
@board.set_piece(Knight.new("white", "G1"))
|
48
|
+
@board.set_piece(Pawn.new("white", "D1"))
|
49
|
+
@board.set_piece(Pawn.new("white", "F2"))
|
50
|
+
@board.set_piece(Pawn.new("white", "D2"))
|
51
|
+
@board.set_piece(Rook.new("white", "F1"))
|
52
|
+
@white_king = @board["E1"]
|
53
|
+
expect(@white_king.checkmate?(@board)).to be_falsey
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
data/spec/pawn_spec.rb
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
require_relative '../lib/chess/pawn.rb'
|
2
|
+
require 'stringio'
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Pawn do
|
6
|
+
|
7
|
+
it { should be_a_kind_of(Piece) }
|
8
|
+
it { should respond_to :color }
|
9
|
+
it { should respond_to :location }
|
10
|
+
|
11
|
+
it "should know what its character is" do
|
12
|
+
@pawn = Pawn.new("White", "A1")
|
13
|
+
expect(@pawn.icon).to eq("\u2659")
|
14
|
+
end
|
15
|
+
|
16
|
+
it "white should print the white pawn character" do
|
17
|
+
@pawn = Pawn.new("white")
|
18
|
+
$stdout = StringIO.new
|
19
|
+
print @pawn
|
20
|
+
expect($stdout.string).to include("\u2659")
|
21
|
+
end
|
22
|
+
|
23
|
+
it "black should print the white pawn character" do
|
24
|
+
@pawn = Pawn.new("black")
|
25
|
+
$stdout = StringIO.new
|
26
|
+
print @pawn
|
27
|
+
expect($stdout.string).to include("\u265F")
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "valid moves" do
|
31
|
+
before do
|
32
|
+
@board = Board.new
|
33
|
+
@white_pawn = @board.board["D2"]
|
34
|
+
@black_pawn = @board.board["D7"]
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should be able to move forward once ordinarility" do
|
38
|
+
expect(@white_pawn.valid_move?("D3", @board)).to be_truthy
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should be able to move forward once ordinarility" do
|
42
|
+
expect(@black_pawn.valid_move?("D6", @board)).to be_truthy
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should be able to move forward twice on the first move" do
|
46
|
+
expect(@white_pawn.valid_move?("D4", @board)).to be_truthy
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should not be able to move forward when the square is occupied" do
|
50
|
+
@opponent = Pawn.new("black", "D3")
|
51
|
+
@board.set_piece(@opponent)
|
52
|
+
expect(@white_pawn.valid_move?("D3", @board)).to be_falsey
|
53
|
+
expect(@white_pawn.valid_move?("D4", @board)).to be_falsey
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
it "should not be able to move forward three times" do
|
58
|
+
expect(@white_pawn.valid_move?("D5", @board)).to be_falsey
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should not be able to move left or right" do
|
62
|
+
expect(@white_pawn.valid_move?("C2", @board)).to be_falsey
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should not be able to move diagonally" do
|
66
|
+
expect(@white_pawn.valid_move?("C3", @board)).to be_falsey
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should not be able to move backwards" do
|
70
|
+
expect(@white_pawn.valid_move?("D1", @board)).to be_falsey
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
describe "taking other pieces" do
|
76
|
+
|
77
|
+
it "should be able to move diagonally to take other pieces" do
|
78
|
+
@board = Board.new
|
79
|
+
@white_pawn = @board.board["D2"]
|
80
|
+
@black_pawn = Pawn.new("black", "C3")
|
81
|
+
@board.set_piece(@black_pawn)
|
82
|
+
expect(@white_pawn.valid_move?("C3", @board)).to be_truthy
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should occupy the square it takes a piece from" do
|
86
|
+
@board = Board.new
|
87
|
+
@white_pawn = @board.board["D2"]
|
88
|
+
@black_pawn = Pawn.new("black", "C3")
|
89
|
+
@board.set_piece(@black_pawn)
|
90
|
+
@board.move("D2", "C3")
|
91
|
+
expect(@board["C3"]).to eq(@white_pawn)
|
92
|
+
end
|
93
|
+
|
94
|
+
describe "captureing pieces en passant" do
|
95
|
+
before do
|
96
|
+
@board = Board.new
|
97
|
+
@white_pawn = @board.board["D2"]
|
98
|
+
@black_pawn = @board.board["E7"]
|
99
|
+
@board.move("D2", "D4")
|
100
|
+
@board.move("D4", "D5")
|
101
|
+
@board.move("E7", "E5")
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should return the square of the passed piece" do
|
105
|
+
expect(@white_pawn.en_passant?("E6", @board)).to eq("E5")
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should remove the passed piece from the board" do
|
109
|
+
@board.move("D5", "E6")
|
110
|
+
expect(@board["E5"]).to eq(" ")
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should place the pawn in the moved-to location" do
|
114
|
+
@board.move("D5", "E6")
|
115
|
+
expect(@board["E6"]).to be_a(Pawn)
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should only be able to pass a just-moved piece" do
|
119
|
+
@board.move("A2", "A4")
|
120
|
+
@board.move("A7", "A5")
|
121
|
+
expect(@board.move("D5", "E6")).to be_falsey
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
describe "upgrading pawns" do
|
127
|
+
|
128
|
+
before do
|
129
|
+
@board = Board.new
|
130
|
+
@white_pawn = Pawn.new("white", "A8")
|
131
|
+
@white_rook = Rook.new("white", "H8")
|
132
|
+
@board.set_piece(@white_pawn)
|
133
|
+
end
|
134
|
+
|
135
|
+
it "should be able to promote when on last rank" do
|
136
|
+
expect(@white_pawn.can_promote?).to be_truthy
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should not be able to promote a non-pawn piece" do
|
140
|
+
expect(@white_rook.can_promote?).to be_falsey
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
144
|
+
end
|
data/spec/piece_spec.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require_relative '../lib/chess/piece.rb'
|
2
|
+
|
3
|
+
describe Piece do
|
4
|
+
|
5
|
+
it { should respond_to :color }
|
6
|
+
it { should respond_to :location }
|
7
|
+
it { should respond_to :move }
|
8
|
+
|
9
|
+
describe "validating moves" do
|
10
|
+
|
11
|
+
# see specific piece specs
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should know if another piece is an opponent" do
|
16
|
+
@black_piece = Piece.new("black", "A1")
|
17
|
+
@white_piece = Piece.new("white", "A2")
|
18
|
+
expect(@black_piece.opponent?(@white_piece)).to be_truthy
|
19
|
+
expect(@black_piece.opponent?(@black_piece)).to be_falsey
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should know who its fearless leader (king) is" do
|
23
|
+
@board = Board.new
|
24
|
+
@piece = @board["A1"]
|
25
|
+
expect(@piece.my_king(@board)).to be_a(King)
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "rank and file" do
|
29
|
+
before do
|
30
|
+
@board = Board.new
|
31
|
+
@piece = @board["A1"]
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should know what the piece's file is" do
|
35
|
+
expect(@piece.file).to eq("A")
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should know what the piece's rank is" do
|
39
|
+
expect(@piece.rank).to eq("1")
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
data/spec/player_spec.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require_relative "../lib/chess/player.rb"
|
2
|
+
require_relative "../lib/chess/board.rb"
|
3
|
+
|
4
|
+
describe Player do
|
5
|
+
|
6
|
+
it { should respond_to :color }
|
7
|
+
it { should respond_to :move }
|
8
|
+
|
9
|
+
describe "making moves" do
|
10
|
+
before do
|
11
|
+
@board = Board.new
|
12
|
+
@w_player = Player.new("white")
|
13
|
+
@b_player = Player.new("black")
|
14
|
+
end
|
15
|
+
it "should be able to move its own color" do
|
16
|
+
@w_player.move("D2", "D3", @board)
|
17
|
+
expect(@board["D3"]).to be_a(Pawn)
|
18
|
+
end
|
19
|
+
it "should not be able to move the other player's pieces" do
|
20
|
+
@w_player.move("D7", "D6", @board)
|
21
|
+
expect(@board["D6"]).to eq(" ")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should know which piece is their king" do
|
26
|
+
@board = Board.new
|
27
|
+
@w_player = Player.new("white")
|
28
|
+
@b_player = Player.new("black")
|
29
|
+
@w_king = @w_player.king(@board)
|
30
|
+
@b_king = @b_player.king(@board)
|
31
|
+
expect(@w_king).to be_a(King)
|
32
|
+
expect(@b_king).to be_a(King)
|
33
|
+
expect(@w_king.location).to eq("E1")
|
34
|
+
expect(@b_king.location).to eq("E8")
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should not let the player make a move that puts the king in check" do
|
38
|
+
@board = Board.new
|
39
|
+
@w_player = Player.new("white")
|
40
|
+
@b_player = Player.new("black")
|
41
|
+
@w_player.move("E2", "E3", @board)
|
42
|
+
@w_player.move("F1", "B5", @board)
|
43
|
+
expect(@b_player.move("D7", "D6", @board)).to_not eq("D6")
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
end
|