Permalink
Browse files

use rotation matrix for piece rotation

  • Loading branch information...
1 parent d4c4cd7 commit ef6a1b98bcfbd2e042599ce3996c0a0664addc48 @andrewfader committed Feb 12, 2013
Showing with 41 additions and 57 deletions.
  1. +16 −32 piece.rb
  2. +25 −25 piece_spec.rb
View
@@ -1,4 +1,5 @@
require './grid'
+require 'matrix'
class Piece
HORIZONTAL_MOVEMENT_AMOUNT = 28
VERTICAL_MOVEMENT_AMOUNT = 12
@@ -9,8 +10,7 @@ def initialize(window, grid)
@x = 512
@y = Grid::GRID_TOP
# type = [:I, :O].shuffle.first
- type = [:I,:J,:L,:O,:S,:T,:Z].shuffle.first
- @shape = Piece.set_shape(type)
+ @type = [:I,:J,:L,:O,:S,:T,:Z].shuffle.first
@rotation = 0
@grid = grid
@window = window
@@ -24,46 +24,30 @@ def draw
end
def current_shape
- @shape[@rotation]
+ Piece.set_shape(@type, @rotation)
end
- def self.set_shape(type)
- case type
+ def self.set_shape(type, rotation)
+ shape = case type
when :I
- {0 => [[1,0],[2,0],[3,0]],
- 1 => [[0,-1],[0,-2],[0,-3]],
- 2 => [[-1,0],[-2,0],[-3,0]],
- 3 => [[0,1],[0,2],[0,3]] }
+ [[1,0],[2,0],[3,0]]
when :J
- {0 => [[1,0],[2,0],[2,-1]],
- 1 => [[0,-1],[0,-2],[-1,-2]],
- 2 => [[-1,0],[-2,0],[-2,1]],
- 3 => [[0,1],[0,2],[1,2]] }
+ [[1,0],[2,0],[2,-1]]
when :L
- {0 => [[1,0],[2,0],[2,-1]],
- 1 => [[0,-1],[0,-2],[-1,-2]],
- 2 => [[-1,0],[-2,0],[-2,1]],
- 3 => [[0,1],[0,2],[1,2]] }
+ [[1,0],[2,0],[2,-1]]
when :O
- (0..3).each_with_index.inject({}) do |hash,index|
- hash.merge!({index[0] => [[0,1],[1,1],[1,0]]})
- end
+ [[0,1],[1,1],[1,0]]
when :S
- {0 => [[1,0],[0,-1],[-1,-1]],
- 1 => [[0,-1],[1,-1],[1,-2]],
- 2 => [[1,0],[0,-1],[-1,-1]],
- 3 => [[0,-1],[1,-1],[1,-2]] }
+ [[1,0],[0,-1],[-1,-1]]
when :T
- {0 => [[-1,0],[1,0],[0,-1]],
- 1 => [[0,-1],[0,1],[-1,0]],
- 2 => [[1,0],[-1,0],[0,1]],
- 3 => [[0,1],[0,-1],[1,0]] }
+ [[-1,0],[1,0],[0,-1]]
when :Z
- {0 => [[-1,0],[-1,-1],[-2,-1]],
- 1 => [[0,1],[1,1],[1,2]],
- 2 => [[-1,0],[-1,-1],[-2,-1]],
- 3 => [[0,1],[1,1],[1,2]] }
+ [[-1,0],[-1,-1],[-2,-1]]
+ end
+ rotation.times do
+ shape = shape.map{|arr|(Matrix[arr] * Matrix[[0,-1],[1,0]]).to_a.flatten}
end
+ shape.to_a
end
def rotate_right
View
@@ -13,51 +13,51 @@
end
it "I" do
- Piece.set_shape(:I)[0].should == [[1,0],[2,0],[3,0]]
- Piece.set_shape(:I)[1].should == [[0,-1],[0,-2],[0,-3]]
- Piece.set_shape(:I)[2].should == [[-1,0],[-2,0],[-3,0]]
- Piece.set_shape(:I)[3].should == [[0,1],[0,2],[0,3]]
+ Piece.set_shape(:I,0).should == [[1,0],[2,0],[3,0]]
+ Piece.set_shape(:I,1).should == [[0,-1],[0,-2],[0,-3]]
+ Piece.set_shape(:I,2).should == [[-1,0],[-2,0],[-3,0]]
+ Piece.set_shape(:I,3).should == [[0,1],[0,2],[0,3]]
end
it "J" do
- Piece.set_shape(:J)[0].should == [[1,0],[2,0],[2,-1]]
- Piece.set_shape(:J)[1].should == [[0,-1],[0,-2],[-1,-2]]
- Piece.set_shape(:J)[2].should == [[-1,0],[-2,0],[-2,1]]
- Piece.set_shape(:J)[3].should == [[0,1],[0,2],[1,2]]
+ Piece.set_shape(:J,0).should == [[1,0],[2,0],[2,-1]]
+ Piece.set_shape(:J,1).should == [[0,-1],[0,-2],[-1,-2]]
+ Piece.set_shape(:J,2).should == [[-1,0],[-2,0],[-2,1]]
+ Piece.set_shape(:J,3).should == [[0,1],[0,2],[1,2]]
end
it "L" do
- Piece.set_shape(:L)[0].should == [[1,0],[2,0],[2,-1]]
- Piece.set_shape(:L)[1].should == [[0,-1],[0,-2],[-1,-2]]
- Piece.set_shape(:L)[2].should == [[-1,0],[-2,0],[-2,1]]
- Piece.set_shape(:L)[3].should == [[0,1],[0,2],[1,2]]
+ Piece.set_shape(:L,0).should == [[1,0],[2,0],[2,-1]]
+ Piece.set_shape(:L,1).should == [[0,-1],[0,-2],[-1,-2]]
+ Piece.set_shape(:L,2).should == [[-1,0],[-2,0],[-2,1]]
+ Piece.set_shape(:L,3).should == [[0,1],[0,2],[1,2]]
end
it "O" do
(1..3).to_a.each do |index|
- Piece.set_shape(:O)[index].should == [[0,1],[1,1],[1,0]]
+ Piece.set_shape(:O,index).should == [[0,1],[1,1],[1,0]]
end
end
it "S" do
- Piece.set_shape(:S)[0].should == [[1,0],[0,-1],[-1,-1]]
- Piece.set_shape(:S)[1].should == [[0,-1],[1,-1],[1,-2]]
- Piece.set_shape(:S)[2].should == [[1,0],[0,-1],[-1,-1]]
- Piece.set_shape(:S)[3].should == [[0,-1],[1,-1],[1,-2]]
+ Piece.set_shape(:S,0).should == [[1,0],[0,-1],[-1,-1]]
+ Piece.set_shape(:S,1).should == [[0,-1],[1,-1],[1,-2]]
+ Piece.set_shape(:S,2).should == [[1,0],[0,-1],[-1,-1]]
+ Piece.set_shape(:S,3).should == [[0,-1],[1,-1],[1,-2]]
end
it "T" do
- Piece.set_shape(:T)[0].should == [[-1,0],[1,0],[0,-1]]
- Piece.set_shape(:T)[1].should == [[0,-1],[0,1],[-1,0]]
- Piece.set_shape(:T)[2].should == [[1,0],[-1,0],[0,1]]
- Piece.set_shape(:T)[3].should == [[0,1],[0,-1],[1,0]]
+ Piece.set_shape(:T,0).should == [[-1,0],[1,0],[0,-1]]
+ Piece.set_shape(:T,1).should == [[0,-1],[0,1],[-1,0]]
+ Piece.set_shape(:T,2).should == [[1,0],[-1,0],[0,1]]
+ Piece.set_shape(:T,3).should == [[0,1],[0,-1],[1,0]]
end
it "Z" do
- Piece.set_shape(:Z)[0].should == [[-1,0],[-1,-1],[-2,-1]]
- Piece.set_shape(:Z)[1].should == [[0,1],[1,1],[1,2]]
- Piece.set_shape(:Z)[2].should == [[-1,0],[-1,-1],[-2,-1]]
- Piece.set_shape(:Z)[3].should == [[0,1],[1,1],[1,2]]
+ Piece.set_shape(:Z,0).should == [[-1,0],[-1,-1],[-2,-1]]
+ Piece.set_shape(:Z,1).should == [[0,1],[1,1],[1,2]]
+ Piece.set_shape(:Z,2).should == [[-1,0],[-1,-1],[-2,-1]]
+ Piece.set_shape(:Z,3).should == [[0,1],[1,1],[1,2]]
end
end
end

0 comments on commit ef6a1b9

Please sign in to comment.