控制台版2014
欢迎指教
操作:
l -- move left
r -- move right
u -- move upward
d -- move downward
效果:
源代码:
core.lua
#!/usr/local/bin/lua
local score = 0
local rowcount = 4
local colcount = 4
local grid = {
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
}
local function GetScore()
return score
end
local function GetGrid()
return grid;
end
local function IsRowHasEmpty(r)
for i=1, colcount do
if grid[r][i] == 0 then
return true
end
end
return false
end
local function GetHasEmptyRow()
local n=1;
local res = {}
for i=1, rowcount do
if IsRowHasEmpty(i) then
res[n] = i;
n = n + 1
end
end
return res
end
local function GetEmptyCol(row)
local n=1
local res = {}
for i=1,colcount do
if grid[row][i] == 0 then
res[n] = i
n = n + 1
end
end
return res
end
local function RandEmptyRow()
local emptyRows = GetHasEmptyRow()
if #emptyRows == 0 then
return nil
end
r = math.random(1, #emptyRows)
return emptyRows[r]
end
local function RandEmptyCol(row)
local emptyCols = GetEmptyCol(row)
col = math.random(1, #emptyCols)
return emptyCols[col]
end
local function Initialize()
math.randomseed(os.time())
r = RandEmptyRow()
c = RandEmptyCol(r)
grid[r][c] = 2
r = RandEmptyRow()
c = RandEmptyCol(r)
grid[r][c] = 2
score = 4
end
local enumDir = {
left = 0,
right = 1,
up = 2,
down = 3,
}
local function GetNextRandValue()
r = math.random(1,2 )
if r == 2 then
return 4
end
return 2
end
local function NextStep()
r = RandEmptyRow()
if r then
c = RandEmptyCol(r)
grid[r][c] = GetNextRandValue()
return grid[r][c]
end
return nil
end
local function MoveDown()
local res = 0
local cc = colcount
local perval = 0
local bchanged = false
for c=1, colcount do
for r=rowcount, 1,-1 do
res = 0
perval = grid[r][c]
for rr=r, 1,-1 do
if res == 0 then
if grid[rr][c] ~= 0 then
res = grid[rr][c]
grid[rr][c] = 0
end
else
if grid[rr][c]== res then
res = res + grid[rr][c]
score = score + res
grid[rr][c] = 0
break
elseif grid[rr][c] ~= 0 then
break
end
end
end
if perval ~= res then
bchanged = true
end
grid[r][c] = res
end
end
return bchanged
end
local function MoveUp()
local res = 0
local cc = colcount
local perval = 0
local bchanged = false
for c=1, colcount do
for r=1, rowcount do
res = 0
perval = grid[r][c]
for rr=r, rowcount do
if res == 0 then
if grid[rr][c] ~= 0 then
res = grid[rr][c]
grid[rr][c] = 0
end
else
if grid[rr][c]== res then
res = res + grid[rr][c]
score = score + res
grid[rr][c] = 0
break
elseif grid[rr][c] ~= 0 then
break
end
end
end
if perval ~= res then
bchanged = true
end
grid[r][c] = res
end
end
return bchanged
end
local function MoveLeft()
local res = 0
local cc = colcount
local perval = 0
local bchanged = false
for r=1, rowcount do
for c= 1, colcount do
res = 0
perval = grid[r][c]
for cc=c, colcount do
if res == 0 then
if grid[r][cc] ~= 0 then
res = grid[r][cc]
grid[r][cc] = 0
end
else
if grid[r][cc]== res then
res = res + grid[r][cc]
score = score + res
grid[r][cc] = 0
break
elseif grid[r][cc] ~= 0 then
break
end
end
end
if res ~= perval then
bchanged = true
end
grid[r][c] = res
end
end
return bchanged
end
local function MoveRight()
-- print("Move Right..")
local res = 0
local cc = colcount
local perval = 0
local bchanged = false
for r=1, rowcount do
for c=colcount, 1,-1 do
res = 0
perval = grid[r][c]
for cc=c, 1, -1 do
if res == 0 then
if grid[r][cc] ~= 0 then
res = grid[r][cc]
grid[r][cc] = 0
end
else
if grid[r][cc]== res then
res = res + grid[r][cc]
score = score + res
grid[r][cc] = 0
break
elseif grid[r][cc] ~= 0 then
break
end
end
end
if perval ~= res then
bchanged = true
end
-- print("Row:"..r.." Col: "..c.." "..res)
grid[r][c] = res
end
end
return bchanged
end
local function Run(dir)
-- print("Move: "..dir)
local res = false
if dir == enumDir.left then
res = MoveLeft()
elseif dir == enumDir.right then
res = MoveRight()
elseif dir == enumDir.up then
res = MoveUp()
elseif dir == enumDir.down then
res = MoveDown()
end
if res then
return NextStep()
end
return 0
end
core2014 = {
score = GetScore,
grid = GetGrid,
init = Initialize,
run = Run,
dir = enumDir
}
game_console.lua
#!/usr/local/bin/lua
dofile("core_2014.lua")
function ShowGrid()
for i=1,4 do
--for j=1,4 do
print(core2014.grid()[i][1].." "..
core2014.grid()[i][2].." "..
core2014.grid()[i][3].." "..
core2014.grid()[i][4])
--end
end
end
function ShowScore()
print("Score: "..core2014.score())
end
function Run()
local c = io.read()
local d = core2014.dir.left
if c=='r'then
d = core2014.dir.right
elseif c=='u' then
d = core2014.dir.up
elseif c=='d' then
d = core2014.dir.down
elseif c=='l' then
d = core2014.dir.left
else
print("error input")
return 1
end
return core2014.run(d)
end
core2014.init()
ShowScore()
ShowGrid()
while Run() do
ShowScore()
ShowGrid()
end