chessmate 0.3.0 → 0.4.0

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.
@@ -1,24 +1,22 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NotationParser
2
- def self.parse_notation(square)
3
- col = square[0].downcase().ord - 97
4
- row = 7 - ( square[1].to_i - 1 )
4
+ def self.parse_notation(square)
5
+ col = square[0].downcase.ord - 97
6
+ row = 7 - (square[1].to_i - 1)
7
+
8
+ return nil unless col >= 0 && col < 8 && row >= 0 && row < 8
9
+
10
+ [row, col]
11
+ end
5
12
 
6
- if col >= 0 && col < 8 && row >= 0 && row < 8
7
- [row,col]
8
- else
9
- return nil
10
- end
11
- end
13
+ def self.encode_notation(coords)
14
+ row, col = coords
12
15
 
13
- def self.encode_notation(coords)
14
- row, col = coords
16
+ return nil unless col >= 0 && col < 8 && row >= 0 && row < 8
15
17
 
16
- if col >= 0 && col < 8 && row >= 0 && row < 8
17
- col = (col + 97).chr
18
- row = (8 - row).to_s
19
- return col + row
20
- else
21
- return nil
22
- end
23
- end
24
- end
18
+ col = (col + 97).chr
19
+ row = (8 - row).to_s
20
+ col + row
21
+ end
22
+ end
@@ -1,12 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'pieces/piece'
2
4
 
3
5
  class Bishop < Piece
4
- def self.move_is_valid?(orig, dest, board)
5
- not_obstructed = !self.is_obstructed?(orig, dest, board)
6
+ def self.move_is_valid?(orig, dest, board)
7
+ not_obstructed = !obstructed?(orig, dest, board)
6
8
 
7
- not_obstructed &&
8
- ( !self.destination_occupied?(dest, board) || self.is_capturable?(orig, dest, board) ) &&
9
- (orig[0] - dest[0]).abs == (orig[1] - dest[1]).abs
10
- end
11
-
12
- end
9
+ not_obstructed &&
10
+ (!destination_occupied?(dest, board) || capturable?(orig, dest, board)) &&
11
+ (orig[0] - dest[0]).abs == (orig[1] - dest[1]).abs
12
+ end
13
+ end
@@ -1,9 +1,47 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'pieces/piece'
4
+ require 'chessmate'
2
5
 
3
6
  class King < Piece
4
- def self.move_is_valid?(orig, dest, board)
5
- (orig[0] - dest[0]).abs <= 1 && (orig[1] - dest[1]).abs <= 1 &&
6
- ( !self.destination_occupied?(dest, board) || self.is_capturable?(orig, dest, board) )
7
- end
8
-
9
- end
7
+ def self.move_is_valid?(orig, dest, board, castling)
8
+ if (orig[1] - dest[1]).abs == 2
9
+ valid_castling_move = valid_castling_move?(orig, dest, board, castling)
10
+ end
11
+
12
+ valid_castling_move || (
13
+ (orig[0] - dest[0]).abs <= 1 && (orig[1] - dest[1]).abs <= 1 &&
14
+ (!destination_occupied?(dest, board) || capturable?(orig, dest, board))
15
+ )
16
+ end
17
+
18
+ def self.valid_castling_move?(orig, dest, board, castling)
19
+ orig_y = orig[0]
20
+ orig_x = orig[1]
21
+ dest_y = dest[0]
22
+ dest_x = dest[1]
23
+
24
+ return false if orig_y != dest_y || (orig_x - dest_x).abs != 2
25
+
26
+ king = board[orig_y][orig_x]
27
+ king_color = king[0] == 'W' ? :white : :black
28
+ castle_direction = orig_x < dest_x ? :kingside : :queenside
29
+
30
+ return false if castling[king_color][castle_direction] == false
31
+
32
+ test_board = board.map(&:dup)
33
+ return false if ChessMate.new(test_board).in_check?(test_board)[king_color]
34
+
35
+ test_range = castle_direction == :kingside ? [5, 6] : [1, 2, 3]
36
+ test_range.each do |x|
37
+ return false if board[orig_y][x]
38
+
39
+ test_board = board.map(&:dup)
40
+ test_board[orig_y][x] = 'WK'
41
+ test_board[orig_y][orig_x] = nil
42
+ test = ChessMate.new(test_board)
43
+ return false if test.in_check?(test_board)[king_color] && x != 1
44
+ end
45
+ true
46
+ end
47
+ end
@@ -1,12 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'pieces/piece'
2
4
 
3
5
  class Knight < Piece
4
- def self.move_is_valid?(orig, dest, board)
5
- x_offset = (orig[0] - dest[0]).abs
6
- y_offset = (orig[1] - dest[1]).abs
6
+ def self.move_is_valid?(orig, dest, board)
7
+ x_offset = (orig[0] - dest[0]).abs
8
+ y_offset = (orig[1] - dest[1]).abs
7
9
 
8
- ( !self.destination_occupied?(dest, board) || self.is_capturable?(orig, dest, board) ) &&
9
- (x_offset == 2 && y_offset == 1) || (x_offset == 1 && y_offset == 2)
10
- end
11
-
12
- end
10
+ (!destination_occupied?(dest, board) || capturable?(orig, dest, board)) &&
11
+ (x_offset == 2 && y_offset == 1) || (x_offset == 1 && y_offset == 2)
12
+ end
13
+ end
@@ -1,34 +1,57 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'pieces/piece'
2
4
 
3
5
  class Pawn < Piece
4
- def self.move_is_valid?(orig, dest, board)
5
- orig_y = orig[0]
6
- orig_x = orig[1]
7
- dest_y = dest[0]
8
- dest_x = dest[1]
9
- piece_type = board[orig_y][orig_x]
10
- piece_color = piece_type[0].downcase()
11
-
12
- if piece_color == "w"
13
- direction = 1
14
- elsif piece_color == "b"
15
- direction = -1
16
- else
17
- return false
18
- end
19
-
20
- if self.is_capturable?(orig,dest,board) &&
21
- (orig_x - dest_x).abs == 1 &&
22
- (orig_y - dest_y) * direction == 1
23
- return true
24
- end
25
-
26
- not_obstructed = !self.is_obstructed?(orig, dest, board)
27
-
28
- basic_move = ( (orig_y - dest_y) * direction == 1 && orig_x == dest_x )
29
- move_double_on_first_turn = ( orig_y - dest_y == (2 * direction) ) && ( orig_x == dest_x )
30
-
31
- move_double_on_first_turn && not_obstructed || basic_move
32
- end
33
-
34
- end
6
+ def self.move_is_valid?(orig, dest, board, en_passant)
7
+ return true if en_passant(orig, dest, board, en_passant)
8
+
9
+ orig_y = orig[0]
10
+ orig_x = orig[1]
11
+ dest_y = dest[0]
12
+ dest_x = dest[1]
13
+ piece_type = board[orig_y][orig_x]
14
+ piece_color = piece_type[0].downcase
15
+
16
+ if piece_color == 'w'
17
+ direction = 1
18
+ elsif piece_color == 'b'
19
+ direction = -1
20
+ else
21
+ return false
22
+ end
23
+
24
+ if capturable?(orig, dest, board) &&
25
+ (orig_x - dest_x).abs == 1 &&
26
+ (orig_y - dest_y) * direction == 1
27
+ return true
28
+ end
29
+
30
+ not_obstructed = !obstructed?(orig, dest, board)
31
+
32
+ basic_move = ((orig_y - dest_y) * direction == 1 && orig_x == dest_x)
33
+ move_double_on_first_turn = (orig_y - dest_y == (2 * direction)) && (orig_x == dest_x)
34
+
35
+ move_double_on_first_turn && not_obstructed || basic_move
36
+ end
37
+
38
+ def self.en_passant(orig, dest, board, en_passant)
39
+ orig_y = orig[0]
40
+ orig_x = orig[1]
41
+ dest_y = dest[0]
42
+ dest_x = dest[1]
43
+ piece_type = board[orig_y][orig_x]
44
+ opposite_color = piece_type[0].downcase == 'w' ? :black : :white
45
+ direction = opposite_color == :white ? -1 : 1
46
+
47
+ return false if en_passant[opposite_color].nil?
48
+
49
+ if en_passant[opposite_color] == [dest[0] + direction, dest[1]]
50
+ if (orig_x - dest_x).abs == 1 &&
51
+ (orig_y - dest_y) * direction == 1
52
+ return true
53
+ end
54
+ end
55
+ false
56
+ end
57
+ end
@@ -1,76 +1,69 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Piece
2
- def self.is_obstructed?(orig, dest, board)
3
- orig_y = orig[0]
4
- orig_x = orig[1]
5
- dest_y = dest[0]
6
- dest_x = dest[1]
7
-
8
- if orig_y == dest_y
9
-
10
- direction = orig_x > dest_x ? 1 : -1
11
- ( (orig_x - dest_x).abs - 1 ).times do |x|
12
- test_pos = orig_x - (x * direction) - direction
13
- if !board[orig_y][test_pos].nil?
14
- return true
15
- end
16
- end
17
- return false
18
-
19
- elsif orig_x == dest_x
20
-
21
- direction = orig_y > dest_y ? 1 : -1
22
- ( (orig_y - dest_y).abs - 1 ).times do |y|
23
- test_pos = orig_y - (y * direction) - direction
24
- if !board[test_pos][orig_x].nil?
25
- return true
26
- end
27
- end
28
-
29
- return false
30
-
31
- elsif (orig_y - dest_y).abs == (orig_x - dest_x).abs
32
-
33
- x_direction = orig_x > dest_x ? 1 : -1
34
- y_direction = orig_y > dest_y ? 1 : -1
35
-
36
- ( (orig_y - dest_y).abs - 1 ).times do |v|
37
- test_y_pos = orig_y - (v * y_direction) - y_direction
38
- test_x_pos = orig_x - (v * x_direction) - x_direction
39
- if !board[test_y_pos][test_x_pos].nil?
40
- return true
41
- end
42
- end
43
-
44
- return false
45
-
46
- else
47
- return nil
48
- end
49
- end
50
-
51
- def self.is_capturable?(orig, dest, board)
52
- orig_y = orig[0]
53
- orig_x = orig[1]
54
- dest_y = dest[0]
55
- dest_x = dest[1]
56
- orig_piece = board[orig_y][orig_x]
57
- dest_piece = board[dest_y][dest_x]
58
-
59
- if orig_piece && dest_piece
60
- orig_piece_color = orig_piece[0]
61
- dest_piece_color = dest_piece[0]
62
- else
63
- return false
64
- end
65
-
66
- orig_piece_color != dest_piece_color
67
-
68
- end
69
-
70
- def self.destination_occupied?(dest, board)
71
- dest_y = dest[0]
72
- dest_x = dest[1]
73
-
74
- !board[dest_y][dest_x].nil?
75
- end
76
- end
4
+ def self.obstructed?(orig, dest, board)
5
+ orig_y = orig[0]
6
+ orig_x = orig[1]
7
+ dest_y = dest[0]
8
+ dest_x = dest[1]
9
+
10
+ if orig_y == dest_y
11
+
12
+ direction = orig_x > dest_x ? 1 : -1
13
+ ((orig_x - dest_x).abs - 1).times do |x|
14
+ test_pos = orig_x - (x * direction) - direction
15
+ return true unless board[orig_y][test_pos].nil?
16
+ end
17
+ return false
18
+
19
+ elsif orig_x == dest_x
20
+
21
+ direction = orig_y > dest_y ? 1 : -1
22
+ ((orig_y - dest_y).abs - 1).times do |y|
23
+ test_pos = orig_y - (y * direction) - direction
24
+ return true unless board[test_pos][orig_x].nil?
25
+ end
26
+
27
+ return false
28
+
29
+ elsif (orig_y - dest_y).abs == (orig_x - dest_x).abs
30
+
31
+ x_direction = orig_x > dest_x ? 1 : -1
32
+ y_direction = orig_y > dest_y ? 1 : -1
33
+
34
+ ((orig_y - dest_y).abs - 1).times do |v|
35
+ test_y_pos = orig_y - (v * y_direction) - y_direction
36
+ test_x_pos = orig_x - (v * x_direction) - x_direction
37
+ return true unless board[test_y_pos][test_x_pos].nil?
38
+ end
39
+
40
+ return false
41
+
42
+ else
43
+ return nil
44
+ end
45
+ end
46
+
47
+ def self.capturable?(orig, dest, board)
48
+ orig_y = orig[0]
49
+ orig_x = orig[1]
50
+ dest_y = dest[0]
51
+ dest_x = dest[1]
52
+ orig_piece = board[orig_y][orig_x]
53
+ dest_piece = board[dest_y][dest_x]
54
+
55
+ return false unless orig_piece && dest_piece
56
+
57
+ orig_piece_color = orig_piece[0]
58
+ dest_piece_color = dest_piece[0]
59
+
60
+ orig_piece_color != dest_piece_color
61
+ end
62
+
63
+ def self.destination_occupied?(dest, board)
64
+ dest_y = dest[0]
65
+ dest_x = dest[1]
66
+
67
+ !board[dest_y][dest_x].nil?
68
+ end
69
+ end
@@ -1,17 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'pieces/piece'
2
4
 
3
5
  class Queen < Piece
4
- def self.move_is_valid?(orig, dest, board)
5
-
6
- not_obstructed = !self.is_obstructed?(orig, dest, board)
6
+ def self.move_is_valid?(orig, dest, board)
7
+ not_obstructed = !obstructed?(orig, dest, board)
7
8
 
8
- not_obstructed &&
9
- ( !self.destination_occupied?(dest, board) || self.is_capturable?(orig, dest, board) ) &&
10
- (
11
- (orig[0] - dest[0]).abs == (orig[1] - dest[1]).abs ||
12
- orig[0] == dest[0] || orig[1] == dest[1]
13
- )
14
-
15
- end
16
-
17
- end
9
+ not_obstructed &&
10
+ (!destination_occupied?(dest, board) || capturable?(orig, dest, board)) &&
11
+ (
12
+ (orig[0] - dest[0]).abs == (orig[1] - dest[1]).abs ||
13
+ orig[0] == dest[0] || orig[1] == dest[1]
14
+ )
15
+ end
16
+ end
@@ -1,16 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'pieces/piece'
2
4
 
3
5
  class Rook < Piece
4
- def self.move_is_valid?(orig, dest, board)
5
-
6
- not_obstructed = !self.is_obstructed?(orig, dest, board)
6
+ def self.move_is_valid?(orig, dest, board)
7
+ not_obstructed = !obstructed?(orig, dest, board)
7
8
 
8
- not_obstructed &&
9
- ( !self.destination_occupied?(dest, board) || self.is_capturable?(orig, dest, board) ) &&
10
- (
11
- orig[0] == dest[0] || orig[1] == dest[1]
12
- )
13
-
14
- end
15
-
16
- end
9
+ not_obstructed &&
10
+ (!destination_occupied?(dest, board) || capturable?(orig, dest, board)) &&
11
+ (
12
+ orig[0] == dest[0] || orig[1] == dest[1]
13
+ )
14
+ end
15
+ end
@@ -1,975 +1,1358 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require_relative '../lib/chessmate'
3
5
  require_relative '../lib/helpers/notation_parser'
4
- Dir["../lib/pieces/*.rb"].each {|file| require file}
6
+ Dir['../lib/pieces/*.rb'].each { |file| require file }
5
7
 
6
8
  describe ChessMate do
9
+ describe 'board method' do
10
+ before :each do
11
+ @chess = ChessMate.new
12
+ end
7
13
 
8
- describe "board method" do
9
- before :each do
10
- @chess = ChessMate.new
11
- end
14
+ 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
+ )
27
+ end
28
+ end
12
29
 
13
- it "should return the board state" do
14
- expect(@chess.board).to eql(
15
- [
16
- ['BR', 'BN', 'BB', 'BQ', 'BK', 'BB', 'BN', 'BR'],
17
- ['BP', 'BP', 'BP', 'BP', 'BP', 'BP', 'BP', 'BP'],
18
- [nil, nil, nil, nil, nil, nil, nil, nil],
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
- ['WP', 'WP', 'WP', 'WP', 'WP', 'WP', 'WP', 'WP'],
23
- ['WR', 'WN', 'WB', 'WQ', 'WK', 'WB', 'WN', 'WR']
24
- ]
25
- )
26
- end
30
+ describe 'update method' do
31
+ before :each do
32
+ @chess = ChessMate.new
27
33
  end
28
34
 
29
- describe "update method" do
30
- before :each do
31
- @chess = ChessMate.new
32
- end
33
-
34
- it "should update the board after moving" do
35
- @chess.update([6,0], [5,0])
36
- expect(@chess.board).to eql(
37
- [
38
- ['BR', 'BN', 'BB', 'BQ', 'BK', 'BB', 'BN', 'BR'],
39
- ['BP', 'BP', 'BP', 'BP', 'BP', 'BP', 'BP', 'BP'],
40
- [nil, nil, nil, nil, nil, nil, nil, nil],
41
- [nil, nil, nil, nil, nil, nil, nil, nil],
42
- [nil, nil, nil, nil, nil, nil, nil, nil],
43
- ['WP', nil, nil, nil, nil, nil, nil, nil],
44
- [nil, 'WP', 'WP', 'WP', 'WP', 'WP', 'WP', 'WP'],
45
- ['WR', 'WN', 'WB', 'WQ', 'WK', 'WB', 'WN', 'WR']
46
- ]
47
- )
48
- end
35
+ it 'should update the board after moving' do
36
+ @chess.update([6, 0], [5, 0])
37
+ expect(@chess.board).to eql(
38
+ [
39
+ %w[BR BN BB BQ BK BB BN BR],
40
+ %w[BP BP BP BP BP BP BP BP],
41
+ [nil, nil, nil, nil, nil, nil, nil, nil],
42
+ [nil, nil, nil, nil, nil, nil, nil, nil],
43
+ [nil, nil, nil, nil, nil, nil, nil, nil],
44
+ ['WP', nil, nil, nil, nil, nil, nil, nil],
45
+ [nil, 'WP', 'WP', 'WP', 'WP', 'WP', 'WP', 'WP'],
46
+ %w[WR WN WB WQ WK WB WN WR]
47
+ ]
48
+ )
49
+ end
49
50
 
50
- it "should update the turn on a successful update" do
51
- turn = @chess.turn
52
- @chess.update([6,0], [5,0])
53
- expect(@chess.turn).to eql(turn + 1)
54
- end
51
+ it 'should update the turn on a successful update' do
52
+ turn = @chess.turn
53
+ @chess.update([6, 0], [5, 0])
54
+ expect(@chess.turn).to eql(turn + 1)
55
55
  end
56
+ end
56
57
 
57
- describe "turn method" do
58
- it "should show the current turn" do
59
- chess = ChessMate.new
60
- expect(chess.turn).to eql(1)
61
- end
58
+ describe 'turn method' do
59
+ it 'should show the current turn' do
60
+ chess = ChessMate.new
61
+ expect(chess.turn).to eql(1)
62
62
  end
63
+ end
63
64
 
64
- describe "in_check method" do
65
- it "should return a hash of values values on game start" do
66
- chess = ChessMate.new
67
- expect(chess.in_check).to eql(
68
- {
69
- "white": false,
70
- "black": false
71
- }
72
- )
73
- end
65
+ describe 'in_check method' do
66
+ it 'should return a hash of values values on game start' do
67
+ chess = ChessMate.new
68
+ expect(chess.in_check).to eql(
69
+ "white": false,
70
+ "black": false
71
+ )
74
72
  end
73
+ end
75
74
 
76
- describe "in_check? method" do
77
- it "should return a hash value" do
78
- chess = ChessMate.new
79
- check = chess.in_check?
80
- expect(check.is_a? Hash).to eql(true)
81
- end
75
+ describe 'in_check? method' do
76
+ it 'should return a hash value' do
77
+ chess = ChessMate.new
78
+ check = chess.in_check?
79
+ expect(check.is_a?(Hash)).to eql(true)
80
+ end
82
81
 
83
- it "should handle a malformed board by returning default value" do
84
- board = Array.new(8) { Array.new(8,nil) }
85
- chess = ChessMate.new(board)
86
- expect(chess.in_check?).to eql(
87
- {
88
- "white": false,
89
- "black": false
90
- }
91
- )
92
- end
82
+ it 'should handle a malformed board by returning default value' do
83
+ board = Array.new(8) { Array.new(8, nil) }
84
+ chess = ChessMate.new(board)
85
+ expect(chess.in_check?).to eql(
86
+ "white": false,
87
+ "black": false
88
+ )
89
+ end
93
90
 
94
- it "should should return false if neither king is in check" do
95
- chess = ChessMate.new
96
- expect(chess.in_check?).to eql(
97
- {
98
- "white": false,
99
- "black": false
100
- }
101
- )
102
- end
91
+ it 'should should return false if neither king is in check' do
92
+ chess = ChessMate.new
93
+ expect(chess.in_check?).to eql(
94
+ "white": false,
95
+ "black": false
96
+ )
97
+ end
103
98
 
104
- it "should return true if the white king is in check" do
105
- board = Array.new(8) { Array.new(8,nil) }
106
- board[7][4] = "WK"
107
- board[0][4] = "BQ"
108
- board[0][5] = "BK"
109
- chess = ChessMate.new(board)
110
- expect(chess.in_check?).to eql(
111
- {
112
- "white": true,
113
- "black": false
114
- }
115
- )
116
- end
99
+ it 'should return true if the white king is in check' do
100
+ board = Array.new(8) { Array.new(8, nil) }
101
+ board[7][4] = 'WK'
102
+ board[0][4] = 'BQ'
103
+ board[0][5] = 'BK'
104
+ chess = ChessMate.new(board)
105
+ expect(chess.in_check?).to eql(
106
+ "white": true,
107
+ "black": false
108
+ )
109
+ end
117
110
 
118
- it "should return true if the black king is in check" do
119
- board = Array.new(8) { Array.new(8,nil) }
120
- board[7][4] = "WK"
121
- board[7][5] = "WQ"
122
- board[0][5] = "BK"
123
- chess = ChessMate.new(board)
124
- expect(chess.in_check?).to eql(
125
- {
126
- "white": false,
127
- "black": true
128
- }
129
- )
130
- end
111
+ it 'should return true if the black king is in check' do
112
+ board = Array.new(8) { Array.new(8, nil) }
113
+ board[7][4] = 'WK'
114
+ board[7][5] = 'WQ'
115
+ board[0][5] = 'BK'
116
+ chess = ChessMate.new(board)
117
+ expect(chess.in_check?).to eql(
118
+ "white": false,
119
+ "black": true
120
+ )
121
+ end
131
122
 
132
- it "should not update the board to test for king in check" do
133
- chess = ChessMate.new
134
- chess.in_check?
135
- expect(chess.board).to eql(
136
- [
137
- ['BR', 'BN', 'BB', 'BQ', 'BK', 'BB', 'BN', 'BR'],
138
- ['BP', 'BP', 'BP', 'BP', 'BP', 'BP', 'BP', 'BP'],
139
- [nil, nil, nil, nil, nil, nil, nil, nil],
140
- [nil, nil, nil, nil, nil, nil, nil, nil],
141
- [nil, nil, nil, nil, nil, nil, nil, nil],
142
- [nil, nil, nil, nil, nil, nil, nil, nil],
143
- ['WP', 'WP', 'WP', 'WP', 'WP', 'WP', 'WP', 'WP'],
144
- ['WR', 'WN', 'WB', 'WQ', 'WK', 'WB', 'WN', 'WR']
145
- ]
146
- )
147
- end
123
+ it 'should not update the board to test for king in check' do
124
+ chess = ChessMate.new
125
+ chess.in_check?
126
+ expect(chess.board).to eql(
127
+ [
128
+ %w[BR BN BB BQ BK BB BN BR],
129
+ %w[BP BP BP BP BP BP BP BP],
130
+ [nil, nil, nil, nil, nil, nil, nil, nil],
131
+ [nil, nil, nil, nil, nil, nil, nil, nil],
132
+ [nil, nil, nil, nil, nil, nil, nil, nil],
133
+ [nil, nil, nil, nil, nil, nil, nil, nil],
134
+ %w[WP WP WP WP WP WP WP WP],
135
+ %w[WR WN WB WQ WK WB WN WR]
136
+ ]
137
+ )
148
138
  end
149
139
 
150
- describe "move method" do
151
- before :each do
152
- @chess = ChessMate.new
153
- end
140
+ it 'should handle new boards being passed in' do
141
+ board = Array.new(8) { Array.new(8, nil) }
142
+ board[0][0] = 'BQ'
143
+ board[7][0] = 'WK'
144
+ chess = ChessMate.new(board)
145
+ expect(chess.in_check?).to eql(
146
+ "white": true,
147
+ "black": false
148
+ )
149
+ end
150
+ end
154
151
 
155
- it "should test that the king is not in check before moving" do
156
- board = Array.new(8) { Array.new(8,nil) }
157
- board[7][4] = "WK"
158
- board[6][0] = "WP"
159
- board[0][4] = "BK"
160
- board[1][4] = "BQ"
161
- chess = ChessMate.new(board)
162
- chess.move('a2', 'a3')
163
- expect(chess.in_check?).to eql(
164
- {
165
- "white": true,
166
- "black": false
167
- }
168
- )
169
- expect(chess.board).to eql(
170
- [
171
- [nil, nil, nil, nil, "BK", nil, nil, nil],
172
- [nil, nil, nil, nil, "BQ", nil, nil, nil],
173
- [nil, nil, nil, nil, nil, nil, nil, nil],
174
- [nil, nil, nil, nil, nil, nil, nil, nil],
175
- [nil, nil, nil, nil, nil, nil, nil, nil],
176
- [nil, nil, nil, nil, nil, nil, nil, nil],
177
- ["WP", nil, nil, nil, nil, nil, nil, nil],
178
- [nil, nil, nil, nil, "WK", nil, nil, nil],
179
- ]
180
- )
181
- end
152
+ describe 'move method' do
153
+ before :each do
154
+ @chess = ChessMate.new
155
+ end
182
156
 
183
- it "should not let players move into check" do
184
- board = Array.new(8) { Array.new(8,nil) }
185
- board[7][4] = "WK"
186
- board[0][4] = "BK"
187
- board[0][3] = "BQ"
188
- chess = ChessMate.new(board)
189
- chess.move('e1', 'd1')
190
- expect(chess.board).to eql(
191
- [
192
- [nil, nil, nil, "BQ", "BK", nil, nil, nil],
193
- [nil, nil, nil, nil, nil, nil, nil, nil],
194
- [nil, nil, nil, nil, nil, nil, nil, nil],
195
- [nil, nil, nil, nil, nil, nil, nil, nil],
196
- [nil, nil, nil, nil, nil, nil, nil, nil],
197
- [nil, nil, nil, nil, nil, nil, nil, nil],
198
- [nil, nil, nil, nil, nil, nil, nil, nil],
199
- [nil, nil, nil, nil, "WK", nil, nil, nil],
200
- ]
201
- )
202
- end
157
+ it 'should test that the king is not in check before moving' do
158
+ board = Array.new(8) { Array.new(8, nil) }
159
+ board[7][4] = 'WK'
160
+ board[6][0] = 'WP'
161
+ board[0][4] = 'BK'
162
+ board[1][4] = 'BQ'
163
+ chess = ChessMate.new(board)
164
+ chess.move('a2', 'a3')
165
+ expect(chess.in_check?).to eql(
166
+ "white": true,
167
+ "black": false
168
+ )
169
+ expect(chess.board).to eql(
170
+ [
171
+ [nil, nil, nil, nil, 'BK', nil, nil, nil],
172
+ [nil, nil, nil, nil, 'BQ', nil, nil, nil],
173
+ [nil, nil, nil, nil, nil, nil, nil, nil],
174
+ [nil, nil, nil, nil, nil, nil, nil, nil],
175
+ [nil, nil, nil, nil, nil, nil, nil, nil],
176
+ [nil, nil, nil, nil, nil, nil, nil, nil],
177
+ ['WP', nil, nil, nil, nil, nil, nil, nil],
178
+ [nil, nil, nil, nil, 'WK', nil, nil, nil]
179
+ ]
180
+ )
181
+ end
203
182
 
204
- it "should let players move out of check" do
205
- board = Array.new(8) { Array.new(8,nil) }
206
- board[7][3] = "WK"
207
- board[0][4] = "BK"
208
- board[0][3] = "BQ"
209
- chess = ChessMate.new(board)
210
- chess.move('d1', 'e1')
211
- expect(chess.board).to eql(
212
- [
213
- [nil, nil, nil, "BQ", "BK", nil, nil, nil],
214
- [nil, nil, nil, nil, nil, nil, nil, nil],
215
- [nil, nil, nil, nil, nil, nil, nil, nil],
216
- [nil, nil, nil, nil, nil, nil, nil, nil],
217
- [nil, nil, nil, nil, nil, nil, nil, nil],
218
- [nil, nil, nil, nil, nil, nil, nil, nil],
219
- [nil, nil, nil, nil, nil, nil, nil, nil],
220
- [nil, nil, nil, nil, "WK", nil, nil, nil],
221
- ]
222
- )
223
- end
183
+ it 'should not let players move into check' do
184
+ board = Array.new(8) { Array.new(8, nil) }
185
+ board[7][4] = 'WK'
186
+ board[0][4] = 'BK'
187
+ board[0][3] = 'BQ'
188
+ chess = ChessMate.new(board)
189
+ chess.move('e1', 'd1')
190
+ expect(chess.board).to eql(
191
+ [
192
+ [nil, nil, nil, 'BQ', 'BK', nil, nil, nil],
193
+ [nil, nil, nil, nil, nil, nil, nil, nil],
194
+ [nil, nil, nil, nil, nil, nil, nil, nil],
195
+ [nil, nil, nil, nil, nil, nil, nil, nil],
196
+ [nil, nil, nil, nil, nil, nil, nil, nil],
197
+ [nil, nil, nil, nil, nil, nil, nil, nil],
198
+ [nil, nil, nil, nil, nil, nil, nil, nil],
199
+ [nil, nil, nil, nil, 'WK', nil, nil, nil]
200
+ ]
201
+ )
202
+ end
224
203
 
225
- it "should return false if the move is not within the bounds of the board" do
226
- expect(@chess.move('z2', 'c3')).to eql(false)
227
- expect(@chess.board).to eql(
228
- [
229
- ['BR', 'BN', 'BB', 'BQ', 'BK', 'BB', 'BN', 'BR'],
230
- ['BP', 'BP', 'BP', 'BP', 'BP', 'BP', 'BP', 'BP'],
231
- [nil, nil, nil, nil, nil, nil, nil, nil],
232
- [nil, nil, nil, nil, nil, nil, nil, nil],
233
- [nil, nil, nil, nil, nil, nil, nil, nil],
234
- [nil, nil, nil, nil, nil, nil, nil, nil],
235
- ['WP', 'WP', 'WP', 'WP', 'WP', 'WP', 'WP', 'WP'],
236
- ['WR', 'WN', 'WB', 'WQ', 'WK', 'WB', 'WN', 'WR']
237
- ]
238
- )
239
- end
204
+ it 'should let players move out of check' do
205
+ board = Array.new(8) { Array.new(8, nil) }
206
+ board[7][3] = 'WK'
207
+ board[0][4] = 'BK'
208
+ board[0][3] = 'BQ'
209
+ chess = ChessMate.new(board)
210
+ chess.move('d1', 'e1')
211
+ expect(chess.board).to eql(
212
+ [
213
+ [nil, nil, nil, 'BQ', 'BK', nil, nil, nil],
214
+ [nil, nil, nil, nil, nil, nil, nil, nil],
215
+ [nil, nil, nil, nil, nil, nil, nil, nil],
216
+ [nil, nil, nil, nil, nil, nil, nil, nil],
217
+ [nil, nil, nil, nil, nil, nil, nil, nil],
218
+ [nil, nil, nil, nil, nil, nil, nil, nil],
219
+ [nil, nil, nil, nil, nil, nil, nil, nil],
220
+ [nil, nil, nil, nil, 'WK', nil, nil, nil]
221
+ ]
222
+ )
223
+ end
240
224
 
241
- it "should return false if the origin or destination are malformed" do
242
- expect(@chess.move('22', 'c3')).to eql(false)
243
- expect(@chess.move('zz', 'c3')).to eql(false)
244
- end
225
+ it 'should return false if the move is not within the bounds of the board' do
226
+ expect(@chess.move('z2', 'c3')).to eql(false)
227
+ expect(@chess.board).to eql(
228
+ [
229
+ %w[BR BN BB BQ BK BB BN BR],
230
+ %w[BP BP BP BP BP BP BP BP],
231
+ [nil, nil, nil, nil, nil, nil, nil, nil],
232
+ [nil, nil, nil, nil, nil, nil, nil, nil],
233
+ [nil, nil, nil, nil, nil, nil, nil, nil],
234
+ [nil, nil, nil, nil, nil, nil, nil, nil],
235
+ %w[WP WP WP WP WP WP WP WP],
236
+ %w[WR WN WB WQ WK WB WN WR]
237
+ ]
238
+ )
239
+ end
245
240
 
246
- it "should generally return false and not update the board if the path is blocked" do
247
- expect(@chess.move('d1', 'a1')).to eql(false)
248
- expect(@chess.move('d1', 'd8')).to eql(false)
249
- expect(@chess.move('d1', 'b3')).to eql(false)
250
- expect(@chess.board).to eql(
251
- [
252
- ['BR', 'BN', 'BB', 'BQ', 'BK', 'BB', 'BN', 'BR'],
253
- ['BP', 'BP', 'BP', 'BP', 'BP', 'BP', 'BP', 'BP'],
254
- [nil, nil, nil, nil, nil, nil, nil, nil],
255
- [nil, nil, nil, nil, nil, nil, nil, nil],
256
- [nil, nil, nil, nil, nil, nil, nil, nil],
257
- [nil, nil, nil, nil, nil, nil, nil, nil],
258
- ['WP', 'WP', 'WP', 'WP', 'WP', 'WP', 'WP', 'WP'],
259
- ['WR', 'WN', 'WB', 'WQ', 'WK', 'WB', 'WN', 'WR']
260
- ]
261
- )
262
- end
241
+ it 'should return false if the origin or destination are malformed' do
242
+ expect(@chess.move('22', 'c3')).to eql(false)
243
+ expect(@chess.move('zz', 'c3')).to eql(false)
244
+ end
263
245
 
264
- context "for pawns" do
265
- it "should update the board if pawn move is valid" do
266
- @chess.move('c2', 'c3')
267
- expect(@chess.board).to eql(
268
- [
269
- ['BR', 'BN', 'BB', 'BQ', 'BK', 'BB', 'BN', 'BR'],
270
- ['BP', 'BP', 'BP', 'BP', 'BP', 'BP', 'BP', 'BP'],
271
- [nil, nil, nil, nil, nil, nil, nil, nil],
272
- [nil, nil, nil, nil, nil, nil, nil, nil],
273
- [nil, nil, nil, nil, nil, nil, nil, nil],
274
- [nil, nil, 'WP', nil, nil, nil, nil, nil],
275
- ['WP', 'WP', nil, 'WP', 'WP', 'WP', 'WP', 'WP'],
276
- ['WR', 'WN', 'WB', 'WQ', 'WK', 'WB', 'WN', 'WR']
277
- ]
278
- )
279
- end
280
-
281
- it "should return false if the pawn move is invalid" do
282
- expect(@chess.move('c2', 'd3')).to eql(false)
283
- end
284
-
285
- it "should update the board if pawn's first move is 2 squares" do
286
- chess = ChessMate.new
287
- chess.move('c2', 'c4')
288
- expect(chess.board).to eql(
289
- [
290
- ['BR', 'BN', 'BB', 'BQ', 'BK', 'BB', 'BN', 'BR'],
291
- ['BP', 'BP', 'BP', 'BP', 'BP', 'BP', 'BP', 'BP'],
292
- [nil, nil, nil, nil, nil, nil, nil, nil],
293
- [nil, nil, nil, nil, nil, nil, nil, nil],
294
- [nil, nil, "WP", nil, nil, nil, nil, nil],
295
- [nil, nil, nil, nil, nil, nil, nil, nil],
296
- ['WP', 'WP', nil, 'WP', 'WP', 'WP', 'WP', 'WP'],
297
- ['WR', 'WN', 'WB', 'WQ', 'WK', 'WB', 'WN', 'WR']
298
- ]
299
- )
300
- chess = ChessMate.new
301
- chess.move('b7', 'b5')
302
- expect(chess.board).to eql(
303
- [
304
- ['BR', 'BN', 'BB', 'BQ', 'BK', 'BB', 'BN', 'BR'],
305
- ['BP', nil, 'BP', 'BP', 'BP', 'BP', 'BP', 'BP'],
306
- [nil, nil, nil, nil, nil, nil, nil, nil],
307
- [nil, 'BP', nil, nil, nil, nil, nil, nil],
308
- [nil, nil, nil, nil, nil, nil, nil, nil],
309
- [nil, nil, nil, nil, nil, nil, nil, nil],
310
- ['WP', 'WP', 'WP', 'WP', 'WP', 'WP', 'WP', 'WP'],
311
- ['WR', 'WN', 'WB', 'WQ', 'WK', 'WB', 'WN', 'WR']
312
- ]
313
- )
314
- end
315
-
316
- it "should not allow a pawn to move 2 spaces if the 1st space is occupied" do
317
- board = Array.new(8) { Array.new(8,nil) }
318
- board[6][0] = "WP"
319
- board[5][0] = "BP"
320
- chess = ChessMate.new(board)
321
- chess.move('a2', 'a4')
322
- expect(chess.board).to eql(
323
- [
324
- [nil, nil, nil, nil, nil, nil, nil, nil],
325
- [nil, nil, nil, nil, nil, nil, nil, nil],
326
- [nil, nil, nil, nil, nil, nil, nil, nil],
327
- [nil, nil, nil, nil, nil, nil, nil, nil],
328
- [nil, nil, nil, nil, nil, nil, nil, nil],
329
- ['BP', nil, nil, nil, nil, nil, nil, nil],
330
- ['WP', nil, nil, nil, nil, nil, nil, nil],
331
- [nil, nil, nil, nil, nil, nil, nil, nil],
332
- ]
333
- )
334
- end
335
-
336
- it "should allow for capturing diagonally" do
337
- board = Array.new(8) { Array.new(8,nil) }
338
- board[5][5] = "WP"
339
- board[4][4] = "BP"
340
- chess = ChessMate.new(board)
341
- chess.move('f3', 'e4')
342
- expect(chess.board).to eql(
343
- [
344
- [nil, nil, nil, nil, nil, nil, nil, nil],
345
- [nil, nil, nil, nil, nil, nil, nil, nil],
346
- [nil, nil, nil, nil, nil, nil, nil, nil],
347
- [nil, nil, nil, nil, nil, nil, nil, nil],
348
- [nil, nil, nil, nil, 'WP', nil, nil, nil],
349
- [nil, nil, nil, nil, nil, nil, nil, nil],
350
- [nil, nil, nil, nil, nil, nil, nil, nil],
351
- [nil, nil, nil, nil, nil, nil, nil, nil],
352
- ]
353
- )
354
- end
355
-
356
- it "should not allow pawns to move backwards to capture" do
357
- board = Array.new(8) { Array.new(8,nil) }
358
- board[4][4] = "WP"
359
- board[5][5] = "BP"
360
- chess = ChessMate.new(board)
361
- chess.move('e4', 'f3')
362
- expect(chess.board).to eql(
363
- [
364
- [nil, nil, nil, nil, nil, nil, nil, nil],
365
- [nil, nil, nil, nil, nil, nil, nil, nil],
366
- [nil, nil, nil, nil, nil, nil, nil, nil],
367
- [nil, nil, nil, nil, nil, nil, nil, nil],
368
- [nil, nil, nil, nil, 'WP', nil, nil, nil],
369
- [nil, nil, nil, nil, nil, 'BP', nil, nil],
370
- [nil, nil, nil, nil, nil, nil, nil, nil],
371
- [nil, nil, nil, nil, nil, nil, nil, nil],
372
- ]
373
- )
374
- end
375
-
376
- it "should not allow pawns to capture pieces of same color" do
377
- board = Array.new(8) { Array.new(8,nil) }
378
- board[4][4] = "WP"
379
- board[5][5] = "WP"
380
- chess = ChessMate.new(board)
381
- chess.move('f3', 'e4')
382
- expect(chess.board).to eql(
383
- [
384
- [nil, nil, nil, nil, nil, nil, nil, nil],
385
- [nil, nil, nil, nil, nil, nil, nil, nil],
386
- [nil, nil, nil, nil, nil, nil, nil, nil],
387
- [nil, nil, nil, nil, nil, nil, nil, nil],
388
- [nil, nil, nil, nil, 'WP', nil, nil, nil],
389
- [nil, nil, nil, nil, nil, 'WP', nil, nil],
390
- [nil, nil, nil, nil, nil, nil, nil, nil],
391
- [nil, nil, nil, nil, nil, nil, nil, nil],
392
- ]
393
- )
394
- end
395
-
396
- it "should not allow pawns to move backwards in general" do
397
- board = Array.new(8) { Array.new(8,nil) }
398
- board[4][4] = "WP"
399
- board[5][5] = "BP"
400
- chess = ChessMate.new(board)
401
- chess.move('e4', 'e3')
402
- expect(chess.board).to eql(
403
- [
404
- [nil, nil, nil, nil, nil, nil, nil, nil],
405
- [nil, nil, nil, nil, nil, nil, nil, nil],
406
- [nil, nil, nil, nil, nil, nil, nil, nil],
407
- [nil, nil, nil, nil, nil, nil, nil, nil],
408
- [nil, nil, nil, nil, 'WP', nil, nil, nil],
409
- [nil, nil, nil, nil, nil, 'BP', nil, nil],
410
- [nil, nil, nil, nil, nil, nil, nil, nil],
411
- [nil, nil, nil, nil, nil, nil, nil, nil],
412
- ]
413
- )
414
- end
415
- end
416
-
417
- context "for rooks" do
418
- it "should update the board if rook moves vertically" do
419
- board = Array.new(8) { Array.new(8,nil) }
420
- board[7][0] = "WR"
421
- chess = ChessMate.new(board)
422
- chess.move('a1', 'a8')
423
- expect(chess.board).to eql(
424
- [
425
- ["WR", nil, nil, nil, nil, nil, nil, nil],
426
- [nil, nil, nil, nil, nil, nil, nil, nil],
427
- [nil, nil, nil, nil, nil, nil, nil, nil],
428
- [nil, nil, nil, nil, nil, nil, nil, nil],
429
- [nil, nil, nil, nil, nil, nil, nil, nil],
430
- [nil, nil, nil, nil, nil, nil, nil, nil],
431
- [nil, nil, nil, nil, nil, nil, nil, nil],
432
- [nil, nil, nil, nil, nil, nil, nil, nil],
433
- ]
434
- )
435
- end
436
-
437
- it "should update the board if the rook moves horizontally" do
438
- board = Array.new(8) { Array.new(8,nil) }
439
- board[7][0] = "WR"
440
- chess = ChessMate.new(board)
441
- chess.move('a1', 'h1')
442
- expect(chess.board).to eql(
443
- [
444
- [nil, nil, nil, nil, nil, nil, nil, nil],
445
- [nil, nil, nil, nil, nil, nil, nil, nil],
446
- [nil, nil, nil, nil, nil, nil, nil, nil],
447
- [nil, nil, nil, nil, nil, nil, nil, nil],
448
- [nil, nil, nil, nil, nil, nil, nil, nil],
449
- [nil, nil, nil, nil, nil, nil, nil, nil],
450
- [nil, nil, nil, nil, nil, nil, nil, nil],
451
- [nil, nil, nil, nil, nil, nil, nil, "WR"],
452
- ]
453
- )
454
- end
455
-
456
- it "should return false for a move that is neither horizontal or vertical" do
457
- board = Array.new(8) { Array.new(8,nil) }
458
- board[7][0] = "WR"
459
- chess = ChessMate.new(board)
460
- expect(chess.move('a1','g8')).to eql(false)
461
- end
462
-
463
- it "should allow capturing pieces of opposite color" do
464
- board = Array.new(8) { Array.new(8,nil) }
465
- board[7][0] = "WR"
466
- board[7][7] = "BR"
467
- chess = ChessMate.new(board)
468
- chess.move('a1', 'h1')
469
- expect(chess.board).to eql(
470
- [
471
- [nil, nil, nil, nil, nil, nil, nil, nil],
472
- [nil, nil, nil, nil, nil, nil, nil, nil],
473
- [nil, nil, nil, nil, nil, nil, nil, nil],
474
- [nil, nil, nil, nil, nil, nil, nil, nil],
475
- [nil, nil, nil, nil, nil, nil, nil, nil],
476
- [nil, nil, nil, nil, nil, nil, nil, nil],
477
- [nil, nil, nil, nil, nil, nil, nil, nil],
478
- [nil, nil, nil, nil, nil, nil, nil, "WR"],
479
- ]
480
- )
481
- end
482
-
483
- it "should not allow capturing pieces of same color" do
484
- board = Array.new(8) { Array.new(8,nil) }
485
- board[7][0] = "WR"
486
- board[7][7] = "WR"
487
- chess = ChessMate.new(board)
488
- chess.move('a1', 'h1')
489
- expect(chess.board).to eql(
490
- [
491
- [nil, nil, nil, nil, nil, nil, nil, nil],
492
- [nil, nil, nil, nil, nil, nil, nil, nil],
493
- [nil, nil, nil, nil, nil, nil, nil, nil],
494
- [nil, nil, nil, nil, nil, nil, nil, nil],
495
- [nil, nil, nil, nil, nil, nil, nil, nil],
496
- [nil, nil, nil, nil, nil, nil, nil, nil],
497
- [nil, nil, nil, nil, nil, nil, nil, nil],
498
- ["WR", nil, nil, nil, nil, nil, nil, "WR"],
499
- ]
500
- )
501
- end
502
- end
246
+ it 'should generally return false and not update the board if the path is blocked' do
247
+ expect(@chess.move('d1', 'a1')).to eql(false)
248
+ expect(@chess.move('d1', 'd8')).to eql(false)
249
+ expect(@chess.move('d1', 'b3')).to eql(false)
250
+ expect(@chess.board).to eql(
251
+ [
252
+ %w[BR BN BB BQ BK BB BN BR],
253
+ %w[BP BP BP BP BP BP BP BP],
254
+ [nil, nil, nil, nil, nil, nil, nil, nil],
255
+ [nil, nil, nil, nil, nil, nil, nil, nil],
256
+ [nil, nil, nil, nil, nil, nil, nil, nil],
257
+ [nil, nil, nil, nil, nil, nil, nil, nil],
258
+ %w[WP WP WP WP WP WP WP WP],
259
+ %w[WR WN WB WQ WK WB WN WR]
260
+ ]
261
+ )
262
+ end
503
263
 
504
- context "for bishops" do
505
- it "should update the board if the bishop moves diagonally" do
506
- board = Array.new(8) { Array.new(8,nil) }
507
- board[7][2] = "WB"
508
- chess = ChessMate.new(board)
509
- chess.move('c1', 'h6')
510
- expect(chess.board).to eql(
511
- [
512
- [nil, nil, nil, nil, nil, nil, nil, nil],
513
- [nil, nil, nil, nil, nil, nil, nil, nil],
514
- [nil, nil, nil, nil, nil, nil, nil, 'WB'],
515
- [nil, nil, nil, nil, nil, nil, nil, nil],
516
- [nil, nil, nil, nil, nil, nil, nil, nil],
517
- [nil, nil, nil, nil, nil, nil, nil, nil],
518
- [nil, nil, nil, nil, nil, nil, nil, nil],
519
- [nil, nil, nil, nil, nil, nil, nil, nil],
520
- ]
521
- )
522
- end
523
-
524
- it "should return false for a move that is not diagonal" do
525
- board = Array.new(8) { Array.new(8,nil) }
526
- board[7][2] = "WB"
527
- chess = ChessMate.new(board)
528
- expect(chess.move('c1','c8')).to eql(false)
529
- expect(chess.move('c1','h7')).to eql(false)
530
- expect(chess.move('c1','h8')).to eql(false)
531
-
532
- end
533
-
534
- it "should allow capturing pieces of opposite color" do
535
- board = Array.new(8) { Array.new(8,nil) }
536
- board[7][2] = "WB"
537
- board[2][7] = "BB"
538
- chess = ChessMate.new(board)
539
- chess.move('c1', 'h6')
540
- expect(chess.board).to eql(
541
- [
542
- [nil, nil, nil, nil, nil, nil, nil, nil],
543
- [nil, nil, nil, nil, nil, nil, nil, nil],
544
- [nil, nil, nil, nil, nil, nil, nil, 'WB'],
545
- [nil, nil, nil, nil, nil, nil, nil, nil],
546
- [nil, nil, nil, nil, nil, nil, nil, nil],
547
- [nil, nil, nil, nil, nil, nil, nil, nil],
548
- [nil, nil, nil, nil, nil, nil, nil, nil],
549
- [nil, nil, nil, nil, nil, nil, nil, nil],
550
- ]
551
- )
552
- end
553
-
554
- it "should not allow capturing pieces of same color" do
555
- board = Array.new(8) { Array.new(8,nil) }
556
- board[7][2] = "WB"
557
- board[2][7] = "WB"
558
- chess = ChessMate.new(board)
559
- chess.move('c1', 'h6')
560
- expect(chess.board).to eql(
561
- [
562
- [nil, nil, nil, nil, nil, nil, nil, nil],
563
- [nil, nil, nil, nil, nil, nil, nil, nil],
564
- [nil, nil, nil, nil, nil, nil, nil, 'WB'],
565
- [nil, nil, nil, nil, nil, nil, nil, nil],
566
- [nil, nil, nil, nil, nil, nil, nil, nil],
567
- [nil, nil, nil, nil, nil, nil, nil, nil],
568
- [nil, nil, nil, nil, nil, nil, nil, nil],
569
- [nil, nil, "WB", nil, nil, nil, nil, nil],
570
- ]
571
- )
572
- end
573
- end
264
+ context 'for pawns' do
265
+ it 'should update the board if pawn move is valid' do
266
+ @chess.move('c2', 'c3')
267
+ expect(@chess.board).to eql(
268
+ [
269
+ %w[BR BN BB BQ BK BB BN BR],
270
+ %w[BP BP BP BP BP BP BP BP],
271
+ [nil, nil, nil, nil, nil, nil, nil, nil],
272
+ [nil, nil, nil, nil, nil, nil, nil, nil],
273
+ [nil, nil, nil, nil, nil, nil, nil, nil],
274
+ [nil, nil, 'WP', nil, nil, nil, nil, nil],
275
+ ['WP', 'WP', nil, 'WP', 'WP', 'WP', 'WP', 'WP'],
276
+ %w[WR WN WB WQ WK WB WN WR]
277
+ ]
278
+ )
279
+ end
574
280
 
575
- context "for knights" do
576
- it "should update the board if the knight moves 2 horizontal, 1 vertical" do
577
- board = Array.new(8) { Array.new(8,nil) }
578
- board[7][2] = "WN"
579
- chess = ChessMate.new(board)
580
- chess.move('c1', 'e2')
581
- expect(chess.board).to eql(
582
- [
583
- [nil, nil, nil, nil, nil, nil, nil, nil],
584
- [nil, nil, nil, nil, nil, nil, nil, nil],
585
- [nil, nil, nil, nil, nil, nil, nil, nil],
586
- [nil, nil, nil, nil, nil, nil, nil, nil],
587
- [nil, nil, nil, nil, nil, nil, nil, nil],
588
- [nil, nil, nil, nil, nil, nil, nil, nil],
589
- [nil, nil, nil, nil, 'WN', nil, nil, nil],
590
- [nil, nil, nil, nil, nil, nil, nil, nil],
591
- ]
592
- )
593
- end
594
-
595
- it "should update the board if the knight moves 1 horizontal, 2 vertical" do
596
- board = Array.new(8) { Array.new(8,nil) }
597
- board[7][2] = "WN"
598
- chess = ChessMate.new(board)
599
- chess.move('c1', 'd3')
600
- expect(chess.board).to eql(
601
- [
602
- [nil, nil, nil, nil, nil, nil, nil, nil],
603
- [nil, nil, nil, nil, nil, nil, nil, nil],
604
- [nil, nil, nil, nil, nil, nil, nil, nil],
605
- [nil, nil, nil, nil, nil, nil, nil, nil],
606
- [nil, nil, nil, nil, nil, nil, nil, nil],
607
- [nil, nil, nil, 'WN', nil, nil, nil, nil],
608
- [nil, nil, nil, nil, nil, nil, nil, nil],
609
- [nil, nil, nil, nil, nil, nil, nil, nil],
610
- ]
611
- )
612
- end
613
-
614
- it "should return false if the move is invalid" do
615
- board = Array.new(8) { Array.new(8,nil) }
616
- board[7][2] = "WN"
617
- chess = ChessMate.new(board)
618
- expect(chess.move('c1','c8')).to eql(false)
619
- end
620
-
621
- it "should allow capturing pieces of opposite color" do
622
- board = Array.new(8) { Array.new(8,nil) }
623
- board[7][2] = "WN"
624
- board[5][3] = "BN"
625
- chess = ChessMate.new(board)
626
- chess.move('c1', 'd3')
627
- expect(chess.board).to eql(
628
- [
629
- [nil, nil, nil, nil, nil, nil, nil, nil],
630
- [nil, nil, nil, nil, nil, nil, nil, nil],
631
- [nil, nil, nil, nil, nil, nil, nil, nil],
632
- [nil, nil, nil, nil, nil, nil, nil, nil],
633
- [nil, nil, nil, nil, nil, nil, nil, nil],
634
- [nil, nil, nil, 'WN', nil, nil, nil, nil],
635
- [nil, nil, nil, nil, nil, nil, nil, nil],
636
- [nil, nil, nil, nil, nil, nil, nil, nil],
637
- ]
638
- )
639
- end
640
-
641
- it "should not allow capturing pieces of same color" do
642
- board = Array.new(8) { Array.new(8,nil) }
643
- board[7][2] = "WN"
644
- board[5][3] = "WN"
645
- chess = ChessMate.new(board)
646
- chess.move('c1', 'd3')
647
- expect(chess.board).to eql(
648
- [
649
- [nil, nil, nil, nil, nil, nil, nil, nil],
650
- [nil, nil, nil, nil, nil, nil, nil, nil],
651
- [nil, nil, nil, nil, nil, nil, nil, nil],
652
- [nil, nil, nil, nil, nil, nil, nil, nil],
653
- [nil, nil, nil, nil, nil, nil, nil, nil],
654
- [nil, nil, nil, 'WN', nil, nil, nil, nil],
655
- [nil, nil, nil, nil, nil, nil, nil, nil],
656
- [nil, nil, 'WN', nil, nil, nil, nil, nil],
657
- ]
658
- )
659
- end
660
- end
281
+ it 'should return false if the pawn move is invalid' do
282
+ expect(@chess.move('c2', 'd3')).to eql(false)
283
+ end
661
284
 
662
- context "for queens" do
663
- it "should update the board if the queen moves vertically" do
664
- board = Array.new(8) { Array.new(8,nil) }
665
- board[7][3] = "WQ"
666
- chess = ChessMate.new(board)
667
- chess.move('d1', 'd8')
668
- expect(chess.board).to eql(
669
- [
670
- [nil, nil, nil, "WQ", nil, nil, nil, nil],
671
- [nil, nil, nil, nil, nil, nil, nil, nil],
672
- [nil, nil, nil, nil, nil, nil, nil, nil],
673
- [nil, nil, nil, nil, nil, nil, nil, nil],
674
- [nil, nil, nil, nil, nil, nil, nil, nil],
675
- [nil, nil, nil, nil, nil, nil, nil, nil],
676
- [nil, nil, nil, nil, nil, nil, nil, nil],
677
- [nil, nil, nil, nil, nil, nil, nil, nil],
678
- ]
679
- )
680
- end
681
-
682
- it "should update the board if the queen moves horizontally" do
683
- board = Array.new(8) { Array.new(8,nil) }
684
- board[7][3] = "WQ"
685
- chess = ChessMate.new(board)
686
- chess.move('d1', 'h1')
687
- expect(chess.board).to eql(
688
- [
689
- [nil, nil, nil, nil, nil, nil, nil, nil],
690
- [nil, nil, nil, nil, nil, nil, nil, nil],
691
- [nil, nil, nil, nil, nil, nil, nil, nil],
692
- [nil, nil, nil, nil, nil, nil, nil, nil],
693
- [nil, nil, nil, nil, nil, nil, nil, nil],
694
- [nil, nil, nil, nil, nil, nil, nil, nil],
695
- [nil, nil, nil, nil, nil, nil, nil, nil],
696
- [nil, nil, nil, nil, nil, nil, nil, "WQ"],
697
- ]
698
- )
699
- end
700
-
701
- it "should update the board if the queen moves diagonally" do
702
- board = Array.new(8) { Array.new(8,nil) }
703
- board[7][3] = "WQ"
704
- chess = ChessMate.new(board)
705
- chess.move('d1', 'h5')
706
- expect(chess.board).to eql(
707
- [
708
- [nil, nil, nil, nil, nil, nil, nil, nil],
709
- [nil, nil, nil, nil, nil, nil, nil, nil],
710
- [nil, nil, nil, nil, nil, nil, nil, nil],
711
- [nil, nil, nil, nil, nil, nil, nil, "WQ"],
712
- [nil, nil, nil, nil, nil, nil, nil, nil],
713
- [nil, nil, nil, nil, nil, nil, nil, nil],
714
- [nil, nil, nil, nil, nil, nil, nil, nil],
715
- [nil, nil, nil, nil, nil, nil, nil, nil],
716
- ]
717
- )
718
- end
719
-
720
- it "should return false if the queen makes an otherwise invalid move" do
721
- board = Array.new(8) { Array.new(8,nil) }
722
- board[7][3] = "WQ"
723
- chess = ChessMate.new(board)
724
- expect(chess.move('d1', 'h8')).to eql(false)
725
- end
726
-
727
- it "should allow capturing pieces of opposite color" do
728
- board = Array.new(8) { Array.new(8,nil) }
729
- board[7][3] = "WQ"
730
- board[7][7] = "BQ"
731
- chess = ChessMate.new(board)
732
- chess.move('d1', 'h1')
733
- expect(chess.board).to eql(
734
- [
735
- [nil, nil, nil, nil, nil, nil, nil, nil],
736
- [nil, nil, nil, nil, nil, nil, nil, nil],
737
- [nil, nil, nil, nil, nil, nil, nil, nil],
738
- [nil, nil, nil, nil, nil, nil, nil, nil],
739
- [nil, nil, nil, nil, nil, nil, nil, nil],
740
- [nil, nil, nil, nil, nil, nil, nil, nil],
741
- [nil, nil, nil, nil, nil, nil, nil, nil],
742
- [nil, nil, nil, nil, nil, nil, nil, "WQ"],
743
- ]
744
- )
745
- end
746
-
747
- it "should not allow capturing pieces of same color" do
748
- board = Array.new(8) { Array.new(8,nil) }
749
- board[7][3] = "WQ"
750
- board[7][7] = "WQ"
751
- chess = ChessMate.new(board)
752
- chess.move('d1', 'h1')
753
- expect(chess.board).to eql(
754
- [
755
- [nil, nil, nil, nil, nil, nil, nil, nil],
756
- [nil, nil, nil, nil, nil, nil, nil, nil],
757
- [nil, nil, nil, nil, nil, nil, nil, nil],
758
- [nil, nil, nil, nil, nil, nil, nil, nil],
759
- [nil, nil, nil, nil, nil, nil, nil, nil],
760
- [nil, nil, nil, nil, nil, nil, nil, nil],
761
- [nil, nil, nil, nil, nil, nil, nil, nil],
762
- [nil, nil, nil, "WQ", nil, nil, nil, "WQ"],
763
- ]
764
- )
765
- end
766
- end
285
+ it "should update the board if pawn's first move is 2 squares" do
286
+ chess = ChessMate.new
287
+ chess.move('c2', 'c4')
288
+ expect(chess.board).to eql(
289
+ [
290
+ %w[BR BN BB BQ BK BB BN BR],
291
+ %w[BP BP BP BP BP BP BP BP],
292
+ [nil, nil, nil, nil, nil, nil, nil, nil],
293
+ [nil, nil, nil, nil, nil, nil, nil, nil],
294
+ [nil, nil, 'WP', nil, nil, nil, nil, nil],
295
+ [nil, nil, nil, nil, nil, nil, nil, nil],
296
+ ['WP', 'WP', nil, 'WP', 'WP', 'WP', 'WP', 'WP'],
297
+ %w[WR WN WB WQ WK WB WN WR]
298
+ ]
299
+ )
300
+ chess = ChessMate.new
301
+ chess.move('b7', 'b5')
302
+ expect(chess.board).to eql(
303
+ [
304
+ %w[BR BN BB BQ BK BB BN BR],
305
+ ['BP', nil, 'BP', 'BP', 'BP', 'BP', 'BP', 'BP'],
306
+ [nil, nil, nil, nil, nil, nil, nil, nil],
307
+ [nil, 'BP', nil, nil, nil, nil, nil, nil],
308
+ [nil, nil, nil, nil, nil, nil, nil, nil],
309
+ [nil, nil, nil, nil, nil, nil, nil, nil],
310
+ %w[WP WP WP WP WP WP WP WP],
311
+ %w[WR WN WB WQ WK WB WN WR]
312
+ ]
313
+ )
314
+ end
767
315
 
768
- context "for kings" do
769
- it "should update the board if the king moves 1 space vertically" do
770
- board = Array.new(8) { Array.new(8,nil) }
771
- board[7][4] = "WK"
772
- chess = ChessMate.new(board)
773
- chess.move('e1', 'e2')
774
- expect(chess.board).to eql(
775
- [
776
- [nil, nil, nil, nil, nil, nil, nil, nil],
777
- [nil, nil, nil, nil, nil, nil, nil, nil],
778
- [nil, nil, nil, nil, nil, nil, nil, nil],
779
- [nil, nil, nil, nil, nil, nil, nil, nil],
780
- [nil, nil, nil, nil, nil, nil, nil, nil],
781
- [nil, nil, nil, nil, nil, nil, nil, nil],
782
- [nil, nil, nil, nil, "WK", nil, nil, nil],
783
- [nil, nil, nil, nil, nil, nil, nil, nil],
784
- ]
785
- )
786
- end
787
-
788
- it "should update the board if the king moves 1 space horizontally" do
789
- board = Array.new(8) { Array.new(8,nil) }
790
- board[7][4] = "WK"
791
- chess = ChessMate.new(board)
792
- chess.move('e1', 'f1')
793
- expect(chess.board).to eql(
794
- [
795
- [nil, nil, nil, nil, nil, nil, nil, nil],
796
- [nil, nil, nil, nil, nil, nil, nil, nil],
797
- [nil, nil, nil, nil, nil, nil, nil, nil],
798
- [nil, nil, nil, nil, nil, nil, nil, nil],
799
- [nil, nil, nil, nil, nil, nil, nil, nil],
800
- [nil, nil, nil, nil, nil, nil, nil, nil],
801
- [nil, nil, nil, nil, nil, nil, nil, nil],
802
- [nil, nil, nil, nil, nil, "WK", nil, nil],
803
- ]
804
- )
805
- end
806
-
807
- it "should update the board if the king moves 1 space diagonally" do
808
- board = Array.new(8) { Array.new(8,nil) }
809
- board[7][4] = "WK"
810
- chess = ChessMate.new(board)
811
- chess.move('e1', 'f2')
812
- expect(chess.board).to eql(
813
- [
814
- [nil, nil, nil, nil, nil, nil, nil, nil],
815
- [nil, nil, nil, nil, nil, nil, nil, nil],
816
- [nil, nil, nil, nil, nil, nil, nil, nil],
817
- [nil, nil, nil, nil, nil, nil, nil, nil],
818
- [nil, nil, nil, nil, nil, nil, nil, nil],
819
- [nil, nil, nil, nil, nil, nil, nil, nil],
820
- [nil, nil, nil, nil, nil, "WK", nil, nil],
821
- [nil, nil, nil, nil, nil, nil, nil, nil],
822
- ]
823
- )
824
- end
825
-
826
- it "should return false if the king makes an otherwise invalid move" do
827
- board = Array.new(8) { Array.new(8,nil) }
828
- board[7][4] = "WK"
829
- chess = ChessMate.new(board)
830
- expect(chess.move('e1', 'h8')).to eql(false)
831
- end
832
-
833
- it "should allow capturing pieces of opposite color" do
834
- board = Array.new(8) { Array.new(8,nil) }
835
- board[7][4] = "WK"
836
- board[6][4] = "BP"
837
- chess = ChessMate.new(board)
838
- chess.move('e1', 'e2')
839
- expect(chess.board).to eql(
840
- [
841
- [nil, nil, nil, nil, nil, nil, nil, nil],
842
- [nil, nil, nil, nil, nil, nil, nil, nil],
843
- [nil, nil, nil, nil, nil, nil, nil, nil],
844
- [nil, nil, nil, nil, nil, nil, nil, nil],
845
- [nil, nil, nil, nil, nil, nil, nil, nil],
846
- [nil, nil, nil, nil, nil, nil, nil, nil],
847
- [nil, nil, nil, nil, "WK", nil, nil, nil],
848
- [nil, nil, nil, nil, nil, nil, nil, nil],
849
- ]
850
- )
851
- end
852
-
853
- it "should not allow capturing pieces of same color" do
854
- board = Array.new(8) { Array.new(8,nil) }
855
- board[7][4] = "WK"
856
- board[6][4] = "WP"
857
- chess = ChessMate.new(board)
858
- chess.move('e1', 'e2')
859
- expect(chess.board).to eql(
860
- [
861
- [nil, nil, nil, nil, nil, nil, nil, nil],
862
- [nil, nil, nil, nil, nil, nil, nil, nil],
863
- [nil, nil, nil, nil, nil, nil, nil, nil],
864
- [nil, nil, nil, nil, nil, nil, nil, nil],
865
- [nil, nil, nil, nil, nil, nil, nil, nil],
866
- [nil, nil, nil, nil, nil, nil, nil, nil],
867
- [nil, nil, nil, nil, "WP", nil, nil, nil],
868
- [nil, nil, nil, nil, "WK", nil, nil, nil],
869
- ]
870
- )
871
- end
872
- end
316
+ it 'should not allow a pawn to move 2 spaces if the 1st space is occupied' do
317
+ board = Array.new(8) { Array.new(8, nil) }
318
+ board[6][0] = 'WP'
319
+ board[5][0] = 'BP'
320
+ chess = ChessMate.new(board)
321
+ chess.move('a2', 'a4')
322
+ expect(chess.board).to eql(
323
+ [
324
+ [nil, nil, nil, nil, nil, nil, nil, nil],
325
+ [nil, nil, nil, nil, nil, nil, nil, nil],
326
+ [nil, nil, nil, nil, nil, nil, nil, nil],
327
+ [nil, nil, nil, nil, nil, nil, nil, nil],
328
+ [nil, nil, nil, nil, nil, nil, nil, nil],
329
+ ['BP', nil, nil, nil, nil, nil, nil, nil],
330
+ ['WP', nil, nil, nil, nil, nil, nil, nil],
331
+ [nil, nil, nil, nil, nil, nil, nil, nil]
332
+ ]
333
+ )
334
+ end
335
+
336
+ it 'should allow for capturing diagonally' do
337
+ board = Array.new(8) { Array.new(8, nil) }
338
+ board[5][5] = 'WP'
339
+ board[4][4] = 'BP'
340
+ chess = ChessMate.new(board)
341
+ chess.move('f3', 'e4')
342
+ expect(chess.board).to eql(
343
+ [
344
+ [nil, nil, nil, nil, nil, nil, nil, nil],
345
+ [nil, nil, nil, nil, nil, nil, nil, nil],
346
+ [nil, nil, nil, nil, nil, nil, nil, nil],
347
+ [nil, nil, nil, nil, nil, nil, nil, nil],
348
+ [nil, nil, nil, nil, 'WP', nil, nil, nil],
349
+ [nil, nil, nil, nil, nil, nil, nil, nil],
350
+ [nil, nil, nil, nil, nil, nil, nil, nil],
351
+ [nil, nil, nil, nil, nil, nil, nil, nil]
352
+ ]
353
+ )
354
+ end
355
+
356
+ it 'should not allow pawns to move backwards to capture' do
357
+ board = Array.new(8) { Array.new(8, nil) }
358
+ board[4][4] = 'WP'
359
+ board[5][5] = 'BP'
360
+ chess = ChessMate.new(board)
361
+ chess.move('e4', 'f3')
362
+ expect(chess.board).to eql(
363
+ [
364
+ [nil, nil, nil, nil, nil, nil, nil, nil],
365
+ [nil, nil, nil, nil, nil, nil, nil, nil],
366
+ [nil, nil, nil, nil, nil, nil, nil, nil],
367
+ [nil, nil, nil, nil, nil, nil, nil, nil],
368
+ [nil, nil, nil, nil, 'WP', nil, nil, nil],
369
+ [nil, nil, nil, nil, nil, 'BP', nil, nil],
370
+ [nil, nil, nil, nil, nil, nil, nil, nil],
371
+ [nil, nil, nil, nil, nil, nil, nil, nil]
372
+ ]
373
+ )
374
+ end
375
+
376
+ it 'should not allow pawns to capture pieces of same color' do
377
+ board = Array.new(8) { Array.new(8, nil) }
378
+ board[4][4] = 'WP'
379
+ board[5][5] = 'WP'
380
+ chess = ChessMate.new(board)
381
+ chess.move('f3', 'e4')
382
+ expect(chess.board).to eql(
383
+ [
384
+ [nil, nil, nil, nil, nil, nil, nil, nil],
385
+ [nil, nil, nil, nil, nil, nil, nil, nil],
386
+ [nil, nil, nil, nil, nil, nil, nil, nil],
387
+ [nil, nil, nil, nil, nil, nil, nil, nil],
388
+ [nil, nil, nil, nil, 'WP', nil, nil, nil],
389
+ [nil, nil, nil, nil, nil, 'WP', nil, nil],
390
+ [nil, nil, nil, nil, nil, nil, nil, nil],
391
+ [nil, nil, nil, nil, nil, nil, nil, nil]
392
+ ]
393
+ )
394
+ end
395
+
396
+ it 'should not allow pawns to move backwards in general' do
397
+ board = Array.new(8) { Array.new(8, nil) }
398
+ board[4][4] = 'WP'
399
+ board[5][5] = 'BP'
400
+ chess = ChessMate.new(board)
401
+ chess.move('e4', 'e3')
402
+ expect(chess.board).to eql(
403
+ [
404
+ [nil, nil, nil, nil, nil, nil, nil, nil],
405
+ [nil, nil, nil, nil, nil, nil, nil, nil],
406
+ [nil, nil, nil, nil, nil, nil, nil, nil],
407
+ [nil, nil, nil, nil, nil, nil, nil, nil],
408
+ [nil, nil, nil, nil, 'WP', nil, nil, nil],
409
+ [nil, nil, nil, nil, nil, 'BP', nil, nil],
410
+ [nil, nil, nil, nil, nil, nil, nil, nil],
411
+ [nil, nil, nil, nil, nil, nil, nil, nil]
412
+ ]
413
+ )
414
+ end
415
+
416
+ it 'should flag a pawn as promotable if moving to the last rank' do
417
+ board = Array.new(8) { Array.new(8, nil) }
418
+ board[1][0] = 'WP'
419
+ chess = ChessMate.new(board)
420
+ chess.move('a7', 'a8')
421
+ expect(chess.board).to eql(
422
+ [
423
+ ['WP', nil, nil, nil, nil, nil, nil, nil],
424
+ [nil, nil, nil, nil, nil, nil, nil, nil],
425
+ [nil, nil, nil, nil, nil, nil, nil, nil],
426
+ [nil, nil, nil, nil, nil, nil, nil, nil],
427
+ [nil, nil, nil, nil, nil, nil, nil, nil],
428
+ [nil, nil, nil, nil, nil, nil, nil, nil],
429
+ [nil, nil, nil, nil, nil, nil, nil, nil],
430
+ [nil, nil, nil, nil, nil, nil, nil, nil]
431
+ ]
432
+ )
433
+ expect(chess.promotable).to eql([0, 0])
434
+ end
435
+
436
+ it 'should allow en passant under correct circumstances for white' do
437
+ board = Array.new(8) { Array.new(8, nil) }
438
+ board[1][0] = 'BP'
439
+ board[3][1] = 'WP'
440
+ chess = ChessMate.new(board)
441
+ chess.move('a7', 'a5')
442
+ chess.move('b5', 'a6')
443
+ expect(chess.board).to eql(
444
+ [
445
+ [nil, nil, nil, nil, nil, nil, nil, nil],
446
+ [nil, nil, nil, nil, nil, nil, nil, nil],
447
+ ['WP', nil, nil, nil, nil, nil, nil, nil],
448
+ [nil, nil, nil, nil, nil, nil, nil, nil],
449
+ [nil, nil, nil, nil, nil, nil, nil, nil],
450
+ [nil, nil, nil, nil, nil, nil, nil, nil],
451
+ [nil, nil, nil, nil, nil, nil, nil, nil],
452
+ [nil, nil, nil, nil, nil, nil, nil, nil]
453
+ ]
454
+ )
455
+ end
456
+
457
+ it 'should allow en passant under correct circumstances for black' do
458
+ board = Array.new(8) { Array.new(8, nil) }
459
+ board[4][0] = 'BP'
460
+ board[6][1] = 'WP'
461
+ chess = ChessMate.new(board)
462
+ chess.move('b2', 'b4')
463
+ chess.move('a4', 'b3')
464
+ expect(chess.board).to eql(
465
+ [
466
+ [nil, nil, nil, nil, nil, nil, nil, nil],
467
+ [nil, nil, nil, nil, nil, nil, nil, nil],
468
+ [nil, nil, nil, nil, nil, nil, nil, nil],
469
+ [nil, nil, nil, nil, nil, nil, nil, nil],
470
+ [nil, nil, nil, nil, nil, nil, nil, nil],
471
+ [nil, 'BP', nil, nil, nil, nil, nil, nil],
472
+ [nil, nil, nil, nil, nil, nil, nil, nil],
473
+ [nil, nil, nil, nil, nil, nil, nil, nil]
474
+ ]
475
+ )
476
+ end
873
477
  end
874
478
 
875
- describe "in_check_after_move? method" do
876
- before :each do
877
- @board = Array.new(8) { Array.new(8,nil) }
878
- @board[7][4] = "WK"
879
- @board[0][4] = "BK"
880
- @board[0][3] = "BQ"
881
- @chess = ChessMate.new(@board)
882
- end
883
- it "should return true if the moving color is in check after the move" do
884
- expect(@chess.in_check_after_move?([7,4], [7,3])).to eql(true)
885
- end
479
+ it 'should not allow en passant after first move' do
480
+ board = Array.new(8) { Array.new(8, nil) }
481
+ board[1][0] = 'BP'
482
+ board[3][1] = 'WP'
483
+ board[6][7] = 'WP'
484
+ board[1][7] = 'BP'
485
+ chess = ChessMate.new(board)
486
+ chess.move('a7', 'a5')
487
+ chess.move('h2', 'h3')
488
+ chess.move('h7', 'h6')
489
+ chess.move('b5', 'a6')
490
+ expect(chess.board).to eql(
491
+ [
492
+ [nil, nil, nil, nil, nil, nil, nil, nil],
493
+ [nil, nil, nil, nil, nil, nil, nil, nil],
494
+ [nil, nil, nil, nil, nil, nil, nil, 'BP'],
495
+ ['BP', 'WP', nil, nil, nil, nil, nil, nil],
496
+ [nil, nil, nil, nil, nil, nil, nil, nil],
497
+ [nil, nil, nil, nil, nil, nil, nil, 'WP'],
498
+ [nil, nil, nil, nil, nil, nil, nil, nil],
499
+ [nil, nil, nil, nil, nil, nil, nil, nil]
500
+ ]
501
+ )
502
+ end
886
503
 
887
- it "should return false if the moving color is not in check after the move" do
888
- expect(@chess.in_check_after_move?([7,4], [7,5])).to eql(false)
889
- end
504
+ context 'for rooks' do
505
+ it 'should update the board if rook moves vertically' do
506
+ board = Array.new(8) { Array.new(8, nil) }
507
+ board[7][0] = 'WR'
508
+ chess = ChessMate.new(board)
509
+ chess.move('a1', 'a8')
510
+ expect(chess.board).to eql(
511
+ [
512
+ ['WR', nil, nil, nil, nil, nil, nil, nil],
513
+ [nil, nil, nil, nil, nil, nil, nil, nil],
514
+ [nil, nil, nil, nil, nil, nil, nil, nil],
515
+ [nil, nil, nil, nil, nil, nil, nil, nil],
516
+ [nil, nil, nil, nil, nil, nil, nil, nil],
517
+ [nil, nil, nil, nil, nil, nil, nil, nil],
518
+ [nil, nil, nil, nil, nil, nil, nil, nil],
519
+ [nil, nil, nil, nil, nil, nil, nil, nil]
520
+ ]
521
+ )
522
+ end
890
523
 
891
- it "should not update the actual game board to test" do
892
- test_board = Array.new(8) { Array.new(8,nil) }
893
- test_board[7][4] = "WK"
894
- test_board[0][4] = "BK"
895
- test_board[0][3] = "BQ"
896
- @chess.in_check_after_move?([7,4], [7,3])
897
- expect(test_board.object_id).to_not eql(@board.object_id)
898
- expect(test_board).to eql(@chess.board)
899
- end
524
+ it 'should update the board if the rook moves horizontally' do
525
+ board = Array.new(8) { Array.new(8, nil) }
526
+ board[7][0] = 'WR'
527
+ chess = ChessMate.new(board)
528
+ chess.move('a1', 'h1')
529
+ expect(chess.board).to eql(
530
+ [
531
+ [nil, nil, nil, nil, nil, nil, nil, nil],
532
+ [nil, nil, nil, nil, nil, nil, nil, nil],
533
+ [nil, nil, nil, nil, nil, nil, nil, nil],
534
+ [nil, nil, nil, nil, nil, nil, nil, nil],
535
+ [nil, nil, nil, nil, nil, nil, nil, nil],
536
+ [nil, nil, nil, nil, nil, nil, nil, nil],
537
+ [nil, nil, nil, nil, nil, nil, nil, nil],
538
+ [nil, nil, nil, nil, nil, nil, nil, 'WR']
539
+ ]
540
+ )
541
+ end
900
542
 
901
- it "should return true if the checking piece is captured, but still results in check" do
902
- board = Array.new(8) { Array.new(8,nil) }
903
- board[7][4] = "WK"
904
- board[6][4] = "BQ"
905
- board[3][4] = "BK"
906
- board[4][4] = "BR"
907
- chess = ChessMate.new(board)
908
- expect(chess.in_check_after_move?([7,4], [6,4])).to eql(true)
909
- end
543
+ it 'should return false for a move that is neither horizontal or vertical' do
544
+ board = Array.new(8) { Array.new(8, nil) }
545
+ board[7][0] = 'WR'
546
+ chess = ChessMate.new(board)
547
+ expect(chess.move('a1', 'g8')).to eql(false)
548
+ end
549
+
550
+ it 'should allow capturing pieces of opposite color' do
551
+ board = Array.new(8) { Array.new(8, nil) }
552
+ board[7][0] = 'WR'
553
+ board[7][7] = 'BR'
554
+ chess = ChessMate.new(board)
555
+ chess.move('a1', 'h1')
556
+ expect(chess.board).to eql(
557
+ [
558
+ [nil, nil, nil, nil, nil, nil, nil, nil],
559
+ [nil, nil, nil, nil, nil, nil, nil, nil],
560
+ [nil, nil, nil, nil, nil, nil, nil, nil],
561
+ [nil, nil, nil, nil, nil, nil, nil, nil],
562
+ [nil, nil, nil, nil, nil, nil, nil, nil],
563
+ [nil, nil, nil, nil, nil, nil, nil, nil],
564
+ [nil, nil, nil, nil, nil, nil, nil, nil],
565
+ [nil, nil, nil, nil, nil, nil, nil, 'WR']
566
+ ]
567
+ )
568
+ end
569
+
570
+ it 'should not allow capturing pieces of same color' do
571
+ board = Array.new(8) { Array.new(8, nil) }
572
+ board[7][0] = 'WR'
573
+ board[7][7] = 'WR'
574
+ chess = ChessMate.new(board)
575
+ chess.move('a1', 'h1')
576
+ expect(chess.board).to eql(
577
+ [
578
+ [nil, nil, nil, nil, nil, nil, nil, nil],
579
+ [nil, nil, nil, nil, nil, nil, nil, nil],
580
+ [nil, nil, nil, nil, nil, nil, nil, nil],
581
+ [nil, nil, nil, nil, nil, nil, nil, nil],
582
+ [nil, nil, nil, nil, nil, nil, nil, nil],
583
+ [nil, nil, nil, nil, nil, nil, nil, nil],
584
+ [nil, nil, nil, nil, nil, nil, nil, nil],
585
+ ['WR', nil, nil, nil, nil, nil, nil, 'WR']
586
+ ]
587
+ )
588
+ end
910
589
  end
911
590
 
912
- describe "valid_moves? method" do
913
- it "should return true if there are valid moves" do
914
- board = Array.new(8) { Array.new(8,nil) }
915
- board[7][4] = "WK"
916
- board[0][4] = "BK"
917
- chess = ChessMate.new(board)
918
- expect(chess.any_valid_moves?("W")).to eql(true)
919
- end
591
+ context 'for bishops' do
592
+ it 'should update the board if the bishop moves diagonally' do
593
+ board = Array.new(8) { Array.new(8, nil) }
594
+ board[7][2] = 'WB'
595
+ chess = ChessMate.new(board)
596
+ chess.move('c1', 'h6')
597
+ expect(chess.board).to eql(
598
+ [
599
+ [nil, nil, nil, nil, nil, nil, nil, nil],
600
+ [nil, nil, nil, nil, nil, nil, nil, nil],
601
+ [nil, nil, nil, nil, nil, nil, nil, 'WB'],
602
+ [nil, nil, nil, nil, nil, nil, nil, nil],
603
+ [nil, nil, nil, nil, nil, nil, nil, nil],
604
+ [nil, nil, nil, nil, nil, nil, nil, nil],
605
+ [nil, nil, nil, nil, nil, nil, nil, nil],
606
+ [nil, nil, nil, nil, nil, nil, nil, nil]
607
+ ]
608
+ )
609
+ end
920
610
 
921
- it "should return false if there are no valid moves" do
922
- board = Array.new(8) { Array.new(8,nil) }
923
- board[7][4] = "WK"
924
- board[6][4] = "BQ"
925
- board[3][4] = "BK"
926
- board[4][4] = "BR"
927
- chess = ChessMate.new(board)
928
- expect(chess.any_valid_moves?("W")).to eql(false)
929
- end
611
+ it 'should return false for a move that is not diagonal' do
612
+ board = Array.new(8) { Array.new(8, nil) }
613
+ board[7][2] = 'WB'
614
+ chess = ChessMate.new(board)
615
+ expect(chess.move('c1', 'c8')).to eql(false)
616
+ expect(chess.move('c1', 'h7')).to eql(false)
617
+ expect(chess.move('c1', 'h8')).to eql(false)
618
+ end
619
+
620
+ it 'should allow capturing pieces of opposite color' do
621
+ board = Array.new(8) { Array.new(8, nil) }
622
+ board[7][2] = 'WB'
623
+ board[2][7] = 'BB'
624
+ chess = ChessMate.new(board)
625
+ chess.move('c1', 'h6')
626
+ expect(chess.board).to eql(
627
+ [
628
+ [nil, nil, nil, nil, nil, nil, nil, nil],
629
+ [nil, nil, nil, nil, nil, nil, nil, nil],
630
+ [nil, nil, nil, nil, nil, nil, nil, 'WB'],
631
+ [nil, nil, nil, nil, nil, nil, nil, nil],
632
+ [nil, nil, nil, nil, nil, nil, nil, nil],
633
+ [nil, nil, nil, nil, nil, nil, nil, nil],
634
+ [nil, nil, nil, nil, nil, nil, nil, nil],
635
+ [nil, nil, nil, nil, nil, nil, nil, nil]
636
+ ]
637
+ )
638
+ end
639
+
640
+ it 'should not allow capturing pieces of same color' do
641
+ board = Array.new(8) { Array.new(8, nil) }
642
+ board[7][2] = 'WB'
643
+ board[2][7] = 'WB'
644
+ chess = ChessMate.new(board)
645
+ chess.move('c1', 'h6')
646
+ expect(chess.board).to eql(
647
+ [
648
+ [nil, nil, nil, nil, nil, nil, nil, nil],
649
+ [nil, nil, nil, nil, nil, nil, nil, nil],
650
+ [nil, nil, nil, nil, nil, nil, nil, 'WB'],
651
+ [nil, nil, nil, nil, nil, nil, nil, nil],
652
+ [nil, nil, nil, nil, nil, nil, nil, nil],
653
+ [nil, nil, nil, nil, nil, nil, nil, nil],
654
+ [nil, nil, nil, nil, nil, nil, nil, nil],
655
+ [nil, nil, 'WB', nil, nil, nil, nil, nil]
656
+ ]
657
+ )
658
+ end
930
659
  end
931
660
 
932
- describe "checkmate? method" do
933
- it "should return true if no valid moves remain and currently in check" do
934
- board = Array.new(8) { Array.new(8,nil) }
935
- board[7][4] = "WK"
936
- board[6][4] = "BQ"
937
- board[3][4] = "BK"
938
- board[4][4] = "BR"
939
- chess = ChessMate.new(board)
940
- expect(chess.checkmate?("W")).to eql(true)
941
- end
661
+ context 'for knights' do
662
+ it 'should update the board if the knight moves 2 horizontal, 1 vertical' do
663
+ board = Array.new(8) { Array.new(8, nil) }
664
+ board[7][2] = 'WN'
665
+ chess = ChessMate.new(board)
666
+ chess.move('c1', 'e2')
667
+ expect(chess.board).to eql(
668
+ [
669
+ [nil, nil, nil, nil, nil, nil, nil, nil],
670
+ [nil, nil, nil, nil, nil, nil, nil, nil],
671
+ [nil, nil, nil, nil, nil, nil, nil, nil],
672
+ [nil, nil, nil, nil, nil, nil, nil, nil],
673
+ [nil, nil, nil, nil, nil, nil, nil, nil],
674
+ [nil, nil, nil, nil, nil, nil, nil, nil],
675
+ [nil, nil, nil, nil, 'WN', nil, nil, nil],
676
+ [nil, nil, nil, nil, nil, nil, nil, nil]
677
+ ]
678
+ )
679
+ end
942
680
 
943
- it "should return false if there are valid moves" do
944
- chess = ChessMate.new
945
- expect(chess.checkmate?("W")).to eql(false)
946
- end
681
+ it 'should update the board if the knight moves 1 horizontal, 2 vertical' do
682
+ board = Array.new(8) { Array.new(8, nil) }
683
+ board[7][2] = 'WN'
684
+ chess = ChessMate.new(board)
685
+ chess.move('c1', 'd3')
686
+ expect(chess.board).to eql(
687
+ [
688
+ [nil, nil, nil, nil, nil, nil, nil, nil],
689
+ [nil, nil, nil, nil, nil, nil, nil, nil],
690
+ [nil, nil, nil, nil, nil, nil, nil, nil],
691
+ [nil, nil, nil, nil, nil, nil, nil, nil],
692
+ [nil, nil, nil, nil, nil, nil, nil, nil],
693
+ [nil, nil, nil, 'WN', nil, nil, nil, nil],
694
+ [nil, nil, nil, nil, nil, nil, nil, nil],
695
+ [nil, nil, nil, nil, nil, nil, nil, nil]
696
+ ]
697
+ )
698
+ end
699
+
700
+ it 'should return false if the move is invalid' do
701
+ board = Array.new(8) { Array.new(8, nil) }
702
+ board[7][2] = 'WN'
703
+ chess = ChessMate.new(board)
704
+ expect(chess.move('c1', 'c8')).to eql(false)
705
+ end
706
+
707
+ it 'should allow capturing pieces of opposite color' do
708
+ board = Array.new(8) { Array.new(8, nil) }
709
+ board[7][2] = 'WN'
710
+ board[5][3] = 'BN'
711
+ chess = ChessMate.new(board)
712
+ chess.move('c1', 'd3')
713
+ expect(chess.board).to eql(
714
+ [
715
+ [nil, nil, nil, nil, nil, nil, nil, nil],
716
+ [nil, nil, nil, nil, nil, nil, nil, nil],
717
+ [nil, nil, nil, nil, nil, nil, nil, nil],
718
+ [nil, nil, nil, nil, nil, nil, nil, nil],
719
+ [nil, nil, nil, nil, nil, nil, nil, nil],
720
+ [nil, nil, nil, 'WN', nil, nil, nil, nil],
721
+ [nil, nil, nil, nil, nil, nil, nil, nil],
722
+ [nil, nil, nil, nil, nil, nil, nil, nil]
723
+ ]
724
+ )
725
+ end
726
+
727
+ it 'should not allow capturing pieces of same color' do
728
+ board = Array.new(8) { Array.new(8, nil) }
729
+ board[7][2] = 'WN'
730
+ board[5][3] = 'WN'
731
+ chess = ChessMate.new(board)
732
+ chess.move('c1', 'd3')
733
+ expect(chess.board).to eql(
734
+ [
735
+ [nil, nil, nil, nil, nil, nil, nil, nil],
736
+ [nil, nil, nil, nil, nil, nil, nil, nil],
737
+ [nil, nil, nil, nil, nil, nil, nil, nil],
738
+ [nil, nil, nil, nil, nil, nil, nil, nil],
739
+ [nil, nil, nil, nil, nil, nil, nil, nil],
740
+ [nil, nil, nil, 'WN', nil, nil, nil, nil],
741
+ [nil, nil, nil, nil, nil, nil, nil, nil],
742
+ [nil, nil, 'WN', nil, nil, nil, nil, nil]
743
+ ]
744
+ )
745
+ end
947
746
  end
948
747
 
949
- describe "draw? method" do
950
- it "should return true if no valid moves remain and currently not in check" do
951
- board = Array.new(8) { Array.new(8,nil) }
952
- board[7][4] = "WK"
953
- board[5][3] = "BQ"
954
- board[5][4] = "BK"
955
- board[4][5] = "BR"
956
- chess = ChessMate.new(board)
957
- expect(chess.draw?("W")).to eql(true)
958
- end
748
+ context 'for queens' do
749
+ it 'should update the board if the queen moves vertically' do
750
+ board = Array.new(8) { Array.new(8, nil) }
751
+ board[7][3] = 'WQ'
752
+ chess = ChessMate.new(board)
753
+ chess.move('d1', 'd8')
754
+ expect(chess.board).to eql(
755
+ [
756
+ [nil, nil, nil, 'WQ', nil, nil, nil, nil],
757
+ [nil, nil, nil, nil, nil, nil, nil, nil],
758
+ [nil, nil, nil, nil, nil, nil, nil, nil],
759
+ [nil, nil, nil, nil, nil, nil, nil, nil],
760
+ [nil, nil, nil, nil, nil, nil, nil, nil],
761
+ [nil, nil, nil, nil, nil, nil, nil, nil],
762
+ [nil, nil, nil, nil, nil, nil, nil, nil],
763
+ [nil, nil, nil, nil, nil, nil, nil, nil]
764
+ ]
765
+ )
766
+ end
767
+
768
+ it 'should update the board if the queen moves horizontally' do
769
+ board = Array.new(8) { Array.new(8, nil) }
770
+ board[7][3] = 'WQ'
771
+ chess = ChessMate.new(board)
772
+ chess.move('d1', 'h1')
773
+ expect(chess.board).to eql(
774
+ [
775
+ [nil, nil, nil, nil, nil, nil, nil, nil],
776
+ [nil, nil, nil, nil, nil, nil, nil, nil],
777
+ [nil, nil, nil, nil, nil, nil, nil, nil],
778
+ [nil, nil, nil, nil, nil, nil, nil, nil],
779
+ [nil, nil, nil, nil, nil, nil, nil, nil],
780
+ [nil, nil, nil, nil, nil, nil, nil, nil],
781
+ [nil, nil, nil, nil, nil, nil, nil, nil],
782
+ [nil, nil, nil, nil, nil, nil, nil, 'WQ']
783
+ ]
784
+ )
785
+ end
786
+
787
+ it 'should update the board if the queen moves diagonally' do
788
+ board = Array.new(8) { Array.new(8, nil) }
789
+ board[7][3] = 'WQ'
790
+ chess = ChessMate.new(board)
791
+ chess.move('d1', 'h5')
792
+ expect(chess.board).to eql(
793
+ [
794
+ [nil, nil, nil, nil, nil, nil, nil, nil],
795
+ [nil, nil, nil, nil, nil, nil, nil, nil],
796
+ [nil, nil, nil, nil, nil, nil, nil, nil],
797
+ [nil, nil, nil, nil, nil, nil, nil, 'WQ'],
798
+ [nil, nil, nil, nil, nil, nil, nil, nil],
799
+ [nil, nil, nil, nil, nil, nil, nil, nil],
800
+ [nil, nil, nil, nil, nil, nil, nil, nil],
801
+ [nil, nil, nil, nil, nil, nil, nil, nil]
802
+ ]
803
+ )
804
+ end
959
805
 
960
- it "should return false if there are valid moves" do
961
- chess = ChessMate.new
962
- expect(chess.checkmate?("W")).to eql(false)
806
+ it 'should return false if the queen makes an otherwise invalid move' do
807
+ board = Array.new(8) { Array.new(8, nil) }
808
+ board[7][3] = 'WQ'
809
+ chess = ChessMate.new(board)
810
+ expect(chess.move('d1', 'h8')).to eql(false)
811
+ end
812
+
813
+ it 'should allow capturing pieces of opposite color' do
814
+ board = Array.new(8) { Array.new(8, nil) }
815
+ board[7][3] = 'WQ'
816
+ board[7][7] = 'BQ'
817
+ chess = ChessMate.new(board)
818
+ chess.move('d1', 'h1')
819
+ expect(chess.board).to eql(
820
+ [
821
+ [nil, nil, nil, nil, nil, nil, nil, nil],
822
+ [nil, nil, nil, nil, nil, nil, nil, nil],
823
+ [nil, nil, nil, nil, nil, nil, nil, nil],
824
+ [nil, nil, nil, nil, nil, nil, nil, nil],
825
+ [nil, nil, nil, nil, nil, nil, nil, nil],
826
+ [nil, nil, nil, nil, nil, nil, nil, nil],
827
+ [nil, nil, nil, nil, nil, nil, nil, nil],
828
+ [nil, nil, nil, nil, nil, nil, nil, 'WQ']
829
+ ]
830
+ )
831
+ end
832
+
833
+ it 'should not allow capturing pieces of same color' do
834
+ board = Array.new(8) { Array.new(8, nil) }
835
+ board[7][3] = 'WQ'
836
+ board[7][7] = 'WQ'
837
+ chess = ChessMate.new(board)
838
+ chess.move('d1', 'h1')
839
+ expect(chess.board).to eql(
840
+ [
841
+ [nil, nil, nil, nil, nil, nil, nil, nil],
842
+ [nil, nil, nil, nil, nil, nil, nil, nil],
843
+ [nil, nil, nil, nil, nil, nil, nil, nil],
844
+ [nil, nil, nil, nil, nil, nil, nil, nil],
845
+ [nil, nil, nil, nil, nil, nil, nil, nil],
846
+ [nil, nil, nil, nil, nil, nil, nil, nil],
847
+ [nil, nil, nil, nil, nil, nil, nil, nil],
848
+ [nil, nil, nil, 'WQ', nil, nil, nil, 'WQ']
849
+ ]
850
+ )
851
+ end
852
+ end
853
+
854
+ context 'for kings' do
855
+ it 'should update the board if the king moves 1 space vertically' do
856
+ board = Array.new(8) { Array.new(8, nil) }
857
+ board[7][4] = 'WK'
858
+ chess = ChessMate.new(board)
859
+ chess.move('e1', 'e2')
860
+ expect(chess.board).to eql(
861
+ [
862
+ [nil, nil, nil, nil, nil, nil, nil, nil],
863
+ [nil, nil, nil, nil, nil, nil, nil, nil],
864
+ [nil, nil, nil, nil, nil, nil, nil, nil],
865
+ [nil, nil, nil, nil, nil, nil, nil, nil],
866
+ [nil, nil, nil, nil, nil, nil, nil, nil],
867
+ [nil, nil, nil, nil, nil, nil, nil, nil],
868
+ [nil, nil, nil, nil, 'WK', nil, nil, nil],
869
+ [nil, nil, nil, nil, nil, nil, nil, nil]
870
+ ]
871
+ )
872
+ end
873
+
874
+ it 'should update the board if the king moves 1 space horizontally' do
875
+ board = Array.new(8) { Array.new(8, nil) }
876
+ board[7][4] = 'WK'
877
+ chess = ChessMate.new(board)
878
+ chess.move('e1', 'f1')
879
+ expect(chess.board).to eql(
880
+ [
881
+ [nil, nil, nil, nil, nil, nil, nil, nil],
882
+ [nil, nil, nil, nil, nil, nil, nil, nil],
883
+ [nil, nil, nil, nil, nil, nil, nil, nil],
884
+ [nil, nil, nil, nil, nil, nil, nil, nil],
885
+ [nil, nil, nil, nil, nil, nil, nil, nil],
886
+ [nil, nil, nil, nil, nil, nil, nil, nil],
887
+ [nil, nil, nil, nil, nil, nil, nil, nil],
888
+ [nil, nil, nil, nil, nil, 'WK', nil, nil]
889
+ ]
890
+ )
891
+ end
892
+
893
+ it 'should update the board if the king moves 1 space diagonally' do
894
+ board = Array.new(8) { Array.new(8, nil) }
895
+ board[7][4] = 'WK'
896
+ chess = ChessMate.new(board)
897
+ chess.move('e1', 'f2')
898
+ expect(chess.board).to eql(
899
+ [
900
+ [nil, nil, nil, nil, nil, nil, nil, nil],
901
+ [nil, nil, nil, nil, nil, nil, nil, nil],
902
+ [nil, nil, nil, nil, nil, nil, nil, nil],
903
+ [nil, nil, nil, nil, nil, nil, nil, nil],
904
+ [nil, nil, nil, nil, nil, nil, nil, nil],
905
+ [nil, nil, nil, nil, nil, nil, nil, nil],
906
+ [nil, nil, nil, nil, nil, 'WK', nil, nil],
907
+ [nil, nil, nil, nil, nil, nil, nil, nil]
908
+ ]
909
+ )
910
+ end
911
+
912
+ it 'should return false if the king makes an otherwise invalid move' do
913
+ board = Array.new(8) { Array.new(8, nil) }
914
+ board[7][4] = 'WK'
915
+ chess = ChessMate.new(board)
916
+ expect(chess.move('e1', 'h8')).to eql(false)
917
+ end
918
+
919
+ it 'should allow capturing pieces of opposite color' do
920
+ board = Array.new(8) { Array.new(8, nil) }
921
+ board[7][4] = 'WK'
922
+ board[6][4] = 'BP'
923
+ chess = ChessMate.new(board)
924
+ chess.move('e1', 'e2')
925
+ expect(chess.board).to eql(
926
+ [
927
+ [nil, nil, nil, nil, nil, nil, nil, nil],
928
+ [nil, nil, nil, nil, nil, nil, nil, nil],
929
+ [nil, nil, nil, nil, nil, nil, nil, nil],
930
+ [nil, nil, nil, nil, nil, nil, nil, nil],
931
+ [nil, nil, nil, nil, nil, nil, nil, nil],
932
+ [nil, nil, nil, nil, nil, nil, nil, nil],
933
+ [nil, nil, nil, nil, 'WK', nil, nil, nil],
934
+ [nil, nil, nil, nil, nil, nil, nil, nil]
935
+ ]
936
+ )
937
+ end
938
+
939
+ it 'should not allow capturing pieces of same color' do
940
+ board = Array.new(8) { Array.new(8, nil) }
941
+ board[7][4] = 'WK'
942
+ board[6][4] = 'WP'
943
+ chess = ChessMate.new(board)
944
+ chess.move('e1', 'e2')
945
+ expect(chess.board).to eql(
946
+ [
947
+ [nil, nil, nil, nil, nil, nil, nil, nil],
948
+ [nil, nil, nil, nil, nil, nil, nil, nil],
949
+ [nil, nil, nil, nil, nil, nil, nil, nil],
950
+ [nil, nil, nil, nil, nil, nil, nil, nil],
951
+ [nil, nil, nil, nil, nil, nil, nil, nil],
952
+ [nil, nil, nil, nil, nil, nil, nil, nil],
953
+ [nil, nil, nil, nil, 'WP', nil, nil, nil],
954
+ [nil, nil, nil, nil, 'WK', nil, nil, nil]
955
+ ]
956
+ )
957
+ end
958
+
959
+ it 'should allow castling kingside under correct circumstances' do
960
+ board = Array.new(8) { Array.new(8, nil) }
961
+ board[7][4] = 'WK'
962
+ board[7][7] = 'WR'
963
+ chess = ChessMate.new(board)
964
+ chess.move('e1', 'g1')
965
+ expect(chess.board).to eql(
966
+ [
967
+ [nil, nil, nil, nil, nil, nil, nil, nil],
968
+ [nil, nil, nil, nil, nil, nil, nil, nil],
969
+ [nil, nil, nil, nil, nil, nil, nil, nil],
970
+ [nil, nil, nil, nil, nil, nil, nil, nil],
971
+ [nil, nil, nil, nil, nil, nil, nil, nil],
972
+ [nil, nil, nil, nil, nil, nil, nil, nil],
973
+ [nil, nil, nil, nil, nil, nil, nil, nil],
974
+ [nil, nil, nil, nil, nil, 'WR', 'WK', nil]
975
+ ]
976
+ )
977
+ end
978
+
979
+ it 'should allow castling queenside under correct circumstances' do
980
+ board = Array.new(8) { Array.new(8, nil) }
981
+ board[7][4] = 'WK'
982
+ board[7][0] = 'WR'
983
+ chess = ChessMate.new(board)
984
+ chess.move('e1', 'c1')
985
+ expect(chess.board).to eql(
986
+ [
987
+ [nil, nil, nil, nil, nil, nil, nil, nil],
988
+ [nil, nil, nil, nil, nil, nil, nil, nil],
989
+ [nil, nil, nil, nil, nil, nil, nil, nil],
990
+ [nil, nil, nil, nil, nil, nil, nil, nil],
991
+ [nil, nil, nil, nil, nil, nil, nil, nil],
992
+ [nil, nil, nil, nil, nil, nil, nil, nil],
993
+ [nil, nil, nil, nil, nil, nil, nil, nil],
994
+ [nil, nil, 'WK', 'WR', nil, nil, nil, nil]
995
+ ]
996
+ )
997
+ end
998
+
999
+ it 'should not allow castling if path is blocked' do
1000
+ chess = ChessMate.new
1001
+ board = chess.board.map(&:dup)
1002
+ chess.move('e1', 'g1')
1003
+ expect(chess.board).to eql(board)
1004
+ end
1005
+
1006
+ it 'should not allow castling if move results in check' do
1007
+ board = Array.new(8) { Array.new(8, nil) }
1008
+ board[7][4] = 'WK'
1009
+ board[7][7] = 'WR'
1010
+ board[0][6] = 'BQ'
1011
+ chess = ChessMate.new(board)
1012
+ chess.move('e1', 'g1')
1013
+ expect(chess.board).to eql(
1014
+ [
1015
+ [nil, nil, nil, nil, nil, nil, 'BQ', nil],
1016
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1017
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1018
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1019
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1020
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1021
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1022
+ [nil, nil, nil, nil, 'WK', nil, nil, 'WR']
1023
+ ]
1024
+ )
1025
+ end
1026
+
1027
+ it 'should not allow castling if king passes through check' do
1028
+ board = Array.new(8) { Array.new(8, nil) }
1029
+ board[7][4] = 'WK'
1030
+ board[7][7] = 'WR'
1031
+ board[0][5] = 'BQ'
1032
+ chess = ChessMate.new(board)
1033
+ chess.move('e1', 'g1')
1034
+ expect(chess.board).to eql(
1035
+ [
1036
+ [nil, nil, nil, nil, nil, 'BQ', nil, nil],
1037
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1038
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1039
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1040
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1041
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1042
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1043
+ [nil, nil, nil, nil, 'WK', nil, nil, 'WR']
1044
+ ]
1045
+ )
1046
+ end
1047
+
1048
+ it 'should not allow castling if king is in check' do
1049
+ board = Array.new(8) { Array.new(8, nil) }
1050
+ board[7][4] = 'WK'
1051
+ board[7][7] = 'WR'
1052
+ board[0][4] = 'BQ'
1053
+ chess = ChessMate.new(board)
1054
+ chess.move('e1', 'g1')
1055
+ expect(chess.board).to eql(
1056
+ [
1057
+ [nil, nil, nil, nil, 'BQ', nil, nil, nil],
1058
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1059
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1060
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1061
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1062
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1063
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1064
+ [nil, nil, nil, nil, 'WK', nil, nil, 'WR']
1065
+ ]
1066
+ )
1067
+ end
1068
+
1069
+ it 'should not allow castling if king has previously moved' do
1070
+ board = Array.new(8) { Array.new(8, nil) }
1071
+ board[7][3] = 'WK'
1072
+ board[7][7] = 'WR'
1073
+ chess = ChessMate.new(board)
1074
+ chess.move('d1', 'e1')
1075
+ chess.move('e1', 'g1')
1076
+ expect(chess.board).to eql(
1077
+ [
1078
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1079
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1080
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1081
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1082
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1083
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1084
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1085
+ [nil, nil, nil, nil, 'WK', nil, nil, 'WR']
1086
+ ]
1087
+ )
1088
+ end
1089
+
1090
+ it 'should not allow castling if rook has previously moved' do
1091
+ board = Array.new(8) { Array.new(8, nil) }
1092
+ board[7][4] = 'WK'
1093
+ board[7][7] = 'WR'
1094
+ chess = ChessMate.new(board)
1095
+ chess.move('h1', 'g1')
1096
+ chess.move('g1', 'h1')
1097
+ chess.move('e1', 'g1')
1098
+ expect(chess.board).to eql(
1099
+ [
1100
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1101
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1102
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1103
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1104
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1105
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1106
+ [nil, nil, nil, nil, nil, nil, nil, nil],
1107
+ [nil, nil, nil, nil, 'WK', nil, nil, 'WR']
1108
+ ]
1109
+ )
1110
+ end
1111
+ end
1112
+ end
1113
+
1114
+ describe 'in_check_after_move? method' do
1115
+ before :each do
1116
+ @board = Array.new(8) { Array.new(8, nil) }
1117
+ @board[7][4] = 'WK'
1118
+ @board[0][4] = 'BK'
1119
+ @board[0][3] = 'BQ'
1120
+ @chess = ChessMate.new(@board)
1121
+ end
1122
+ it 'should return true if the moving color is in check after the move' do
1123
+ expect(@chess.in_check_after_move?([7, 4], [7, 3])).to eql(true)
1124
+ end
1125
+
1126
+ it 'should return false if the moving color is not in check after the move' do
1127
+ expect(@chess.in_check_after_move?([7, 4], [7, 5])).to eql(false)
1128
+ end
1129
+
1130
+ it 'should not update the actual game board to test' do
1131
+ test_board = Array.new(8) { Array.new(8, nil) }
1132
+ test_board[7][4] = 'WK'
1133
+ test_board[0][4] = 'BK'
1134
+ test_board[0][3] = 'BQ'
1135
+ @chess.in_check_after_move?([7, 4], [7, 3])
1136
+ expect(test_board.object_id).to_not eql(@board.object_id)
1137
+ expect(test_board).to eql(@chess.board)
1138
+ end
1139
+
1140
+ it 'should return true if the checking piece is captured, but still results in check' do
1141
+ board = Array.new(8) { Array.new(8, nil) }
1142
+ board[7][4] = 'WK'
1143
+ board[6][4] = 'BQ'
1144
+ board[3][4] = 'BK'
1145
+ board[4][4] = 'BR'
1146
+ chess = ChessMate.new(board)
1147
+ expect(chess.in_check_after_move?([7, 4], [6, 4])).to eql(true)
1148
+ end
1149
+ end
1150
+
1151
+ describe 'valid_moves? method' do
1152
+ it 'should return true if there are valid moves' do
1153
+ board = Array.new(8) { Array.new(8, nil) }
1154
+ board[7][4] = 'WK'
1155
+ board[0][4] = 'BK'
1156
+ chess = ChessMate.new(board)
1157
+ expect(chess.any_valid_moves?('W')).to eql(true)
1158
+ end
1159
+
1160
+ it 'should return false if there are no valid moves' do
1161
+ board = Array.new(8) { Array.new(8, nil) }
1162
+ board[7][4] = 'WK'
1163
+ board[6][4] = 'BQ'
1164
+ board[3][4] = 'BK'
1165
+ board[4][4] = 'BR'
1166
+ chess = ChessMate.new(board)
1167
+ expect(chess.any_valid_moves?('W')).to eql(false)
1168
+ end
1169
+ end
1170
+
1171
+ describe 'checkmate? method' do
1172
+ it 'should return true if no valid moves remain and currently in check' do
1173
+ board = Array.new(8) { Array.new(8, nil) }
1174
+ board[7][4] = 'WK'
1175
+ board[6][4] = 'BQ'
1176
+ board[3][4] = 'BK'
1177
+ board[4][4] = 'BR'
1178
+ chess = ChessMate.new(board)
1179
+ expect(chess.checkmate?('W')).to eql(true)
1180
+ end
1181
+
1182
+ it 'should return false if there are valid moves' do
1183
+ chess = ChessMate.new
1184
+ expect(chess.checkmate?('W')).to eql(false)
1185
+ end
1186
+ end
1187
+
1188
+ describe 'draw? method' do
1189
+ it 'should return true if no valid moves remain and currently not in check' do
1190
+ board = Array.new(8) { Array.new(8, nil) }
1191
+ board[7][4] = 'WK'
1192
+ board[5][3] = 'BQ'
1193
+ board[5][4] = 'BK'
1194
+ board[4][5] = 'BR'
1195
+ chess = ChessMate.new(board)
1196
+ expect(chess.draw?('W')).to eql(true)
1197
+ end
1198
+
1199
+ it 'should return false if there are valid moves' do
1200
+ chess = ChessMate.new
1201
+ expect(chess.checkmate?('W')).to eql(false)
1202
+ end
1203
+
1204
+ it 'should return false if checkmated' do
1205
+ board = Array.new(8) { Array.new(8, nil) }
1206
+ board[7][4] = 'WK'
1207
+ board[6][3] = 'BQ'
1208
+ board[5][4] = 'BK'
1209
+ board[4][5] = 'BR'
1210
+ chess = ChessMate.new(board)
1211
+ expect(chess.draw?('W')).to eql(false)
1212
+ end
1213
+ end
1214
+
1215
+ describe 'promote? method' do
1216
+ it 'should return true if white piece is on last rank' do
1217
+ board = Array.new(8) { Array.new(8, nil) }
1218
+ board[0][0] = 'WP'
1219
+ chess = ChessMate.new(board)
1220
+ expect(chess.promote?([0, 0])).to eql(true)
1221
+ end
1222
+
1223
+ it 'should return true if black piece is on last rank' do
1224
+ board = Array.new(8) { Array.new(8, nil) }
1225
+ board[7][0] = 'BP'
1226
+ chess = ChessMate.new(board)
1227
+ expect(chess.promote?([7, 0])).to eql(true)
1228
+ end
1229
+
1230
+ it 'should return false otherwise' do
1231
+ board = Array.new(8) { Array.new(8, nil) }
1232
+ board[1][0] = 'WP'
1233
+ board[6][0] = 'BP'
1234
+ chess = ChessMate.new(board)
1235
+ expect(chess.promote?([1, 0])).to eql(false)
1236
+ expect(chess.promote?([6, 0])).to eql(false)
1237
+ end
1238
+ end
1239
+
1240
+ describe 'promote! method' do
1241
+ it 'should return nil if piece cannot promote' do
1242
+ board = Array.new(8) { Array.new(8, nil) }
1243
+ board[1][0] = 'WP'
1244
+ chess = ChessMate.new(board)
1245
+ expect(chess.promote!([1, 0], 'queen')).to eql(nil)
1246
+ end
1247
+
1248
+ it 'should return nil for invalid/junk promotion data' do
1249
+ board = Array.new(8) { Array.new(8, nil) }
1250
+ board[0][0] = 'WP'
1251
+ chess = ChessMate.new(board)
1252
+ expect(chess.promote!([0, 0], 'king')).to eql(nil)
1253
+ end
1254
+
1255
+ context 'should promote to' do
1256
+ before :each do
1257
+ board = Array.new(8) { Array.new(8, nil) }
1258
+ board[0][0] = 'WP'
1259
+ @chess = ChessMate.new(board)
1260
+ end
1261
+
1262
+ it 'queen' do
1263
+ @chess.promote!([0, 0], 'queen')
1264
+ expect(@chess.board[0][0]).to eql('WQ')
1265
+ end
1266
+
1267
+ it 'bishop' do
1268
+ @chess.promote!([0, 0], 'bishop')
1269
+ expect(@chess.board[0][0]).to eql('WB')
1270
+ end
1271
+
1272
+ it 'knight' do
1273
+ @chess.promote!([0, 0], 'knight')
1274
+ expect(@chess.board[0][0]).to eql('WN')
1275
+ end
1276
+
1277
+ it 'rook' do
1278
+ @chess.promote!([0, 0], 'rook')
1279
+ expect(@chess.board[0][0]).to eql('WR')
1280
+ end
1281
+ end
1282
+ end
1283
+
1284
+ describe 'en_passant method' do
1285
+ before :each do
1286
+ @chess = ChessMate.new
1287
+ end
1288
+
1289
+ context 'en passant not possible' do
1290
+ it 'should return nil for white' do
1291
+ @chess.move('a2', 'a3')
1292
+ expect(@chess.en_passant[:white]).to be_nil
1293
+ end
1294
+
1295
+ it 'should return nil for black' do
1296
+ @chess.move('a7', 'a6')
1297
+ expect(@chess.en_passant[:white]).to be_nil
1298
+ end
1299
+ end
1300
+
1301
+ context 'en passant possible' do
1302
+ it 'should return coords for white' do
1303
+ @chess.move('a2', 'a4')
1304
+ expect(@chess.en_passant[:white]).to eql([4, 0])
1305
+ end
1306
+
1307
+ it 'should return coords for black' do
1308
+ @chess.move('a7', 'a5')
1309
+ expect(@chess.en_passant[:black]).to eql([3, 0])
1310
+ end
1311
+ end
1312
+ end
1313
+
1314
+ describe 'castling method' do
1315
+ before :each do
1316
+ board = Array.new(8) { Array.new(8, nil) }
1317
+ board[7][4] = 'WK'
1318
+ board[7][0] = 'WR'
1319
+ board[7][7] = 'WR'
1320
+ board[0][4] = 'BK'
1321
+ board[0][0] = 'BR'
1322
+ board[0][7] = 'BR'
1323
+ @chess = ChessMate.new(board)
1324
+ end
1325
+ context 'new game' do
1326
+ it 'should return true for both king/queenside for both colors' do
1327
+ castling = @chess.castling
1328
+ castling.keys.each do |color|
1329
+ castling[color].keys.each do |direction|
1330
+ expect(castling[color][direction]).to eql(true)
1331
+ end
963
1332
  end
1333
+ end
1334
+ end
964
1335
 
965
- it "should return false if checkmated" do
966
- board = Array.new(8) { Array.new(8,nil) }
967
- board[7][4] = "WK"
968
- board[6][3] = "BQ"
969
- board[5][4] = "BK"
970
- board[4][5] = "BR"
971
- chess = ChessMate.new(board)
972
- expect(chess.draw?("W")).to eql(false)
1336
+ context 'pieces moved' do
1337
+ it 'should return false for both king/queenside if king moved' do
1338
+ @chess.move('e1', 'd1')
1339
+ @chess.move('e8', 'd8')
1340
+ castling = @chess.castling
1341
+ castling.keys.each do |color|
1342
+ castling[color].keys.each do |direction|
1343
+ expect(castling[color][direction]).to eql(false)
1344
+ end
973
1345
  end
1346
+ end
1347
+
1348
+ it 'should return false for either side if rook moved' do
1349
+ @chess.move('a8', 'b8')
1350
+ expect(@chess.castling[:black][:queenside]).to eql(false)
1351
+ expect(@chess.castling[:black][:kingside]).to eql(true)
1352
+ @chess.move('h1', 'g1')
1353
+ expect(@chess.castling[:white][:queenside]).to eql(true)
1354
+ expect(@chess.castling[:white][:kingside]).to eql(false)
1355
+ end
974
1356
  end
975
- end
1357
+ end
1358
+ end