私はクラスのプロジェクトに取り組んできましたが、理論的には完了しましたが、速く実行できない問題があります。タスクは以下の通りです:あなたは10x10のボードを持っています。あなたは、ランダムに上、下、左または右に行く。移動が取れたら、ボードをスキップしてください。 1,000,000歩が取られるか、ボードの右上に到達するまでこれを行います。また、1つのタイルが受け取った最小のステップ数を数えます。私は2D配列を使ってこれを行いました。時々カウントされ出力されますが、出力を得るために複数のボタンクリックが必要です。私はこれが、ステップ数を追跡するために2D配列にアクセスしているかどうかに関係するメモリ割り当てエラーであるかどうかはわかりません。私はjavascriptには比較的新しいので、自分のやり方がすべて効率的かどうかはわかりません。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>
コードにいくつかの問題がありますが、コードがハングする理由はありません。私はいくつかの編集を行い、問題なくローカルに実行しました。問題:配列のインデックスは0から9であり、1から10ではなく、xまたはyを使用することはありません。minは0から始まるため、常に0になります(Infinityなどで開始します)。 – csander
@csander私はあなたが見つけたものを修正しようとしましたが、正しく行ったかどうかはわかりません。最小値は常に0に固定されています。しかし、配列インデックスが0〜9であるかどうかはわかりません。自分の条件で、あなたが記述していると思ったものを更新しました。また、人が現在どの空間を使っているのか、セル[] []配列をインクリメントするときにxとyを使います。私に何か不足している場合は教えてください。 – bkedge
gameBoardの要素が-1である可能性があると思います。なぜなら、0の場合、それらから1を引いても問題ないからです。ロジックをよりシンプルにすることができると思います。 "if(gameBoard [0])gameboard [0] - " oneMove == 1の場合です。 – csander