lua----2014----控制台版

本文介绍了一个控制台版2014游戏的基本操作、效果和源代码实现,包括移动、得分、游戏网格初始化、随机空位生成、上下左右移动逻辑等核心功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

控制台版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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值