美文网首页
使用cocos2d-lua 实现 五子棋

使用cocos2d-lua 实现 五子棋

作者: 和歌先生 | 来源:发表于2019-03-19 09:46 被阅读0次

    常量

    local CHESSMAN_TYPE = {
        EMPTY = 0,
        BLACK = -1,
        WHITE = 1,
    }
    
    local PLAY_TYPE ={
        MAN = -1,
        AI = 1,
    }
    
    
    local GAMESTATUS = {
        PLAYING = 1,
        WIN  = 2,
        DEAD  = 3,
    }
    
    local BoardSizeNum = 15
    

    初始化界面


    image.png

    初始化棋盘数据


    image.png

    AI的下棋的函数

    function GameGobangScene:actionByAI(row, col)
    
        self.playerFlag = PLAY_TYPE.AI
    
        self:calcuateScore()
    
        local maxScore = 0
    
        local maxPoints = {}
    
        for row = 1, BoardSizeNum do
            for col = 1, BoardSizeNum do
                if self.gameMap[row][col] == 0 then
                    if self.scoreMap[row][col] > maxScore then
                        maxPoints = {}
                        maxScore = self.scoreMap[row][col]
                        local t = {row , col}
                        table.insert(maxPoints, t)
                    elseif self.scoreMap[row][col] == maxScore then
                        local t = {row , col}
                        table.insert(maxPoints, t)
                    end
                end
            end
        end
    
        math.randomseed(os.time())
    
        local index = math.random(#maxPoints)
    
        local aiRow = maxPoints[index][1]
        local aiCol = maxPoints[index][2]
    
        local randTime = math.random()
    
        self:performWithDelay(function ()
        
            local posX = self.minX + (self.gap * (aiRow - 1))
            local posY = self.minY + (self.gap * (aiCol - 1))
            self.gameMap[aiRow][aiCol] = CHESSMAN_TYPE.WHITE
    
            self:createChessMan(CHESSMAN_TYPE.WHITE, posX, posY)
            printInfo("GameGobangScene:actionByAI 白字棋子在棋盘  ===========> "..aiRow .. " , ".. aiCol)
            self:showGameMapData()
    
            self:performWithDelay(function ()
                
                self.playerFlag = PLAY_TYPE.MAN 
                
                if self:isWin(aiRow, aiCol, CHESSMAN_TYPE.WHITE) then
                    self:gameOver()
                end
    
                if self:isDeadGame() then
                    self:gameOver()
                end
    
            end, 0.2)
    
    
            -- body
        end, randTime)
    end
    

    AI的评分函数,通过评分来确定下子
    关于ai方面的代码 引用

    https://blog.csdn.net/u012234115/article/details/53871009

    function GameGobangScene:calcuateScore()
    
        local personNum = 0
        local botNum = 0
        local emptyNum = 0
    
        self.scoreMap = {}
    
        for i = 1, BoardSizeNum, 1  do
            local lineScore = {}
            for j = 1, BoardSizeNum, 1 do
                 table.insert(lineScore, 0)
            end
            self.scoreMap[i] = lineScore
        end
    
        for row = 1, BoardSizeNum do
            for col = 1, BoardSizeNum do
    
                if row > 1 and col > 1 and self.gameMap[row][col] == 0 then
    
                    for y = -1, 1 do
                        for x = -1,  1 do
                            personNum = 0
                            botNum = 0
                            emptyNum = 0
    
                            if not (y == 0 and x == 0) then
    
                                for i = 1, 4 do
                                    if row + i * y > 0 and row + i * y < BoardSizeNum and
                                       col + i * x > 0 and col + i * x < BoardSizeNum and
                                       self.gameMap[row + i * y][col + i * x] == -1 then
    
                                       personNum = personNum + 1
    
                                    elseif row + i * y > 0 and row + i * y < BoardSizeNum and
                                           col + i * x > 0 and col + i * x <BoardSizeNum and
                                           self.gameMap[row + i * y][col + i * x] == 0 then
    
                                           emptyNum = emptyNum + 1
                                           break
                                    else
                                        break
                                    end
                                end
    
    
                                for i = 1, 4 do
                                    if row - i * y > 0 and row - i * y < BoardSizeNum and
                                       col - i * x > 0 and col - i * x < BoardSizeNum and
                                       self.gameMap[row - i * y][col - i * x ] == -1 then
    
                                       personNum = personNum + 1
    
                                    elseif row - i * y > 0 and row - i * y < BoardSizeNum and
                                           col - i * x > 0 and col - i * x < BoardSizeNum and
                                           self.gameMap[row - i * y][col - i * x] == CHESSMAN_TYPE.EMPTY then
    
                                           emptyNum = emptyNum + 1
                                           break
                                    else
                                        break
                                    end
                                end
    
                                if personNum == 1 then
                                    self.scoreMap[row][col] = self.scoreMap[row][col] + 10
                                elseif personNum == 2 then
    
                                    if  emptyNum == 1 then
                                        self.scoreMap[row][col] = self.scoreMap[row][col] + 30
                                    elseif emptyNum == 2 then
                                        self.scoreMap[row][col] = self.scoreMap[row][col] + 40
                                    end
                                elseif personNum ==  3 then
    
                                    if emptyNum == 1 then
                                        self.scoreMap[row][col] = self.scoreMap[row][col] + 60
                                    elseif emptyNum == 2 then
                                        self.scoreMap[row][col] = self.scoreMap[row][col] + 110
                                    end
    
                                elseif personNum == 4  then
                                    self.scoreMap[row][col] = self.scoreMap[row][col] + 10100;
                                end
    
                                emptyNum = 0
    
                                -- 对ai评分
    
    
                                for i = 1, 4 do
                                    if row + i * y > 0 and row + i * y < BoardSizeNum and
                                       col + i * x > 0 and col + i * x < BoardSizeNum and
                                       self.gameMap[row + i * y][col + i * x] == 1 then
    
                                       botNum = botNum + 1
    
                                    elseif row + i * y > 0 and row + i * y < BoardSizeNum and
                                           col + i * x > 0 and col + i * x < BoardSizeNum and
                                           self.gameMap[row + i * y][col + i * x] == 0 then
    
                                        emptyNum = emptyNum + 1
                                        break
                                    else
                                        break
    
                                    end
                                end
    
    
                                for i = 1, 4 do
                                    if row - i * y > 0 and row - i * y < BoardSizeNum and
                                       col - i * x > 0 and col - i * x < BoardSizeNum and
                                       self.gameMap[row - i * y][col - i * x] == 1 then -- Ai的子
    
                                       botNum = botNum + 1
    
                                    elseif row - i * y > 0 and row - i * y < BoardSizeNum and
                                           col - i * x > 0 and col - i * x < BoardSizeNum and
                                           self.gameMap[row - i * y][col - i * x] == 0 then
    
                                           emptyNum = emptyNum + 1
                                           break
                                    else
                                        break
                                    end
    
                                end
    
    
                                if botNum == 0 then
    
                                    self.scoreMap[row][col] = self.scoreMap[row][col] + 5
    
                                elseif botNum == 1 then
    
                                    self.scoreMap[row][col] = self.scoreMap[row][col] + 10
    
                                elseif botNum == 2 then
    
                                    if  emptyNum == 1 then
    
                                        self.scoreMap[row][col] = self.scoreMap[row][col] + 25
    
                                    elseif emptyNum == 2 then
    
                                        self.scoreMap[row][col] = self.scoreMap[row][col] + 50
    
                                    end
    
                                elseif botNum == 3 then
    
                                    if emptyNum == 1 then
                                          self.scoreMap[row][col] = self.scoreMap[row][col] + 55
    
                                    elseif emptyNum == 2 then
                                        self.scoreMap[row][col] = self.scoreMap[row][col] + 100
                                    end
    
                                elseif botNum >= 4 then
                                    self.scoreMap[row][col] = self.scoreMap[row][col] + 10050
                                end
    
                            end
                        end
                    end
                end
            end
        end
    end
    

    结算函数

    function GameGobangScene:isWin(row, col, chessType)
        -- Todo 计算是否胜利 判断黑方 白方的棋子成功连成一片
        local i = row
        local j = col
        local count = 0
    
        --[[ 判断水平方向 ]] 
    
        while i > 0 and self.gameMap[i][j] == chessType do
            i = i - 1
            count = count + 1
        end
    
        i = row + 1
    
        while i <= 15 and self.gameMap[i][j] == chessType do
            i = i + 1
            count = count + 1
        end
    
    
        if count >= 5 then
            return true
        end
    
        --[[ 判断竖直方向 ]] 
    
        i = row
        count = 0
    
        while j > 0 and self.gameMap[i][j] == chessType do
            j = j - 1
            count = count + 1
        end
    
        j = col + 1
    
        while j <= 15 and self.gameMap[i][j] == chessType do
            j = j + 1
            count = count + 1
        end
    
        if count >= 5 then
            return true
        end
    
        --[[ 判断左上右下方向 ]] 
    
        j = col
        count = 0
    
        while i > 0 and j > 0 and self.gameMap[i][j] == chessType do
            i = i - 1
            j = j - 1
            count = count + 1
        end
    
        i = row + 1
        j = col + 1
    
        while j <= 15 and i <= 15 and self.gameMap[i][j] == chessType do
            i = i + 1
            j = j + 1
            count = count + 1
        end
    
        if count >= 5 then
            return true
        end
    
        --[[ 判断右上左下方向 ]] 
    
        i = row
        j = col
        count = 0
    
        while i <= 15 and j > 0 and self.gameMap[i][j] ==  chessType do
            i = i + 1
            j = j - 1
            count = count + 1
        end
    
        i = row - 1
        j = col + 1
    
        while i > 0 and j <= 15 and self.gameMap[i][j] == chessType do
            i = i - 1
            j = j + 1
            count = count + 1
        end
    
        if count >= 5 then
            return true
        end
    
    
        return false
    end
    

    触摸检测

    function GameGobangScene:onTouchBegan(event)
    
        -- Todo 要判断 棋子归属于那一个点,生成棋子
        self.startX = event.x
        self.startY = event.y
    
        local x = self.startX
        local y = self.startY
    
        if self.gameStatus ~= GAMESTATUS.PLAYING then
            return 
        end
    
    
        if x >= self.minX and  x <= self.maxX and  y >= self.minY and y <= self.maxY and self.playerFlag ==  PLAY_TYPE.MAN then
    
            printInfo("GameGobangScene:onTouchBegan begin --- pos(x = %d, y = %d )",self.startX, self.startY)
    
            local mx = (x - self.minX) / self.gap
            local row = math.round(mx) + 1
            if mx - row > 0.4 then
                row = row + 1
            end
    
    
            local my = (y - self.minY) / self.gap
            local col = math.round(my) + 1
            if my - col > 0.4 then
                col = col +1
            end
    
    
            printInfo("GameGobangScene:onTouchBegan 黑子棋子在棋盘  ===========> "..row .. " , ".. col);
    
            local posX = self.minX + (self.gap * (row - 1))
            local posY = self.minY + (self.gap * (col - 1))
    
            if self.gameMap[row][col] == CHESSMAN_TYPE.EMPTY then
    
                if self.playerFlag == PLAY_TYPE.MAN then
    
                    self.gameMap[row][col] = CHESSMAN_TYPE.BLACK
                    self:createChessMan(CHESSMAN_TYPE.BLACK, posX, posY)
                end
    
                self:actionByAI(row, col)
    
                if self:isWin(row, col, CHESSMAN_TYPE.BLACK) then
                    self.score = self.score + 100
                    self.textScoreInScoreNode:setString("当前分数:"..self.score)
                    self:gameOver()
                elseif  self:isDeadGame() then
                    self.score = 0
                    self:gameOver()
                end
        
            end
    
    
    
        end 
    
    end
    
    

    相关文章

      网友评论

          本文标题:使用cocos2d-lua 实现 五子棋

          本文链接:https://www.haomeiwen.com/subject/swbwmqtx.html