在用javascript制作tic-tac-toe游戏时,如何创建一个a.I.来选择任何随机框,但其中一个没有';你没有被选中吗?
我在做一个井字游戏,我有点卡住了。我做了一个a.I.在你身后移动,但一切都有点混乱。你自己试试,看看会发生什么。任何人都可以看看,看看他们是否能够改进它,并解释他们是如何做到的。让事情变得简单一点,我怎么能让人工智能选择任何一个还没有被选中的盒子呢。 代码如下:在用javascript制作tic-tac-toe游戏时,如何创建一个a.I.来选择任何随机框,但其中一个没有';你没有被选中吗?,javascript,html,algorithm,javascript-events,Javascript,Html,Algorithm,Javascript Events,我在做一个井字游戏,我有点卡住了。我做了一个a.I.在你身后移动,但一切都有点混乱。你自己试试,看看会发生什么。任何人都可以看看,看看他们是否能够改进它,并解释他们是如何做到的。让事情变得简单一点,我怎么能让人工智能选择任何一个还没有被选中的盒子呢。 代码如下: <!DOCTYPE html> <html> <body> <input type="button" id="k1" value=" " onclick="tictactoe(this
<!DOCTYPE html>
<html>
<body>
<input type="button" id="k1" value=" " onclick="tictactoe(this)">
<input type="button" id="k2" value=" " onclick="tictactoe(this)">
<input type="button" id="k3" value=" " onclick="tictactoe(this)">
<br />
<input type="button" id="k4" value=" " onclick="tictactoe(this)">
<input type="button" id="k5" value=" " onclick="tictactoe(this)">
<input type="button" id="k6" value=" " onclick="tictactoe(this)">
<br />
<input type="button" id="k7" value=" " onclick="tictactoe(this)">
<input type="button" id="k8" value=" " onclick="tictactoe(this)">
<input type="button" id="k9" value=" " onclick="tictactoe(this)">
<script>
var Xturn = true;
var nummoves = 0;
var cat;
function tictactoe(square) {
var value = square.value;
var doc1 = document.getElementById("k1").value;
var doc2 = document.getElementById("k2").value;
var doc3 = document.getElementById("k3").value;
var doc4 = document.getElementById("k4").value;
var doc5 = document.getElementById("k5").value;
var doc6 = document.getElementById("k6").value;
var doc7 = document.getElementById("k7").value;
var doc8 = document.getElementById("k8").value;
var doc9 = document.getElementById("k9").value;
for (nummoves = 0; nummoves < 2; nummoves++) {
if (doc1 == "X") {
cat = document.getElementById("k2").value = "O";
Xturn = true;
}
if (doc2 = "X") {
cat = document.getElementById("k4").value = "O";
Xturn = true;
}
if (doc3 == "X") {
cat = document.getElementById("k5").value = "O";
Xturn = true;
}
if (doc4 == "X") {
car = document.getElementById("k9").value = "O";
}
}
for (nummoves = 2; nummoves < 3; nummoves++) {
if (doc1 == "X") {
cat = document.getElementById("k7").value = "O";
Xturn = true;
}
}
if (value != "X" && value != "O") {
if (Xturn == true) {
square.value = "X";
return Xturn = false;
nummoves++;
} else if (Xturn == false) {
square.value = "O";
return Xturn = true;
nummoves++;
}
} else {
alert("That square has been clicked.");
}
}
</script>
</body>
</html>
var xtorn=真;
var nummoves=0;
var-cat;
功能Tictatcoe(方形){
var值=平方值;
var doc1=document.getElementById(“k1”).value;
var doc2=document.getElementById(“k2”).value;
var doc3=document.getElementById(“k3”).value;
var doc4=document.getElementById(“k4”).value;
var doc5=document.getElementById(“k5”).value;
var doc6=document.getElementById(“k6”).value;
var doc7=document.getElementById(“k7”).value;
var doc8=document.getElementById(“k8”).value;
var doc9=document.getElementById(“k9”).value;
对于(nummoves=0;nummoves<2;nummoves++){
如果(doc1==“X”){
cat=document.getElementById(“k2”).value=“O”;
xtrn=真;
}
如果(doc2=“X”){
cat=document.getElementById(“k4”).value=“O”;
xtrn=真;
}
如果(doc3==“X”){
cat=document.getElementById(“k5”).value=“O”;
xtrn=真;
}
如果(doc4==“X”){
car=document.getElementById(“k9”).value=“O”;
}
}
对于(nummoves=2;nummoves<3;nummoves++){
如果(doc1==“X”){
cat=document.getElementById(“k7”).value=“O”;
xtrn=真;
}
}
如果(值!=“X”&&value!=“O”){
如果(xtrn==true){
square.value=“X”;
返回xtrn=false;
nummoves++;
}else if(xtrn==false){
square.value=“O”;
返回xtrn=true;
nummoves++;
}
}否则{
警报(“该方块已被单击”);
}
}
请注意,我承认整个概念不是我的,但我做了一点a.I.部分,有点混乱 您可以迭代“按钮”并选择第一个未选中的按钮,或者根据随机选择另一个按钮
for(i=1;i<10;i++) {
if (document.getElementById('k'+i).value = ' ') {
// not played yet !
}
}
对于(i=1;i您可以迭代“按钮”,然后选择第一个未选中的按钮,或者根据随机选择另一个按钮
for(i=1;i<10;i++) {
if (document.getElementById('k'+i).value = ' ') {
// not played yet !
}
}
对于(i=1;i考虑以下逻辑:
// function that does an AI move
function doAIMove(xOrO) {
// randomly gets a number from 1 to 9
var randomSquare = document.getElementById("k" + getRandomInt(1, 9));
while (randomSquare.value != " ") {
randomSquare = document.getElementById("k" + getRandomInt(1, 9));
}
randomSquare.value(xOrO);
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
这不是很有效,但它按照您的要求工作。无论如何,您需要检查是否还有剩余的方块需要填充
你也应该考虑实现“TiC Tac To-AI”,这是非常简单的。你应该遵循这个伪算法,以便:
当制作一个类似于tic-tac的游戏时,AI应该这样工作:
1. Check if there is a tile that you can win in 1 move
if there is no such tile:
2. Check if there is a tile that your opponent can win in 1 move
if there is no such tile:
3. Check if there is a tile that can make two tiles apply to the rule #1
if there is no such tile:
4. Check if there is a tile that your opponent can make two tiles apply to the rule #2
if there is no such tile:
5. implement your own AI form this point
考虑以下逻辑:
// function that does an AI move
function doAIMove(xOrO) {
// randomly gets a number from 1 to 9
var randomSquare = document.getElementById("k" + getRandomInt(1, 9));
while (randomSquare.value != " ") {
randomSquare = document.getElementById("k" + getRandomInt(1, 9));
}
randomSquare.value(xOrO);
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
这不是很有效,但它按照您的要求工作。无论如何,您需要检查是否还有剩余的方块需要填充
你也应该考虑实现“TiC Tac To-AI”,这是非常简单的。你应该遵循这个伪算法,以便:
当制作一个类似于tic-tac的游戏时,AI应该这样工作:
1. Check if there is a tile that you can win in 1 move
if there is no such tile:
2. Check if there is a tile that your opponent can win in 1 move
if there is no such tile:
3. Check if there is a tile that can make two tiles apply to the rule #1
if there is no such tile:
4. Check if there is a tile that your opponent can make two tiles apply to the rule #2
if there is no such tile:
5. implement your own AI form this point
跟踪一个开放方块列表,然后从该列表中随机选择
这样就可以消除循环。跟踪一个开放方块列表,然后从该列表中随机选择
这样就可以消除循环。用于Tic-Tac-Toe的HTML和JS/JQuery实现的链接。
目前它只是一个没有计算机作为对手的两人游戏。希望你能在它的基础上继续发展
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title> - jsFiddle demo by bhatkrishnakishor</title>
<script type='text/javascript' src='//code.jquery.com/jquery-1.9.1.js'></script>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<style type='text/css'>
.tictactoe {
width: 125px;
height: 125px;
background: #A2A8A1;
};
</style>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
//this is a demo tic tac toe game
$(document).ready($('input.tictactoe').click(tictactoe));
$(document).ready($('#ff').click(reset));
var whoseMove = 'X';
var xMove = new Array();
var oMove = new Array();
var gameOver = false;
var winningConditions = new Array( 'aa/ab/ac','ba/bb/bc','ca/cb/cc','aa/ba/ca','ab/bb/cb','ac/bc/cc','aa/bb/cc','ac/bb/ca');
var whoWon = '';
function tictactoe() {
if(gameOver == false && this.value == ' '){
if(whoseMove == 'X'){
this.value = whoseMove;
xMove[xMove.length] = this.id;
whoseMove = 'O';
}
else {
this.value = whoseMove;
oMove[oMove.length] = this.id;
whoseMove = 'X';
}
}
if(xMove.length >2){
whoWon = endGame();
}
if(gameOver && whoWon != '' && whoWon != 'draw') {
alert(whoWon + ' won!')
}
if(!gameOver && whoWon == 'draw') {
alert('Games been draw!');
}
}
function endGame() {
var winningCombinations = new Array();
//set this variable value to true incase the game is over
gameOver = true;
for(var index = 0; index < 8; index = index + 1){
var xMatchCount = 0;
var oMatchCount = 0;
winningCombinations = winningConditions[index].split('/');
for(var i = 0; i < 3; i = i + 1){
console.log('winningCombinations ' + winningCombinations[i]);
for(var j = 0; j < xMove.length; j = j + 1){
console.log('xMove ' + xMove[j]);
if(winningCombinations[i] == xMove[j]){
xMatchCount = xMatchCount + 1;
if(xMatchCount == 3){
return 'X';
}
}
}
for(var k = 0; k < oMove.length; k = k + 1){
//console.log('oMove ' + oMove[k]);
if(winningCombinations[i] == oMove[k]){
oMatchCount = oMatchCount + 1;
if(oMatchCount == 3){
return 'O';
}
}
}
}
}
console.log('x Move Count ' + xMove.length);
console.log('o Move Count ' + oMove.length);
if(xMatchCount < 3 && oMatchCount < 3){
gameOver = false;
}
if(xMove.length + oMove.length == 9){
return 'draw';
}
}
function reset() {
console.log('Xs Move - ' + xMove.join('/'));
console.log('Os Move - ' + oMove.join('/'));
console.log(winningConditions.length);
whoseMove = 'X';
xMove = new Array();
oMove = new Array();
gameOver = false;
whoWon = '';
$('input').filter(function() {
if(this.id != 'ff') {
this.value = ' ';
}
});
}
});//]]>
</script>
</head>
<body>
<input type="button" id="aa" class="tictactoe" value=" ">
<input type="button" id="ab" class="tictactoe" value=" ">
<input type="button" id="ac" class="tictactoe" value=" ">
<br />
<input type="button" id="ba" class="tictactoe" value=" ">
<input type="button" id="bb" class="tictactoe" value=" ">
<input type="button" id="bc" class="tictactoe" value=" ">
<br />
<input type="button" id="ca" class="tictactoe" value=" ">
<input type="button" id="cb" class="tictactoe" value=" ">
<input type="button" id="cc" class="tictactoe" value=" ">
<br /><br />
<input type="button" id="ff" value="Reset">
</body>
-bhatkrishnakishor的JSFIDLE演示
蒂克塔克托先生{
宽度:125px;
高度:125px;
背景:#A2A8A1;
};
//2){
whowen=终局();
}
如果(gameOver&&whowen!=''&&whowen!='draw'){
警报(whowen+‘won!')
}
如果(!gameOver&&whowen=='draw'){
警惕(‘比赛被平局!’);
}
}
函数endGame(){
var winningcombines=新数组();
//如果游戏结束,请将此变量值设置为true
gameOver=true;
对于(var指数=0;指数<8;指数=指数+1){
var xMatchCount=0;
var oMatchCount=0;
WinningCombines=winningConditions[索引]。拆分('/');
对于(变量i=0;i<3;i=i+1){
console.log('winningcombines'+winningcombines[i]);
对于(变量j=0;j