私は数日間単純なプロジェクトゲーム(Tic Tac Toe)を作成しようとしていました。 私はプレーヤーとコンピュータの間のシミュレーションを作成したいと私は何とか物事を整理するために管理:)定期的なコードを減らし、プレーヤーの移動を上書きしないようにする方法(JavaScript)
は、私は2つの問題に直面した:私は今、これを行うことができますので、 はどのようにして、移動の書き換え制限することができます。 私の他の問題は:繰り返しのコードを避ける方法です。
var nextMove = "X".fontsize(8);
var playerChar = "X".fontsize(8);
var computerChar = "O".fontsize(8);
var dataContent = [cell1x1, cell1x2, cell1x3, cell2x1, cell2x2, cell2x3, cell3x1, cell3x2, cell3x3];
function playerMove() {
if (nextMove == "X") {
nextMove = computerMove();
}
else {
nextMove = playerChar;
}
}
function computerMove() {
\t
for(var i = 0; i < dataContent.length; i++){
\t \t
\t \t if (cell1x1.innerHTML === "X" || cell1x1.innerHTML === "O") {
return;
\t \t }
\t \t if(cell1x1.innerHTML === ""){
\t \t \t
\t \t \t dataContent[0].innerHTML = computerChar;
\t \t \t return;
\t \t }
\t \t if (cell1x2.innerHTML === "X" || cell1x2.innerHTML === "O") {
return;
\t \t }
\t \t if(cell1x2.innerHTML === ""){
\t \t \t
\t \t \t dataContent[1].innerHTML = computerChar;
\t \t \t return;
\t \t }
\t \t if (cell1x3.innerHTML === "X" || cell1x3.innerHTML === "O") {
return;
\t \t }
\t \t if(cell1x3.innerHTML === ""){
\t \t \t
\t \t \t dataContent[2].innerHTML = computerChar;
\t \t \t return;
\t \t }
\t \t if (cell2x1.innerHTML === "X" || cell2x1.innerHTML === "O") {
return;
\t \t }
\t \t if(cell2x1.innerHTML === ""){
\t \t \t
\t \t \t dataContent[3].innerHTML = computerChar;
\t \t \t return;
\t \t }
\t \t if (cell2x2.innerHTML === "X" || cell2x2.innerHTML === "O") {
return;
\t \t }
\t \t if(cell2x2.innerHTML === ""){
\t \t \t
\t \t \t dataContent[4].innerHTML = computerChar;
\t \t \t return;
\t \t }
\t \t if (cell2x3.innerHTML === "X" || cell2x3.innerHTML === "O") {
return;
\t \t }
\t \t if(cell2x3.innerHTML === ""){
\t \t \t
\t \t \t dataContent[5].innerHTML = computerChar;
\t \t \t return;
\t \t }
\t \t if (cell3x1.innerHTML === "X" || cell3x1.innerHTML === "O") {
return;
\t \t }
\t \t if(cell3x1.innerHTML === ""){
\t \t \t
\t \t \t dataContent[6].innerHTML = computerChar;
\t \t \t return;
\t \t }
\t \t if (cell3x2.innerHTML === "X" || cell3x2.innerHTML === "O") {
return;
\t \t }
\t \t if(cell3x2.innerHTML === ""){
\t \t \t
\t \t \t dataContent[7].innerHTML = computerChar;
\t \t \t return;
\t \t }
\t \t
\t \t if (cell3x3.innerHTML === "X" || cell3x3.innerHTML === "O") {
return;
\t \t }
\t \t if(cell3x3.innerHTML === ""){
\t \t \t
\t \t \t dataContent[8].innerHTML = computerChar;
\t \t \t return;
\t \t }
\t \t
\t \t
\t \t
\t \t
\t }
}
$("#cell1x1").click(function(){
\t if (cell1x1.innerHTML === "X" || cell1x1.innerHTML === "O") {
return;
}
\t \t cell1x1.innerHTML = "X";
\t \t cell1x1.innerHTML = nextMove;
\t \t playerMove();
\t \t computerMove();
\t
});
$("#cell1x2").click(function(){
\t if (cell1x2.innerHTML === "X" || cell1x2.innerHTML === "O") {
return;
\t }
\t \t cell1x2.innerHTML = "X";
\t \t cell1x2.innerHTML = nextMove;
\t \t playerMove();
\t \t computerMove();
\t \t
});
$("#cell1x3").click(function(){
\t if (cell1x3.innerHTML === "X" || cell1x3.innerHTML === "O") {
return;
}
\t \t cell1x3.innerHTML = "X";
\t \t cell1x3.innerHTML = nextMove;
\t \t playerMove();
\t \t computerMove();
});
$("#cell2x1").click(function(){
\t if (cell2x1.innerHTML === "X" || cell2x1.innerHTML === "O") {
return;
}
\t \t cell2x1.innerHTML = "X";
\t \t cell2x1.innerHTML = nextMove;
\t \t playerMove();
\t \t computerMove();
});
$("#cell2x2").click(function(){
\t if (cell2x2.innerHTML === "X" || cell2x2.innerHTML === "O") {
return;
}
\t \t cell2x2.innerHTML = "X";
\t \t cell2x2.innerHTML = nextMove;
\t \t playerMove();
\t \t computerMove();
});
$("#cell2x3").click(function(){
\t if (cell2x3.innerHTML === "X" || cell2x3.innerHTML === "O") {
return;
}
\t \t cell2x3.innerHTML = "X";
\t \t cell2x3.innerHTML = nextMove;
\t \t playerMove();
\t \t computerMove();
});
$("#cell3x1").click(function(){
\t if (cell3x1.innerHTML === "X" || cell3x1.innerHTML === "O") {
return;
}
\t \t cell3x1.innerHTML = "X";
\t \t cell3x1.innerHTML = nextMove;
\t \t playerMove();
\t \t computerMove();
});
$("#cell3x2").click(function(){
\t if (cell3x2.innerHTML === "X" || cell3x2.innerHTML === "O") {
return;
}
\t \t cell3x2.innerHTML = "X";
\t \t cell3x2.innerHTML = nextMove;
\t \t playerMove();
\t \t computerMove();
});
$("#cell3x3").click(function(){
\t if (cell3x3.innerHTML === "X" || cell3x3.innerHTML === "O") {
return;
}
\t \t cell3x3.innerHTML = "X";
\t \t cell3x3.innerHTML = nextMove;
\t \t playerMove();
\t \t computerMove();
});
.data1x1 ,.data1x2 ,.data1x3,.data2x1 ,.data2x2 ,.data2x3 ,.data3x1 ,.data3x2 ,.data3x3{
\t width: 80px;
\t height: 80px;
border-right:1px;
\t border-style: solid;
}
.tableContent{
background-color: white;
margin: 150px auto auto;
}
.body{
background-color: lightskyblue;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Tic tac toe</title>
<link rel="stylesheet" type="text/css" href="myCss.css">
</head>
<body class="body">
<div class="container">
\t <table class="tableContent">
<tr class="rowContent">
\t \t \t <td id="cell1x1" class="data1x1"></td>
\t \t \t <td id="cell1x2" class="data1x2"></td>
\t \t \t <td id="cell1x3" class="data1x3"></td>
\t \t </tr>
\t \t <tr class="rowContent">
\t \t \t <td id="cell2x1" class="data2x1"></td>
\t \t \t <td id="cell2x2" class="data2x2"></td>
\t \t \t <td id="cell2x3" class="data2x3"></td>
\t \t </tr>
\t \t <tr class="rowContent">
\t \t \t <td id="cell3x1" class="data3x1"></td>
\t \t \t <td id="cell3x2" class="data3x2"></td>
\t \t \t <td id="cell3x3" class="data3x3"></td>
\t \t </tr>
\t </table>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="myScript.js" ></script>
</body>
</html>
https://codereview.stackexchange.com/ – Sphinxxx