Lua implementation of the 2048 small game _lua

Source: Internet
Author: User
Tags lua

The 2048 small games that Lua implements can be played in an environment where LUA scripts can be run.

Copy Code code as follows:

--[[=============================================================================
# Filename:2048.lua
# Desc:lua Console 2048
# Author:hanxi
# email:hanxi.info@gmail.com
# Homepage:http://www.hanxi.info
# version:0.0.1
# lastchange:2014-04-28 11:05:09
# History:
=============================================================================]]
Local function Initgrid (m,n)
Local Grid = {}
For I=1,m do
If not grid[i] Then
Grid[i] = {}
End
For J=1,n do
GRID[I][J] = 0
End
End
return grid
End
Local function PrintGrid (GRID)
Local Celllen = 8--The number of characters per grid occupied
Local gridstrlines = {}
Table.insert (Gridstrlines, "-------------------------------------")
For I,row in Ipairs (GRID) do
Local line = {}
For _,num in Ipairs (row) do
If Num==0 Then
Local pres = ""
For Tmp=1,celllen do
pres = pres. " "
End
Local s = String.Format ("%s", pres)
Table.insert (Line,s)
Else
Local s = tostring (num)
Local L = String.len (s)
Local L = (celllen-l)/2
Local Prel = Math.floor (l)
Local SUFL = Math.ceil (l)
Local pres = ""
For Tmp=1,prel do
pres = pres. " "
End
Local SUFS = Pres
If Sufl>prel Then
Sufs = pres. " "
End
Local s = String.Format ("%s%s%s", Pres,s,sufs)
Table.insert (Line,s)
End
End
Local line = Table.concat (line, "|")
line = "|". Line.. "|"
Table.insert (Gridstrlines,line)
Table.insert (Gridstrlines, "-------------------------------------")
End
Local GRIDSTR = Table.concat (gridstrlines, "\ n")
Print (GRIDSTR)
End
Local function Randomgrid (GRID)
Local m = #grid
Local n = #grid [1]
For I=1,m do
For J=1,n do
Local R = math.random (1,5)
Local num = 2^r
GRID[I][J] = num
End
End
End
Local function Getrandomzeropos (GRID)
Local m = #grid
Local n = #grid [1]
Local zeros = {}
For I=1,m do
For J=1,n do
If Grid[i][j]==0 Then
Table.insert (Zeros,{i=i,j=j})
End
End
End
If #zeros >0 Then
Local R = Math.random (1, #zeros)
Return ZEROS[R].I,ZEROS[R].J
End
End
Local function Randomnum (GRID)
Local i,j = Getrandomzeropos (GRID)
If I and J then
Local r = Math.random ()
If r<0.9 Then
GRID[I][J] = 2
Else
GRID[I][J] = 4
End
Return i,j
End
End
Local function MoveLeft (GRID)
Print ("==============moveleft===============")
Local m = #grid
Local n = #grid [1]
For I=1,m do
Local line = {}
For J=1,n do
If Grid[i][j]~=0 Then
Table.insert (Line,grid[i][j])
End
End
Local k= #line
For J=1,n do
If J<=k Then
GRID[I][J] = Line[j]
Else
GRID[I][J] = 0
End
End
For J=1,k-1 do
If GRID[I][J]==GRID[I][J+1] Then
GRID[I][J+1] = Grid[i][j] + grid[i][j+1]
For x=j,n-1 do
GRID[I][X] = grid[i][x+1]
End
Grid[i][n] = 0
End
End
End
End
Local function MoveRight (GRID)
Print ("==============moveright==============")
Local m = #grid
Local n = #grid [1]
For I=1,m do
Local line = {}
For J=n,1,-1 do
If Grid[i][j]~=0 Then
Table.insert (Line,grid[i][j])
End
End
Local k = #line
For J=n,1,-1 do
If N-j+1<=k Then
GRID[I][J] = line[n-j+1]
Else
GRID[I][J] = 0
End
End
For J=n,n-k+2,-1 do
If GRID[I][J]==GRID[I][J-1] Then
GRID[I][J-1] = Grid[i][j] + grid[i][j-1]
For X=j,2,-1 do
GRID[I][X] = grid[i][x-1]
End
GRID[I][1] = 0
End
End
End
End
Local function moveUp (GRID)
Print ("===============moveup================")
Local m = #grid
Local n = #grid [1]
For J=1,n do
Local line = {}
For I=1,m do
If Grid[i][j]~=0 Then
Table.insert (Line,grid[i][j])
End
End
Local k = #line
For I=1,m do
If I<=k Then
GRID[I][J] = Line[i]
Else
GRID[I][J] = 0
End
End
For I=1,k-1 do
If GRID[I][J]==GRID[I+1][J] Then
GRID[I+1][J] = Grid[i][j] + grid[i+1][j]
For X=i,m-1 do
GRID[X][J] = Grid[x+1][j]
End
GRID[M][J] = 0
End
End
End
End
Local function MoveDown (GRID)
Print ("==============movedown===============")
Local m = #grid
Local n = #grid [1]
For J=1,n do
Local line = {}
For I=m,1,-1 do
If Grid[i][j]~=0 Then
Table.insert (Line,grid[i][j])
End
End
Local k = #line
For I=m,1,-1 do
If M-i+1<=k Then
GRID[I][J] = line[m-i+1]
Else
GRID[I][J] = 0
End
End
For I=m,m-k+2,-1 do
If GRID[I][J]==GRID[I-1][J] Then
GRID[I-1][J] = Grid[i][j] + grid[i-1][j]
For X=i,2,-1 do
GRID[X][J] = Grid[x-1][j]
End
GRID[1][J] = 0
End
End
End
End
Local function Canmove (GRID)
Local m = #grid
Local n = #grid [1]
For I=1,m do
For J=1,n do
If Grid[i][j]==0 Then
return True
End
if (I<m and J<n)
and (Grid[i][j]==grid[i][j+1]
or grid[i][j]==grid[i+1][j]) then
return True
End
End
End
return False
End
Local function Main ()
Local Grid = Initgrid (4,4)
Randomnum (GRID)
PrintGrid (GRID)
Io.write ("Next Step ' a" [←], ' w ' [↑], ' s ' [↓], ' d ' [→], ' q ' [exit] >>]
Local input = Io.read ()
While input~= "Q" does
If input== "a" or input== "W" or input== "s" or input== "D" then
If input== "a" then
MoveLeft (GRID)
ElseIf input== "W" Then
MoveUp (GRID)
ElseIf input== "S" Then
MoveDown (GRID)
ElseIf input== "D" then
MoveRight (GRID)
End
Randomnum (GRID)
PrintGrid (GRID)
Else
Print ("error input.") Please input ' a ' [←] or ' w ' [↑] or ' s ' [↓] or ' d ' [→] or ' q ' [Exit] ']
End
Io.write ("Next Step ' a" [←], ' w ' [↑], ' s ' [↓], ' d ' [→], ' q ' [exit] >>]
input = Io.read ()
End
End
Main ()

The above is the full content of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.