は、私は、これはその後、gameOnAgainは再び、その後、実行するを実行したい...しかし、私はゲームが最初のプレイヤーは、div要素をクリックし、その後、内側に文字を配置することができるようになっている無限ループこれは決して終わりのないループですか?
をgettng保ちます次のプレイヤーに行く。しかし、それぞれに2ターン後、ゲームはあなたがJSBinですべてのコードを配置し、リンクを共有する場合、それは助けたでしょう
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();
})
} }
「winner()」とは何ですか? – wlh
このコードは非常に冗長です。 ID(この正確なノード/セル、この特定のプレイヤーが回る、...)のような絶対的なものではなく、セル/行または列のリスト、選手のリストなどのグループ/タイプの観点から考えてみてください。あなたの場合、2人の選手がいます。すべての奇妙なターンはplayerOnes(1,3,5、...)playerTwoでも今度は(2,4,6、...)です。この例をチェックしてください*(これはあなたが構築していると思います)* https://jsfiddle.net/pg6xpyjo/正しい/唯一の方法だとは限りませんが、そのコードから確実に学ぶことができます。 – Thomas