Javascript大数组挂起

时间:2016-08-24 00:10:00

标签: javascript html arrays loops memory

我一直在为我的班级做一个项目,而且我已经在理论上完成了它,但它有一些问题,它没有像我想的那样快速执行。任务如下:你有一个10x10的板来进行移动。你随机上下左右。如果一个移动让你离开董事会跳过它。这样做直到采取1,000,000步或者到达电路板的右上角。我们还应该计算单个图块接收的最大步数,并且最小值相同。我使用2D数组完成了这项工作,它有时会计数并输出,但需要多次按下才能获得输出。我不确定这是否是与我如何访问2D阵列以跟踪步数有关的内存分配错误。我对javascript相对较新,所以我不知道我的方式是否有效。

代码

<!DOCTYPE html>
<html>
<head>

</head>
<body>

    <h1>Path Game</h1>

    <!-- Starts Game -->
    <button onclick="doGame()">Click Here to start Game</button>
    <p id="MaxSteps"></p>
    <p id="MinSteps"></p>
    <p id="totalSteps"></p>
    <p id="reachedSteps"></p>
    <p id="reachedSquare"></p>
    <p id="reachedBoth"></p>



    <!-- JS -->
    <script>

        function doGame()
        {
            var gameBoard = [0,0];
            var stepCount = 0;
            //10x10 array to hold step counts (may be a better way. check back later)

            //Cell counter
            var cells = [
                         [0,0,0,0,0,0,0,0,0,0],
                         [0,0,0,0,0,0,0,0,0,0],
                         [0,0,0,0,0,0,0,0,0,0],
                         [0,0,0,0,0,0,0,0,0,0],
                         [0,0,0,0,0,0,0,0,0,0],
                         [0,0,0,0,0,0,0,0,0,0],
                         [0,0,0,0,0,0,0,0,0,0],
                         [0,0,0,0,0,0,0,0,0,0],
                         [0,0,0,0,0,0,0,0,0,0],
                         [0,0,0,0,0,0,0,0,0,0]
                        ];




            while(true)
            {
                var oneMove = Math.floor(1+(Math.random()*4));

                //Conditional checks

                //Check if both square and step counts are satisfied
                if(gameBoard[0] == 9 && gameBoard[1] == 9 && stepCount == 1000000)
                {
                    document.getElementById("reachedBoth").innerHTML = "Reached 1,000,000 steps and top square";
                    break;
                }
                //Reached Top right before 1,000,000 steps
                else if(gameBoard[0] == 9 && gameBoard[1] == 9)
                {
                        document.getElementById("reachedSquare").innerHTML = "Reached Top right square";
                        break;
                }
                //Reached 1,000,000 steps before top right
                else if(stepCount == 1000000)
                {
                    document.getElementById("reachedSteps").innerHTML = "Reached 1,000,000 steps";
                    break;
                }

                //Movement on the board
                var x = gameBoard[0];
                var y = gameBoard[1];
                cells[x][y] += 1;



                    //Move left
                    if(oneMove == 1)
                    {
                        //Initialized at 1 so less than is suitable
                        if(gameBoard[0] < 0)
                        {
                            gameBoard[0] = 0; //Reset
                        }
                        else{
                            gameBoard[0]--;//Goes left
                        }
                    }
                    //Move right
                    else if(oneMove == 2)
                    {
                        //If its at the edge, keep it there or keeps from going over
                        if(gameBoard[0] >= 9)
                        {
                            gameBoard[0] = 9; //Reset
                        }
                        else{
                            gameBoard[0]++;//Goes right
                        }
                    }
                    //Move up
                    else if(oneMove == 3)
                    {
                        //If its at the edge, keep it there or keeps from going over
                        if(gameBoard[1] >= 9)
                        {
                            gameBoard[1] = 9; //Reset
                        }
                        else{
                            gameBoard[1]++;//Goes up
                        }
                    }
                    //Move down
                    else if(oneMove == 4)
                    {
                        //Initialized at 1 so less than is suitable
                        if(gameBoard[1] < 0)
                        {
                            gameBoard[1] = 0; //Reset    
                        }
                        else{
                            gameBoard[1]--;//Goes down
                        }
                    }


                stepCount++; //Count the steps

            }


           var max = 0;
           var min = Infinity;

            //Find max
            for(var i = 0; i < cells.length;i++)
            {
                for(var j = 0; j < cells[i].length; j++)
                {
                    if(max < cells[i][j])
                        {
                            max = cells[i][j];
                        }
                }
            }


            //Find min
            for(var i = 0; i < cells.length;i++)
            {
                for(var j = 0; j < cells[i].length; j++)
                {
                    if(min > cells[i][j])
                        {
                            min = cells[i][j];
                        }
                }
            }



            //Total Steps print
            document.getElementById("MaxSteps").innerHTML = "Max steps were: " + max;

            document.getElementById("MinSteps").innerHTML = "Min steps were: " + min;

            document.getElementById("totalSteps").innerHTML = "Total steps were: " + stepCount;


        }
    </script>


</body>
</html>

1 个答案:

答案 0 :(得分:1)

这个块让我觉得特别低效:

                if(oneMove == 1)
                {
                    //Initialized at 1 so less than is suitable
                    if(gameBoard[0] < 1)
                    {
                        gameBoard[0] = 1; //Reset
                    }
                    else{
                        gameBoard[0]--;//Goes left
                    }
                }
                //Move right
                else if(oneMove == 2)
                {
                    //If its at the edge, keep it there or keeps from going over
                    if(gameBoard[0] >= 10)
                    {
                        gameBoard[0] = 10; //Reset
                    }
                    else{
                        gameBoard[0]++;//Goes right
                    }
                }
                //Move up
                else if(oneMove == 3)
                {
                    //If its at the edge, keep it there or keeps from going over
                    if(gameBoard[1] >= 10)
                    {
                        gameBoard[1] = 10; //Reset
                    }
                    else{
                        gameBoard[1]++;//Goes up
                    }
                }
                //Move down
                else if(oneMove == 4)
                {
                    //Initialized at 1 so less than is suitable
                    if(gameBoard[1] < 1)
                    {
                        gameBoard[1] = 1; //Reset    
                    }
                    else{
                        gameBoard[1]--;//Goes down
                    }
                }

这是一个课程,所以我不会直接给你一个答案,但你能想到一个解决方案,而不是直接使用生成的随机值递增或递减你的游戏板计数器吗?

例如,如果我有一个简单的1维游戏板,那么:

var gameboard = [0,0,0];
var position = 0,
    direction = 0;

function move() {
    direction = Math.round(Math.random()) * 2 - 1;
    position += direction;
}

唯一缺少的是考虑到我离开游戏板的可能性。如果要求在电路板的另一侧开始你的标记(想想吃豆人),这也可以使用modulo运算符完成,在JS中是%:

function move() {
    direction = Math.round(Math.random()) * 2 - 1;
    position += direction;
    position = (position + 3) % 3;
}

不幸的是,根据您的要求,如果留在董事会的条件,我就不会有办法解决:

    position = position < 0 ? 0 : position;
    position = position > 2 ? 2 : position;

希望这会让你朝着正确的方向前进。上面三行代码实际上可以组合成一行,但我更喜欢让它们更具可读性。

还有一些说明:

  • 将x和y位置存储在名为的双元素数组中 gameBoard只是令人困惑。只需将它们称为x和y(正如你所做的那样) 你的代码结束)。
  • 虽然要求不要求,但请尝试 生成游戏板并执行所有数学运算而不是 10个元素,游戏可以通过仅改变来改变为n个元素 代码中的一个值。拥有最小的一套是件好事 控制尽可能。

祝你上课好运!