Skip to content

Commit

Permalink
refactored to use custom ValidationError
Browse files Browse the repository at this point in the history
  • Loading branch information
mspwong committed Aug 28, 2011
1 parent 8459081 commit f23122b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
20 changes: 13 additions & 7 deletions game.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def move(piece, new_position)
(new_position.first.is_a? Integer) && (VALID_COORDINATES.include? new_position.first) && (new_position.first.is_a? Integer) && (VALID_COORDINATES.include? new_position.first) &&
(new_position.last.is_a? Integer) && (VALID_COORDINATES.include? new_position.last) ) (new_position.last.is_a? Integer) && (VALID_COORDINATES.include? new_position.last) )


raise RuntimeError if (!immediate_forward_and_diagonal(piece, new_position) || occupied(new_position)) validate_immediate_forward_diagonal! piece, new_position
validate_not_occupied! new_position


@teams[piece[:team]][piece[:piece_num]] = new_position @teams[piece[:team]][piece[:piece_num]] = new_position
end end
Expand All @@ -30,23 +31,28 @@ def position(piece)


VALID_COORDINATES = (1..8).to_a VALID_COORDINATES = (1..8).to_a


def immediate_forward_and_diagonal(piece, new_position) def validate_immediate_forward_diagonal!(piece, new_position)
p = position(piece) p = position(piece)
current_x = p.first current_x = p.first
current_y = p.last current_y = p.last
if piece[:team] == :white if piece[:team] == :white
(new_position.last == current_y+1) && ((new_position.first == current_x+1) || (new_position.first == current_x-1)) raise ValidationError.new("must only move to a position immediately forward and diagonal") unless (new_position.last == current_y+1) && ((new_position.first == current_x+1) || (new_position.first == current_x-1))
elsif piece[:team] == :red elsif piece[:team] == :red
(new_position.last == current_y-1) && ((new_position.first == current_x+1) || (new_position.first == current_x-1)) raise ValidationError.new("must only move to a position immediately forward and diagonal") unless (new_position.last == current_y-1) && ((new_position.first == current_x+1) || (new_position.first == current_x-1))
else else
raise RuntimeError("unknown team: #{piece[:team]}") raise ArgumentError("unknown team: #{piece[:team]}")
end end
end end


def occupied(new_position) def validate_not_occupied!(new_position)
@teams.values.any? do |pieces| raise ValidationError.new("must not move to a position that is already occupied") if @teams.values.any? do |pieces|
pieces.values.include? new_position pieces.values.include? new_position
end end
end end


end



class ValidationError < StandardError
end end
46 changes: 23 additions & 23 deletions spec/game_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
require "rspec" require "rspec"
require File.dirname("__FILE__") + "/game" require File.dirname(__FILE__) + "/../game"


describe "Checker game: " do describe "Checker game: " do


Expand All @@ -12,34 +12,34 @@


describe "to invalid square" do describe "to invalid square" do
it "results in exception" do it "results in exception" do
lambda { @game.move(@piece, [3.5, @y]) }.should raise_error(ArgumentError) lambda { @game.move(@piece, [3.5, @y]) }.should raise_error ArgumentError
lambda { @game.move(@piece, [3.5, 3.5]) }.should raise_error(ArgumentError) lambda { @game.move(@piece, [3.5, 3.5]) }.should raise_error ArgumentError
lambda { @game.move(@piece, [@x, -1]) }.should raise_error(ArgumentError) lambda { @game.move(@piece, [@x, -1]) }.should raise_error ArgumentError
lambda { @game.move(@piece, [-1, @y]) }.should raise_error(ArgumentError) lambda { @game.move(@piece, [-1, @y]) }.should raise_error ArgumentError
lambda { @game.move(@piece, [0, @y]) }.should raise_error(ArgumentError) lambda { @game.move(@piece, [0, @y]) }.should raise_error ArgumentError
lambda { @game.move(@piece, [@x, 0]) }.should raise_error(ArgumentError) lambda { @game.move(@piece, [@x, 0]) }.should raise_error ArgumentError
lambda { @game.move(@piece, ["does not matter", @y]) }.should raise_error(ArgumentError) lambda { @game.move(@piece, ["does not matter", @y]) }.should raise_error ArgumentError
lambda { @game.move(@piece, [@x, 9]) }.should raise_error(ArgumentError) lambda { @game.move(@piece, [@x, 9]) }.should raise_error ArgumentError
lambda { @game.move(@piece, [9, @y]) }.should raise_error(ArgumentError) lambda { @game.move(@piece, [9, @y]) }.should raise_error ArgumentError
lambda { @game.move(@piece, [9, 9]) }.should raise_error(ArgumentError) lambda { @game.move(@piece, [9, 9]) }.should raise_error ArgumentError
end end
end end


describe "to light colored square" do describe "to light colored square" do
it "results in exception" do it "results in exception" do
lambda { @game.move(@piece, [4, 4]) }.should raise_error(RuntimeError) lambda { @game.move(@piece, [4, 4]) }.should raise_error ValidationError
end end
end end


describe "to horizontally adjacent (side way) square" do describe "to horizontally adjacent (side way) square" do
it "results in exception" do it "results in exception" do
lambda { @game.move(@piece, [8, 3]) }.should raise_error(RuntimeError) lambda { @game.move(@piece, [8, 3]) }.should raise_error ValidationError
end end
end end


describe "to vertically adjacent (up or down) square" do describe "to vertically adjacent (up or down) square" do
it "results in exception" do it "results in exception" do
lambda { @game.move(@piece, [7, 4]) }.should raise_error(RuntimeError) lambda { @game.move(@piece, [7, 4]) }.should raise_error ValidationError
end end
end end


Expand All @@ -48,35 +48,35 @@
it "results in exception" do it "results in exception" do
piece = {:team => :red, :piece_num => 4} piece = {:team => :red, :piece_num => 4}
x, y = @game.position(piece).first, @game.position(piece).last x, y = @game.position(piece).first, @game.position(piece).last
lambda { @game.move(piece, [x-1, y+1]) }.should raise_error(RuntimeError) lambda { @game.move(piece, [x-1, y+1]) }.should raise_error ValidationError
end end
end end


describe "by a white piece" do describe "by a white piece" do
it "results in exception" do it "results in exception" do
lambda { @game.move(@piece, [@x, @y-1]) }.should raise_error(RuntimeError) lambda { @game.move(@piece, [@x, @y-1]) }.should raise_error ValidationError
end end
end end
end end


describe "forward by more than 1 diagonal" do describe "forward by more than 1 diagonal" do
it "results in exception" do it "results in exception" do
lambda { @game.move(@piece, [5, 5]) }.should raise_error(RuntimeError) lambda { @game.move(@piece, [5, 5]) }.should raise_error ValidationError
end end
end end


describe "to square" do describe "to square" do
describe "occupied by own team" do describe "occupied by own team" do
it "results in exception" do it "results in exception" do
piece = {:team => :white, :piece_num => 7} piece = {:team => :white, :piece_num => 7}
lambda { @game.move(piece, [@x, @y]) }.should raise_error(RuntimeError) lambda { @game.move(piece, [@x, @y]) }.should raise_error ValidationError
end end
end end


describe "occupied by opponent" do describe "occupied by opponent" do
it "results in exception" do it "results in exception" do
piece = {:team => :red, :piece_num => 4} piece = {:team => :red, :piece_num => 4}
lambda { @game.move(piece, [@x, @y]) }.should raise_error(RuntimeError) lambda { @game.move(piece, [@x, @y]) }.should raise_error ValidationError
end end
end end
end end
Expand Down Expand Up @@ -125,7 +125,7 @@
piece = {:team => :red, :piece_num => 8} piece = {:team => :red, :piece_num => 8}
position = @game.position(piece) position = @game.position(piece)
x, y = position.first, position.last x, y = position.first, position.last
lambda { @game.move(piece, [x, y]) }.should raise_error lambda { @game.move(piece, [x, y]) }.should raise_error ValidationError


piece = {:team => :red, :piece_num => 4} piece = {:team => :red, :piece_num => 4}
position = @game.position(piece) position = @game.position(piece)
Expand All @@ -139,7 +139,7 @@
piece = {:team => :white, :piece_num => 12} piece = {:team => :white, :piece_num => 12}
position = @game.position(piece) position = @game.position(piece)
x, y = position.first, position.last x, y = position.first, position.last
lambda { @game.move(piece, [x+1, y+1]) }.should raise_error lambda { @game.move(piece, [x+1, y+1]) }.should raise_error ValidationError


piece = {:team => :red, :piece_num => 8} piece = {:team => :red, :piece_num => 8}
position = @game.position(piece) position = @game.position(piece)
Expand All @@ -159,7 +159,7 @@
piece = {:team => :red, :piece_num => 2} piece = {:team => :red, :piece_num => 2}
position = @game.position(piece) position = @game.position(piece)
x, y = position.first, position.last x, y = position.first, position.last
lambda { @game.move(piece, [x+1, y-1]) }.should raise_error lambda { @game.move(piece, [x+1, y-1]) }.should raise_error ValidationError


piece = {:team => :white, :piece_num => 8} piece = {:team => :white, :piece_num => 8}
position = @game.position(piece) position = @game.position(piece)
Expand All @@ -171,7 +171,7 @@
position = @game.position(piece) position = @game.position(piece)
x, y = position.first, position.last x, y = position.first, position.last
[x, y].should eq [6, 4] [x, y].should eq [6, 4]
lambda { @game.move(piece, [x-1, y+1]) }.should raise_error lambda { @game.move(piece, [x-1, y+1]) }.should raise_error ValidationError


# keep playing # keep playing
end end
Expand Down

0 comments on commit f23122b

Please sign in to comment.