chessmate 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +6 -0
- data/Gemfile.lock +56 -0
- data/lib/chessmate.rb +16 -35
- data/lib/helpers/default.rb +28 -0
- data/lib/pieces/king.rb +2 -2
- data/spec/chessmate_spec.rb +120 -75
- metadata +32 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 595baa2afab688559adcd0dd14039cd28bffa2b8568e3e2458a1dcdebee6a51a
|
4
|
+
data.tar.gz: 6fd00a068a098dfda0883f351cd57c6d5630ccd6e5023aeeb01db1c54d3aedff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4d8672cceaadf0b52ef6237af0ea5d1f3b101b3dedd7636026a208bf0e8ba2496bb81aa8dc925f2d00339ebe396c32ac18bb7a73be6d7ee6a5b41349b8e4ad98
|
7
|
+
data.tar.gz: 9db677c4bb14ea3256948c6a2de4acb8aa2511cc04093ea79f31e47121e5f4c64a38785a49644ff94468f8efc5753e489c4780567ae29248596fc876dc329814
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
chessmate (0.4.0)
|
5
|
+
deep_dup
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
ast (2.4.0)
|
11
|
+
coderay (1.1.2)
|
12
|
+
deep_dup (0.0.3)
|
13
|
+
diff-lcs (1.3)
|
14
|
+
jaro_winkler (1.5.3)
|
15
|
+
method_source (0.9.2)
|
16
|
+
parallel (1.18.0)
|
17
|
+
parser (2.6.5.0)
|
18
|
+
ast (~> 2.4.0)
|
19
|
+
pry (0.12.2)
|
20
|
+
coderay (~> 1.1.0)
|
21
|
+
method_source (~> 0.9.0)
|
22
|
+
rainbow (3.0.0)
|
23
|
+
rspec (3.9.0)
|
24
|
+
rspec-core (~> 3.9.0)
|
25
|
+
rspec-expectations (~> 3.9.0)
|
26
|
+
rspec-mocks (~> 3.9.0)
|
27
|
+
rspec-core (3.9.0)
|
28
|
+
rspec-support (~> 3.9.0)
|
29
|
+
rspec-expectations (3.9.0)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.9.0)
|
32
|
+
rspec-mocks (3.9.0)
|
33
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
34
|
+
rspec-support (~> 3.9.0)
|
35
|
+
rspec-support (3.9.0)
|
36
|
+
rubocop (0.75.1)
|
37
|
+
jaro_winkler (~> 1.5.1)
|
38
|
+
parallel (~> 1.10)
|
39
|
+
parser (>= 2.6)
|
40
|
+
rainbow (>= 2.2.2, < 4.0)
|
41
|
+
ruby-progressbar (~> 1.7)
|
42
|
+
unicode-display_width (>= 1.4.0, < 1.7)
|
43
|
+
ruby-progressbar (1.10.1)
|
44
|
+
unicode-display_width (1.6.0)
|
45
|
+
|
46
|
+
PLATFORMS
|
47
|
+
ruby
|
48
|
+
|
49
|
+
DEPENDENCIES
|
50
|
+
chessmate!
|
51
|
+
pry
|
52
|
+
rspec
|
53
|
+
rubocop
|
54
|
+
|
55
|
+
BUNDLED WITH
|
56
|
+
2.0.2
|
data/lib/chessmate.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'pry'
|
4
|
+
|
3
5
|
class ChessMate
|
4
6
|
require 'helpers/notation_parser'
|
5
7
|
require 'pieces/pawn'
|
@@ -8,44 +10,23 @@ class ChessMate
|
|
8
10
|
require 'pieces/knight'
|
9
11
|
require 'pieces/queen'
|
10
12
|
require 'pieces/king'
|
13
|
+
require 'helpers/default'
|
14
|
+
require 'deep_dup'
|
11
15
|
|
12
16
|
attr_reader :board, :turn, :in_check, :promotable, :en_passant, :castling
|
13
17
|
|
14
|
-
def initialize(board
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
else
|
27
|
-
board
|
28
|
-
end
|
29
|
-
|
30
|
-
@turn = if turn.nil?
|
31
|
-
1
|
32
|
-
else
|
33
|
-
turn
|
34
|
-
end
|
35
|
-
|
36
|
-
@promotable = nil
|
37
|
-
@en_passant = { white: nil, black: nil }
|
38
|
-
@castling = {
|
39
|
-
white: {
|
40
|
-
kingside: true,
|
41
|
-
queenside: true
|
42
|
-
},
|
43
|
-
black: {
|
44
|
-
kingside: true,
|
45
|
-
queenside: true
|
46
|
-
}
|
47
|
-
}
|
48
|
-
@in_check = { "white": false, "black": false }
|
18
|
+
def initialize(board: nil,
|
19
|
+
turn: nil,
|
20
|
+
promotable: nil,
|
21
|
+
en_passant: nil,
|
22
|
+
castling: nil,
|
23
|
+
in_check: nil)
|
24
|
+
@board = board || DEFAULT[:board].map(&:dup)
|
25
|
+
@turn = turn || DEFAULT[:turn]
|
26
|
+
@promotable = promotable || DeepDup.deep_dup(DEFAULT[:promotable])
|
27
|
+
@en_passant = en_passant || DeepDup.deep_dup(DEFAULT[:en_passant])
|
28
|
+
@castling = castling || DeepDup.deep_dup(DEFAULT[:castling])
|
29
|
+
@in_check = in_check || DeepDup.deep_dup(DEFAULT[:in_check])
|
49
30
|
end
|
50
31
|
|
51
32
|
def update(orig, dest = nil)
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
DEFAULT = {
|
4
|
+
board: [
|
5
|
+
%w[BR BN BB BQ BK BB BN BR],
|
6
|
+
%w[BP BP BP BP BP BP BP BP],
|
7
|
+
[nil, nil, nil, nil, nil, nil, nil, nil],
|
8
|
+
[nil, nil, nil, nil, nil, nil, nil, nil],
|
9
|
+
[nil, nil, nil, nil, nil, nil, nil, nil],
|
10
|
+
[nil, nil, nil, nil, nil, nil, nil, nil],
|
11
|
+
%w[WP WP WP WP WP WP WP WP],
|
12
|
+
%w[WR WN WB WQ WK WB WN WR]
|
13
|
+
],
|
14
|
+
turn: 1,
|
15
|
+
promotable: nil,
|
16
|
+
en_passant: { white: nil, black: nil },
|
17
|
+
in_check: { "white": false, "black": false },
|
18
|
+
castling: {
|
19
|
+
white: {
|
20
|
+
kingside: true,
|
21
|
+
queenside: true
|
22
|
+
},
|
23
|
+
black: {
|
24
|
+
kingside: true,
|
25
|
+
queenside: true
|
26
|
+
}
|
27
|
+
}
|
28
|
+
}.freeze
|
data/lib/pieces/king.rb
CHANGED
@@ -30,7 +30,7 @@ class King < Piece
|
|
30
30
|
return false if castling[king_color][castle_direction] == false
|
31
31
|
|
32
32
|
test_board = board.map(&:dup)
|
33
|
-
return false if ChessMate.new(test_board).in_check?(test_board)[king_color]
|
33
|
+
return false if ChessMate.new(board: test_board).in_check?(test_board)[king_color]
|
34
34
|
|
35
35
|
test_range = castle_direction == :kingside ? [5, 6] : [1, 2, 3]
|
36
36
|
test_range.each do |x|
|
@@ -39,7 +39,7 @@ class King < Piece
|
|
39
39
|
test_board = board.map(&:dup)
|
40
40
|
test_board[orig_y][x] = 'WK'
|
41
41
|
test_board[orig_y][orig_x] = nil
|
42
|
-
test = ChessMate.new(test_board)
|
42
|
+
test = ChessMate.new(board: test_board)
|
43
43
|
return false if test.in_check?(test_board)[king_color] && x != 1
|
44
44
|
end
|
45
45
|
true
|
data/spec/chessmate_spec.rb
CHANGED
@@ -3,27 +3,72 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
require_relative '../lib/chessmate'
|
5
5
|
require_relative '../lib/helpers/notation_parser'
|
6
|
+
require_relative '../lib/helpers/default'
|
6
7
|
Dir['../lib/pieces/*.rb'].each { |file| require file }
|
7
8
|
|
8
9
|
describe ChessMate do
|
10
|
+
describe 'initialize method' do
|
11
|
+
context 'should accept custom parameters' do
|
12
|
+
it 'for board' do
|
13
|
+
board = Array.new(8) { Array.new(8, nil) }
|
14
|
+
chess = ChessMate.new(board: board)
|
15
|
+
expect(chess.board).to eql(board)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'for en_passant' do
|
19
|
+
en_passant = { white: true, black: nil }
|
20
|
+
chess = ChessMate.new(en_passant: en_passant)
|
21
|
+
expect(chess.en_passant).to eql(en_passant)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'for castling' do
|
25
|
+
castling = {
|
26
|
+
white: {
|
27
|
+
kingside: false,
|
28
|
+
queenside: true
|
29
|
+
},
|
30
|
+
black: {
|
31
|
+
kingside: true,
|
32
|
+
queenside: true
|
33
|
+
}
|
34
|
+
}
|
35
|
+
chess = ChessMate.new(castling: castling)
|
36
|
+
expect(chess.castling).to eql(castling)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'for turn' do
|
40
|
+
turn = rand(10)
|
41
|
+
chess = ChessMate.new(turn: turn)
|
42
|
+
expect(chess.turn).to eql(turn)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'for in_check' do
|
46
|
+
in_check = { "white": true, "black": false }
|
47
|
+
chess = ChessMate.new(in_check: in_check)
|
48
|
+
expect(chess.in_check).to eql(in_check)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'no custom parameters' do
|
53
|
+
it 'should generate a default game' do
|
54
|
+
chess = ChessMate.new
|
55
|
+
expect(chess.board).to eql(DEFAULT[:board])
|
56
|
+
expect(chess.turn).to eql(DEFAULT[:turn])
|
57
|
+
expect(chess.promotable).to eql(DEFAULT[:promotable])
|
58
|
+
expect(chess.en_passant).to eql(DEFAULT[:en_passant])
|
59
|
+
expect(chess.castling).to eql(DEFAULT[:castling])
|
60
|
+
expect(chess.in_check).to eql(DEFAULT[:in_check])
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
9
65
|
describe 'board method' do
|
10
66
|
before :each do
|
11
67
|
@chess = ChessMate.new
|
12
68
|
end
|
13
69
|
|
14
70
|
it 'should return the board state' do
|
15
|
-
expect(@chess.board).to eql(
|
16
|
-
[
|
17
|
-
%w[BR BN BB BQ BK BB BN BR],
|
18
|
-
%w[BP BP BP BP BP BP BP BP],
|
19
|
-
[nil, nil, nil, nil, nil, nil, nil, nil],
|
20
|
-
[nil, nil, nil, nil, nil, nil, nil, nil],
|
21
|
-
[nil, nil, nil, nil, nil, nil, nil, nil],
|
22
|
-
[nil, nil, nil, nil, nil, nil, nil, nil],
|
23
|
-
%w[WP WP WP WP WP WP WP WP],
|
24
|
-
%w[WR WN WB WQ WK WB WN WR]
|
25
|
-
]
|
26
|
-
)
|
71
|
+
expect(@chess.board).to eql(DEFAULT[:board])
|
27
72
|
end
|
28
73
|
end
|
29
74
|
|
@@ -81,7 +126,7 @@ describe ChessMate do
|
|
81
126
|
|
82
127
|
it 'should handle a malformed board by returning default value' do
|
83
128
|
board = Array.new(8) { Array.new(8, nil) }
|
84
|
-
chess = ChessMate.new(board)
|
129
|
+
chess = ChessMate.new(board: board)
|
85
130
|
expect(chess.in_check?).to eql(
|
86
131
|
"white": false,
|
87
132
|
"black": false
|
@@ -101,7 +146,7 @@ describe ChessMate do
|
|
101
146
|
board[7][4] = 'WK'
|
102
147
|
board[0][4] = 'BQ'
|
103
148
|
board[0][5] = 'BK'
|
104
|
-
chess = ChessMate.new(board)
|
149
|
+
chess = ChessMate.new(board: board)
|
105
150
|
expect(chess.in_check?).to eql(
|
106
151
|
"white": true,
|
107
152
|
"black": false
|
@@ -113,7 +158,7 @@ describe ChessMate do
|
|
113
158
|
board[7][4] = 'WK'
|
114
159
|
board[7][5] = 'WQ'
|
115
160
|
board[0][5] = 'BK'
|
116
|
-
chess = ChessMate.new(board)
|
161
|
+
chess = ChessMate.new(board: board)
|
117
162
|
expect(chess.in_check?).to eql(
|
118
163
|
"white": false,
|
119
164
|
"black": true
|
@@ -141,7 +186,7 @@ describe ChessMate do
|
|
141
186
|
board = Array.new(8) { Array.new(8, nil) }
|
142
187
|
board[0][0] = 'BQ'
|
143
188
|
board[7][0] = 'WK'
|
144
|
-
chess = ChessMate.new(board)
|
189
|
+
chess = ChessMate.new(board: board)
|
145
190
|
expect(chess.in_check?).to eql(
|
146
191
|
"white": true,
|
147
192
|
"black": false
|
@@ -160,7 +205,7 @@ describe ChessMate do
|
|
160
205
|
board[6][0] = 'WP'
|
161
206
|
board[0][4] = 'BK'
|
162
207
|
board[1][4] = 'BQ'
|
163
|
-
chess = ChessMate.new(board)
|
208
|
+
chess = ChessMate.new(board: board)
|
164
209
|
chess.move('a2', 'a3')
|
165
210
|
expect(chess.in_check?).to eql(
|
166
211
|
"white": true,
|
@@ -185,7 +230,7 @@ describe ChessMate do
|
|
185
230
|
board[7][4] = 'WK'
|
186
231
|
board[0][4] = 'BK'
|
187
232
|
board[0][3] = 'BQ'
|
188
|
-
chess = ChessMate.new(board)
|
233
|
+
chess = ChessMate.new(board: board)
|
189
234
|
chess.move('e1', 'd1')
|
190
235
|
expect(chess.board).to eql(
|
191
236
|
[
|
@@ -206,7 +251,7 @@ describe ChessMate do
|
|
206
251
|
board[7][3] = 'WK'
|
207
252
|
board[0][4] = 'BK'
|
208
253
|
board[0][3] = 'BQ'
|
209
|
-
chess = ChessMate.new(board)
|
254
|
+
chess = ChessMate.new(board: board)
|
210
255
|
chess.move('d1', 'e1')
|
211
256
|
expect(chess.board).to eql(
|
212
257
|
[
|
@@ -317,7 +362,7 @@ describe ChessMate do
|
|
317
362
|
board = Array.new(8) { Array.new(8, nil) }
|
318
363
|
board[6][0] = 'WP'
|
319
364
|
board[5][0] = 'BP'
|
320
|
-
chess = ChessMate.new(board)
|
365
|
+
chess = ChessMate.new(board: board)
|
321
366
|
chess.move('a2', 'a4')
|
322
367
|
expect(chess.board).to eql(
|
323
368
|
[
|
@@ -337,7 +382,7 @@ describe ChessMate do
|
|
337
382
|
board = Array.new(8) { Array.new(8, nil) }
|
338
383
|
board[5][5] = 'WP'
|
339
384
|
board[4][4] = 'BP'
|
340
|
-
chess = ChessMate.new(board)
|
385
|
+
chess = ChessMate.new(board: board)
|
341
386
|
chess.move('f3', 'e4')
|
342
387
|
expect(chess.board).to eql(
|
343
388
|
[
|
@@ -357,7 +402,7 @@ describe ChessMate do
|
|
357
402
|
board = Array.new(8) { Array.new(8, nil) }
|
358
403
|
board[4][4] = 'WP'
|
359
404
|
board[5][5] = 'BP'
|
360
|
-
chess = ChessMate.new(board)
|
405
|
+
chess = ChessMate.new(board: board)
|
361
406
|
chess.move('e4', 'f3')
|
362
407
|
expect(chess.board).to eql(
|
363
408
|
[
|
@@ -377,7 +422,7 @@ describe ChessMate do
|
|
377
422
|
board = Array.new(8) { Array.new(8, nil) }
|
378
423
|
board[4][4] = 'WP'
|
379
424
|
board[5][5] = 'WP'
|
380
|
-
chess = ChessMate.new(board)
|
425
|
+
chess = ChessMate.new(board: board)
|
381
426
|
chess.move('f3', 'e4')
|
382
427
|
expect(chess.board).to eql(
|
383
428
|
[
|
@@ -397,7 +442,7 @@ describe ChessMate do
|
|
397
442
|
board = Array.new(8) { Array.new(8, nil) }
|
398
443
|
board[4][4] = 'WP'
|
399
444
|
board[5][5] = 'BP'
|
400
|
-
chess = ChessMate.new(board)
|
445
|
+
chess = ChessMate.new(board: board)
|
401
446
|
chess.move('e4', 'e3')
|
402
447
|
expect(chess.board).to eql(
|
403
448
|
[
|
@@ -416,7 +461,7 @@ describe ChessMate do
|
|
416
461
|
it 'should flag a pawn as promotable if moving to the last rank' do
|
417
462
|
board = Array.new(8) { Array.new(8, nil) }
|
418
463
|
board[1][0] = 'WP'
|
419
|
-
chess = ChessMate.new(board)
|
464
|
+
chess = ChessMate.new(board: board)
|
420
465
|
chess.move('a7', 'a8')
|
421
466
|
expect(chess.board).to eql(
|
422
467
|
[
|
@@ -437,7 +482,7 @@ describe ChessMate do
|
|
437
482
|
board = Array.new(8) { Array.new(8, nil) }
|
438
483
|
board[1][0] = 'BP'
|
439
484
|
board[3][1] = 'WP'
|
440
|
-
chess = ChessMate.new(board)
|
485
|
+
chess = ChessMate.new(board: board)
|
441
486
|
chess.move('a7', 'a5')
|
442
487
|
chess.move('b5', 'a6')
|
443
488
|
expect(chess.board).to eql(
|
@@ -458,7 +503,7 @@ describe ChessMate do
|
|
458
503
|
board = Array.new(8) { Array.new(8, nil) }
|
459
504
|
board[4][0] = 'BP'
|
460
505
|
board[6][1] = 'WP'
|
461
|
-
chess = ChessMate.new(board)
|
506
|
+
chess = ChessMate.new(board: board)
|
462
507
|
chess.move('b2', 'b4')
|
463
508
|
chess.move('a4', 'b3')
|
464
509
|
expect(chess.board).to eql(
|
@@ -482,7 +527,7 @@ describe ChessMate do
|
|
482
527
|
board[3][1] = 'WP'
|
483
528
|
board[6][7] = 'WP'
|
484
529
|
board[1][7] = 'BP'
|
485
|
-
chess = ChessMate.new(board)
|
530
|
+
chess = ChessMate.new(board: board)
|
486
531
|
chess.move('a7', 'a5')
|
487
532
|
chess.move('h2', 'h3')
|
488
533
|
chess.move('h7', 'h6')
|
@@ -505,7 +550,7 @@ describe ChessMate do
|
|
505
550
|
it 'should update the board if rook moves vertically' do
|
506
551
|
board = Array.new(8) { Array.new(8, nil) }
|
507
552
|
board[7][0] = 'WR'
|
508
|
-
chess = ChessMate.new(board)
|
553
|
+
chess = ChessMate.new(board: board)
|
509
554
|
chess.move('a1', 'a8')
|
510
555
|
expect(chess.board).to eql(
|
511
556
|
[
|
@@ -524,7 +569,7 @@ describe ChessMate do
|
|
524
569
|
it 'should update the board if the rook moves horizontally' do
|
525
570
|
board = Array.new(8) { Array.new(8, nil) }
|
526
571
|
board[7][0] = 'WR'
|
527
|
-
chess = ChessMate.new(board)
|
572
|
+
chess = ChessMate.new(board: board)
|
528
573
|
chess.move('a1', 'h1')
|
529
574
|
expect(chess.board).to eql(
|
530
575
|
[
|
@@ -543,7 +588,7 @@ describe ChessMate do
|
|
543
588
|
it 'should return false for a move that is neither horizontal or vertical' do
|
544
589
|
board = Array.new(8) { Array.new(8, nil) }
|
545
590
|
board[7][0] = 'WR'
|
546
|
-
chess = ChessMate.new(board)
|
591
|
+
chess = ChessMate.new(board: board)
|
547
592
|
expect(chess.move('a1', 'g8')).to eql(false)
|
548
593
|
end
|
549
594
|
|
@@ -551,7 +596,7 @@ describe ChessMate do
|
|
551
596
|
board = Array.new(8) { Array.new(8, nil) }
|
552
597
|
board[7][0] = 'WR'
|
553
598
|
board[7][7] = 'BR'
|
554
|
-
chess = ChessMate.new(board)
|
599
|
+
chess = ChessMate.new(board: board)
|
555
600
|
chess.move('a1', 'h1')
|
556
601
|
expect(chess.board).to eql(
|
557
602
|
[
|
@@ -571,7 +616,7 @@ describe ChessMate do
|
|
571
616
|
board = Array.new(8) { Array.new(8, nil) }
|
572
617
|
board[7][0] = 'WR'
|
573
618
|
board[7][7] = 'WR'
|
574
|
-
chess = ChessMate.new(board)
|
619
|
+
chess = ChessMate.new(board: board)
|
575
620
|
chess.move('a1', 'h1')
|
576
621
|
expect(chess.board).to eql(
|
577
622
|
[
|
@@ -592,7 +637,7 @@ describe ChessMate do
|
|
592
637
|
it 'should update the board if the bishop moves diagonally' do
|
593
638
|
board = Array.new(8) { Array.new(8, nil) }
|
594
639
|
board[7][2] = 'WB'
|
595
|
-
chess = ChessMate.new(board)
|
640
|
+
chess = ChessMate.new(board: board)
|
596
641
|
chess.move('c1', 'h6')
|
597
642
|
expect(chess.board).to eql(
|
598
643
|
[
|
@@ -611,7 +656,7 @@ describe ChessMate do
|
|
611
656
|
it 'should return false for a move that is not diagonal' do
|
612
657
|
board = Array.new(8) { Array.new(8, nil) }
|
613
658
|
board[7][2] = 'WB'
|
614
|
-
chess = ChessMate.new(board)
|
659
|
+
chess = ChessMate.new(board: board)
|
615
660
|
expect(chess.move('c1', 'c8')).to eql(false)
|
616
661
|
expect(chess.move('c1', 'h7')).to eql(false)
|
617
662
|
expect(chess.move('c1', 'h8')).to eql(false)
|
@@ -621,7 +666,7 @@ describe ChessMate do
|
|
621
666
|
board = Array.new(8) { Array.new(8, nil) }
|
622
667
|
board[7][2] = 'WB'
|
623
668
|
board[2][7] = 'BB'
|
624
|
-
chess = ChessMate.new(board)
|
669
|
+
chess = ChessMate.new(board: board)
|
625
670
|
chess.move('c1', 'h6')
|
626
671
|
expect(chess.board).to eql(
|
627
672
|
[
|
@@ -641,7 +686,7 @@ describe ChessMate do
|
|
641
686
|
board = Array.new(8) { Array.new(8, nil) }
|
642
687
|
board[7][2] = 'WB'
|
643
688
|
board[2][7] = 'WB'
|
644
|
-
chess = ChessMate.new(board)
|
689
|
+
chess = ChessMate.new(board: board)
|
645
690
|
chess.move('c1', 'h6')
|
646
691
|
expect(chess.board).to eql(
|
647
692
|
[
|
@@ -662,7 +707,7 @@ describe ChessMate do
|
|
662
707
|
it 'should update the board if the knight moves 2 horizontal, 1 vertical' do
|
663
708
|
board = Array.new(8) { Array.new(8, nil) }
|
664
709
|
board[7][2] = 'WN'
|
665
|
-
chess = ChessMate.new(board)
|
710
|
+
chess = ChessMate.new(board: board)
|
666
711
|
chess.move('c1', 'e2')
|
667
712
|
expect(chess.board).to eql(
|
668
713
|
[
|
@@ -681,7 +726,7 @@ describe ChessMate do
|
|
681
726
|
it 'should update the board if the knight moves 1 horizontal, 2 vertical' do
|
682
727
|
board = Array.new(8) { Array.new(8, nil) }
|
683
728
|
board[7][2] = 'WN'
|
684
|
-
chess = ChessMate.new(board)
|
729
|
+
chess = ChessMate.new(board: board)
|
685
730
|
chess.move('c1', 'd3')
|
686
731
|
expect(chess.board).to eql(
|
687
732
|
[
|
@@ -700,7 +745,7 @@ describe ChessMate do
|
|
700
745
|
it 'should return false if the move is invalid' do
|
701
746
|
board = Array.new(8) { Array.new(8, nil) }
|
702
747
|
board[7][2] = 'WN'
|
703
|
-
chess = ChessMate.new(board)
|
748
|
+
chess = ChessMate.new(board: board)
|
704
749
|
expect(chess.move('c1', 'c8')).to eql(false)
|
705
750
|
end
|
706
751
|
|
@@ -708,7 +753,7 @@ describe ChessMate do
|
|
708
753
|
board = Array.new(8) { Array.new(8, nil) }
|
709
754
|
board[7][2] = 'WN'
|
710
755
|
board[5][3] = 'BN'
|
711
|
-
chess = ChessMate.new(board)
|
756
|
+
chess = ChessMate.new(board: board)
|
712
757
|
chess.move('c1', 'd3')
|
713
758
|
expect(chess.board).to eql(
|
714
759
|
[
|
@@ -728,7 +773,7 @@ describe ChessMate do
|
|
728
773
|
board = Array.new(8) { Array.new(8, nil) }
|
729
774
|
board[7][2] = 'WN'
|
730
775
|
board[5][3] = 'WN'
|
731
|
-
chess = ChessMate.new(board)
|
776
|
+
chess = ChessMate.new(board: board)
|
732
777
|
chess.move('c1', 'd3')
|
733
778
|
expect(chess.board).to eql(
|
734
779
|
[
|
@@ -749,7 +794,7 @@ describe ChessMate do
|
|
749
794
|
it 'should update the board if the queen moves vertically' do
|
750
795
|
board = Array.new(8) { Array.new(8, nil) }
|
751
796
|
board[7][3] = 'WQ'
|
752
|
-
chess = ChessMate.new(board)
|
797
|
+
chess = ChessMate.new(board: board)
|
753
798
|
chess.move('d1', 'd8')
|
754
799
|
expect(chess.board).to eql(
|
755
800
|
[
|
@@ -768,7 +813,7 @@ describe ChessMate do
|
|
768
813
|
it 'should update the board if the queen moves horizontally' do
|
769
814
|
board = Array.new(8) { Array.new(8, nil) }
|
770
815
|
board[7][3] = 'WQ'
|
771
|
-
chess = ChessMate.new(board)
|
816
|
+
chess = ChessMate.new(board: board)
|
772
817
|
chess.move('d1', 'h1')
|
773
818
|
expect(chess.board).to eql(
|
774
819
|
[
|
@@ -787,7 +832,7 @@ describe ChessMate do
|
|
787
832
|
it 'should update the board if the queen moves diagonally' do
|
788
833
|
board = Array.new(8) { Array.new(8, nil) }
|
789
834
|
board[7][3] = 'WQ'
|
790
|
-
chess = ChessMate.new(board)
|
835
|
+
chess = ChessMate.new(board: board)
|
791
836
|
chess.move('d1', 'h5')
|
792
837
|
expect(chess.board).to eql(
|
793
838
|
[
|
@@ -806,7 +851,7 @@ describe ChessMate do
|
|
806
851
|
it 'should return false if the queen makes an otherwise invalid move' do
|
807
852
|
board = Array.new(8) { Array.new(8, nil) }
|
808
853
|
board[7][3] = 'WQ'
|
809
|
-
chess = ChessMate.new(board)
|
854
|
+
chess = ChessMate.new(board: board)
|
810
855
|
expect(chess.move('d1', 'h8')).to eql(false)
|
811
856
|
end
|
812
857
|
|
@@ -814,7 +859,7 @@ describe ChessMate do
|
|
814
859
|
board = Array.new(8) { Array.new(8, nil) }
|
815
860
|
board[7][3] = 'WQ'
|
816
861
|
board[7][7] = 'BQ'
|
817
|
-
chess = ChessMate.new(board)
|
862
|
+
chess = ChessMate.new(board: board)
|
818
863
|
chess.move('d1', 'h1')
|
819
864
|
expect(chess.board).to eql(
|
820
865
|
[
|
@@ -834,7 +879,7 @@ describe ChessMate do
|
|
834
879
|
board = Array.new(8) { Array.new(8, nil) }
|
835
880
|
board[7][3] = 'WQ'
|
836
881
|
board[7][7] = 'WQ'
|
837
|
-
chess = ChessMate.new(board)
|
882
|
+
chess = ChessMate.new(board: board)
|
838
883
|
chess.move('d1', 'h1')
|
839
884
|
expect(chess.board).to eql(
|
840
885
|
[
|
@@ -855,7 +900,7 @@ describe ChessMate do
|
|
855
900
|
it 'should update the board if the king moves 1 space vertically' do
|
856
901
|
board = Array.new(8) { Array.new(8, nil) }
|
857
902
|
board[7][4] = 'WK'
|
858
|
-
chess = ChessMate.new(board)
|
903
|
+
chess = ChessMate.new(board: board)
|
859
904
|
chess.move('e1', 'e2')
|
860
905
|
expect(chess.board).to eql(
|
861
906
|
[
|
@@ -874,7 +919,7 @@ describe ChessMate do
|
|
874
919
|
it 'should update the board if the king moves 1 space horizontally' do
|
875
920
|
board = Array.new(8) { Array.new(8, nil) }
|
876
921
|
board[7][4] = 'WK'
|
877
|
-
chess = ChessMate.new(board)
|
922
|
+
chess = ChessMate.new(board: board)
|
878
923
|
chess.move('e1', 'f1')
|
879
924
|
expect(chess.board).to eql(
|
880
925
|
[
|
@@ -893,7 +938,7 @@ describe ChessMate do
|
|
893
938
|
it 'should update the board if the king moves 1 space diagonally' do
|
894
939
|
board = Array.new(8) { Array.new(8, nil) }
|
895
940
|
board[7][4] = 'WK'
|
896
|
-
chess = ChessMate.new(board)
|
941
|
+
chess = ChessMate.new(board: board)
|
897
942
|
chess.move('e1', 'f2')
|
898
943
|
expect(chess.board).to eql(
|
899
944
|
[
|
@@ -912,7 +957,7 @@ describe ChessMate do
|
|
912
957
|
it 'should return false if the king makes an otherwise invalid move' do
|
913
958
|
board = Array.new(8) { Array.new(8, nil) }
|
914
959
|
board[7][4] = 'WK'
|
915
|
-
chess = ChessMate.new(board)
|
960
|
+
chess = ChessMate.new(board: board)
|
916
961
|
expect(chess.move('e1', 'h8')).to eql(false)
|
917
962
|
end
|
918
963
|
|
@@ -920,7 +965,7 @@ describe ChessMate do
|
|
920
965
|
board = Array.new(8) { Array.new(8, nil) }
|
921
966
|
board[7][4] = 'WK'
|
922
967
|
board[6][4] = 'BP'
|
923
|
-
chess = ChessMate.new(board)
|
968
|
+
chess = ChessMate.new(board: board)
|
924
969
|
chess.move('e1', 'e2')
|
925
970
|
expect(chess.board).to eql(
|
926
971
|
[
|
@@ -940,7 +985,7 @@ describe ChessMate do
|
|
940
985
|
board = Array.new(8) { Array.new(8, nil) }
|
941
986
|
board[7][4] = 'WK'
|
942
987
|
board[6][4] = 'WP'
|
943
|
-
chess = ChessMate.new(board)
|
988
|
+
chess = ChessMate.new(board: board)
|
944
989
|
chess.move('e1', 'e2')
|
945
990
|
expect(chess.board).to eql(
|
946
991
|
[
|
@@ -960,7 +1005,7 @@ describe ChessMate do
|
|
960
1005
|
board = Array.new(8) { Array.new(8, nil) }
|
961
1006
|
board[7][4] = 'WK'
|
962
1007
|
board[7][7] = 'WR'
|
963
|
-
chess = ChessMate.new(board)
|
1008
|
+
chess = ChessMate.new(board: board)
|
964
1009
|
chess.move('e1', 'g1')
|
965
1010
|
expect(chess.board).to eql(
|
966
1011
|
[
|
@@ -980,7 +1025,7 @@ describe ChessMate do
|
|
980
1025
|
board = Array.new(8) { Array.new(8, nil) }
|
981
1026
|
board[7][4] = 'WK'
|
982
1027
|
board[7][0] = 'WR'
|
983
|
-
chess = ChessMate.new(board)
|
1028
|
+
chess = ChessMate.new(board: board)
|
984
1029
|
chess.move('e1', 'c1')
|
985
1030
|
expect(chess.board).to eql(
|
986
1031
|
[
|
@@ -1008,7 +1053,7 @@ describe ChessMate do
|
|
1008
1053
|
board[7][4] = 'WK'
|
1009
1054
|
board[7][7] = 'WR'
|
1010
1055
|
board[0][6] = 'BQ'
|
1011
|
-
chess = ChessMate.new(board)
|
1056
|
+
chess = ChessMate.new(board: board)
|
1012
1057
|
chess.move('e1', 'g1')
|
1013
1058
|
expect(chess.board).to eql(
|
1014
1059
|
[
|
@@ -1029,7 +1074,7 @@ describe ChessMate do
|
|
1029
1074
|
board[7][4] = 'WK'
|
1030
1075
|
board[7][7] = 'WR'
|
1031
1076
|
board[0][5] = 'BQ'
|
1032
|
-
chess = ChessMate.new(board)
|
1077
|
+
chess = ChessMate.new(board: board)
|
1033
1078
|
chess.move('e1', 'g1')
|
1034
1079
|
expect(chess.board).to eql(
|
1035
1080
|
[
|
@@ -1050,7 +1095,7 @@ describe ChessMate do
|
|
1050
1095
|
board[7][4] = 'WK'
|
1051
1096
|
board[7][7] = 'WR'
|
1052
1097
|
board[0][4] = 'BQ'
|
1053
|
-
chess = ChessMate.new(board)
|
1098
|
+
chess = ChessMate.new(board: board)
|
1054
1099
|
chess.move('e1', 'g1')
|
1055
1100
|
expect(chess.board).to eql(
|
1056
1101
|
[
|
@@ -1070,7 +1115,7 @@ describe ChessMate do
|
|
1070
1115
|
board = Array.new(8) { Array.new(8, nil) }
|
1071
1116
|
board[7][3] = 'WK'
|
1072
1117
|
board[7][7] = 'WR'
|
1073
|
-
chess = ChessMate.new(board)
|
1118
|
+
chess = ChessMate.new(board: board)
|
1074
1119
|
chess.move('d1', 'e1')
|
1075
1120
|
chess.move('e1', 'g1')
|
1076
1121
|
expect(chess.board).to eql(
|
@@ -1091,7 +1136,7 @@ describe ChessMate do
|
|
1091
1136
|
board = Array.new(8) { Array.new(8, nil) }
|
1092
1137
|
board[7][4] = 'WK'
|
1093
1138
|
board[7][7] = 'WR'
|
1094
|
-
chess = ChessMate.new(board)
|
1139
|
+
chess = ChessMate.new(board: board)
|
1095
1140
|
chess.move('h1', 'g1')
|
1096
1141
|
chess.move('g1', 'h1')
|
1097
1142
|
chess.move('e1', 'g1')
|
@@ -1117,7 +1162,7 @@ describe ChessMate do
|
|
1117
1162
|
@board[7][4] = 'WK'
|
1118
1163
|
@board[0][4] = 'BK'
|
1119
1164
|
@board[0][3] = 'BQ'
|
1120
|
-
@chess = ChessMate.new(@board)
|
1165
|
+
@chess = ChessMate.new(board: @board)
|
1121
1166
|
end
|
1122
1167
|
it 'should return true if the moving color is in check after the move' do
|
1123
1168
|
expect(@chess.in_check_after_move?([7, 4], [7, 3])).to eql(true)
|
@@ -1143,7 +1188,7 @@ describe ChessMate do
|
|
1143
1188
|
board[6][4] = 'BQ'
|
1144
1189
|
board[3][4] = 'BK'
|
1145
1190
|
board[4][4] = 'BR'
|
1146
|
-
chess = ChessMate.new(board)
|
1191
|
+
chess = ChessMate.new(board: board)
|
1147
1192
|
expect(chess.in_check_after_move?([7, 4], [6, 4])).to eql(true)
|
1148
1193
|
end
|
1149
1194
|
end
|
@@ -1153,7 +1198,7 @@ describe ChessMate do
|
|
1153
1198
|
board = Array.new(8) { Array.new(8, nil) }
|
1154
1199
|
board[7][4] = 'WK'
|
1155
1200
|
board[0][4] = 'BK'
|
1156
|
-
chess = ChessMate.new(board)
|
1201
|
+
chess = ChessMate.new(board: board)
|
1157
1202
|
expect(chess.any_valid_moves?('W')).to eql(true)
|
1158
1203
|
end
|
1159
1204
|
|
@@ -1163,7 +1208,7 @@ describe ChessMate do
|
|
1163
1208
|
board[6][4] = 'BQ'
|
1164
1209
|
board[3][4] = 'BK'
|
1165
1210
|
board[4][4] = 'BR'
|
1166
|
-
chess = ChessMate.new(board)
|
1211
|
+
chess = ChessMate.new(board: board)
|
1167
1212
|
expect(chess.any_valid_moves?('W')).to eql(false)
|
1168
1213
|
end
|
1169
1214
|
end
|
@@ -1175,7 +1220,7 @@ describe ChessMate do
|
|
1175
1220
|
board[6][4] = 'BQ'
|
1176
1221
|
board[3][4] = 'BK'
|
1177
1222
|
board[4][4] = 'BR'
|
1178
|
-
chess = ChessMate.new(board)
|
1223
|
+
chess = ChessMate.new(board: board)
|
1179
1224
|
expect(chess.checkmate?('W')).to eql(true)
|
1180
1225
|
end
|
1181
1226
|
|
@@ -1192,7 +1237,7 @@ describe ChessMate do
|
|
1192
1237
|
board[5][3] = 'BQ'
|
1193
1238
|
board[5][4] = 'BK'
|
1194
1239
|
board[4][5] = 'BR'
|
1195
|
-
chess = ChessMate.new(board)
|
1240
|
+
chess = ChessMate.new(board: board)
|
1196
1241
|
expect(chess.draw?('W')).to eql(true)
|
1197
1242
|
end
|
1198
1243
|
|
@@ -1207,7 +1252,7 @@ describe ChessMate do
|
|
1207
1252
|
board[6][3] = 'BQ'
|
1208
1253
|
board[5][4] = 'BK'
|
1209
1254
|
board[4][5] = 'BR'
|
1210
|
-
chess = ChessMate.new(board)
|
1255
|
+
chess = ChessMate.new(board: board)
|
1211
1256
|
expect(chess.draw?('W')).to eql(false)
|
1212
1257
|
end
|
1213
1258
|
end
|
@@ -1216,14 +1261,14 @@ describe ChessMate do
|
|
1216
1261
|
it 'should return true if white piece is on last rank' do
|
1217
1262
|
board = Array.new(8) { Array.new(8, nil) }
|
1218
1263
|
board[0][0] = 'WP'
|
1219
|
-
chess = ChessMate.new(board)
|
1264
|
+
chess = ChessMate.new(board: board)
|
1220
1265
|
expect(chess.promote?([0, 0])).to eql(true)
|
1221
1266
|
end
|
1222
1267
|
|
1223
1268
|
it 'should return true if black piece is on last rank' do
|
1224
1269
|
board = Array.new(8) { Array.new(8, nil) }
|
1225
1270
|
board[7][0] = 'BP'
|
1226
|
-
chess = ChessMate.new(board)
|
1271
|
+
chess = ChessMate.new(board: board)
|
1227
1272
|
expect(chess.promote?([7, 0])).to eql(true)
|
1228
1273
|
end
|
1229
1274
|
|
@@ -1231,7 +1276,7 @@ describe ChessMate do
|
|
1231
1276
|
board = Array.new(8) { Array.new(8, nil) }
|
1232
1277
|
board[1][0] = 'WP'
|
1233
1278
|
board[6][0] = 'BP'
|
1234
|
-
chess = ChessMate.new(board)
|
1279
|
+
chess = ChessMate.new(board: board)
|
1235
1280
|
expect(chess.promote?([1, 0])).to eql(false)
|
1236
1281
|
expect(chess.promote?([6, 0])).to eql(false)
|
1237
1282
|
end
|
@@ -1241,14 +1286,14 @@ describe ChessMate do
|
|
1241
1286
|
it 'should return nil if piece cannot promote' do
|
1242
1287
|
board = Array.new(8) { Array.new(8, nil) }
|
1243
1288
|
board[1][0] = 'WP'
|
1244
|
-
chess = ChessMate.new(board)
|
1289
|
+
chess = ChessMate.new(board: board)
|
1245
1290
|
expect(chess.promote!([1, 0], 'queen')).to eql(nil)
|
1246
1291
|
end
|
1247
1292
|
|
1248
1293
|
it 'should return nil for invalid/junk promotion data' do
|
1249
1294
|
board = Array.new(8) { Array.new(8, nil) }
|
1250
1295
|
board[0][0] = 'WP'
|
1251
|
-
chess = ChessMate.new(board)
|
1296
|
+
chess = ChessMate.new(board: board)
|
1252
1297
|
expect(chess.promote!([0, 0], 'king')).to eql(nil)
|
1253
1298
|
end
|
1254
1299
|
|
@@ -1256,7 +1301,7 @@ describe ChessMate do
|
|
1256
1301
|
before :each do
|
1257
1302
|
board = Array.new(8) { Array.new(8, nil) }
|
1258
1303
|
board[0][0] = 'WP'
|
1259
|
-
@chess = ChessMate.new(board)
|
1304
|
+
@chess = ChessMate.new(board: board)
|
1260
1305
|
end
|
1261
1306
|
|
1262
1307
|
it 'queen' do
|
@@ -1320,7 +1365,7 @@ describe ChessMate do
|
|
1320
1365
|
board[0][4] = 'BK'
|
1321
1366
|
board[0][0] = 'BR'
|
1322
1367
|
board[0][7] = 'BR'
|
1323
|
-
@chess = ChessMate.new(board)
|
1368
|
+
@chess = ChessMate.new(board: board)
|
1324
1369
|
end
|
1325
1370
|
context 'new game' do
|
1326
1371
|
it 'should return true for both king/queenside for both colors' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chessmate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tyler Porter
|
@@ -10,6 +10,20 @@ bindir: bin
|
|
10
10
|
cert_chain: []
|
11
11
|
date: 2019-10-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: pry
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: rspec
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,13 +52,30 @@ dependencies:
|
|
38
52
|
- - ">="
|
39
53
|
- !ruby/object:Gem::Version
|
40
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: deep_dup
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
41
69
|
description: A simple chess move validator
|
42
70
|
email: tyler.b.porter@gmail.com
|
43
71
|
executables: []
|
44
72
|
extensions: []
|
45
73
|
extra_rdoc_files: []
|
46
74
|
files:
|
75
|
+
- Gemfile
|
76
|
+
- Gemfile.lock
|
47
77
|
- lib/chessmate.rb
|
78
|
+
- lib/helpers/default.rb
|
48
79
|
- lib/helpers/notation_parser.rb
|
49
80
|
- lib/pieces/bishop.rb
|
50
81
|
- lib/pieces/king.rb
|