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.
@@ -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
- while true
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
- puts "Invalid Input!"
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
- puts "Invalid Move!"
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
-
@@ -1,17 +1,15 @@
1
1
  module ChessVwong
2
2
  class King < Piece
3
-
4
3
  def character
5
- color == "w" ? "\u{265A}" : "\u{2654}"
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 = [[ 1, 0], [-1, 0], [0, 1], [0, -1], [-1, 1], [1, -1], [1,1], [-1,-1]]
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
@@ -1,17 +1,15 @@
1
1
  module ChessVwong
2
2
  class Knight < Piece
3
-
4
3
  def character
5
- color == "w" ? "\u{265E}" : "\u{2658}"
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 = [[ 1, 2], [-1, 2], [ 1,-2], [-1,-2], [ 2, 1], [-2, 1], [ 2,-1], [-2,-1]]
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
@@ -6,4 +6,4 @@ module ChessVwong
6
6
  @visited = []
7
7
  end
8
8
  end
9
- end
9
+ end
@@ -1,44 +1,45 @@
1
1
  module ChessVwong
2
2
  class Pawn < Piece
3
-
3
+ attr_accessor :doubled
4
4
  def character
5
- color == "w" ? "\u{265F}" : "\u{2659}"
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 == "w" ? moves = [[0, -1]] : moves = [[0, 1]]
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 == "w"
21
- moves << [-1,-1] if node1 &&!node1.occupied.empty? && node1.occupied.first.color == "b"
22
- moves << [1,-1] if node2 && !node2.occupied.empty? && node2.occupied.first.color == "b"
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 && !node1.occupied.empty? && node1.occupied.first.color == "w"
25
- moves << [1,1] if node2 && !node2.occupied.empty? && node2.occupied.first.color == "w"
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
- private
31
+ def kill_conditions(node, color)
32
+ node && !node.occupied.empty? && node.occupied.first.color == color
33
+ end
31
34
 
32
- def first_turn?(current_space, moves)
33
- if current_space[1] == 2 #Black
34
- moves << [0,2]
35
- elsif current_space[1] == 7 #White
36
- moves << [0,-2]
37
- end
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
-
@@ -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
- @neighbours = []
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
- return color
19
+ color
18
20
  end
19
21
 
20
22
  private
21
23
 
22
- # Create 1 new Neighbouring space
23
- def neigbour_helper(current_space, x, y)
24
- new_x = current_space[0] + x
25
- new_y = current_space[1] + y
26
- neighbour = [new_x, new_y]
27
- self.neighbours << neighbour if valid_space?(neighbour)
28
- end
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
-
@@ -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="w")
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
@@ -1,65 +1,66 @@
1
1
  module ChessVwong
2
- class Board
2
+ class Board
3
3
  # private
4
- # White on the Bottom, Black on Top
5
- def preload_pieces
6
- white_pieces = [Rook.new([0,7], "w"), Knight.new([1,7], "w"), Bishop.new([2,7], "w"), Queen.new([3,7], "w"),
7
- King.new([4,7], "w"), Bishop.new([5,7], "w"), Knight.new([6,7], "w"), Rook.new([7,7], "w")]
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
- black_pieces = [Rook.new([0,0], "b"), Knight.new([1,0], "b"), Bishop.new([2,0], "b"), Queen.new([3,0], "b"),
10
- King.new([4,0], "b"), Bishop.new([5,0], "b"), Knight.new([6,0], "b"), Rook.new([7,0], "b")]
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
- #Load Coord-Displays
16
- load_alphabet(grid[0])
17
- load_num_coord
18
- # Black Pawns
19
- load_pawns(grid[2], "b")
20
- #White Pawns
21
- load_pawns(grid[7], "w")
22
- # White Pieces
23
- load_back_pieces(grid[1], black_pieces)
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
- end
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
- def load_alphabet(row)
38
- alphabet = ("A".."H").to_a
39
- i = 0
40
- while i < 8
41
- row[i+1].occupied << Piece.new([0,0], alphabet[i])
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
- def load_num_coord
47
- i = 8
48
- j=1
49
- while i > 0
50
- grid[j][0].occupied << Piece.new([0,0], i)
51
- i -= 1
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
- def load_back_pieces(row, pieces)
57
- i = 0
58
- while i < 8
59
- row[i+1].occupied << pieces[i]
60
- i += 1
61
- end
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
@@ -1,28 +1,25 @@
1
1
  module ChessVwong
2
2
  class Queen < Piece
3
-
4
3
  def character
5
- color == "w" ? "\u{265B}" : "\u{2655}"
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
-
@@ -1,22 +1,19 @@
1
1
  module ChessVwong
2
2
  class Rook < Piece
3
-
4
3
  def character
5
- color == "w" ? "\u{265C}" : "\u{2656}"
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
-