Skip to content

Commit

Permalink
- code made closer to the functional paradigm
Browse files Browse the repository at this point in the history
  • Loading branch information
fornost committed Jul 18, 2016
1 parent 57a9e75 commit 1fb14b8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
21 changes: 15 additions & 6 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@
# All notable changes to this project will be documented in this file.
# This project adheres to [Semantic Versioning](http://semver.org/).

0.2.3 (2016-07-18)
code changes:
- code made closer to the functional paradigm

0.2.2 (2016-07-16)
fixed:
- score decreased too fast

0.2.1 (2016-07-16)
changed:
- timer duration for score decrease is now shared with timer duration for tetromino fall
- [code improvement] informations about printing messages were grouped in a table

code changes:
- informations about printing messages were grouped in a table

0.2.0 (2016-07-15)
changed:
- new formula for acceleration of tetrominoes
- [code improvement] timers were grouped in a table

code changes:
- timers were grouped in a table

0.1.0 (2016-07-13)
added:
Expand All @@ -31,11 +39,12 @@
- score system is cleaner
- saner defaults for grid position
- window size and position of the elements within the window
- [code improvement] array initialization in frozenSquares moved to Tables module and made more generic
- [code improvement] the structure of each tetromino was updated so that they can be created without checking if they exceed the frame (except if they are randomly rotated)
if they are randomly rotated)

fixed:
- color randomization sometimes produced squares of the same color as the background and thus not visible

removed:
- [code improvement] Strings module, because so little is now printed (with the cleaner score system) that it became unused
code changes:
- array initialization in frozenSquares moved to Tables module and made more generic
- the structure of each tetromino was updated so that they can be created without checking if they exceed the frame (except if they are randomly rotated)
- Strings module removed, because so little is now printed (with the cleaner score system) that it became unused
2 changes: 1 addition & 1 deletion src/code/Square.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function Square:isBlocked(direction, frozenSquares)
end

function Square:forceTranslation(vector)
self.position:translate(vector)
self.position = vector:translate(self.position)
end

function Square:realPosition(grid)
Expand Down
6 changes: 2 additions & 4 deletions src/code/Tetromino.lua
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,13 @@ end
function Tetromino:forceRotation(n)
local tetrominoCenter = self:center()
local function rotateSquare(sq)
local posRelativeToTetrominoCenter = sq:getCenter() - tetrominoCenter
posRelativeToTetrominoCenter:rotateCounterclockwise(n)
sq:setCenter(posRelativeToTetrominoCenter + tetrominoCenter)
sq:setCenter(sq:getCenter():rotateCounterclockwiseAround(tetrominoCenter, n))
end
self:forEachSquare(rotateSquare)
end

function Tetromino:randomRotation()
self:forceRotation(math.random(0, 3))
self:forceRotation(math.random(4))
end

function Tetromino:rotate(frozenSquares)
Expand Down
23 changes: 11 additions & 12 deletions src/code/Vector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,40 +41,39 @@ function Vector.__tostring(t)
return "Vector(" .. t.x .. ", " .. t.y .. ")"
end

function Vector:assimilate(t)
self.x = t.x
self.y = t.y
function Vector:translate(t)
return self + t
end

function Vector:translate(t)
self:assimilate(self + t)
function Vector:rotateCounterclockwiseAround(origin, numberOfRotations)
return (self - origin):rotateCounterclockwise(numberOfRotations) + origin
end

function Vector:rotateCounterclockwise(numberOfRotations)
local n = (numberOfRotations or 1) % 4
if(n == 0) then
return
return self
elseif(n == 1) then
self:rotateOnceCounterclockwise()
return self:rotateOnceCounterclockwise()
elseif(n == 2) then
self:rotateTwice()
return self:rotateTwice()
elseif(n == 3) then
self:rotateOnceClockwise()
return self:rotateOnceClockwise()
else
error("Vector:rotateCounterclockwise: bad argument: " .. tostring(n), 2)
end
end

function Vector:rotateTwice()
self:assimilate(Vector(-self.x, -self.y))
return Vector(-self.x, -self.y)
end

function Vector:rotateOnceCounterclockwise()
self:assimilate(Vector(-self.y, self.x))
return Vector(-self.y, self.x)
end

function Vector:rotateOnceClockwise()
self:assimilate(Vector(self.y, -self.x))
return Vector(self.y, -self.x)
end

directions =
Expand Down

0 comments on commit 1fb14b8

Please sign in to comment.