Class: ChessVwong::Pawn

Inherits:
Piece
  • Object
show all
Defined in:
lib/chess_vwong/pawn.rb

Instance Attribute Summary

Attributes inherited from Piece

#color, #current_space, #neighbours

Instance Method Summary collapse

Methods inherited from Piece

#initialize, #valid_space?

Constructor Details

This class inherits a constructor from ChessVwong::Piece

Instance Method Details

#characterObject



4
5
6
# File 'lib/chess_vwong/pawn.rb', line 4

def character
  color == "w" ? "\u{265F}" : "\u{2659}"
end

#generate_neighbours(current_space, node1 = nil, node2 = nil) ⇒ Object

Generate all possible Neighbouring Spaces



9
10
11
12
13
14
15
16
# File 'lib/chess_vwong/pawn.rb', line 9

def generate_neighbours(current_space, node1=nil, node2=nil)
  color == "w" ? moves = [[0, -1]] : moves = [[0, 1]]
  first_turn?(current_space, moves)
  kill_move(node1, node2, moves)   
  moves.each do |move|
    neigbour_helper(current_space, move[0], move[1])
  end
end

#kill_move(node1, node2, moves) ⇒ Object

if there is a enemy piece nearby, then give option to attack



19
20
21
22
23
24
25
26
27
# File 'lib/chess_vwong/pawn.rb', line 19

def kill_move(node1, node2, moves)
  if color == "w"
    moves << [-1,-1] if  node1 &&!node1.occupied.empty? && node1.occupied.first.color == "b"
    moves << [1,-1] if node2 && !node2.occupied.empty? && node2.occupied.first.color == "b"
  else
    moves << [-1,1] if node1 && !node1.occupied.empty? && node1.occupied.first.color == "w"
    moves << [1,1] if node2 && !node2.occupied.empty? && node2.occupied.first.color == "w"
  end
end