chess_vwong 0.0.1 → 0.0.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/.DS_Store +0 -0
- data/README.md +27 -14
- data/Rakefile +3 -3
- data/bin/chess_vwong +10 -10
- data/chess_vwong-0.0.1.gem +0 -0
- data/chess_vwong.gemspec +12 -12
- data/example/example_game.rb +17 -17
- data/lib/chess_vwong.rb +13 -17
- data/lib/chess_vwong/bishop.rb +6 -9
- data/lib/chess_vwong/board.rb +151 -89
- data/lib/chess_vwong/game.rb +21 -16
- data/lib/chess_vwong/king.rb +4 -6
- data/lib/chess_vwong/knight.rb +4 -6
- data/lib/chess_vwong/node.rb +1 -1
- data/lib/chess_vwong/pawn.rb +23 -22
- data/lib/chess_vwong/piece.rb +13 -15
- data/lib/chess_vwong/player.rb +2 -2
- data/lib/chess_vwong/preload.rb +49 -48
- data/lib/chess_vwong/queen.rb +10 -13
- data/lib/chess_vwong/rook.rb +6 -9
- data/lib/chess_vwong/version.rb +1 -1
- data/spec/bishop_spec.rb +24 -27
- data/spec/board_spec.rb +227 -147
- data/spec/game_spec.rb +13 -15
- data/spec/king_spec.rb +24 -28
- data/spec/knight_spec.rb +31 -33
- data/spec/node_spec.rb +10 -11
- data/spec/pawn_spec.rb +54 -56
- data/spec/piece_spec.rb +23 -23
- data/spec/player_spec.rb +11 -13
- data/spec/queen_spec.rb +24 -27
- data/spec/rook_spec.rb +24 -27
- data/spec/spec_helper.rb +1 -1
- metadata +5 -3
data/lib/chess_vwong/game.rb
CHANGED
@@ -10,23 +10,20 @@ module ChessVwong
|
|
10
10
|
|
11
11
|
def switch_players
|
12
12
|
@current_player, @other_player = @other_player, @current_player
|
13
|
-
end
|
14
|
-
|
15
|
-
|
13
|
+
end
|
16
14
|
|
17
15
|
def game_over
|
18
16
|
return current_player if current_player.kill_list.last.instance_of?(King)
|
19
17
|
false
|
20
18
|
end
|
21
19
|
|
22
|
-
|
23
20
|
def play
|
24
21
|
board.preload_pieces
|
25
|
-
|
22
|
+
loop do
|
26
23
|
board.formatted_grid
|
27
|
-
puts
|
24
|
+
puts ''
|
28
25
|
solicit_get_piece
|
29
|
-
puts
|
26
|
+
puts ''
|
30
27
|
solicit_set_piece
|
31
28
|
if game_over
|
32
29
|
puts game_over_message
|
@@ -40,15 +37,27 @@ module ChessVwong
|
|
40
37
|
|
41
38
|
private
|
42
39
|
|
40
|
+
|
41
|
+
|
42
|
+
# def reset_doubled
|
43
|
+
# grid.each do |row|
|
44
|
+
# row.each do |node|
|
45
|
+
# if node.occupied.first.instance_of?(Pawn)
|
46
|
+
|
47
|
+
# end
|
48
|
+
# end
|
49
|
+
# end
|
50
|
+
# end
|
51
|
+
|
43
52
|
def solicit_get_piece
|
44
53
|
loop do
|
45
54
|
puts "#{current_player.name}, Select a piece:"
|
46
55
|
if board.get_piece(current_player)
|
47
56
|
break
|
48
57
|
else
|
49
|
-
|
58
|
+
puts 'Invalid Input!'
|
50
59
|
end
|
51
|
-
end
|
60
|
+
end
|
52
61
|
end
|
53
62
|
|
54
63
|
def solicit_set_piece
|
@@ -57,17 +66,13 @@ module ChessVwong
|
|
57
66
|
if board.set_piece(current_player)
|
58
67
|
break
|
59
68
|
else
|
60
|
-
|
69
|
+
puts 'Invalid Move!'
|
61
70
|
end
|
62
|
-
end
|
63
|
-
end
|
71
|
+
end
|
72
|
+
end
|
64
73
|
|
65
74
|
def game_over_message
|
66
75
|
"#{current_player.name} wins!"
|
67
76
|
end
|
68
|
-
|
69
|
-
|
70
77
|
end
|
71
78
|
end
|
72
|
-
|
73
|
-
|
data/lib/chess_vwong/king.rb
CHANGED
@@ -1,17 +1,15 @@
|
|
1
1
|
module ChessVwong
|
2
2
|
class King < Piece
|
3
|
-
|
4
3
|
def character
|
5
|
-
color ==
|
4
|
+
color == 'w' ? "\u{2654}" : "\u{265A}"
|
6
5
|
end
|
7
6
|
|
8
7
|
# Generate all possible Neighbouring Spaces
|
9
8
|
def generate_neighbours(current_space)
|
10
|
-
moves = [[
|
9
|
+
moves = [[1, 0], [-1, 0], [0, 1], [0, -1], [-1, 1], [1, -1], [1, 1], [-1, -1]]
|
11
10
|
moves.each do |move|
|
12
11
|
neigbour_helper(current_space, move[0], move[1])
|
13
12
|
end
|
14
|
-
end
|
15
|
-
|
13
|
+
end
|
16
14
|
end
|
17
|
-
end
|
15
|
+
end
|
data/lib/chess_vwong/knight.rb
CHANGED
@@ -1,17 +1,15 @@
|
|
1
1
|
module ChessVwong
|
2
2
|
class Knight < Piece
|
3
|
-
|
4
3
|
def character
|
5
|
-
color ==
|
4
|
+
color == 'w' ? "\u{2658}" : "\u{265E}"
|
6
5
|
end
|
7
6
|
|
8
7
|
# Generate all possible Neighbouring Spaces
|
9
8
|
def generate_neighbours(current_space)
|
10
|
-
moves = [[
|
9
|
+
moves = [[1, 2], [-1, 2], [1, -2], [-1, -2], [2, 1], [-2, 1], [2, -1], [-2, -1]]
|
11
10
|
moves.each do |move|
|
12
11
|
neigbour_helper(current_space, move[0], move[1])
|
13
12
|
end
|
14
|
-
end
|
15
|
-
|
13
|
+
end
|
16
14
|
end
|
17
|
-
end
|
15
|
+
end
|
data/lib/chess_vwong/node.rb
CHANGED
data/lib/chess_vwong/pawn.rb
CHANGED
@@ -1,44 +1,45 @@
|
|
1
1
|
module ChessVwong
|
2
2
|
class Pawn < Piece
|
3
|
-
|
3
|
+
attr_accessor :doubled
|
4
4
|
def character
|
5
|
-
color ==
|
5
|
+
color == 'w' ? "\u{2659}" : "\u{265F}"
|
6
6
|
end
|
7
7
|
|
8
8
|
# Generate all possible Neighbouring Spaces
|
9
|
-
def generate_neighbours(current_space, node1=nil, node2=nil)
|
10
|
-
color ==
|
9
|
+
def generate_neighbours(current_space, node1 = nil, node2 = nil)
|
10
|
+
color == 'w' ? moves = [[0, -1]] : moves = [[0, 1]]
|
11
11
|
first_turn?(current_space, moves)
|
12
|
-
kill_move(node1, node2, moves)
|
12
|
+
kill_move(node1, node2, moves)
|
13
13
|
moves.each do |move|
|
14
14
|
neigbour_helper(current_space, move[0], move[1])
|
15
15
|
end
|
16
|
-
end
|
16
|
+
end
|
17
17
|
|
18
18
|
# if there is a enemy piece nearby, then give option to attack
|
19
19
|
def kill_move(node1, node2, moves)
|
20
|
-
if color ==
|
21
|
-
moves << [-1
|
22
|
-
moves << [1
|
20
|
+
if color == 'w'
|
21
|
+
moves << [-1, -1] if kill_conditions(node1, 'b') || ep_kill.first == "L"
|
22
|
+
moves << [1, -1] if kill_conditions(node2, 'b') || ep_kill.first == "R"
|
23
23
|
else
|
24
|
-
moves << [-1,1] if node1
|
25
|
-
moves << [1,1] if
|
24
|
+
moves << [-1, 1] if kill_conditions(node1, 'w') || ep_kill.first == "L"
|
25
|
+
moves << [1, 1] if kill_conditions(node1, 'w') || ep_kill.first == "R"
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
+
private
|
29
30
|
|
30
|
-
|
31
|
+
def kill_conditions(node, color)
|
32
|
+
node && !node.occupied.empty? && node.occupied.first.color == color
|
33
|
+
end
|
31
34
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
35
|
+
def first_turn?(current_space, moves)
|
36
|
+
if current_space[1] == 2 # Black
|
37
|
+
moves << [0, 2]
|
38
|
+
doubled = true
|
39
|
+
elsif current_space[1] == 7 # White
|
40
|
+
moves << [0, -2]
|
41
|
+
doubled = true
|
38
42
|
end
|
39
|
-
|
43
|
+
end
|
40
44
|
end
|
41
45
|
end
|
42
|
-
|
43
|
-
|
44
|
-
|
data/lib/chess_vwong/piece.rb
CHANGED
@@ -1,34 +1,32 @@
|
|
1
1
|
module ChessVwong
|
2
2
|
class Piece
|
3
3
|
attr_reader :color
|
4
|
-
attr_accessor :neighbours, :current_space
|
4
|
+
attr_accessor :neighbours, :current_space, :turns, :ep_kill
|
5
5
|
def initialize(current_space, color)
|
6
6
|
@current_space = current_space
|
7
7
|
@color = color
|
8
|
-
@
|
8
|
+
@turns = 0
|
9
|
+
@neighbours = []
|
10
|
+
@ep_kill = []
|
9
11
|
end
|
10
12
|
|
11
13
|
# Ensuring the pieces can't go off board
|
12
14
|
def valid_space?(space)
|
13
|
-
true if space.all? {|coordinate| coordinate >= 1 && coordinate <9}
|
15
|
+
true if space.all? { |coordinate| coordinate >= 1 && coordinate < 9 }
|
14
16
|
end
|
15
17
|
|
16
18
|
def character
|
17
|
-
|
19
|
+
color
|
18
20
|
end
|
19
21
|
|
20
22
|
private
|
21
23
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
24
|
+
# Create 1 new Neighbouring space
|
25
|
+
def neigbour_helper(current_space, x, y)
|
26
|
+
new_x = current_space[0] + x
|
27
|
+
new_y = current_space[1] + y
|
28
|
+
neighbour = [new_x, new_y]
|
29
|
+
neighbours << neighbour if valid_space?(neighbour)
|
30
|
+
end
|
32
31
|
end
|
33
32
|
end
|
34
|
-
|
data/lib/chess_vwong/player.rb
CHANGED
@@ -2,11 +2,11 @@ module ChessVwong
|
|
2
2
|
class Player
|
3
3
|
attr_reader :name, :color
|
4
4
|
attr_accessor :kill_list, :chosen_piece
|
5
|
-
def initialize(name, color=
|
5
|
+
def initialize(name, color = 'w')
|
6
6
|
@name = name
|
7
7
|
@color = color
|
8
8
|
@kill_list = []
|
9
9
|
@chosen_piece = []
|
10
10
|
end
|
11
11
|
end
|
12
|
-
end
|
12
|
+
end
|
data/lib/chess_vwong/preload.rb
CHANGED
@@ -1,65 +1,66 @@
|
|
1
1
|
module ChessVwong
|
2
|
-
class Board
|
2
|
+
class Board
|
3
3
|
# private
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
# White on the Bottom, Black on Top
|
5
|
+
def preload_pieces
|
6
|
+
white_pieces = [Rook.new([1, 8], 'w'), Knight.new([2, 8], 'w'), Bishop.new([3, 8], 'w'), Queen.new([4, 8], 'w'),
|
7
|
+
King.new([5, 8], 'w'), Bishop.new([6, 8], 'w'), Knight.new([7, 8], 'w'), Rook.new([8, 8], 'w')]
|
8
8
|
|
9
|
-
|
10
|
-
|
9
|
+
black_pieces = [Rook.new([1, 1], 'b'), Knight.new([2, 1], 'b'), Bishop.new([3, 1], 'b'), Queen.new([4, 1], 'b'),
|
10
|
+
King.new([5, 1], 'b'), Bishop.new([6, 1], 'b'), Knight.new([7, 1], 'b'), Rook.new([8, 1], 'b')]
|
11
11
|
|
12
|
+
# Load Coord-Displays
|
13
|
+
load_alphabet(grid[0])
|
14
|
+
load_num_coord
|
12
15
|
|
16
|
+
|
13
17
|
|
14
18
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
#Black Pieces
|
25
|
-
load_back_pieces(grid[8], white_pieces)
|
19
|
+
# Black Pawns
|
20
|
+
load_pawns(grid[2], 'b')
|
21
|
+
# # White Pawns
|
22
|
+
load_pawns(grid[7], 'w')
|
23
|
+
# # White Pieces
|
24
|
+
load_back_pieces(grid[1], black_pieces)
|
25
|
+
# # Black Pieces
|
26
|
+
load_back_pieces(grid[8], white_pieces)
|
27
|
+
end
|
26
28
|
|
27
|
-
|
28
|
-
private
|
29
|
-
def load_pawns(row, color)
|
30
|
-
i = 0
|
31
|
-
row.each do |node|
|
32
|
-
node.occupied << Pawn.new([i,1],color)
|
33
|
-
i += 1
|
34
|
-
end
|
35
|
-
end
|
29
|
+
private
|
36
30
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
i += 1
|
43
|
-
end
|
31
|
+
def load_pawns(row, color)
|
32
|
+
i = 0
|
33
|
+
row.each do |node|
|
34
|
+
node.occupied << Pawn.new([i, 1], color)
|
35
|
+
i += 1
|
44
36
|
end
|
37
|
+
end
|
45
38
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
j += 1
|
53
|
-
end
|
39
|
+
def load_alphabet(row)
|
40
|
+
alphabet = ('A'..'H').to_a
|
41
|
+
i = 0
|
42
|
+
while i < 8
|
43
|
+
row[i + 1].occupied << Piece.new([0, 0], alphabet[i])
|
44
|
+
i += 1
|
54
45
|
end
|
46
|
+
end
|
55
47
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
48
|
+
def load_num_coord
|
49
|
+
i = 8
|
50
|
+
j = 1
|
51
|
+
while i > 0
|
52
|
+
grid[j][0].occupied << Piece.new([0, 0], i)
|
53
|
+
i -= 1
|
54
|
+
j += 1
|
62
55
|
end
|
56
|
+
end
|
63
57
|
|
58
|
+
def load_back_pieces(row, pieces)
|
59
|
+
i = 0
|
60
|
+
while i < 8
|
61
|
+
row[i + 1].occupied << pieces[i]
|
62
|
+
i += 1
|
63
|
+
end
|
64
|
+
end
|
64
65
|
end
|
65
66
|
end
|
data/lib/chess_vwong/queen.rb
CHANGED
@@ -1,28 +1,25 @@
|
|
1
1
|
module ChessVwong
|
2
2
|
class Queen < Piece
|
3
|
-
|
4
3
|
def character
|
5
|
-
color ==
|
4
|
+
color == 'w' ? "\u{2655}" : "\u{265B}"
|
6
5
|
end
|
7
6
|
|
8
7
|
# Generate all possible Neighbouring Nodes
|
9
8
|
def generate_neighbours(current_space)
|
10
9
|
moves = []
|
11
10
|
# Diaganol
|
12
|
-
(1..8).each {|i| moves << [i, i]}
|
13
|
-
(1..8).each {|i| moves << [i, -i]}
|
14
|
-
(1..8).each {|i| moves << [-i, i]}
|
15
|
-
(1..8).each {|i| moves << [-i, -i]}
|
11
|
+
(1..8).each { |i| moves << [i, i] }
|
12
|
+
(1..8).each { |i| moves << [i, -i] }
|
13
|
+
(1..8).each { |i| moves << [-i, i] }
|
14
|
+
(1..8).each { |i| moves << [-i, -i] }
|
16
15
|
# Straight
|
17
|
-
(1..8).each {|i| moves << [i, 0]}
|
18
|
-
(1..8).each {|i| moves << [0, i]}
|
19
|
-
(1..8).each {|i| moves << [-i, 0]}
|
20
|
-
(1..8).each {|i| moves << [0, -i]}
|
16
|
+
(1..8).each { |i| moves << [i, 0] }
|
17
|
+
(1..8).each { |i| moves << [0, i] }
|
18
|
+
(1..8).each { |i| moves << [-i, 0] }
|
19
|
+
(1..8).each { |i| moves << [0, -i] }
|
21
20
|
moves.each do |move|
|
22
21
|
neigbour_helper(current_space, move[0], move[1])
|
23
22
|
end
|
24
|
-
end
|
25
|
-
|
23
|
+
end
|
26
24
|
end
|
27
25
|
end
|
28
|
-
|
data/lib/chess_vwong/rook.rb
CHANGED
@@ -1,22 +1,19 @@
|
|
1
1
|
module ChessVwong
|
2
2
|
class Rook < Piece
|
3
|
-
|
4
3
|
def character
|
5
|
-
color ==
|
4
|
+
color == 'w' ? "\u{2656}" : "\u{265C}"
|
6
5
|
end
|
7
6
|
|
8
7
|
# Generate all possible Neighbouring Nodes
|
9
8
|
def generate_neighbours(current_space)
|
10
9
|
moves = []
|
11
|
-
(1..8).each {|i| moves << [i, 0]}
|
12
|
-
(1..8).each {|i| moves << [0, i]}
|
13
|
-
(1..8).each {|i| moves << [-i, 0]}
|
14
|
-
(1..8).each {|i| moves << [0, -i]}
|
10
|
+
(1..8).each { |i| moves << [i, 0] }
|
11
|
+
(1..8).each { |i| moves << [0, i] }
|
12
|
+
(1..8).each { |i| moves << [-i, 0] }
|
13
|
+
(1..8).each { |i| moves << [0, -i] }
|
15
14
|
moves.each do |move|
|
16
15
|
neigbour_helper(current_space, move[0], move[1])
|
17
16
|
end
|
18
|
-
end
|
19
|
-
|
17
|
+
end
|
20
18
|
end
|
21
19
|
end
|
22
|
-
|