chess_vwong 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- 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
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb647ae34dfaa1eaa314c5ad0cf0701d0f0168c4
|
4
|
+
data.tar.gz: 0228a61d3ca95cf530ee2a18e5d349f6a6af5c87
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c902df6af2f44d4c80cee1bb94876c874a0d11fa17e218e29efaf04d7acc8cab86cfbf60b940da679f68ac64d50c5748b5a4347e5ec2c7f0564e90fe4eee0a2f
|
7
|
+
data.tar.gz: 91fdd57b4579d629b0f4caa6e0114830e843dc0b74226ea638ead07a51d3b09385f7d83f1e5e29d004d2d33b084f486f0c38400501ffbde5b6ec9c03f7206f53
|
data/.DS_Store
ADDED
Binary file
|
data/README.md
CHANGED
@@ -1,35 +1,42 @@
|
|
1
1
|
# Chess
|
2
2
|
|
3
|
-
A barebones version of command-line Chess for 2 players.
|
3
|
+
A barebones version of command-line Chess for 2 players.
|
4
|
+
|
5
|
+
Although currently missing the feature that declares Check, it is completely playable and includes all special moves(castling, en passant, promotion, and a pawn's double step on its first move).
|
4
6
|
|
5
7
|
TODO:
|
6
|
-
1.
|
7
|
-
2.
|
8
|
+
1. Declare Check/Mate
|
9
|
+
2. En-Passant(first-turn) bug
|
8
10
|
|
9
11
|
##Gameplay
|
10
|
-
The game is played on a standard
|
12
|
+
The game is played on a standard 8x8 board as shown below.
|
11
13
|
|
12
14
|
A B C D E F G H
|
13
|
-
8
|
14
|
-
7
|
15
|
+
8 ♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜
|
16
|
+
7 ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
|
15
17
|
6 _ _ _ _ _ _ _ _
|
16
18
|
5 _ _ _ _ _ _ _ _
|
17
19
|
4 _ _ _ _ _ _ _ _
|
18
20
|
3 _ _ _ _ _ _ _ _
|
19
|
-
2
|
20
|
-
1
|
21
|
+
2 ♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙
|
22
|
+
1 ♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖
|
21
23
|
|
22
24
|
A user will first be prompted to select a chess piece belonging to his/her color. Once a the chess piece has been chosen, he/she will then be prompted to select a space to move that chess piece.
|
23
25
|
|
24
|
-
|
25
|
-
|
26
|
-
* H7
|
27
|
-
|
28
|
-
(Lowercases are also accepted)
|
26
|
+
###Basic Commands
|
27
|
+
To enter a command, simply type in the X and Y coordinate without any spaces or commas seperating the two. Eg. A2, H7 or B6 (Lowercases are also accepted).
|
29
28
|
|
30
29
|
If a command is invalid, the use will be alerted and prompted again until a valid command is entered.
|
31
30
|
|
31
|
+
###Special moves
|
32
|
+
|
33
|
+
All special moves in a chess game (castling, en passant, promotion, and a pawn's double step on its first move) are recognized and allowed during game play.
|
34
|
+
|
35
|
+
Note:
|
32
36
|
|
37
|
+
i) To initialize Castling, select Rook THEN select King. Restricted by Check not applied yet.
|
38
|
+
|
39
|
+
ii) Restriction of En-Passant as first turn since double-stepped not applied yet.
|
33
40
|
|
34
41
|
## Installation
|
35
42
|
|
@@ -39,7 +46,13 @@ To install:
|
|
39
46
|
|
40
47
|
## Usage
|
41
48
|
|
42
|
-
|
49
|
+
Once gem has successfully installed, simply type:
|
50
|
+
|
51
|
+
$ chess_vwong
|
52
|
+
|
53
|
+
Or clone the repo locally and run:
|
54
|
+
|
55
|
+
$ rake play
|
43
56
|
|
44
57
|
## Contributing
|
45
58
|
|
data/Rakefile
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
desc
|
1
|
+
desc 'Play Chess'
|
2
2
|
task :play do
|
3
|
-
ruby
|
4
|
-
end
|
3
|
+
ruby '-w bin/chess_vwong'
|
4
|
+
end
|
data/bin/chess_vwong
CHANGED
@@ -1,21 +1,21 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
require 'chess_vwong'
|
3
3
|
|
4
|
-
puts
|
5
|
-
puts
|
4
|
+
puts 'Welcome to Chess'
|
5
|
+
puts 'Player 1, please Enter your name:'
|
6
6
|
name_1 = gets.chomp
|
7
|
-
puts
|
7
|
+
puts 'Player 2, please Enter your name:'
|
8
8
|
name_2 = gets.chomp
|
9
|
-
puts
|
9
|
+
puts ''
|
10
10
|
|
11
|
-
puts
|
11
|
+
puts '---------------------------'
|
12
12
|
puts "#{name_1} will be White"
|
13
13
|
puts "#{name_2} will be Black"
|
14
|
-
puts
|
15
|
-
puts
|
14
|
+
puts '---------------------------'
|
15
|
+
puts ''
|
16
16
|
puts "Let's begin!"
|
17
17
|
|
18
|
-
bob = ChessVwong::Player.new(name_1,
|
19
|
-
peter = ChessVwong::Player.new(name_2,
|
18
|
+
bob = ChessVwong::Player.new(name_1, 'w')
|
19
|
+
peter = ChessVwong::Player.new(name_2, 'b')
|
20
20
|
players = [bob, peter]
|
21
|
-
ChessVwong::Game.new(players).play
|
21
|
+
ChessVwong::Game.new(players).play
|
Binary file
|
data/chess_vwong.gemspec
CHANGED
@@ -4,21 +4,21 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'chess_vwong/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'chess_vwong'
|
8
8
|
spec.version = ChessVwong::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
11
|
-
spec.summary =
|
12
|
-
spec.description =
|
13
|
-
spec.homepage =
|
14
|
-
spec.license =
|
9
|
+
spec.authors = ['Vincent Wong']
|
10
|
+
spec.email = ['wingyu64@gmail.com']
|
11
|
+
spec.summary = '2-player CLI Chess'
|
12
|
+
spec.description = '2-player CLI Chess'
|
13
|
+
spec.homepage = 'https://github.com/wingyu/chess_vwong'
|
14
|
+
spec.license = 'MIT'
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0")
|
17
|
-
spec.executables =
|
17
|
+
spec.executables = 'chess_vwong' # spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
-
spec.require_paths = [
|
19
|
+
spec.require_paths = ['lib']
|
20
20
|
|
21
|
-
spec.add_development_dependency
|
22
|
-
spec.add_development_dependency
|
23
|
-
spec.add_development_dependency
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
22
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
23
|
+
spec.add_development_dependency 'rspec'
|
24
24
|
end
|
data/example/example_game.rb
CHANGED
@@ -1,20 +1,20 @@
|
|
1
|
-
require_relative
|
2
|
-
|
3
|
-
puts "Welcome to Chess"
|
4
|
-
puts "Player 1, please Enter your name:"
|
5
|
-
name_1 = gets.chomp
|
6
|
-
puts "Player 2, please Enter your name:"
|
7
|
-
name_2 = gets.chomp
|
8
|
-
puts ""
|
1
|
+
require_relative '../lib/chess_vwong.rb'
|
9
2
|
|
10
|
-
puts
|
11
|
-
puts "
|
12
|
-
|
13
|
-
puts "
|
14
|
-
|
15
|
-
puts "
|
3
|
+
puts 'Welcome to Chess'
|
4
|
+
# puts "Player 1, please Enter your name:"
|
5
|
+
# name_1 = gets.chomp
|
6
|
+
# puts "Player 2, please Enter your name:"
|
7
|
+
# name_2 = gets.chomp
|
8
|
+
# puts ""
|
16
9
|
|
17
|
-
|
18
|
-
|
10
|
+
# puts "---------------------------"
|
11
|
+
# puts "#{name_1} will be White"
|
12
|
+
# puts "#{name_2} will be Black"
|
13
|
+
# puts "---------------------------"
|
14
|
+
# puts ""
|
15
|
+
# puts "Let's begin!"
|
16
|
+
|
17
|
+
bob = ChessVwong::Player.new('WHITE', 'w')
|
18
|
+
peter = ChessVwong::Player.new('BLACK', 'b')
|
19
19
|
players = [bob, peter]
|
20
|
-
ChessVwong::Game.new(players).play
|
20
|
+
ChessVwong::Game.new(players).play
|
data/lib/chess_vwong.rb
CHANGED
@@ -1,23 +1,19 @@
|
|
1
|
-
|
1
|
+
require 'chess_vwong/version'
|
2
2
|
|
3
3
|
module ChessVwong
|
4
4
|
# Your code goes here...
|
5
5
|
end
|
6
6
|
|
7
|
+
require_relative './chess_vwong/node.rb'
|
8
|
+
require_relative './chess_vwong/player.rb'
|
9
|
+
require_relative './chess_vwong/piece.rb'
|
10
|
+
require_relative './chess_vwong/knight.rb'
|
11
|
+
require_relative './chess_vwong/rook.rb'
|
12
|
+
require_relative './chess_vwong/king.rb'
|
13
|
+
require_relative './chess_vwong/bishop.rb'
|
14
|
+
require_relative './chess_vwong/queen.rb'
|
15
|
+
require_relative './chess_vwong/pawn.rb'
|
16
|
+
require_relative './chess_vwong/board.rb'
|
17
|
+
require_relative './chess_vwong/preload.rb'
|
18
|
+
require_relative './chess_vwong/game.rb'
|
7
19
|
|
8
|
-
require_relative "./chess_vwong/node.rb"
|
9
|
-
require_relative "./chess_vwong/player.rb"
|
10
|
-
require_relative "./chess_vwong/piece.rb"
|
11
|
-
require_relative "./chess_vwong/knight.rb"
|
12
|
-
require_relative "./chess_vwong/rook.rb"
|
13
|
-
require_relative "./chess_vwong/king.rb"
|
14
|
-
require_relative "./chess_vwong/bishop.rb"
|
15
|
-
require_relative "./chess_vwong/queen.rb"
|
16
|
-
require_relative "./chess_vwong/pawn.rb"
|
17
|
-
require_relative "./chess_vwong/board.rb"
|
18
|
-
require_relative "./chess_vwong/preload.rb"
|
19
|
-
require_relative "./chess_vwong/game.rb"
|
20
|
-
|
21
|
-
# board = ChessVwong::Board.new
|
22
|
-
# board.preload_pieces
|
23
|
-
# board.formatted_grid
|
data/lib/chess_vwong/bishop.rb
CHANGED
@@ -1,22 +1,19 @@
|
|
1
1
|
module ChessVwong
|
2
2
|
class Bishop < Piece
|
3
|
-
|
4
3
|
def character
|
5
|
-
color ==
|
4
|
+
color == 'w' ? "\u{2657}" : "\u{265D}"
|
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, 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]}
|
10
|
+
(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] }
|
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
|
-
|
data/lib/chess_vwong/board.rb
CHANGED
@@ -7,86 +7,75 @@ module ChessVwong
|
|
7
7
|
@grid = default_grid
|
8
8
|
end
|
9
9
|
|
10
|
-
# Convert letter/number input into grid coordinates, if improper coordinates: empty array is returned
|
10
|
+
# Convert letter/number input into grid coordinates, if improper coordinates: empty array is returned (bandaid fix) so loop continues
|
11
11
|
def process_input(input)
|
12
12
|
if input[0] =~ /[A-Za-z]/
|
13
|
-
grid_values = input.tr!(
|
14
|
-
grid_values = input.upcase.tr!(
|
15
|
-
grid_values = grid_values.split(
|
16
|
-
grid_values.map!
|
13
|
+
grid_values = input.tr!('87654321', '12345678')
|
14
|
+
grid_values = input.upcase.tr!('ABCDEFGH', '12345678')
|
15
|
+
grid_values = grid_values.split('')
|
16
|
+
grid_values.map!(&:to_i)
|
17
17
|
else
|
18
18
|
return []
|
19
|
-
end
|
19
|
+
end
|
20
20
|
end
|
21
21
|
|
22
22
|
# Get piece & generate neighbours, extra helper-methods required for Pawns due to unique nature
|
23
|
-
def get_piece(input=gets.chomp, player)
|
23
|
+
def get_piece(input = gets.chomp, player)
|
24
24
|
@get_value = process_input(input)
|
25
|
-
if get_value.count ==2
|
25
|
+
if get_value.count == 2
|
26
26
|
chosen_node = grid[get_value[1]][get_value[0]]
|
27
27
|
unless chosen_node.occupied.empty? || chosen_node.occupied.first.color != player.color
|
28
|
-
@chosen_piece = chosen_node.occupied.pop
|
29
|
-
chosen_piece
|
30
|
-
return
|
31
|
-
end
|
28
|
+
@chosen_piece = chosen_node.occupied.pop
|
29
|
+
pawn_moves_helper(chosen_piece) #if pawn selected, check if unique moves apply
|
30
|
+
return chosen_piece
|
31
|
+
end
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
|
-
|
36
|
-
|
35
|
+
|
36
|
+
|
37
|
+
# Sets piece if within neighbour range & valid, also checks for unique moves
|
38
|
+
def set_piece(input = gets.chomp, player)
|
37
39
|
value = process_input(input)
|
38
|
-
if value.count ==2
|
40
|
+
if value.count == 2
|
39
41
|
chosen_node = grid[value[1]][value[0]]
|
42
|
+
#Castling
|
43
|
+
return apply_castling(chosen_piece) if chosen_piece.instance_of?(Rook) && chosen_node.occupied.first.instance_of?(King) && clear_path?(get_value, value)
|
44
|
+
#En Passant
|
45
|
+
return en_pass_kill(chosen_piece, player, chosen_node) if chosen_piece.instance_of?(Pawn) && chosen_node.occupied.empty? && !chosen_piece.ep_kill.empty?
|
46
|
+
#Standard & Promotion
|
40
47
|
if chosen_piece.neighbours.include?(value) && valid_path?(@get_value, value)
|
41
|
-
chosen_piece
|
42
|
-
chosen_node.occupied << chosen_piece
|
43
|
-
player.kill_list << chosen_node.occupied.shift() if chosen_node.occupied.count > 1
|
48
|
+
standard_move(chosen_piece, chosen_node, value, player)
|
44
49
|
pawn_to_queen(chosen_piece, chosen_node) if chosen_piece.instance_of?(Pawn)
|
45
50
|
return chosen_node.occupied
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
# Print's out Board
|
51
|
-
def formatted_grid
|
52
|
-
grid.each do |row|
|
53
|
-
puts row.map { |node| node.occupied.empty? ? "_" : node.occupied.first.character}.join(" ")
|
51
|
+
end
|
54
52
|
end
|
55
53
|
end
|
56
54
|
|
57
|
-
|
58
|
-
|
59
|
-
#Checks if path is valid
|
55
|
+
# Checks if path is valid
|
60
56
|
def valid_path?(start, dest)
|
61
|
-
#if End node is empty OR end_node is occupied with enemy_piece then check if path is clear
|
62
|
-
if grid[dest[1]][dest[0]].occupied.empty? || !grid[dest[1]][dest[0]].occupied.empty? && grid[dest[1]][dest[0]].occupied.first.color != chosen_piece.color
|
57
|
+
# if End node is empty OR end_node is occupied with enemy_piece then check if path is clear
|
58
|
+
if grid[dest[1]][dest[0]].occupied.empty? || !grid[dest[1]][dest[0]].occupied.empty? && grid[dest[1]][dest[0]].occupied.first.color != chosen_piece.color
|
63
59
|
chosen_piece.instance_of?(Knight) ? true : clear_path?(start, dest)
|
64
60
|
else
|
65
61
|
false
|
66
62
|
end
|
67
63
|
end
|
68
64
|
|
69
|
-
|
70
|
-
def scan_for_check
|
71
|
-
# execute it for selected piece that was most recently moves AND every king move
|
72
|
-
end
|
73
|
-
|
74
|
-
#Checks if path is clear [x,y] = grid[y][x]
|
65
|
+
# Checks if path is clear. [x,y] = grid[y][x]
|
75
66
|
def clear_path?(start, dest)
|
76
67
|
if (start[0] < dest[0] && start[1] > dest[1]) || (start[0] > dest[0] && start[1] < dest[1])
|
77
|
-
rising_path(start, dest)
|
68
|
+
rising_path(start, dest)
|
78
69
|
elsif (start[0] < dest[0] && start[1] < dest[1]) || (start[0] > dest[0] && start[1] > dest[1])
|
79
|
-
falling_path(start, dest)
|
70
|
+
falling_path(start, dest)
|
80
71
|
elsif start[1] != dest[1]
|
81
72
|
vertical_path(start, dest)
|
82
73
|
elsif start[0] != dest[0]
|
83
74
|
horizontal_path(start, dest)
|
84
|
-
end
|
85
|
-
|
75
|
+
end
|
76
|
+
process_path
|
86
77
|
end
|
87
78
|
|
88
|
-
|
89
|
-
|
90
79
|
def vertical_path(start, dest)
|
91
80
|
@node_path = []
|
92
81
|
# DOWN
|
@@ -96,7 +85,7 @@ module ChessVwong
|
|
96
85
|
end
|
97
86
|
# UP
|
98
87
|
elsif start[1] > dest[1]
|
99
|
-
start[1].step(dest[1]+1, -1) do |i|
|
88
|
+
start[1].step(dest[1] + 1, -1) do |i|
|
100
89
|
node_path << grid[i][start[0]]
|
101
90
|
end
|
102
91
|
end
|
@@ -112,7 +101,7 @@ module ChessVwong
|
|
112
101
|
end
|
113
102
|
# LEFT
|
114
103
|
elsif start[0] > dest[0]
|
115
|
-
start[0].step(dest[0]+1, -1) do |i|
|
104
|
+
start[0].step(dest[0] + 1, -1) do |i|
|
116
105
|
node_path << grid[start[1]][i]
|
117
106
|
end
|
118
107
|
end
|
@@ -122,21 +111,21 @@ module ChessVwong
|
|
122
111
|
def rising_path(start, dest)
|
123
112
|
@node_path = []
|
124
113
|
i = 0
|
125
|
-
j = start[0]
|
126
|
-
#UP_RIGHT
|
114
|
+
j = start[0]
|
115
|
+
# UP_RIGHT
|
127
116
|
if start[0] < dest[0] && start[1] > dest[1]
|
128
117
|
while j < dest[0]
|
129
|
-
node_path << grid[start[1] - i][start[0]+i]
|
118
|
+
node_path << grid[start[1] - i][start[0] + i]
|
130
119
|
i += 1
|
131
120
|
j += 1
|
132
|
-
end
|
133
|
-
#DOWN_LEFT
|
121
|
+
end
|
122
|
+
# DOWN_LEFT
|
134
123
|
elsif start[0] > dest[0] && start[1] < dest[1]
|
135
124
|
while j > dest[0]
|
136
|
-
node_path << grid[start[1] + i][start[0]-i]
|
125
|
+
node_path << grid[start[1] + i][start[0] - i]
|
137
126
|
i += 1
|
138
127
|
j -= 1
|
139
|
-
end
|
128
|
+
end
|
140
129
|
end
|
141
130
|
node_path
|
142
131
|
end
|
@@ -144,64 +133,137 @@ module ChessVwong
|
|
144
133
|
def falling_path(start, dest)
|
145
134
|
@node_path = []
|
146
135
|
i = 0
|
147
|
-
j = start[0]
|
148
|
-
# DOWN_RIGHT
|
136
|
+
j = start[0]
|
137
|
+
# DOWN_RIGHT
|
149
138
|
if start[0] < dest[0] && start[1] < dest[1]
|
150
139
|
while j < dest[0]
|
151
|
-
node_path << grid[start[1] + i][start[0]+i]
|
140
|
+
node_path << grid[start[1] + i][start[0] + i]
|
152
141
|
i += 1
|
153
142
|
j += 1
|
154
|
-
end
|
143
|
+
end
|
155
144
|
# UP_LEFT
|
156
145
|
elsif start[0] > dest[0] && start[1] > dest[1]
|
157
146
|
while j > dest[0]
|
158
|
-
node_path << grid[start[1]-i][start[0]-i]
|
147
|
+
node_path << grid[start[1] - i][start[0] - i]
|
159
148
|
i += 1
|
160
149
|
j -= 1
|
161
|
-
end
|
150
|
+
end
|
162
151
|
end
|
163
|
-
node_path
|
152
|
+
node_path
|
164
153
|
end
|
165
154
|
|
155
|
+
def apply_castling(rook)
|
156
|
+
king = grid[rook.current_space[1]][5].occupied.pop
|
157
|
+
if king.turns == 0 && rook.turns == 0
|
158
|
+
if rook.current_space[0] == 1 # Long Castle
|
159
|
+
switch_rook_castle(rook, king, 4, 3)
|
160
|
+
elsif rook.current_space[0] == 8 # Short Castle
|
161
|
+
switch_rook_castle(rook, king, 6, 7)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
166
165
|
|
167
|
-
|
168
|
-
|
169
|
-
|
166
|
+
# Print's out Board
|
167
|
+
def formatted_grid
|
168
|
+
grid.each do |row|
|
169
|
+
puts row.map { |node| node.occupied.empty? ? '_' : node.occupied.first.character }.join(' ')
|
170
170
|
end
|
171
|
+
end
|
172
|
+
|
173
|
+
private
|
174
|
+
|
175
|
+
def default_grid
|
176
|
+
Array.new(9) { Array.new(9) { Node.new } }
|
177
|
+
end
|
171
178
|
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
end
|
179
|
+
# Goes through each node to see if it's Blocked
|
180
|
+
def process_path
|
181
|
+
node_path.each do |node|
|
182
|
+
unless node.occupied.empty?
|
183
|
+
return false
|
178
184
|
end
|
179
185
|
end
|
186
|
+
end
|
180
187
|
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
188
|
+
def standard_move(piece, node, value, player)
|
189
|
+
piece.ep_kill =[] #resetting ability to En Passant
|
190
|
+
piece.current_space = value
|
191
|
+
piece.turns += 1
|
192
|
+
node.occupied << piece
|
193
|
+
player.kill_list << node.occupied.shift if node.occupied.count > 1
|
194
|
+
end
|
195
|
+
|
196
|
+
# ######UNIQUE MOVES##############
|
197
|
+
|
198
|
+
# Check for en_passant or normal kill moves
|
199
|
+
def pawn_moves_helper(piece)
|
200
|
+
if piece.instance_of?(Pawn)
|
201
|
+
en_pass_check(piece, piece.current_space[0], piece.current_space[1])
|
202
|
+
pawn_kill_helper(piece, get_value)
|
203
|
+
else
|
204
|
+
@chosen_piece.generate_neighbours(get_value)
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
# if Pawn is selected, nearby spaces analyzed if enemies are present, if so they have additional moves
|
209
|
+
def pawn_kill_helper(chosen_piece, get_value)
|
210
|
+
if chosen_piece.color == 'w'
|
211
|
+
node1 = grid[get_value[1] - 1][get_value[0] - 1]
|
212
|
+
node2 = grid[get_value[1] - 1][get_value[0] + 1]
|
213
|
+
@chosen_piece.generate_neighbours(get_value, node1, node2)
|
214
|
+
else
|
215
|
+
node1 = grid[get_value[1] + 1][get_value[0] - 1]
|
216
|
+
node2 = grid[get_value[1] + 1][get_value[0] + 1]
|
217
|
+
@chosen_piece.generate_neighbours(get_value, node1, node2)
|
192
218
|
end
|
219
|
+
end
|
193
220
|
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
end
|
221
|
+
def pawn_to_queen(pawn, current_node)
|
222
|
+
if pawn.color == 'w' && pawn.current_space[1] == 1
|
223
|
+
current_node.occupied.pop
|
224
|
+
current_node.occupied << Queen.new(pawn.current_space, 'w')
|
225
|
+
elsif pawn.color == 'b' && pawn.current_space[1] == 8
|
226
|
+
current_node.occupied.pop
|
227
|
+
current_node.occupied << Queen.new(pawn.current_space, 'b')
|
202
228
|
end
|
229
|
+
end
|
203
230
|
|
204
|
-
|
205
|
-
|
231
|
+
|
232
|
+
def en_pass_kill(pawn, player, node)
|
233
|
+
x = pawn.current_space[0]
|
234
|
+
y = pawn.current_space[1]
|
235
|
+
if pawn.ep_kill.last == "L"
|
236
|
+
player.kill_list << grid[y][x-1].occupied.pop()
|
237
|
+
node.occupied << pawn
|
238
|
+
elsif pawn.ep_kill.last == "R"
|
239
|
+
player.kill_list << grid[y][x+1].occupied.pop()
|
240
|
+
node.occupied << pawn
|
241
|
+
end
|
242
|
+
pawn.ep_kill =[] #resetting ability to En Passant
|
243
|
+
end
|
244
|
+
|
245
|
+
def en_pass_check(pawn, x, y)
|
246
|
+
l_piece = grid[y][x-1].occupied.first
|
247
|
+
r_piece = grid[y][x+1].occupied.first
|
248
|
+
en_pass_helper(pawn, l_piece, "L") #adjacent enemy on left
|
249
|
+
en_pass_helper(pawn, r_piece, "R") #adjacent enemy on right
|
250
|
+
end
|
206
251
|
|
252
|
+
def en_pass_helper(pawn, piece, side)
|
253
|
+
if piece.instance_of?(Pawn) && piece.color != pawn.color
|
254
|
+
pawn.color == 'w' ? set_en_pass(pawn, piece, 4, side) : set_en_pass(pawn, piece, 5, side) #set row depending on color
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
def set_en_pass(pawn, piece, row, side)
|
259
|
+
pawn.ep_kill << side if piece.turns == 1 && piece.current_space[1] == row #if enemy pawn has double-stepped, add ep_kill to pawn
|
260
|
+
end
|
207
261
|
|
262
|
+
def switch_rook_castle(rook, king, rook_space, king_space)
|
263
|
+
rook.current_space[0] = rook_space
|
264
|
+
grid[rook.current_space[1]][rook_space].occupied << rook
|
265
|
+
king.current_space[0] = king_space
|
266
|
+
grid[rook.current_space[1]][king_space].occupied << king
|
267
|
+
end
|
268
|
+
end
|
269
|
+
end
|