Commits

Steven! Ragnarök committed d661bac

Work on pawn moves continued. Going to try alternate implementation.

  • Participants
  • Parent commits 4165cdf

Comments (0)

Files changed (5)

File 13/bin/move_validator

 
 require './lib/board'
 
+def usage
+  puts "USAGE: move_validator BOARD_FILE MOVES_FILE"
+end
+
+unless ARGV.length == 2
+  usage
+  exit
+end
+
 board = Board.new File.read ARGV.shift
 
 move_list = File.read(ARGV.shift).split("\n").map(&:split)

File 13/lib/board.rb

   end
 
   def empty? place
-    send(place) == '--'
+    case place
+    when Symbol, String
+      send(place) == '--'
+    when Fixnum
+      @board_matrix[place] == '--'
+    end
   end
 
   def index_from_algebraic symbol
     (8 - symbol[1].to_i) * 8 + BOARD_LETTERS[symbol[0]]
   end
 
+  def algebraic_from_index index
+    "#{BOARD_LETTERS.invert[index %  8]}#{8 - index / 8}"
+  end
+
   def method_missing symbol, *args, &block
     if algebraic? symbol
       @board_matrix[index_from_algebraic symbol]

File 13/lib/piece/pawn.rb

 
   def initialize color, location, board
     @color, @location, @board = color, location, board
-    
   end
 
   # Returns an array with the path from location to destination or an empty
   def move destination
   end
 
-  # returns an array of valid destinations for this piece's move.
+  # Pawns can move forward one row (where forward depends on color).
+  # They can also move forward two rows on their first move.
+  # The can also capture diagonally forward left or right.
+  # All moves must keep the piece on the chess board so filter out values not
+  # between 0 and 63.
   def destinations
-    
+    [].tap do |dest|
+      dest << forward_one if @board.empty? forward_one
+      dest << forward_two if first_move? and @board.empty? forward_two
+      dest << diagonal_left if @board.capturable? color, diagonal_left
+      dest << diagonal_right if @board.capturable? color, diagonal_right
+    end.select{ |location| location >= 0 && location <= 63 }
+  end
+
+  def forward_one
+    if color == 'b'
+      location + 8 * direction
+    else
+      location + 8 * direction
+    end
+  end
+
+  def diagonal_left
+
+  end
+
+  def diagonal_right
+
+  end
+
+  def forward_two
+
+  end
+
+  def direction
+    if color == 'b'
+      1
+    else
+      -1
+    end
+  end
+
+  def first_move?
+    return true if color == 'b' and (8 - location / 8) == 7
+    return true if color == 'w' and (8 - location / 8) == 2
+    false
   end
 end
+

File 13/test/board_test.rb

     assert_equal 63, @board.index_from_algebraic(:h1)
   end
 
+  def test_board_can_turn_indices_into_algebraic
+    assert_equal "b7", @board.algebraic_from_index(9)
+    assert_equal "g7", @board.algebraic_from_index(14)
+    assert_equal "g2", @board.algebraic_from_index(54)
+    assert_equal "b2", @board.algebraic_from_index(49)
+  end
+
   def test_identifies_empty_squares
     assert @board.empty?(:d4), "Expected no piece at d4 but one was found."
-    refute @board.empty?(:a2), "Expected a piece at a2 but none was found."
+    refute @board.empty?("a2"), "Expected a piece at a2 but none was found."
+  end
+
+  def test_identifies_empty_squares_from_indices
+    assert @board.empty?(35), "Expected no piece at d4 but one was found."
+    refute @board.empty?(49), "Expected a piece at a2 but none was found."
   end
 
   def test_board_responds_to_algebraic_methods
     refute @board.valid_move?(%w[a7 b6]), "Move from a7 to b6 shouldn't be legal"
     refute @board.valid_move?(%w[d1 g4]), "Move from d1 to g4 shouldn't be legal"
   end
+
+  def test_board_identifies_capturable_pieces
+    assert @board.capturable? 'w', 8
+    assert @board.capturable? 'w', 4
+    refute @board.capturable? 'b', 4
+    refute @board.capturable? 'b', 34
+  end
 end

File 13/test/pawn_test.rb

 wR wN wB wQ wK wB wN wR
 BOARD
 
-    @white_pawn = Pawn.new 'wP', :a2, @board
+    @white_pawn = Pawn.new 'wP', 49, @board
   end
 
   def test_pawns_move_straight_ahead
   def test_pawns_can_move_two_squares_in_first_turn
     assert_includes @white_pawn.destinations, :a4
   end
+
+  def test_pawns_can_capture_diagonally
+    board = Board.new <<BOARD
+bR bN bB bQ bK bB bN bR
+bP bP bP bP bP bP bP bP
+-- -- -- -- -- -- -- --
+-- -- -- -- -- -- -- --
+-- -- -- -- -- -- -- --
+bP -- -- -- -- -- -- --
+wP wP wP wP wP wP wP wP
+wR wN wB wQ wK wB wN wR
+BOARD
+    pawn = Pawn.new 'w', 49, board
+  end
 end