Steven! Ragnarök avatar Steven! Ragnarök committed 48a7bc1

Experiment with different pieces and styles.

Comments (0)

Files changed (4)

     end
   end
 
+  def capturable? by_color, location
+    
+  end
+
   def index_from_algebraic symbol
     (8 - symbol[1].to_i) * 8 + BOARD_LETTERS[symbol[0]]
   end
+class Pawn < Piece
+  moves :forward, 1
+  moves :forward, 2, :when => :first_move
+  captures :diagonally_forward, 1
+end
+
+class Rook
+  moves :forward
+  moves :backward
+  moves :sideways
+  captures :forward
+  captures :backward
+  captures :sideways
+end
+
+class Bishop
+  moves :diagonally_forward
+  moves :diagonally_backward
+  captures :diagonally_forward
+  captures :diagonally_backward
+end
+
+class King
+  moves :forward, 1, :when => :avoids_check
+  moves :backward, 1, :when => :avoids_check
+  moves :sideways, 1, :when => :avoids_check
+  moves :diagonally_forward, 1, :when => :avoids_check
+  moves :diagonally_backward, 1, :when => :avoids_check
+  moves :forward, 1, :when => :avoids_check
+  moves :backward, 1, :when => :avoids_check
+  moves :sideways, 1, :when => :avoids_check
+  moves :diagonally_forward, 1, :when => :avoids_check
+  moves :diagonally_backward, 1, :when => :avoids_check
+end
+
+class Queen
+  moves :forward
+  moves :backward
+  moves :diagonally_forward
+  moves :diagonally_backward
+
+  captures :forward
+  captures :backward
+  captures :diagonally_forward
+  captures :diagonally_backward
+end
+
+class Knight
+  jumps :forward, 2, :then => [:sideways, 3]
+  jumps :backward, 2, :then => [:sideways, 3]
+  jumps :forward, 2, :then => [:sideways, 2]
+  jumps :backward, 3, :then => [:sideways, 2]
+end

13/lib/piece.rb

-module Piece
-  TYPES = { 'P' => :Pawn, 'R' => :Rook, 'N' => :Knight, 'B' => :Bishop,
-            'Q' => :Queen, 'K' => :King }
-
-  def new piece, location
-    TYPES[piece.last].new piece.first, location
-  end
-  module_function :new
-end
-

13/lib/piece/pawn.rb

-class Piece::Pawn
- attr_reader :color, :location
-
-  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
-  # array if none exists.
-  def move destination
-  end
-
-  # 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
-
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.