【CSOL】2048挑战
if UI then
Screen = {x = UI.ScreenSize().width, y = UI.ScreenSize().height}
Ui_BoardText = {}
Ui_BoardBox = {}
-- 初始化游戏棋盘
function initBoard()
board = {}
for i = 1, 4 do
board[i] = {}
for j = 1, 4 do
board[i][j] = 0
end
end
end
-- 在空白位置随机生成一个新的数字(2或4)
function generateNewNumber()
local emptyCells = {}
for i = 1, 4 do
for j = 1, 4 do
if board[i][j] == 0 then
table.insert(emptyCells, {row = i, col = j})
end
end
end
if #emptyCells > 0 then
local randomCell = emptyCells[math.random(#emptyCells)]
board[randomCell.row][randomCell.col] = math.random() < 0.9 and 2 or 4
end
end
-- 检查游戏是否结束(无法再进行移动)
function isGameOver()
for i = 1, 4 do
for j = 1, 4 do
if board[i][j] == 0 then
return false
end
if i < 4 and board[i][j] == board[i + 1][j] then
return false
end
if j < 4 and board[i][j] == board[i][j + 1] then
return false
end
end
end
return true
end
-- 打印当前游戏棋盘
function printBoard()
for i = 1, 4 do
for j = 1, 4 do
print(board[i][j] .. "\t")
end
print()
end
end
function DrawBoard()
for i=1,4 do
for j=1,4 do
Ui_BoardBox[i] = UI.Box.Create()
Ui_BoardText[i]= UI.Text.Create()
Ui_BoardBox[i]:Set({x = Screen.x/3+i*80, y = Screen.y/3+j*80, width = Screen.x/20, height = Screen.y/15, r = 255, g = 255, b = 255, a = 255})
if board[i][j]~=0 then
Ui_BoardText[i]:Set({
text=tostring(board[i][j]),
align='center',
font='medium',
x=Screen.x/3+i*80,
y=Screen.y/3+j*80,
width=Screen.x/20,
height=Screen.y/15,
r=0,g=0,b=0,a=255
})
else
Ui_BoardText[i]:Set({
text="",
align='center',
font='medium',
x=Screen.x/3+i*80,
y=Screen.y/3+j*80,
width=Screen.x/20,
height=Screen.y/15,
r=0,g=0,b=0,a=255
})
end
end
end
end
Framework.UIPlug.OnUpdate:Register(function()
DrawBoard()
end)
-- 合并相同的数字
function mergeNumbers(row, col, drow, dcol)
if board[row][col] == 0 then
return
end
local currentRow, currentCol = row, col
local nextRow, nextCol = row + drow, col + dcol
while nextRow >= 1 and nextRow <= 4 and nextCol >= 1 and nextCol <= 4 do
if board[nextRow][nextCol] == 0 then
-- 如果下一个方块是空白,则继续前进
currentRow = nextRow
currentCol = nextCol
nextRow = currentRow + drow
nextCol = currentCol + dcol
elseif board[nextRow][nextCol] == board[currentRow][currentCol] then
-- 如果下一个方块与当前方块相等,则合并两个方块
board[currentRow][currentCol] = board[currentRow][currentCol] * 2
board[nextRow][nextCol] = 0
break
else
-- 如果下一个方块与当前方块不相等,则停止移动
break
end
end
-- 继续前进直到遇到边界或空白方块
while nextRow >= 1 and nextRow <= 4 and nextCol >= 1 and nextCol <= 4 and board[nextRow][nextCol] == 0 do
currentRow = nextRow
currentCol = nextCol
nextRow = currentRow + drow
nextCol = currentCol + dcol
end
-- 将当前方块移动到空白方块的位置
if currentRow ~= row or currentCol ~= col then
board[currentRow][currentCol] = board[row][col]
board[row][col] = 0
end
end
--[[
function mergeNumbers(row, col, drow, dcol)
if board[row][col] == 0 then
return
end
local currentRow, currentCol = row, col
local nextRow, nextCol = row + drow, col + dcol
-- 判断下一个方块是否存在且相等
while board[nextRow] and board[nextRow][nextCol] and board[currentRow][currentCol] == 0 do
-- 移动当前方块到下一个方块位置
board[currentRow][currentCol] = board[nextRow][nextCol]
board[nextRow][nextCol] = 0
currentRow = nextRow
currentCol = nextCol
nextRow = nextRow + drow
nextCol = nextCol + dcol
end
-- 如果下一个方块存在且值相等,则合并两个方块
if board[nextRow] and board[nextRow][nextCol] and board[currentRow][currentCol] == board[nextRow][nextCol] then
board[currentRow][currentCol] = board[currentRow][currentCol] * 2
board[nextRow][nextCol] = 0
end
end
]]
-- 移动棋盘
Framework.UIPlug.OnKeyDown:Register(function(inputs)
if inputs[UI.KEY.W]==true then
moveBoard("left")
printBoard() --更新棋盘
end
if inputs[UI.KEY.A]==true then
moveBoard("up")
printBoard()
end
if inputs[UI.KEY.S]==true then
moveBoard("right")
printBoard()
end
if inputs[UI.KEY.D]==true then
moveBoard("down")
printBoard()
end
end)
Framework.UIPlug.OnChat:Register(function(msg,signal)
if msg=="重新开始" then
initBoard()--初始化棋盘
end
end)
-- 移动棋盘
function moveBoard(direction)
if direction == "up" then
-- 向上移动棋盘
for i = 2, 4 do
for j = 1, 4 do
mergeNumbers(i, j, -1, 0)
end
end
elseif direction == "down" then
-- 向下移动棋盘
for i = 3, 1, -1 do
for j = 1, 4 do
mergeNumbers(i, j, 1, 0)
end
end
elseif direction == "left" then
-- 向左移动棋盘
for i = 1, 4 do
for j = 2, 4 do
mergeNumbers(i, j, 0, -1)
end
end
elseif direction == "right" then
-- 向右移动棋盘
for i = 1, 4 do
for j = 3, 1, -1 do
mergeNumbers(i, j, 0, 1)
end
end
end
generateNewNumber()
end
-- 游戏主循环
function gameLoop()
initBoard()
generateNewNumber()
local direction = ""
printBoard()
end
-- 启动游戏,传入移动方向参数
gameLoop("up") -- 这里可以传入上下左右四个方向之一,例如:"up", "down", "left", "right"
end