首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Othello有效移动算法不工作javascript

Othello有效移动算法不工作javascript
EN

Stack Overflow用户
提问于 2021-04-08 17:33:43
回答 1查看 37关注 0票数 1

我正在尝试编写一个奥赛罗游戏,现在我正在尝试编写返回有效位置的算法。我从检索旁边有黑色或白色石头的空方块的方向开始,但我的代码不能运行。我不知道我是否做错了什么,但似乎当我启动程序时,代码运行缓慢,因为算法过于杀伤力。我能为此做些什么呢?

以下是我的代码(顺便说一句,我使用React.js):

下面是正方形数组的数据结构:

代码语言:javascript
复制
history: [{
                squares: [
                    Array(8).fill(null),
                    Array(8).fill(null),
                    Array(8).fill(null),
                    [null,null,null,'black','white',null,null,null],
                    [null,null,null,'white','black',null,null,null],
                    Array(8).fill(null),
                    Array(8).fill(null),
                    Array(8).fill(null)
                ]
            }],

该算法(目前,只返回旁边有一块石头的方块(向上、向上、向右、向下、向左等)):

代码语言:javascript
复制
    checkMoveValidity(isBlack) {
        const history = this.state.history.slice(0, this.state.stepNumber + 1);
        const current = history[history.length - 1];
        const squares = current.squares.slice()

        // Get all the empty spaces
        const emptySpaces  = []
        for (var row=1;row<squares.length-1;row++) {
            for (var col=1;col<squares[row].length-1;col++) {
                if (!squares[row][col]) {
                    emptySpaces.push([row,col])
                }
            }
        }

        const cordAndDirs = []

        for (var sp=0;sp<emptySpaces.length;sp++) {
            const element = emptySpaces[sp]
            const elObject = {
                coordinates: element,
                directions: []
            }
            if (element === [0,0])
            {
                // Check down
                if (squares[1][0]) {
                    const downEls = []
                    for (var i=1;i<squares.length;i++) {
                        downEls.push([i,0])
                    }
                    elObject.directions.push(downEls)
                }
                // Check down right (diagonal)
                if (squares[1][1]) {
                    const drEls = []
                    for (var i=1;i<squares.length;i++) {
                        drEls.push([i,i])
                    }
                    elObject.directions.push(drEls)
                }
                // Check right
                if (squares[0][1]) {
                    const rightEls = []
                    for (var i=1;i<squares.length;i++) {
                        rightEls.push([0,i])
                    }
                    elObject.directions.push(rightEls)
                }
            }
            else if (emptySpaces[sp] === [0,7])
            {
                // Check down
                if (squares[1][7]) {
                    const downEls = []
                    for (var i=1;i<squares.length;i++) {
                        downEls.push([i,7])
                    }
                    elObject.directions.push(downEls)
                }
                // Check down left (diagonal)
                if (squares[1][6]) {
                    const drEls = []
                    for (var i=1;i<squares.length;i++) {
                        drEls.push([i,7 - i])
                    }
                    elObject.directions.push(drEls)
                }
                // Check left
                if (squares[0][6]) {
                    const leftEls = []
                    for (var i=1;i<squares.length;i++) {
                        leftEls.push([0,7 - i])
                    }
                    elObject.directions.push(leftEls)
                }
            }
            else if (emptySpaces[sp] === [7,0])
            {
                // Check up
                if (squares[6][0]) {
                    const upEls = []
                    for (var i=1;i<squares.length;i++) {
                        upEls.push([7 - i,0])
                    }
                    elObject.directions.push(upEls)
                }
                // Check up right
                if (squares[6][1]) {
                    const urEls = []
                    for (var i=1;i<squares.length;i++) {
                        urEls.push([7 - i,i])
                    }
                    elObject.directions.push(urEls)
                }
                // Check right
                if (squares[7][1]) {
                    const rightEls = []
                    for (var i=1;i<squares.length;i++) {
                        rightEls.push([7,i - 1])
                    }
                    elObject.directions.push(rightEls)
                }
            }
            else if (emptySpaces[sp] === [7,7])
            {
                //Check up
                if (squares[6][7]) {
                    const upEls = []
                    for (var i=1;i<squares.length;i++) {
                        upEls.push([7 - i,7])
                    }
                    elObject.directions.push(upEls)
                }
                // Check up left
                if (squares[6][6]) {
                    const ulEls = []
                    for (var i=1;i<squares.length;i++) {
                        ulEls.push([7 - i,7 - i])
                    }
                    elObject.directions.push(ulEls)
                }
                // Check left
                if (squares[7][6]) {
                    const leftEls = []
                    for (var i=1;i<squares.length;i++) {
                        leftEls.push([7,7 - i])
                    }
                    elObject.directions.push(leftEls)
                }
            }
            else
            {
                const crdsY = emptySpaces[sp][0]
                const crdsX = emptySpaces[sp][1]
                //Check up
                if (squares[crdsY - 1][crdsX]) {
                    const upEls = []
                    var i = 1
                    while (crdsY - i >= 0) {
                        upEls.push([crdsY - i,crdsX])
                        i++;
                    }
                    elObject.directions.push(upEls)
                }

                // Check up right
                if (squares[crdsY - 1][crdsX + 1]) {
                    const urEls = []
                    var i = 1
                    while (crdsY - i >= 0 && crdsX + i <= 7) {
                        urEls.push([crdsY - i,crdsX + i])
                        i++;
                    }
                    elObject.directions.push(urEls)
                }
                // Check right
                if (squares[crdsY][crdsX + 1]) {
                    const rightEls = []
                    var i = 1
                    while (crdsX + i <= 7) {
                        rightEls.push([crdsY,crdsX + i])
                        i++;
                    }
                    elObject.directions.push(rightEls)
                }
                // Check down right
                if (squares[crdsY + 1][crdsX + 1]) {
                    const rightEls = []
                    var i = 1
                    while (crdsY + i <= 7 && crdsX + i <= 7) {
                        rightEls.push([crdsY + i,crdsX + i])
                        i++;
                    }
                    elObject.directions.push(rightEls)
                }
                // Check down
                if (squares[crdsY + 1][crdsX]) {
                    const rightEls = []
                    var i = 1
                    while (crdsY + i <= 7) {
                        rightEls.push([crdsY + i,crdsX])
                        i++;
                    }
                    elObject.directions.push(rightEls)
                }
                // Check down left
                if (squares[crdsY + 1][crdsX - 1]) {
                    const rightEls = []
                    var i = 1
                    while (crdsY + i <= 7 && crdsX - i >= 0) {
                        rightEls.push([crdsY + i,crdsX - i])
                        i++;
                    }
                    elObject.directions.push(rightEls)
                }
                // Check left
                if (squares[crdsY][crdsX - 1]) {
                    const rightEls = []
                    var i = 1
                    while (crdsX - i >= 0) {
                        rightEls.push([crdsY,crdsX - i])
                        i++;
                    }
                    elObject.directions.push(rightEls)
                }
                // Check up left
                if (squares[crdsY  - 1][crdsX - 1]) {
                    const rightEls = []
                    var i = 1
                    while (crdsY - i >= 0 && crdsX - i >= 0) {
                        rightEls.push([crdsY - i,crdsX - i])
                    }
                    elObject.directions.push(rightEls)
                }
                
            }

            if (elObject.directions.length === 0) {
                continue
            } else {
                cordAndDirs.push(elObject)
            }
        }

        return cordAndDirs
    }

这就是我尝试实现它的地方:

代码语言:javascript
复制
render() {
        const history = this.state.history
        const current = history[this.state.stepNumber]

        // Return the game scene here
        return (
            <div className="master-container">
                <GameBoard squares={current.squares} onClick={(row,col) => {
                    const elems = this.checkMoveValidity(this.state.blackIsNext)
                    console.log(elems)
                }}/>
            </div>
        )
    }

顺便说一句,我检查了命令是否到达了GameBoard组件内部的元素。它没有任何问题,问题发生在我实现算法的时候。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-09 00:11:56

我做到了。这很简单,我甚至简化了代码。我忘了在最后一个while循环中添加i++;,我永远被卡住了,然后我不得不处理边框,这样我就不会收到错误。例如,当上边界方块下面没有方块时,我不能//Check up

这就是我如何更改条件语句的:

代码语言:javascript
复制
for (var sp=0;sp<emptySpaces.length;sp++) {
            const element = emptySpaces[sp]
            const elObject = {
                coordinates: element,
                directions: []
            }
                
            // You apply the same methods for all the other squares here (can't use constant numbers in 'squares' now...)
            const crdsY = emptySpaces[sp][0]
            const crdsX = emptySpaces[sp][1]
            var i;
            //Check up
            if (crdsY !== 0 && squares[crdsY - 1][crdsX]) {
                const upEls = []
                i = 1
                while (crdsY - i >= 0) {
                    upEls.push([crdsY - i,crdsX])
                    i++;
                }
                elObject.directions.push(upEls)
            }

            // Check up right
            if (crdsX !== 7 && crdsY !== 0 && squares[crdsY - 1][crdsX + 1]) {
                const urEls = []
                i = 1
                while (crdsY - i >= 0 && crdsX + i <= 7) {
                    urEls.push([crdsY - i,crdsX + i])
                    i++;
                }
                elObject.directions.push(urEls)
            }
            // Check right
            if (crdsX !== 7 && squares[crdsY][crdsX + 1]) {
                const rightEls = []
                i = 1
                while (crdsX + i <= 7) {
                    rightEls.push([crdsY,crdsX + i])
                    i++;
                }
                    elObject.directions.push(rightEls)
            }
            // Check down right
            if (crdsX !== 7 && crdsY !== 7 && squares[crdsY + 1][crdsX + 1]) {
                const rightEls = []
                i = 1
                while (crdsY + i <= 7 && crdsX + i <= 7) {
                    rightEls.push([crdsY + i,crdsX + i])
                    i++;
                }
                elObject.directions.push(rightEls)
            }
            // Check down
            if (crdsY !== 7 && squares[crdsY + 1][crdsX]) {
                const rightEls = []
                i = 1
                while (crdsY + i <= 7) {
                    rightEls.push([crdsY + i,crdsX])
                    i++;
                }
                elObject.directions.push(rightEls)
            }
            // Check down left
            if (crdsY !== 7 && crdsX !== 0 && squares[crdsY + 1][crdsX - 1]) {
                const rightEls = []
                i = 1
                while (crdsY + i <= 7 && crdsX - i >= 0) {
                    rightEls.push([crdsY + i,crdsX - i])
                    i++;
                }
                elObject.directions.push(rightEls)
            }
            // Check left
            if (crdsX !== 0 && squares[crdsY][crdsX - 1]) {
                const rightEls = []
                i = 1
                while (crdsX - i >= 0) {
                    rightEls.push([crdsY,crdsX - i])
                    i++;
                }
                elObject.directions.push(rightEls)
            }
            // Check up left
            if (crdsX !== 0 && crdsY !== 0 && squares[crdsY  - 1][crdsX - 1]) {
                const rightEls = []
                i = 1
                while (crdsY - i >= 0 && crdsX - i >= 0) {
                    rightEls.push([crdsY - i,crdsX - i])
                    i++;
                }
                elObject.directions.push(rightEls)
            }
            

            if (elObject.directions.length !== 0) {
                cordAndDirs.push(elObject)
            }

我还更改了游戏板的onClick属性,如下所示:

代码语言:javascript
复制
return (
            <div className="master-container">
                <GameBoard squares={current.squares} onClick={(row,col) => {
                    const elems = this.checkElementsAround(this.checkMoveValidity(this.state.blackIsNext))
                    console.log(elems)
                    
                    for (let el=0;el<elems.length;el++) {
                        if (row === elems[el].coordinates[0] && col === elems[el].coordinates[1]) {
                            this.handleMove(row,col);
                            break
                        }
                    }
                }}/>
            </div>
        )
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67001238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档