chessmate 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/chessmate.rb +284 -225
- data/lib/helpers/notation_parser.rb +18 -20
- data/lib/pieces/bishop.rb +9 -8
- data/lib/pieces/king.rb +44 -6
- data/lib/pieces/knight.rb +9 -8
- data/lib/pieces/pawn.rb +54 -31
- data/lib/pieces/piece.rb +68 -75
- data/lib/pieces/queen.rb +12 -13
- data/lib/pieces/rook.rb +11 -12
- data/spec/chessmate_spec.rb +1306 -923
- data/spec/notation_parser_spec.rb +45 -44
- data/spec/piece_spec.rb +83 -81
- data/spec/spec_helper.rb +53 -53
- metadata +16 -2
@@ -1,44 +1,45 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require_relative '../lib/helpers/notation_parser'
|
5
|
+
|
6
|
+
describe 'NotationParser' do
|
7
|
+
LTRS = {
|
8
|
+
'a' => 0,
|
9
|
+
'b' => 1,
|
10
|
+
'c' => 2,
|
11
|
+
'd' => 3,
|
12
|
+
'e' => 4,
|
13
|
+
'f' => 5,
|
14
|
+
'g' => 6,
|
15
|
+
'h' => 7
|
16
|
+
}.freeze
|
17
|
+
|
18
|
+
it 'should parse chess notation to an array of coords' do
|
19
|
+
10.times do
|
20
|
+
ltr = 'abcdefgh'.chars.sample
|
21
|
+
num = rand(1..8).to_s
|
22
|
+
notation = ltr + num
|
23
|
+
expect(NotationParser.parse_notation(notation)).to eql(
|
24
|
+
[
|
25
|
+
7 - (num.to_i - 1),
|
26
|
+
LTRS[ltr]
|
27
|
+
]
|
28
|
+
)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'encode_notation method' do
|
33
|
+
it 'should encode an array of coords as chess notation' do
|
34
|
+
10.times do
|
35
|
+
y = rand(0..7)
|
36
|
+
x = rand(0..7)
|
37
|
+
coords = [y, x]
|
38
|
+
ltr = LTRS.key(x)
|
39
|
+
num = (8 - y).to_s
|
40
|
+
notation = ltr + num
|
41
|
+
expect(NotationParser.encode_notation(coords)).to eql(notation)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/spec/piece_spec.rb
CHANGED
@@ -1,96 +1,98 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
require_relative '../lib/pieces/piece'
|
3
5
|
|
4
6
|
describe Piece do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
7
|
+
describe 'obstructed? method' do
|
8
|
+
it 'should return false if not obstructed vertically' do
|
9
|
+
board = Array.new(8) { Array.new(8, nil) }
|
10
|
+
board[7][3] = 'WQ'
|
11
|
+
piece = Piece.new
|
12
|
+
expect(piece.class.obstructed?([7, 3], [0, 3], board)).to eql(false)
|
13
|
+
end
|
12
14
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
it 'should return false if not obstructed horizontally' do
|
16
|
+
board = Array.new(8) { Array.new(8, nil) }
|
17
|
+
board[7][3] = 'WQ'
|
18
|
+
piece = Piece.new
|
19
|
+
expect(piece.class.obstructed?([7, 3], [7, 7], board)).to eql(false)
|
20
|
+
end
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
it 'should return false if not obstructed diagonally' do
|
23
|
+
board = Array.new(8) { Array.new(8, nil) }
|
24
|
+
board[7][3] = 'WQ'
|
25
|
+
piece = Piece.new
|
26
|
+
expect(piece.class.obstructed?([7, 3], [4, 0], board)).to eql(false)
|
27
|
+
end
|
26
28
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
29
|
+
it 'should return true if obstructed vertically' do
|
30
|
+
board = Array.new(8) { Array.new(8, nil) }
|
31
|
+
board[7][3] = 'WQ'
|
32
|
+
board[6][3] = 'WP'
|
33
|
+
piece = Piece.new
|
34
|
+
expect(piece.class.obstructed?([7, 3], [0, 3], board)).to eql(true)
|
35
|
+
end
|
34
36
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
37
|
+
it 'should return true if obstructed horizontally' do
|
38
|
+
board = Array.new(8) { Array.new(8, nil) }
|
39
|
+
board[7][3] = 'WQ'
|
40
|
+
board[7][4] = 'WK'
|
41
|
+
piece = Piece.new
|
42
|
+
expect(piece.class.obstructed?([7, 3], [7, 7], board)).to eql(true)
|
43
|
+
end
|
42
44
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
45
|
+
it 'should return true if obstructed diagonally' do
|
46
|
+
board = Array.new(8) { Array.new(8, nil) }
|
47
|
+
board[7][3] = 'WQ'
|
48
|
+
board[5][1] = 'WP'
|
49
|
+
piece = Piece.new
|
50
|
+
expect(piece.class.obstructed?([7, 3], [4, 0], board)).to eql(true)
|
51
|
+
end
|
50
52
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
53
|
+
it 'should ignore the origin and end positions' do
|
54
|
+
board = Array.new(8) { Array.new(8, nil) }
|
55
|
+
board[7][3] = 'WQ'
|
56
|
+
board[7][7] = 'WR'
|
57
|
+
piece = Piece.new
|
58
|
+
expect(piece.class.obstructed?([7, 3], [7, 7], board)).to eql(false)
|
57
59
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
60
|
+
board = Array.new(8) { Array.new(8, nil) }
|
61
|
+
board[7][3] = 'WQ'
|
62
|
+
board[0][3] = 'BQ'
|
63
|
+
piece = Piece.new
|
64
|
+
expect(piece.class.obstructed?([7, 3], [0, 3], board)).to eql(false)
|
63
65
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
66
|
+
board = Array.new(8) { Array.new(8, nil) }
|
67
|
+
board[7][3] = 'WQ'
|
68
|
+
board[4][0] = 'WP'
|
69
|
+
piece = Piece.new
|
70
|
+
expect(piece.class.obstructed?([7, 3], [4, 0], board)).to eql(false)
|
71
|
+
end
|
72
|
+
end
|
71
73
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
board[4][4] =
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
74
|
+
describe 'capturable? method' do
|
75
|
+
it 'should return true if pieces are of opposite colors' do
|
76
|
+
board = Array.new(8) { Array.new(8, nil) }
|
77
|
+
board[5][5] = 'WP'
|
78
|
+
board[4][4] = 'BP'
|
79
|
+
piece = Piece.new
|
80
|
+
expect(piece.class.capturable?([5, 5], [4, 4], board)).to eql(true)
|
81
|
+
end
|
82
|
+
end
|
81
83
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
84
|
+
describe 'destination_occupied? method' do
|
85
|
+
it 'should return true if a piece occupies the destination' do
|
86
|
+
board = Array.new(8) { Array.new(8, nil) }
|
87
|
+
board[5][5] = 'WP'
|
88
|
+
piece = Piece.new
|
89
|
+
expect(piece.class.destination_occupied?([5, 5], board)).to eql(true)
|
90
|
+
end
|
89
91
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
end
|
92
|
+
it 'should return false if a piece does not occupy the destination' do
|
93
|
+
board = Array.new(8) { Array.new(8, nil) }
|
94
|
+
piece = Piece.new
|
95
|
+
expect(piece.class.destination_occupied?([5, 5], board)).to eql(false)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
4
|
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
5
|
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
@@ -45,57 +47,55 @@ RSpec.configure do |config|
|
|
45
47
|
# triggering implicit auto-inclusion in groups with matching metadata.
|
46
48
|
config.shared_context_metadata_behavior = :apply_to_host_groups
|
47
49
|
|
48
|
-
# The settings below are suggested to provide a good initial experience
|
49
|
-
# with RSpec, but feel free to customize to your heart's content.
|
50
|
-
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
54
|
-
#
|
55
|
-
#
|
56
|
-
|
57
|
-
|
58
|
-
#
|
59
|
-
#
|
60
|
-
#
|
61
|
-
|
62
|
-
|
63
|
-
#
|
64
|
-
#
|
65
|
-
# - http://
|
66
|
-
# - http://
|
67
|
-
#
|
68
|
-
|
69
|
-
|
70
|
-
#
|
71
|
-
#
|
72
|
-
|
73
|
-
|
74
|
-
#
|
75
|
-
#
|
76
|
-
#
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
#
|
85
|
-
#
|
86
|
-
#
|
87
|
-
|
88
|
-
|
89
|
-
#
|
90
|
-
#
|
91
|
-
#
|
92
|
-
#
|
93
|
-
|
94
|
-
|
95
|
-
#
|
96
|
-
#
|
97
|
-
#
|
98
|
-
#
|
99
|
-
Kernel.srand config.seed
|
100
|
-
=end
|
50
|
+
# The settings below are suggested to provide a good initial experience
|
51
|
+
# with RSpec, but feel free to customize to your heart's content.
|
52
|
+
# # This allows you to limit a spec run to individual examples or groups
|
53
|
+
# # you care about by tagging them with `:focus` metadata. When nothing
|
54
|
+
# # is tagged with `:focus`, all examples get run. RSpec also provides
|
55
|
+
# # aliases for `it`, `describe`, and `context` that include `:focus`
|
56
|
+
# # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
|
57
|
+
# config.filter_run_when_matching :focus
|
58
|
+
#
|
59
|
+
# # Allows RSpec to persist some state between runs in order to support
|
60
|
+
# # the `--only-failures` and `--next-failure` CLI options. We recommend
|
61
|
+
# # you configure your source control system to ignore this file.
|
62
|
+
# config.example_status_persistence_file_path = "spec/examples.txt"
|
63
|
+
#
|
64
|
+
# # Limits the available syntax to the non-monkey patched syntax that is
|
65
|
+
# # recommended. For more details, see:
|
66
|
+
# # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
67
|
+
# # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
68
|
+
# # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
69
|
+
# config.disable_monkey_patching!
|
70
|
+
#
|
71
|
+
# # This setting enables warnings. It's recommended, but in some cases may
|
72
|
+
# # be too noisy due to issues in dependencies.
|
73
|
+
# config.warnings = true
|
74
|
+
#
|
75
|
+
# # Many RSpec users commonly either run the entire suite or an individual
|
76
|
+
# # file, and it's useful to allow more verbose output when running an
|
77
|
+
# # individual spec file.
|
78
|
+
# if config.files_to_run.one?
|
79
|
+
# # Use the documentation formatter for detailed output,
|
80
|
+
# # unless a formatter has already been configured
|
81
|
+
# # (e.g. via a command-line flag).
|
82
|
+
# config.default_formatter = "doc"
|
83
|
+
# end
|
84
|
+
#
|
85
|
+
# # Print the 10 slowest examples and example groups at the
|
86
|
+
# # end of the spec run, to help surface which specs are running
|
87
|
+
# # particularly slow.
|
88
|
+
# config.profile_examples = 10
|
89
|
+
#
|
90
|
+
# # Run specs in random order to surface order dependencies. If you find an
|
91
|
+
# # order dependency and want to debug it, you can fix the order by providing
|
92
|
+
# # the seed, which is printed after each run.
|
93
|
+
# # --seed 1234
|
94
|
+
# config.order = :random
|
95
|
+
#
|
96
|
+
# # Seed global randomization in this process using the `--seed` CLI option.
|
97
|
+
# # Setting this allows you to use `--seed` to deterministically reproduce
|
98
|
+
# # test failures related to randomization by passing the same `--seed` value
|
99
|
+
# # as the one that triggered the failure.
|
100
|
+
# Kernel.srand config.seed
|
101
101
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chessmate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tyler Porter
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-10-
|
11
|
+
date: 2019-10-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rubocop
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
description: A simple chess move validator
|
28
42
|
email: tyler.b.porter@gmail.com
|
29
43
|
executables: []
|