随机的javascript AI功能,用于tic tac toe无法正常工作

时间:2017-02-21 00:28:45

标签: javascript

使用直接JS与玩家对cpu设置进行Tic Tac Toe游戏。我试图找出cpu转向的随机函数。它正在发挥作用并经历它。 console.log(cpuTurn());在玩家标记正方形后,始终返回false。

我玩过这一行我认为是问题所在:if(i ==“”)blanks.push([i]);

如果我声明一个var之类的Val并将它放在前面,就像这样:if(Val(i)==“”)...我会得到一个错误,说'Val'不是函数。

我对这里需要做的事情感到很困惑......

var player = "X";
var cpu = "O";
var currentTurn = player;

function startGame() {

	for(var i = 1; i <= 9; i++) {
		clearBoard(i);
	}

	document.winner = null;
	setMessage("Player " + currentTurn + ", please start the game.");
}

function setMessage(message) {
	document.getElementById("message").innerText = message;
}	

function nextMove(square) {
	if (document.winner != null) {
		setMessage(currentTurn + " has already won.");
	} else if(square.innerText == '') {
	  square.innerText = currentTurn;
	  swapTurn();		
	} else {
		setMessage("No cheating, choose another square.");
	}
}

function swapTurn() {
	if(checkForWinner(currentTurn)) {
		setMessage(currentTurn + " has won!");
		document.winner = currentTurn; 
	} else if(checkForDraw(currentTurn)) {
		setMessage("Its a draw!");
	} else if(currentTurn == player){
		currentTurn = cpu;
		setMessage("Player " + currentTurn + ", it's your turn.");
		cpuMove();
	} else {
		currentTurn = player;
		setMessage("Player " + currentTurn + ", it's your turn.");
	}
}

function checkForWinner(move) {
	var result = false;
	if(checkRow(1,2,3, move) ||
	   checkRow(4,5,6, move) ||
	   checkRow(7,8,9, move) ||
	   checkRow(1,4,7, move) ||
	   checkRow(2,5,8, move) ||
	   checkRow(3,6,9, move) ||
	   checkRow(1,5,9, move) ||
	   checkRow(3,5,7, move)) {
	   	result = true;
	   }
	   return result;
}

function checkForDraw(move) {
	var draw = false;
	if (((getBox(1) == "X") || (getBox(1) == "O")) && 
		((getBox(2) == "X") || (getBox(2) == "O")) && 
		((getBox(3) == "X") || (getBox(3) == "O")) && 
		((getBox(4) == "X") || (getBox(4) == "O")) && 
		((getBox(5) == "X") || (getBox(5) == "O")) && 
		((getBox(6) == "X") || (getBox(6) == "O")) && 
		((getBox(7) == "X") || (getBox(7) == "O")) && 
		((getBox(8) == "X") || (getBox(8) == "O")) && 
		((getBox(9) == "X") || (getBox(9) == "O"))) {
         draw = true;
        }
         return draw;  
}

function checkRow(a, b, c, move) {
	var result = false;
	if(getBox(a) == move && getBox(b) == move && getBox(c) == move) {
		result = true;
	}
	return result;
}

function getBox(number) {									
	return document.getElementById("s" + number).innerText;	
}

function clearBoard(number) {
	document.getElementById("s" + number).innerText = "";
}

function cpuMove() {
	var blanks = [];
	//var val = 0;
	for (var i = 1; i <= 9; i++) {
		if (i == "")blanks.push([i]);
	}
	if (blanks.length > 0) {
		 var r = Math.floor((Math.random()*blanks.length));
        return blanks[r];
      } else {
        return false;
      }	
}
	<style>

		body {
			width:550px;
			margin: auto;
		}

		h1 {

		}

		#message {
			font-family:; 
			font-size:;
		}

		.square {
			height: 100px;
			width: 100px;
			font-size: 60pt;
			text-align: center;
			font-weight: bold;
		}

	</style>
<body onload="startGame();">
	<h1>Tom's Tic Tac Toe in JS</h1>

	<div id="message">MESSAGE HERE</div>

	<table border = "1">
		<tr>
			<td class="square" id="s1" onClick="nextMove(this)"></td>
			<td class="square" id="s2" onClick="nextMove(this)"></td>
			<td class="square" id="s3" onClick="nextMove(this)"></td>
		</tr>
		<tr>
			<td class="square" id="s4" onClick="nextMove(this)"></td>
			<td class="square" id="s5" onClick="nextMove(this)"></td>
			<td class="square" id="s6" onClick="nextMove(this)"></td>
		</tr>
		<tr>
			<td class="square" id="s7" onClick="nextMove(this)"></td>
			<td class="square" id="s8" onClick="nextMove(this)"></td>
			<td class="square" id="s9" onClick="nextMove(this)"></td>
		</tr>
	</table>
	<a href="javascript: startGame();" id="restart">New Game</a>
	<script src="main.js"></script>
</body>
</html>

我已经阅读了Stack中的大量问题,但似乎没有任何类似于我的问题。在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

以下是适合您的解决方案。 如果您理解我会解释

,请告诉我
function cpuMove() {

  var blanks = [];
  for (var i = 1; i <= 9; i++) {
    var place = document.getElementById("s"+i).innerText
    if(place == "") blanks.push([i]);
  }
  if (blanks.length > 0) {
    var r = Math.floor((Math.random() * blanks.length));
    nextMove(document.getElementById("s"+blanks[r]));
  } 
}