Advertisement
Guest User

2048 nspire lua

a guest
Apr 5th, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.78 KB | None | 0 0
  1. -- Nspire Lua 2048
  2. -- (C) 2014 Adriweb
  3. -- TI-Planet.org  -  Inspired-Lua.org
  4. -- Game idea from http://gabrielecirulli.github.io/2048/
  5.  
  6. -------------
  7. -- Globals --
  8. -------------
  9. W, H = 318, 212        -- will be overwritten by on.resize
  10. score = 0
  11. bestScore = 0          -- will be overwritten by on.restore
  12. newTileX = 0
  13. newTileY = 0
  14.  
  15. colorTbl = {
  16.     [2]    = { 0xee, 0xe4, 0xda },
  17.     [4]    = { 0xed, 0xe0, 0xc8 },
  18.     [8]    = { 0xf2, 0xb1, 0x79 },
  19.     [16]    = { 0xf5, 0x95, 0x63 },
  20.     [32]   = { 0xf6, 0x7c, 0x5f },
  21.     [64]   = { 0xf6, 0x5e, 0x3b },
  22.     [128]   = { 0xed, 0xcf, 0x72},
  23.     [256]  = { 0xed, 0xcc, 0x61},
  24.     [512]  = { 0xed, 0xc8, 0x50 },
  25.     [1024]  = { 0xed, 0xc5, 0x3f },
  26.     [2048] = { 0xed, 0xc2, 0x2e }
  27. }
  28.  
  29. local mathrandom = math.random
  30.  
  31. local function uCol(tbl)
  32.     return tbl[1], tbl[2], tbl[3]
  33. end
  34.  
  35. -------------
  36. -- Classes --
  37. -------------
  38.  
  39. Game = class()
  40.  
  41. function Game:init(size)
  42.     if ((not size) or size<2 or size>10) then
  43.         size=4
  44.         print("Invalid size. Setting to 4")
  45.     end
  46.     self.size = size
  47.     self.gridOffset = nil
  48.     self:restart()
  49. end
  50.  
  51. function Game:makeGrid()
  52.     self.grid = {}
  53.     self.filled = false
  54.     for j=1,self.size do
  55.         self.grid[j] = {}
  56.         for i=1,self.size do
  57.             self.grid[j][i] = 0
  58.         end
  59.     end
  60. end
  61.  
  62. function Game:restart()
  63.     score = 0
  64.     self:makeGrid()
  65.     local r1x = mathrandom(1,self.size)
  66.     local r1y = mathrandom(1,self.size)
  67.     local r2x = mathrandom(1,self.size)
  68.     local r2y = mathrandom(1,self.size)
  69.     while (r1x==r2x and r1y==r2y) do
  70.         r2x = mathrandom(1,self.size)
  71.     end
  72.  
  73.     self.grid[r1y][r1x] = mathrandom(1,4)>3 and 4 or 2
  74.     self.grid[r2y][r2x] = mathrandom(1,4)>3 and 4 or 2
  75. end
  76.  
  77. function Game:spawnNumber()
  78.     local x = mathrandom(1,self.size)
  79.     local y = mathrandom(1,self.size)
  80.     while (self.grid[y][x]>0) do
  81.         x, y = mathrandom(1,self.size), mathrandom(1,self.size)
  82.     end
  83.     newTileX, newTileY = x, y
  84.     self.grid[y][x] = mathrandom(1,4)>3 and 4 or 2
  85. end
  86.  
  87. function Game:countFilled()
  88.     local count = 0
  89.     for j=1,self.size do
  90.         for i=1,self.size do
  91.             if (self.grid[j][i])>0 then count = count + 1 end
  92.         end
  93.     end
  94.     return count
  95. end
  96.  
  97. -- merge 1 into 2
  98. function Game:mergeCellsIfPossible(x1, y1, x2, y2)
  99.     local new = self.grid[y2][x2]
  100.     if ((self.grid[y1][x1] == new) or (new == 0)) then
  101.         self.grid[y2][x2] = new + self.grid[y1][x1]
  102.         self.grid[y1][x1] = 0
  103.         score = score + (new>0 and self.grid[y2][x2] or 0)
  104.         if score>bestScore then
  105.             bestScore = score
  106.         end
  107.         return true
  108.     else
  109.         return false
  110.     end
  111. end
  112.  
  113. function Game:move(direction)
  114.     local tmpx1 = 0
  115.     local tmpy1 = 0
  116.     local tmpx2 = 0
  117.     local tmpy2 = 0
  118.     local moved = false
  119.     if direction=="up" then
  120.         for j=2,self.size do
  121.             for i=1,self.size do
  122.                 if self.grid[j][i]>0 then
  123.                     tmpy1, tmpy2 = j, j-1
  124.                     while (tmpy2>=1 and tmpy1>=0
  125.                             and self:mergeCellsIfPossible(i, tmpy1, i, tmpy2)) do
  126.                         tmpy2 = tmpy2-1
  127.                         tmpy1 = tmpy1-1
  128.                         moved = true
  129.                     end
  130.                 end
  131.             end
  132.         end
  133.     elseif direction=="right" then
  134.         for j=1,self.size do
  135.             for i=self.size,1,-1 do
  136.                 if self.grid[j][i]>0 then
  137.                     tmpx1, tmpx2 = i, i+1
  138.                     while (tmpx2<=self.size and tmpx1<=self.size
  139.                             and self:mergeCellsIfPossible(tmpx1, j, tmpx2, j)) do
  140.                         tmpx1 = tmpx1+1
  141.                         tmpx2 = tmpx2+1
  142.                         moved = true
  143.                     end
  144.                 end
  145.             end
  146.         end
  147.     elseif direction=="down" then
  148.         for j=self.size-1,1,-1 do
  149.             for i=1,self.size do
  150.                 if self.grid[j][i]>0 then
  151.                     tmpy1, tmpy2 = j, j+1
  152.                     while (tmpy2<=self.size and tmpy1<=self.size
  153.                             and self:mergeCellsIfPossible(i, tmpy1, i, tmpy2)) do
  154.                         tmpy2 = tmpy2+1
  155.                         tmpy1 = tmpy1+1
  156.                         moved = true
  157.                     end
  158.                 end
  159.             end
  160.         end
  161.     elseif direction=="left" then
  162.         for j=1,self.size do
  163.             for i=1,self.size do
  164.                 if self.grid[j][i]>0 then
  165.                     tmpx1, tmpx2 = i, i-1
  166.                     while (tmpx2>=0 and tmpx1>0
  167.                             and self:mergeCellsIfPossible(tmpx1, j, tmpx2, j)) do
  168.                         tmpx1 = tmpx1-1
  169.                         tmpx2 = tmpx2-1
  170.                         moved = true
  171.                     end
  172.                 end
  173.             end
  174.         end
  175.     end
  176.     self.filled = (self:countFilled() == self.size*self.size)
  177.     if self.filled then -- TODO : add check for no move available
  178.         print("Lost !")
  179.         if score>=bestScore then
  180.             document.markChanged()
  181.         end
  182.     else
  183.         if moved then
  184.             self:spawnNumber()
  185.         end
  186.     end
  187. end
  188.  
  189. function Game:resize()
  190.     self.gridOffset = .05*H
  191.     self.gridStepX = W/(self.size+1)
  192.     self.gridStepY = H/(self.size+1)
  193. end
  194.  
  195. function Game:paint(gc)
  196.     gc:setFont("sansserif", "b", W/25)
  197.     gc:setPen()
  198.     if not self.gridOffset then self:resize() end
  199.     for j=1,self.size do
  200.         for i=1,self.size do
  201.             gc:drawLine(.1*W, j*self.gridStepY-0.08*H+self.gridOffset, .9*W, j*self.gridStepY-0.08*H+self.gridOffset)
  202.             gc:drawLine(i*self.gridStepX-.1*W, .12*H+self.gridOffset, i*self.gridStepX-.1*W, .92*H+self.gridOffset)
  203.             if (self.grid[j][i] > 0) then
  204.                 gc:setColorRGB(uCol(colorTbl[self.grid[j][i]]))
  205.                 gc:fillRect(i*self.gridStepX-.1*W+1, j*self.gridStepY-0.08*H+self.gridOffset+1, self.gridStepX, self.gridStepY)
  206.                 if (newTileX == i and newTileY == j) then
  207.                     gc:setColorRGB(255,0,0)
  208.                     gc:drawRect(i*self.gridStepX-.1*W+1, j*self.gridStepY-0.08*H+self.gridOffset+1, self.gridStepX-2, self.gridStepY-2)
  209.                     gc:drawRect(i*self.gridStepX-.1*W+2, j*self.gridStepY-0.08*H+self.gridOffset+2, self.gridStepX-4, self.gridStepY-4)
  210.                 end
  211.                 gc:setColorRGB(0, 0, 0)
  212.                 gc:drawString(self.grid[j][i],
  213.                     i*(W/(self.size+1)) - gc:getStringWidth(self.grid[j][i])*.3,
  214.                     j*(H/(self.size+1)) + self.gridOffset  - gc:getStringHeight(self.grid[j][i])*.3,
  215.                     "top")
  216.             end
  217.         end
  218.     end
  219.     gc:setPen("medium", "smooth")
  220.     gc:drawLine((self.size+1)*self.gridStepX-.1*W, .12*H+self.gridOffset+1, (self.size+1)*self.gridStepX-.1*W, .92*H+self.gridOffset)
  221.     gc:drawLine(.1*W+1, (self.size+1)*self.gridStepY-0.08*H+self.gridOffset, .9*W, (self.size+1)*self.gridStepY-0.08*H+self.gridOffset)
  222.     gc:drawString("NspireLua2048", .1*W, .03*H, "top")
  223.     gc:setFont("sansserif", "b", W/30)
  224.     local str = "Score : " .. score
  225.     gc:drawString(str, .9*W-gc:getStringWidth(str), .005*H, "top")
  226.     str = "Best : " .. bestScore
  227.     gc:drawString(str, .9*W-gc:getStringWidth(str), .07*H, "top")
  228. end
  229.  
  230.  
  231. theGame = Game(4)
  232.  
  233.  
  234. ------------
  235. -- Events --
  236. ------------
  237.  
  238. function on.paint(gc)
  239.     theGame:paint(gc)
  240. end
  241.  
  242. function on.arrowKey(key)
  243.     if (key=="right" or key=="left" or key=="up" or key=="down") then
  244.         theGame:move(key)
  245.         platform.window:invalidate()
  246.     end
  247. end
  248.  
  249. function on.charIn(ch)
  250.     local tbl = {["2"]="down", ["4"]="left", ["8"]="up", ["6"]="right"}
  251.     on.arrowKey(tbl[ch])
  252.     platform.window:invalidate()
  253. end
  254.  
  255. function on.escapeKey()
  256.     theGame:restart()
  257.     platform.window:invalidate()
  258. end
  259.  
  260. function on.resize(w, h)
  261.     W, H = w, h
  262.     theGame:resize()
  263.     platform.window:invalidate()
  264. end
  265.  
  266. function on.save()
  267.     return {bestScore}
  268. end
  269.  
  270. function on.restore(data)
  271.     bestScore = data[1] or 0
  272. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement