プレイヤー対CPUの設定でストレートJSのチックタックトーのゲームに取り組んでいます。私はCPUの回転のためのランダムな機能を理解しようとしました。それは機能を打ち、それを通過しています。 console.log(cpuTurn());プレーヤーが正方形をマークした後は常にfalseを返します。tic tac toeのためのランダムなjavascript AI関数
私は問題と思われるこの行を使いました。if(i == "")blanks.push([i]);
もし私がValのようなvarを宣言し、それを次のように置くと:if(Val(i)== "")... 'Val'は関数ではないというエラーが出ます。
私はここに、スタック内の質問のトンを読みましたが、何もであるように思わない...
var player = "X";
var cpu = "O";
var currentTurn = player;
function startGame() {
\t for(var i = 1; i <= 9; i++) {
\t \t clearBoard(i);
\t }
\t document.winner = null;
\t setMessage("Player " + currentTurn + ", please start the game.");
}
function setMessage(message) {
\t document.getElementById("message").innerText = message;
} \t
function nextMove(square) {
\t if (document.winner != null) {
\t \t setMessage(currentTurn + " has already won.");
\t } else if(square.innerText == '') {
\t square.innerText = currentTurn;
\t swapTurn(); \t \t
\t } else {
\t \t setMessage("No cheating, choose another square.");
\t }
}
function swapTurn() {
\t if(checkForWinner(currentTurn)) {
\t \t setMessage(currentTurn + " has won!");
\t \t document.winner = currentTurn;
\t } else if(checkForDraw(currentTurn)) {
\t \t setMessage("Its a draw!");
\t } else if(currentTurn == player){
\t \t currentTurn = cpu;
\t \t setMessage("Player " + currentTurn + ", it's your turn.");
\t \t cpuMove();
\t } else {
\t \t currentTurn = player;
\t \t setMessage("Player " + currentTurn + ", it's your turn.");
\t }
}
function checkForWinner(move) {
\t var result = false;
\t if(checkRow(1,2,3, move) ||
\t checkRow(4,5,6, move) ||
\t checkRow(7,8,9, move) ||
\t checkRow(1,4,7, move) ||
\t checkRow(2,5,8, move) ||
\t checkRow(3,6,9, move) ||
\t checkRow(1,5,9, move) ||
\t checkRow(3,5,7, move)) {
\t \t result = true;
\t }
\t return result;
}
function checkForDraw(move) {
\t var draw = false;
\t if (((getBox(1) == "X") || (getBox(1) == "O")) &&
\t \t ((getBox(2) == "X") || (getBox(2) == "O")) &&
\t \t ((getBox(3) == "X") || (getBox(3) == "O")) &&
\t \t ((getBox(4) == "X") || (getBox(4) == "O")) &&
\t \t ((getBox(5) == "X") || (getBox(5) == "O")) &&
\t \t ((getBox(6) == "X") || (getBox(6) == "O")) &&
\t \t ((getBox(7) == "X") || (getBox(7) == "O")) &&
\t \t ((getBox(8) == "X") || (getBox(8) == "O")) &&
\t \t ((getBox(9) == "X") || (getBox(9) == "O"))) {
draw = true;
}
return draw;
}
function checkRow(a, b, c, move) {
\t var result = false;
\t if(getBox(a) == move && getBox(b) == move && getBox(c) == move) {
\t \t result = true;
\t }
\t return result;
}
function getBox(number) { \t \t \t \t \t \t \t \t \t
\t return document.getElementById("s" + number).innerText; \t
}
function clearBoard(number) {
\t document.getElementById("s" + number).innerText = "";
}
function cpuMove() {
\t var blanks = [];
\t //var val = 0;
\t for (var i = 1; i <= 9; i++) {
\t \t if (i == "")blanks.push([i]);
\t }
\t if (blanks.length > 0) {
\t \t var r = Math.floor((Math.random()*blanks.length));
return blanks[r];
} else {
return false;
} \t
}
\t <style>
\t \t body {
\t \t \t width:550px;
\t \t \t margin: auto;
\t \t }
\t \t h1 {
\t \t }
\t \t #message {
\t \t \t font-family:;
\t \t \t font-size:;
\t \t }
\t \t .square {
\t \t \t height: 100px;
\t \t \t width: 100px;
\t \t \t font-size: 60pt;
\t \t \t text-align: center;
\t \t \t font-weight: bold;
\t \t }
\t </style>
<body onload="startGame();">
\t <h1>Tom's Tic Tac Toe in JS</h1>
\t <div id="message">MESSAGE HERE</div>
\t <table border = "1">
\t \t <tr>
\t \t \t <td class="square" id="s1" onClick="nextMove(this)"></td>
\t \t \t <td class="square" id="s2" onClick="nextMove(this)"></td>
\t \t \t <td class="square" id="s3" onClick="nextMove(this)"></td>
\t \t </tr>
\t \t <tr>
\t \t \t <td class="square" id="s4" onClick="nextMove(this)"></td>
\t \t \t <td class="square" id="s5" onClick="nextMove(this)"></td>
\t \t \t <td class="square" id="s6" onClick="nextMove(this)"></td>
\t \t </tr>
\t \t <tr>
\t \t \t <td class="square" id="s7" onClick="nextMove(this)"></td>
\t \t \t <td class="square" id="s8" onClick="nextMove(this)"></td>
\t \t \t <td class="square" id="s9" onClick="nextMove(this)"></td>
\t \t </tr>
\t </table>
\t <a href="javascript: startGame();" id="restart">New Game</a>
\t <script src="main.js"></script>
</body>
</html>
私はここで何をする必要があるかのように完全に混乱しています私の問題に似ています。助けを前にありがとう。
デバッガをよく理解して説明しますあなたが悩みの理解を持っているなら、私に教えてください。 –
var Val = 0; Val(i)のように使用していますか? –
'i ==" "'文字列と比較する整数はどうですか? –