rb_chess 0.0.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.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +68 -0
- data/bin/rb_chess +6 -0
- data/lib/rb_chess/board.rb +171 -0
- data/lib/rb_chess/castling_rights.rb +31 -0
- data/lib/rb_chess/cli/cli.rb +237 -0
- data/lib/rb_chess/cli/game_saver.rb +33 -0
- data/lib/rb_chess/cli/players.rb +20 -0
- data/lib/rb_chess/errors.rb +5 -0
- data/lib/rb_chess/fen_parser.rb +150 -0
- data/lib/rb_chess/game.rb +166 -0
- data/lib/rb_chess/game_modules/move_generator.rb +154 -0
- data/lib/rb_chess/game_modules/move_parser.rb +43 -0
- data/lib/rb_chess/game_modules/move_validator.rb +47 -0
- data/lib/rb_chess/letter_display.rb +42 -0
- data/lib/rb_chess/move.rb +33 -0
- data/lib/rb_chess/move_set.rb +18 -0
- data/lib/rb_chess/pieces/bishop.rb +24 -0
- data/lib/rb_chess/pieces/king.rb +26 -0
- data/lib/rb_chess/pieces/knight.rb +25 -0
- data/lib/rb_chess/pieces/pawn.rb +45 -0
- data/lib/rb_chess/pieces/piece.rb +19 -0
- data/lib/rb_chess/pieces/piece_constants.rb +26 -0
- data/lib/rb_chess/pieces/queen.rb +25 -0
- data/lib/rb_chess/pieces/rook.rb +24 -0
- data/lib/rb_chess/position.rb +72 -0
- data/lib/rb_chess.rb +3 -0
- metadata +70 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../move_set'
|
|
4
|
+
require_relative 'piece'
|
|
5
|
+
|
|
6
|
+
# A class to represent a knight in a chess game
|
|
7
|
+
module RbChess
|
|
8
|
+
class Knight < Piece
|
|
9
|
+
attr_reader :move_sets
|
|
10
|
+
|
|
11
|
+
def initialize(color, position)
|
|
12
|
+
super
|
|
13
|
+
increments = [{ y: 2, x: 1 }, { y: 2, x: -1 }, { y: 1, x: 2 },
|
|
14
|
+
{ y: 1, x: -2 }, { y: -2, x: 1 }, { y: -2, x: -1 },
|
|
15
|
+
{ y: -1, x: 2 }, { y: -1, x: -2 }]
|
|
16
|
+
@move_sets = [
|
|
17
|
+
MoveSet.new(
|
|
18
|
+
increments: increments,
|
|
19
|
+
repeat: 1,
|
|
20
|
+
blocked_by: [:same]
|
|
21
|
+
)
|
|
22
|
+
]
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../move_set'
|
|
4
|
+
require_relative 'piece'
|
|
5
|
+
|
|
6
|
+
# A class to reperesnt a chess pawn
|
|
7
|
+
module RbChess
|
|
8
|
+
class Pawn < Piece
|
|
9
|
+
attr_reader :move_sets, :direction
|
|
10
|
+
|
|
11
|
+
def initialize(color, position)
|
|
12
|
+
super
|
|
13
|
+
|
|
14
|
+
# where the pawn is facing -1 for up, 1 for down
|
|
15
|
+
@direction = color == :white ? -1 : 1
|
|
16
|
+
repeat = position.starting_pawn_rank?(color) ? 2 : 1
|
|
17
|
+
@move_sets = [
|
|
18
|
+
# for normal move
|
|
19
|
+
MoveSet.new(
|
|
20
|
+
increments: [{ y: direction, x: 0 }],
|
|
21
|
+
repeat: repeat,
|
|
22
|
+
blocked_by: [:piece],
|
|
23
|
+
special_moves: %i[en_passant],
|
|
24
|
+
promotable: true
|
|
25
|
+
),
|
|
26
|
+
# for capture
|
|
27
|
+
MoveSet.new(
|
|
28
|
+
repeat: 1,
|
|
29
|
+
increments: [{ y: direction, x: 1 }, { y: direction, x: -1 }],
|
|
30
|
+
blocked_by: %i[same empty],
|
|
31
|
+
promotable: true
|
|
32
|
+
)
|
|
33
|
+
]
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def can_promote?(pos)
|
|
37
|
+
rank = color == :white ? 0 : 7
|
|
38
|
+
pos.y == rank
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def promotion_pieces
|
|
42
|
+
%w[n r q b]
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# A super class for the chess pieces
|
|
4
|
+
module RbChess
|
|
5
|
+
class Piece
|
|
6
|
+
attr_reader :color, :position
|
|
7
|
+
attr_accessor :has_moved
|
|
8
|
+
|
|
9
|
+
def initialize(color, position)
|
|
10
|
+
@color = color
|
|
11
|
+
@has_moved = false
|
|
12
|
+
@position = position
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def update_position(new_pos)
|
|
16
|
+
self.class.new(color, new_pos)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'require_all'
|
|
4
|
+
require_rel './'
|
|
5
|
+
|
|
6
|
+
module RbChess
|
|
7
|
+
module PieceConstants
|
|
8
|
+
LETTER_TO_PIECE_CLASS = {
|
|
9
|
+
p: Pawn,
|
|
10
|
+
k: King,
|
|
11
|
+
q: Queen,
|
|
12
|
+
r: Rook,
|
|
13
|
+
b: Bishop,
|
|
14
|
+
n: Knight
|
|
15
|
+
}.freeze
|
|
16
|
+
|
|
17
|
+
PIECE_CLASS_TO_LETTER = {
|
|
18
|
+
Pawn: 'p',
|
|
19
|
+
Rook: 'r',
|
|
20
|
+
Knight: 'n',
|
|
21
|
+
Bishop: 'b',
|
|
22
|
+
Queen: 'q',
|
|
23
|
+
King: 'k'
|
|
24
|
+
}.freeze
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../move_set'
|
|
4
|
+
require_relative 'piece'
|
|
5
|
+
|
|
6
|
+
# A class to represent a queen in a chess game
|
|
7
|
+
module RbChess
|
|
8
|
+
class Queen < Piece
|
|
9
|
+
attr_reader :move_sets
|
|
10
|
+
|
|
11
|
+
def initialize(color, position)
|
|
12
|
+
super
|
|
13
|
+
increments = [{ y: 1, x: 1 }, { y: -1, x: 1 }, { y: -1, x: -1 },
|
|
14
|
+
{ y: 1, x: -1 }, { y: 1, x: 0 }, { y: -1, x: 0 },
|
|
15
|
+
{ y: 0, x: 1 }, { y: 0, x: -1 }]
|
|
16
|
+
@move_sets = [
|
|
17
|
+
MoveSet.new(
|
|
18
|
+
increments: increments,
|
|
19
|
+
repeat: Float::INFINITY,
|
|
20
|
+
blocked_by: [:same]
|
|
21
|
+
)
|
|
22
|
+
]
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../move_set'
|
|
4
|
+
require_relative 'piece'
|
|
5
|
+
|
|
6
|
+
# A class to represent a rook in a chess game
|
|
7
|
+
module RbChess
|
|
8
|
+
class Rook < Piece
|
|
9
|
+
attr_reader :move_sets
|
|
10
|
+
|
|
11
|
+
def initialize(color, position)
|
|
12
|
+
super
|
|
13
|
+
increments = [{ y: 1, x: 0 }, { y: -1, x: 0 }, { y: 0, x: 1 },
|
|
14
|
+
{ y: 0, x: -1 }]
|
|
15
|
+
@move_sets = [
|
|
16
|
+
MoveSet.new(
|
|
17
|
+
increments: increments,
|
|
18
|
+
repeat: Float::INFINITY,
|
|
19
|
+
blocked_by: [:same]
|
|
20
|
+
)
|
|
21
|
+
]
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative './errors'
|
|
4
|
+
|
|
5
|
+
# a class to represent a position in a chess game
|
|
6
|
+
module RbChess
|
|
7
|
+
class Position
|
|
8
|
+
attr_reader :y, :x
|
|
9
|
+
|
|
10
|
+
def self.parse(string)
|
|
11
|
+
raise ChessError, 'invalid position' unless /^[a-h][1-8]$/.match string
|
|
12
|
+
|
|
13
|
+
x = string[0].ord - 97
|
|
14
|
+
y = 8 - string[1].to_i
|
|
15
|
+
Position.new(y: y, x: x)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def initialize(y:, x:)
|
|
19
|
+
@y = y
|
|
20
|
+
@x = x
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def to_s
|
|
24
|
+
"#{(97 + x).chr}#{8 - y}"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def ==(other)
|
|
28
|
+
return false unless other.is_a? Position
|
|
29
|
+
|
|
30
|
+
x == other.x && y == other.y
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def increment(y: 0, x: 0)
|
|
34
|
+
Position.new(y: self.y + y, x: self.x + x)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def square_type
|
|
38
|
+
(y + x).even? ? :light : :dark
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def in_bounds?
|
|
42
|
+
y.between?(0, 7) && x.between?(0, 7)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def out_of_bounds?
|
|
46
|
+
!in_bounds?
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def starting_pawn_rank?(color)
|
|
50
|
+
(color == :white && y == 6) || (color == :black && y == 1)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def en_passant_rank?(color)
|
|
54
|
+
(color == :white && y == 4) || (color == :black && y == 3)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def king_pos?(color)
|
|
58
|
+
y = color == :black ? 0 : 7
|
|
59
|
+
self.y == y && x == 4
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def kingside_rook_pos?(color)
|
|
63
|
+
y = color == :black ? 0 : 7
|
|
64
|
+
self.y == y && x == 7
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def queenside_rook_pos?(color)
|
|
68
|
+
y = color == :black ? 0 : 7
|
|
69
|
+
self.y == y && x.zero?
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
data/lib/rb_chess.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rb_chess
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Peter Abah
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2022-03-17 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description:
|
|
14
|
+
email: peterabah.ob@gmail.com
|
|
15
|
+
executables:
|
|
16
|
+
- rb_chess
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- LICENSE
|
|
21
|
+
- README.md
|
|
22
|
+
- bin/rb_chess
|
|
23
|
+
- lib/rb_chess.rb
|
|
24
|
+
- lib/rb_chess/board.rb
|
|
25
|
+
- lib/rb_chess/castling_rights.rb
|
|
26
|
+
- lib/rb_chess/cli/cli.rb
|
|
27
|
+
- lib/rb_chess/cli/game_saver.rb
|
|
28
|
+
- lib/rb_chess/cli/players.rb
|
|
29
|
+
- lib/rb_chess/errors.rb
|
|
30
|
+
- lib/rb_chess/fen_parser.rb
|
|
31
|
+
- lib/rb_chess/game.rb
|
|
32
|
+
- lib/rb_chess/game_modules/move_generator.rb
|
|
33
|
+
- lib/rb_chess/game_modules/move_parser.rb
|
|
34
|
+
- lib/rb_chess/game_modules/move_validator.rb
|
|
35
|
+
- lib/rb_chess/letter_display.rb
|
|
36
|
+
- lib/rb_chess/move.rb
|
|
37
|
+
- lib/rb_chess/move_set.rb
|
|
38
|
+
- lib/rb_chess/pieces/bishop.rb
|
|
39
|
+
- lib/rb_chess/pieces/king.rb
|
|
40
|
+
- lib/rb_chess/pieces/knight.rb
|
|
41
|
+
- lib/rb_chess/pieces/pawn.rb
|
|
42
|
+
- lib/rb_chess/pieces/piece.rb
|
|
43
|
+
- lib/rb_chess/pieces/piece_constants.rb
|
|
44
|
+
- lib/rb_chess/pieces/queen.rb
|
|
45
|
+
- lib/rb_chess/pieces/rook.rb
|
|
46
|
+
- lib/rb_chess/position.rb
|
|
47
|
+
homepage: https://github.com/peter-abah/rb_chess
|
|
48
|
+
licenses:
|
|
49
|
+
- MIT
|
|
50
|
+
metadata: {}
|
|
51
|
+
post_install_message:
|
|
52
|
+
rdoc_options: []
|
|
53
|
+
require_paths:
|
|
54
|
+
- lib
|
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
56
|
+
requirements:
|
|
57
|
+
- - ">="
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: '0'
|
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
|
+
requirements:
|
|
62
|
+
- - ">="
|
|
63
|
+
- !ruby/object:Gem::Version
|
|
64
|
+
version: '0'
|
|
65
|
+
requirements: []
|
|
66
|
+
rubygems_version: 3.1.6
|
|
67
|
+
signing_key:
|
|
68
|
+
specification_version: 4
|
|
69
|
+
summary: A chess library written in ruby
|
|
70
|
+
test_files: []
|