0
我想這運行,那麼gameOnAgain跑,然後再回到......但我一直gettng無限循環爲什麼這是一個永無止境的循環?
這場比賽是應該允許的第一個球員點擊一個div,然後把裏面,然後一個字符繼續下一個玩家。但2輪流在每個之後,遊戲中創建了一個無限循環墜毀我的瀏覽器
function gameOn() {
count++;
winner();
if (playerOneTurn === true) {
$("#top1").on("click", function() {
$("#top1").html(`<h2>${symbolp1}</h2>`);
cell1 = true;
playerTwoTurn = true;
playerOneTurn = false;
gameOnAgain();
})
$("#top2").on("click", function() {
$("#top2").html(`<h2>${symbolp1}</h2>`);
cell2 = true;
playerTwoTurn = true;
playerOneTurn = false;
gameOnAgain();
})
$("#top3").on("click", function() {
$("#top3").html(`<h2>${symbolp1}</h2>`);
cell3 = true;
playerTwoTurn = true;
playerOneTurn = false;
gameOnAgain();
})
$("#middle1").on("click", function() {
$("#middle1").html(`<h2>${symbolp1}</h2>`);
cell4 = true;
playerTwoTurn = true;
playerOneTurn = false;
gameOnAgain();
})
$("#middle2").on("click", function() {
$("#middle2").html(`<h2>${symbolp1}</h2>`);
cell5 = true;
playerTwoTurn = true;
playerOneTurn = false;
gameOnAgain();
})
$("#middle3").on("click", function() {
$("#middle3").html(`<h2>${symbolp1}</h2>`);
cell6 = true;
playerTwoTurn = true;
playerOneTurn = false;
gameOnAgain();
})
$("#bottom1").on("click", function() {
$("#bottom1").html(`<h2>${symbolp1}</h2>`);
cell8 = true;
playerTwoTurn = true;
playerOneTurn = false;
gameOnAgain();
})
$("#bottom2").on("click", function() {
$("#bottom2").html(`<h2>${symbolp1}</h2>`);
cell8 = true;
playerTwoTurn = true;
playerOneTurn = false;
gameOnAgain();
})
$("#bottom3").on("click", function() {
$("#bottom3").html(`<h2>${symbolp1}</h2>`);
cell9 = true;
playerTwoTurn = true;
playerOneTurn = false;
gameOnAgain();
})
} }
function gameOnAgain() {
winner();
if (playerTwoTurn === true) {
$("#top1").on("click", function() {
$("#top1").html(`<h2>${symbolp2}</h2>`);
cell1 = false;
playerOneTurn = true;
playerTwoTurn = false;
gameOn();
})
$("#top2").on("click", function() {
$("#top2").html(`<h2>${symbolp2}</h2>`);
cell2 = false;
playerOneTurn = true;
playerTwoTurn = false;
gameOn();
})
$("#top3").on("click", function() {
$("#top3").html(`<h2>${symbolp2}</h2>`);
cell3 = false;
playerOneTurn = true;
playerTwoTurn = false;
gameOn();
})
$("#middle1").on("click", function() {
$("#middle1").html(`<h2>${symbolp2}</h2>`);
cell4 = false;
playerOneTurn = true;
playerTwoTurn = false;
gameOn();
})
$("#middle2").on("click", function() {
$("#middle2").html(`<h2>${symbolp2}</h2>`);
cell5 = false;
playerOneTurn = true;
playerTwoTurn = false;
gameOn();
})
$("#middle3").on("click", function() {
$("#middle3").html(`<h2>${symbolp2}</h2>`);
cell6 = false;
playerOneTurn = true;
playerTwoTurn = false;
gameOn();
})
$("#bottom1").on("click", function() {
$("#bottom1").html(`<h2>${symbolp2}</h2>`);
cell7 = false;
playerOneTurn = true;
playerTwoTurn = false;
gameOn();
})
$("#bottom2").on("click", function() {
$("#bottom2").html(`<h2>${symbolp2}</h2>`);
cell8 = false;
playerOneTurn = true;
playerTwoTurn = false;
gameOn();
})
$("#bottom3").on("click", function() {
$("#bottom3").html(`<h2>${symbolp2}</h2>`);
cell9 = false;
playerOneTurn = true;
playerTwoTurn = false;
gameOn();
})
} }
「贏家()」是做什麼的? – wlh
這段代碼非常多餘。停止像ID這樣的絕對值(這個確切的節點/單元格,這個特定的玩家轉過來......),嘗試用組/類型來思考,比如單元格或行或列的列表,玩家列表等。在你的情況下,有兩個玩家;每一個奇怪的轉動都是playerOnes(1,3,5,...)playerTwo甚至轉過(2,4,6,...)。檢查這個例子*(我想,這就是你正在構建的)* https://jsfiddle.net/pg6xpyjo/並不意味着這是正確的/唯一的方法,但你絕對可以從該代碼中學習。 – Thomas