Commits

Steven! Ragnarök committed 59c8b7a

Switched to single-dimensional array board implementation.

Comments (0)

Files changed (5)

 #
 # Part of my solution to PuzzleNode #13.
 # Replication of a chess board.
+
+#<pre>
 #    a  b  c  d  e  f  g  h
 # 8 bR bN bB bQ bK bB bN bR 8
 # 7 bP bP bP bP bP bP bP bP 7
 # 2 wP wP wP wP wP wP wP wP 2
 # 1 wR wN wB wQ wK wB wN wR 1
 #    a  b  c  d  e  f  g  h
+#</pre>
+
+# We will represent the board as a single array of length 64 beginning with 0
+# at a8 and continuing with 63 at h1. Each row and column can then be found by
+# taking the index divided by 8 and the index modulo 8. For example 8 - (40 /
+# 8) = 3 so 40 is in row 3. The column of 40 is found by taking 40 mod 8 = 0 so
+# 40 is in column a.
+
+#<pre>
+#      a  b  c  d  e  f  g  h
+# 8 | 00 01 02 03 04 05 06 07 | 8
+# 7 | 08 09 10 11 12 13 14 15 | 7
+# 6 | 16 17 18 19 20 21 22 23 | 6
+# 5 | 24 25 26 27 28 29 30 31 | 5
+# 4 | 32 33 34 35 36 37 38 39 | 4
+# 3 | 40 41 42 43 44 45 46 47 | 3
+# 2 | 48 49 50 51 52 53 54 55 | 2
+# 1 | 56 57 58 59 60 61 62 63 | 1
+#      a  b  c  d  e  f  g  h
+#</pre>
 class Board
   BOARD_LETTERS ={ 'a' => 0, 'b' => 1, 'c' => 2, 'd' => 3, 'e' => 4, 'f' => 5,
                    'g' => 6, 'h' => 7 }
 
   def initialize input_string
-    @board_matrix = input_string.lines.map &:split
+    @board_matrix = input_string.split
   end
 
   def [] index
   end
 
   def index_from_algebraic symbol
-    [8 - symbol[1].to_i, BOARD_LETTERS[symbol[0]]]
+    (8 - symbol[1].to_i) * 8 + BOARD_LETTERS[symbol[0]]
   end
 
   def method_missing symbol, *args, &block
     if algebraic? symbol
-      row, col = index_from_algebraic symbol
-      @board_matrix[row][col]
+      @board_matrix[index_from_algebraic symbol]
     else
       super symbol, *args, &block
     end
 module Piece
-  TYPES = { 'P' => Pawn, 'R' => Rook, 'N' => Knight, 'B' => Bishop,
-            'Q' => Queen, 'K' => King }
+  TYPES = { 'P' => :Pawn, 'R' => :Rook, 'N' => :Knight, 'B' => :Bishop,
+            'Q' => :Queen, 'K' => :King }
 
   def new piece, location
     TYPES[piece.last].new piece.first, location

13/lib/piece/pawn.rb

  attr_reader :color, :location
 
   def initialize color, location, board
-    @color, @location = color, location
+    @color, @location, @board = color, location, board
+    
   end
 
   # Returns an array with the path from location to destination or an empty
   # array if none exists.
   def move destination
-
   end
 
   # returns an array of valid destinations for this piece's move.
   def destinations
+    
   end
 end

13/test/board_test.rb

   end
 
   def test_board_reads_board_state_from_input
-    assert_equal "bR", @board[0][0]
+    assert_equal "bR", @board[0]
   end
 
   def test_algebraic_predicate
   end
 
   def test_board_gets_index_from_algebraic
-    assert_equal [0, 0], @board.index_from_algebraic(:a8)
-    assert_equal [7, 0], @board.index_from_algebraic(:a1)
+    assert_equal 0, @board.index_from_algebraic(:a8)
+    assert_equal 7, @board.index_from_algebraic(:h8)
+    assert_equal 56, @board.index_from_algebraic(:a1)
+    assert_equal 63, @board.index_from_algebraic(:h1)
   end
 
   def test_identifies_empty_squares

13/test/pawn_test.rb

 require 'minitest/unit'
 require 'minitest/autorun'
 require './lib/board'
+require './lib/piece'
 require './lib/piece/pawn'
 
 class PawnTest < MiniTest::Unit::TestCase
+  include Piece
 
   def setup
     @board = Board.new <<BOARD
 wR wN wB wQ wK wB wN wR
 BOARD
 
-    @white_pawn = Pawn.new 'wP', :a2
+    @white_pawn = Pawn.new 'wP', :a2, @board
   end
 
   def test_pawns_move_straight_ahead
     assert_includes @white_pawn.destinations, :a3
   end
+
+  def test_pawns_can_move_two_squares_in_first_turn
+    assert_includes @white_pawn.destinations, :a4
+  end
 end