ari_chess 1.1.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/ari_chess/error.rb +2 -4
- data/lib/ari_chess/game.rb +6 -5
- data/lib/ari_chess/named_pieces.rb +6 -164
- data/lib/ari_chess/pieces/bishop.rb +15 -0
- data/lib/ari_chess/pieces/king.rb +19 -0
- data/lib/ari_chess/pieces/knight.rb +19 -0
- data/lib/ari_chess/pieces/pawn.rb +87 -0
- data/lib/ari_chess/pieces/queen.rb +19 -0
- data/lib/ari_chess/pieces/rook.rb +15 -0
- data/lib/ari_chess/{piece.rb → pieces/super/piece.rb} +0 -25
- data/lib/ari_chess/pieces/super/sliding_piece.rb +15 -0
- data/lib/ari_chess/pieces/super/stepping_piece.rb +8 -0
- metadata +11 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0e68a6f8b5a6e22e6c5db0dbf2078bea34e702fa
|
4
|
+
data.tar.gz: a316bdf89347ba6ee4403660c46a260ca68ca0d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ecf272f68effb6ad157d595d99a420e62b593cb9ef3e0e1cd456a0e4f9cd21281280adef0519c8bd3467dc883f0d3bf039432663663a686be17cbaaf18ddd949
|
7
|
+
data.tar.gz: b7e2358fa830ed775d6f373b8f010c6849cfe51bb5e88564427b3c0584560cf0e98ab22f1a183c7abb1fea1b8566b982db4295543aec32b0e36b40b2f652859c
|
data/lib/ari_chess/error.rb
CHANGED
data/lib/ari_chess/game.rb
CHANGED
@@ -32,8 +32,9 @@ class Game
|
|
32
32
|
def play_turn
|
33
33
|
board.move(*get_move)
|
34
34
|
rescue InvalidMoveError => e
|
35
|
-
|
36
|
-
|
35
|
+
print "\n"
|
36
|
+
puts e.message
|
37
|
+
retry
|
37
38
|
end
|
38
39
|
|
39
40
|
def get_move
|
@@ -41,7 +42,6 @@ class Game
|
|
41
42
|
parse_positions(input)
|
42
43
|
end
|
43
44
|
|
44
|
-
|
45
45
|
def parse_positions(input)
|
46
46
|
matched_input = /\s*(\w\d),*\s*(\w\d)\s*/.match(input)
|
47
47
|
raise InvalidMoveError if matched_input.nil?
|
@@ -62,7 +62,7 @@ class Game
|
|
62
62
|
error_message = "Invalid move! Chosen piece is not yours."
|
63
63
|
end
|
64
64
|
|
65
|
-
raise InvalidMoveError.new
|
65
|
+
raise InvalidMoveError.new, error_message unless error_message.nil?
|
66
66
|
|
67
67
|
nil
|
68
68
|
end
|
@@ -76,10 +76,11 @@ class Game
|
|
76
76
|
|
77
77
|
def parse(move)
|
78
78
|
raise InvalidMoveError if move.length != 2
|
79
|
+
|
79
80
|
col, row = move.split("")
|
80
81
|
row = to_i(row)
|
81
82
|
unless move_in_range?(row, col)
|
82
|
-
raise InvalidMoveError
|
83
|
+
raise InvalidMoveError.new, "Invalid move! Not a valid board position."
|
83
84
|
end
|
84
85
|
|
85
86
|
[(-1 * row) + 8, col.ord - 97]
|
@@ -1,164 +1,6 @@
|
|
1
|
-
require_relative '
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
]
|
8
|
-
|
9
|
-
STEP_DELTA = [
|
10
|
-
[1, 0] #anytime
|
11
|
-
]
|
12
|
-
|
13
|
-
DOUBLE_STEP_DELTA = [
|
14
|
-
[2, 0], #first move
|
15
|
-
]
|
16
|
-
|
17
|
-
attr_accessor :first_move
|
18
|
-
|
19
|
-
def initialize(pos, board, color)
|
20
|
-
super
|
21
|
-
@first_move = true
|
22
|
-
end
|
23
|
-
|
24
|
-
def first_move?
|
25
|
-
@first_move
|
26
|
-
end
|
27
|
-
|
28
|
-
def dup(new_board)
|
29
|
-
copy = super
|
30
|
-
copy.first_move = first_move
|
31
|
-
|
32
|
-
copy
|
33
|
-
end
|
34
|
-
|
35
|
-
def moves
|
36
|
-
possible_moves = []
|
37
|
-
|
38
|
-
possible_moves.concat(capturable_positions)
|
39
|
-
|
40
|
-
step_pos = new_pos(deltas(STEP_DELTA).first)
|
41
|
-
|
42
|
-
if board[step_pos].nil?
|
43
|
-
possible_moves << step_pos
|
44
|
-
else
|
45
|
-
return possible_moves
|
46
|
-
end
|
47
|
-
|
48
|
-
if first_move?
|
49
|
-
possible_moves << double_step_pos unless double_step_pos.empty?
|
50
|
-
end
|
51
|
-
|
52
|
-
possible_moves
|
53
|
-
end
|
54
|
-
|
55
|
-
def double_step_pos
|
56
|
-
position = new_pos(deltas(DOUBLE_STEP_DELTA).first)
|
57
|
-
return position if board[position].nil?
|
58
|
-
|
59
|
-
[]
|
60
|
-
end
|
61
|
-
|
62
|
-
def capturable_positions
|
63
|
-
deltas(CAPTURE_DELTAS).map { |delta| new_pos(delta) }.select do |position|
|
64
|
-
!board[position].nil? && board[position].color != color
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
def new_pos(delta)
|
69
|
-
x, y = pos
|
70
|
-
dx, dy = delta
|
71
|
-
|
72
|
-
[x + dx, y + dy]
|
73
|
-
end
|
74
|
-
|
75
|
-
def deltas(specific_deltas)
|
76
|
-
color == :B ? specific_deltas : specific_deltas.map { |dx, dy| [dx * -1, dy] }
|
77
|
-
end
|
78
|
-
|
79
|
-
def update_piece(pos)
|
80
|
-
super
|
81
|
-
self.first_move = false
|
82
|
-
end
|
83
|
-
|
84
|
-
def to_s
|
85
|
-
color == :W ? "\u2659" : "\u265F"
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
class Knight < SteppingPiece
|
90
|
-
DELTAS = [
|
91
|
-
[-2, -1],
|
92
|
-
[-2, 1],
|
93
|
-
[-1, -2],
|
94
|
-
[-1, 2],
|
95
|
-
[ 1, -2],
|
96
|
-
[ 1, 2],
|
97
|
-
[ 2, -1],
|
98
|
-
[ 2, 1]
|
99
|
-
]
|
100
|
-
|
101
|
-
def to_s
|
102
|
-
color == :W ? "\u2658" : "\u265E"
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
class King < SteppingPiece
|
107
|
-
DELTAS = [
|
108
|
-
[ 1, 1],
|
109
|
-
[ 1, 0],
|
110
|
-
[ 1, -1],
|
111
|
-
[ 0, -1],
|
112
|
-
[-1, -1],
|
113
|
-
[-1, 0],
|
114
|
-
[-1, 1],
|
115
|
-
[ 0, 1],
|
116
|
-
]
|
117
|
-
|
118
|
-
def to_s
|
119
|
-
color == :W ? "\u2654" : "\u265A"
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
123
|
-
class Bishop < SlidingPiece
|
124
|
-
DELTAS = [
|
125
|
-
[ 1, 1],
|
126
|
-
[ 1, -1],
|
127
|
-
[-1, -1],
|
128
|
-
[-1, 1],
|
129
|
-
]
|
130
|
-
|
131
|
-
def to_s
|
132
|
-
color == :W ? "\u2657" : "\u265D"
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
class Rook < SlidingPiece
|
137
|
-
DELTAS = [
|
138
|
-
[ 1, 0],
|
139
|
-
[ 0, -1],
|
140
|
-
[-1, 0],
|
141
|
-
[ 0, 1],
|
142
|
-
]
|
143
|
-
|
144
|
-
def to_s
|
145
|
-
color == :W ? "\u2656" : "\u265C"
|
146
|
-
end
|
147
|
-
end
|
148
|
-
|
149
|
-
class Queen < SlidingPiece
|
150
|
-
DELTAS = [
|
151
|
-
[ 1, 1],
|
152
|
-
[ 1, 0],
|
153
|
-
[ 1, -1],
|
154
|
-
[ 0, -1],
|
155
|
-
[-1, -1],
|
156
|
-
[-1, 0],
|
157
|
-
[-1, 1],
|
158
|
-
[ 0, 1],
|
159
|
-
]
|
160
|
-
|
161
|
-
def to_s
|
162
|
-
color == :W ? "\u2655" : "\u265B"
|
163
|
-
end
|
164
|
-
end
|
1
|
+
require_relative 'pieces/pawn'
|
2
|
+
require_relative 'pieces/bishop'
|
3
|
+
require_relative 'pieces/king'
|
4
|
+
require_relative 'pieces/knight'
|
5
|
+
require_relative 'pieces/queen'
|
6
|
+
require_relative 'pieces/rook'
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require_relative 'super/piece'
|
2
|
+
require_relative 'super/stepping_piece'
|
3
|
+
|
4
|
+
class King < SteppingPiece
|
5
|
+
DELTAS = [
|
6
|
+
[ 1, 1],
|
7
|
+
[ 1, 0],
|
8
|
+
[ 1, -1],
|
9
|
+
[ 0, -1],
|
10
|
+
[-1, -1],
|
11
|
+
[-1, 0],
|
12
|
+
[-1, 1],
|
13
|
+
[ 0, 1],
|
14
|
+
]
|
15
|
+
|
16
|
+
def to_s
|
17
|
+
color == :W ? "\u2654" : "\u265A"
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require_relative 'super/piece'
|
2
|
+
require_relative 'super/stepping_piece'
|
3
|
+
|
4
|
+
class Knight < SteppingPiece
|
5
|
+
DELTAS = [
|
6
|
+
[-2, -1],
|
7
|
+
[-2, 1],
|
8
|
+
[-1, -2],
|
9
|
+
[-1, 2],
|
10
|
+
[ 1, -2],
|
11
|
+
[ 1, 2],
|
12
|
+
[ 2, -1],
|
13
|
+
[ 2, 1]
|
14
|
+
]
|
15
|
+
|
16
|
+
def to_s
|
17
|
+
color == :W ? "\u2658" : "\u265E"
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require_relative 'super/piece'
|
2
|
+
|
3
|
+
class Pawn < Piece
|
4
|
+
CAPTURE_DELTAS = [
|
5
|
+
[1, -1], #capture
|
6
|
+
[1, 1], #capture
|
7
|
+
]
|
8
|
+
|
9
|
+
STEP_DELTA = [
|
10
|
+
[1, 0] #anytime
|
11
|
+
]
|
12
|
+
|
13
|
+
DOUBLE_STEP_DELTA = [
|
14
|
+
[2, 0], #first move
|
15
|
+
]
|
16
|
+
|
17
|
+
attr_accessor :first_move
|
18
|
+
|
19
|
+
def initialize(pos, board, color)
|
20
|
+
super
|
21
|
+
@first_move = true
|
22
|
+
end
|
23
|
+
|
24
|
+
def first_move?
|
25
|
+
@first_move
|
26
|
+
end
|
27
|
+
|
28
|
+
def dup(new_board)
|
29
|
+
copy = super
|
30
|
+
copy.first_move = first_move
|
31
|
+
|
32
|
+
copy
|
33
|
+
end
|
34
|
+
|
35
|
+
def moves
|
36
|
+
possible_moves = []
|
37
|
+
|
38
|
+
possible_moves.concat(capturable_positions)
|
39
|
+
|
40
|
+
step_pos = new_pos(deltas(STEP_DELTA).first)
|
41
|
+
|
42
|
+
if board[step_pos].nil?
|
43
|
+
possible_moves << step_pos
|
44
|
+
else
|
45
|
+
return possible_moves
|
46
|
+
end
|
47
|
+
|
48
|
+
if first_move?
|
49
|
+
possible_moves << double_step_pos unless double_step_pos.empty?
|
50
|
+
end
|
51
|
+
|
52
|
+
possible_moves
|
53
|
+
end
|
54
|
+
|
55
|
+
def double_step_pos
|
56
|
+
position = new_pos(deltas(DOUBLE_STEP_DELTA).first)
|
57
|
+
return position if board[position].nil?
|
58
|
+
|
59
|
+
[]
|
60
|
+
end
|
61
|
+
|
62
|
+
def capturable_positions
|
63
|
+
deltas(CAPTURE_DELTAS).map { |delta| new_pos(delta) }.select do |position|
|
64
|
+
!board[position].nil? && board[position].color != color
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def new_pos(delta)
|
69
|
+
x, y = pos
|
70
|
+
dx, dy = delta
|
71
|
+
|
72
|
+
[x + dx, y + dy]
|
73
|
+
end
|
74
|
+
|
75
|
+
def deltas(specific_deltas)
|
76
|
+
color == :B ? specific_deltas : specific_deltas.map { |dx, dy| [dx * -1, dy] }
|
77
|
+
end
|
78
|
+
|
79
|
+
def update_piece(pos)
|
80
|
+
super
|
81
|
+
self.first_move = false
|
82
|
+
end
|
83
|
+
|
84
|
+
def to_s
|
85
|
+
color == :W ? "\u2659" : "\u265F"
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require_relative 'super/piece'
|
2
|
+
require_relative 'super/sliding_piece'
|
3
|
+
|
4
|
+
class Queen < SlidingPiece
|
5
|
+
DELTAS = [
|
6
|
+
[ 1, 1],
|
7
|
+
[ 1, 0],
|
8
|
+
[ 1, -1],
|
9
|
+
[ 0, -1],
|
10
|
+
[-1, -1],
|
11
|
+
[-1, 0],
|
12
|
+
[-1, 1],
|
13
|
+
[ 0, 1],
|
14
|
+
]
|
15
|
+
|
16
|
+
def to_s
|
17
|
+
color == :W ? "\u2655" : "\u265B"
|
18
|
+
end
|
19
|
+
end
|
@@ -51,28 +51,3 @@ class Piece
|
|
51
51
|
self.class.new(pos, new_board, color)
|
52
52
|
end
|
53
53
|
end
|
54
|
-
|
55
|
-
class SlidingPiece < Piece
|
56
|
-
def potential_moves(delta)
|
57
|
-
moves = []
|
58
|
-
captured_piece = false
|
59
|
-
current_pos = pos
|
60
|
-
|
61
|
-
until captured_piece || !valid_pos?(move = next_pos(current_pos, delta))
|
62
|
-
moves << move
|
63
|
-
captured_piece = true if !board[move].nil? && board[move].color != color
|
64
|
-
current_pos = move
|
65
|
-
end
|
66
|
-
|
67
|
-
moves
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
class SteppingPiece < Piece
|
72
|
-
def potential_moves(delta)
|
73
|
-
move = next_pos(pos, delta)
|
74
|
-
return [move] if valid_pos?(move)
|
75
|
-
|
76
|
-
[]
|
77
|
-
end
|
78
|
-
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class SlidingPiece < Piece
|
2
|
+
def potential_moves(delta)
|
3
|
+
moves = []
|
4
|
+
captured_piece = false
|
5
|
+
current_pos = pos
|
6
|
+
|
7
|
+
until captured_piece || !valid_pos?(move = next_pos(current_pos, delta))
|
8
|
+
moves << move
|
9
|
+
captured_piece = true if !board[move].nil? && board[move].color != color
|
10
|
+
current_pos = move
|
11
|
+
end
|
12
|
+
|
13
|
+
moves
|
14
|
+
end
|
15
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ari_chess
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ari Borensztein
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: colorize
|
@@ -35,7 +35,15 @@ files:
|
|
35
35
|
- lib/ari_chess/error.rb
|
36
36
|
- lib/ari_chess/game.rb
|
37
37
|
- lib/ari_chess/named_pieces.rb
|
38
|
-
- lib/ari_chess/
|
38
|
+
- lib/ari_chess/pieces/bishop.rb
|
39
|
+
- lib/ari_chess/pieces/king.rb
|
40
|
+
- lib/ari_chess/pieces/knight.rb
|
41
|
+
- lib/ari_chess/pieces/pawn.rb
|
42
|
+
- lib/ari_chess/pieces/queen.rb
|
43
|
+
- lib/ari_chess/pieces/rook.rb
|
44
|
+
- lib/ari_chess/pieces/super/piece.rb
|
45
|
+
- lib/ari_chess/pieces/super/sliding_piece.rb
|
46
|
+
- lib/ari_chess/pieces/super/stepping_piece.rb
|
39
47
|
homepage: http://rubygems.org/gems/ari_chess
|
40
48
|
licenses:
|
41
49
|
- MIT
|