lua实现的2048小游戏_Lua

lua实现的2048小游戏,只要可以运行lua脚本的环境下都可以玩。

复制代码 代码如下:

--[[=============================================================================
#     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  -- 每个格子占用字符数
    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" do
        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()

以上就是本文所述的全部内容了,希望大家能够喜欢。

时间: 2024-09-15 01:45:34

lua实现的2048小游戏_Lua的相关文章

javascript版2048小游戏

 本文给大家分享的是使用javascript制作的2048小游戏的代码,仅仅是想锻炼下自己的编程代码风格,尽量做的规范些,小伙伴们多多给些建议.     没有技术含量,只是用来练习代码逻辑的.为了代码结构清晰,我把逻辑控制部分写在全局变量里,用户界面操作封装在UI对象里,大概就这样了.仅供参考.工作时候,我的编码风格有人吐槽太乱了,所以我想试着写一个不是那么乱的东西出来..   代码如下: <HTML> <head> <title>2048 DEMO</title

Android编写2048小游戏_Android

先来说说2048游戏规则: 开始时棋盘内随机出现两个数字,出现的数字仅可能为2或4 玩家可以选择上下左右四个方向,若棋盘内的数字出现位移或合并,视为有效移动 玩家选择的方向上若有相同的数字则合并,每次有效移动可以同时合并,但不可以连续合并 合并所得的所有新生成数字想加即为该步的有效得分 玩家选择的方向行或列前方有空格则出现位移 每有效移动一步,棋盘的空位(无数字处)随机出现一个数字(依然可能为2或4) 棋盘被数字填满,无法进行有效移动,判负,游戏结束 棋盘上出现2048,判胜,游戏结束 下面代码

python实现2048小游戏_python

2048的python实现.修改自某网友的代码,解决了原网友版本的两个小bug: 1. 原版游戏每次只消除一次,而不是递归消除.如 [2 ,2 ,2 ,2] 左移动的话应该是 [4, 4, 0, 0] , 而不是[8 , 0 , 0 ,0] 2. 对游戏结束的侦测有bug,已经改正. 2048game.py # -*- coding: utf-8 -*- """ Created on Tue Jul 1 14:15:39 2014 @author: kelvin "

Java完美实现2048小游戏_java

完美地模仿了2048游戏,是根据网友的一个2048改的. Block.java import javax.swing.*; import java.awt.*; public class Block extends JLabel { private int value; public Block() { value = 0;//初始化值为0 setFont(new Font("font", Font.PLAIN, 40));//设定字体 setBackground(Color.gray

Android实现2048小游戏_Android

本文实例介绍了Android实现2048小游戏的相关代码,分享给大家供大家参考,具体内容如下 根据界面,主要实现4*4的格子方块比较麻烦,其他的都挺简单的.总体为实现4*4的格子,自定义GridLayout,并在其中添加触摸监听事件,进行一系列的操作,从而实现游戏的逻辑,最后再添加动画效果即可完成. 下面是设计思路: 一.GameView的设计 首先自定义一个类,继承GridLayout,添加两个构造方法 public class GameView extends GridLayout { //

Javascript编写2048小游戏_javascript技巧

去年2048很火, 本来我也没玩过, 同事说如果用JS写2048 只要100多行代码: 今天试了一下, 逻辑也不复杂, 主要是数据构造函数上的数据的各种操作, 然后通过重新渲染DOM实现界面的更新, 整体不复杂, JS,css,和HTML合起来就300多行: 界面的生成使用了underscore.js的template方法, 使用了jQuery,主要是DOM的选择和操作以及动画效果,事件的绑定只做了PC端的兼容,只绑定了keydown事件: 把代码放到github-page上, 通过点击这里查看

Android高仿2048小游戏实现代码_Android

刚开始进入Splash界面: 1.SplashActivity.Java(两秒后进入开始界面,Splash界面的布局只有一个图片,在博客后,会展示给大家看) public class SplashActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.a

使用Javascript写的2048小游戏_javascript技巧

最近心血来潮,项目结束了,使用javascript写个小游戏,练练收吧,写的不好还请各位大侠给出批评建议. HTML代码如下 <!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8" /> <link rel="stylesheet" href="2048.css"/> <scri

Android实现2048小游戏

本文实例介绍了Android实现2048小游戏的相关代码,分享给大家供大家参考,具体内容如下 根据界面,主要实现4*4的格子方块比较麻烦,其他的都挺简单的.总体为实现4*4的格子,自定义GridLayout,并在其中添加触摸监听事件,进行一系列的操作,从而实现游戏的逻辑,最后再添加动画效果即可完成. 下面是设计思路: 一.GameView的设计 首先自定义一个类,继承GridLayout,添加两个构造方法 public class GameView extends GridLayout { //