PDA

View Full Version : Freecodecamp Tic Tac Toe Game Challenge - AI not working



Kavi Jo
Feb 2nd, 2017, 05:07 AM
Hello Everyone,

I am new here so excuse me if i am breaking any rules. I am a bit desperate.

I am doing a stuck on my current project and would really appreciate the help.

The project can be found at http://codepen.io/Daemon0205/pen/PWEdmZ?editors=1111

If you run it, you will find that when it comes to the AI turn to play, nothing happens.

I was wondering if anyone could help me figure out what i did wrong. Any help would greatly appreciate.

Thanks

Philip M
Feb 2nd, 2017, 08:20 AM
People are often reluctant to open links suggested by unknown poster. Have you tried using your error console (F12 key)?

Kavi Jo
Feb 2nd, 2017, 09:17 PM
oic - im sorry about the link.

i did console things out and fixed the AI but now i have 2 more issues.
1 - the win and loss is not registering,
2 - the count is going up by 2 instead of 1.

any help is really appreciated. here is the full code below:

$(document).ready(function() {

$("#Results, #Count, #Board, #Return").hide();

var player, Acells, Playerboard, AIboard, ai;
var Play = false;
var PlayerCount = 0;
var AICount = 0;
var DrawCount = 0;


$("#Xteam").click(function() {

player = 'X';
startGame();

});

$("#Oteam").click(function() {

player = 'O';
startGame();

});

$(".cell").each(function() {
$(this).click(function() {
play($(this).attr("id"));

});
});

$("#Return").click(quit);

function startGame() {
$( "#Count, #Board, #Return").show();

$("#Select").hide();

Acells = ["A-1", "A-2", "A-3", "B-1", "B-2", "B-3", "C-1", "C-2", "C-3"];
Playerboard = [
[false, false, false],
[false, false, false],
[false, false, false]
];
AIboard = [
[false, false, false],
[false, false, false],
[false, false, false]
];
Play = true;

clearAll();

}

function play(cellid) {

var cell = Acells.indexOf(cellid);

if (Play) {
if (cell !== -1) {
$("#" + cellid).text(player);
Acells.splice(cell, 1);

var x = updateBoard(cellid, Playerboard);

if (!x) {
Play = false;
AIplay();
}
}
}
}

function computeAImove(nBoard, AIboard) {

var fullBoard = [];
for (var arr = 0; arr < nBoard, length; arr++) {

var temp = [];
for (var i = 0; i < nBoard[arr].length; i++) {
if (nBoard[arr][i] || AIboard[arr][i]) {
temp.push(true);
} else {
temp.push(false);
}
}
fullBoard.push(temp);
}

var move;

if ((AIboard[0][0] && AIboard[0][1]) && nBoard[0][2] === false) {
move = "A-3";
} else if ((AIboard[0][1] && AIboard[0][2]) && nBoard[0][0] === false) {
move = "A-1";
} else if ((AIboard[1][0] && AIboard[1][1]) && nBoard[1][2] === false) {
move = "A-3";
} else if ((AIboard[1][1] && AIboard[1][2]) && nBoard[1][0] === false) {
move = "B-1";
} else if ((AIboard[2][0] && AIboard[2][1]) && nBoard[2][2] === false) {
move = "C-3";
} else if ((AIboard[2][1] && AIboard[2][2]) && nBoard[2][0] === false) {
move = "C-1";
} else if ((AIboard[0][0] && AIboard[1][0]) && nBoard[2][0] === false) {
move = "C-1";
} else if ((AIboard[1][0] && AIboard[2][0]) && nBoard[0][0] === false) {
move = "A-1";
} else if ((AIboard[0][1] && AIboard[1][1]) && nBoard[2][1] === false) {
move = "C-2";
} else if ((AIboard[1][1] && AIboard[2][1]) && nBoard[0][1] === false) {
move = "A-2";
} else if ((AIboard[0][2] && AIboard[1][2]) && nBoard[2][2] === false) {
move = "C-3";
} else if ((AIboard[1][2] && AIboard[2][2]) && nBoard[0][2] === false) {
move = "A-3";
} else if ((AIboard[2][0] && AIboard[1][1]) && nBoard[0][2] === false) {
move = "A-3";
} else if ((AIboard[1][1] && AIboard[0][2]) && nBoard[2][0] === false) {
move = "C-1";
} else if ((AIboard[0][0] && AIboard[1][1]) && nBoard[2][2] === false) {
move = "C-3";
} else if ((AIboard[1][1] && AIboard[2][2]) && nBoard[0][0] === false) {
move = "A-1";
} else if ((AIboard[2][0] && AIboard[0][2]) && nBoard[1][1] === false) {
move = "B-2";
} else if ((AIboard[0][0] && AIboard[2][2]) && nBoard[1][1] === false) {
move = "B-2";
} else if ((AIboard[0][0] && AIboard[0][2]) && nBoard[0][1] === false) {
move = "A-2";
} else if ((AIboard[1][0] && AIboard[1][2]) && nBoard[1][1] === false) {
move = "B-2";
} else if ((AIboard[2][0] && AIboard[2][2]) && nBoard[2][1] === false) {
move = "C-2";
} else if ((AIboard[0][0] && AIboard[2][0]) && nBoard[1][0] === false) {
move = "B-1";
} else if ((AIboard[0][1] && AIboard[2][1]) && nBoard[1][1] === false) {
move = "B-2";
} else if ((AIboard[0][2] && AIboard[2][2]) && nBoard[1][2] === false) {
move = "B-3";
} else {
if ((nBoard[0][0] && nBoard[0][1]) && AIboard[0][2] === false) {
move = "A-3";
} else if ((nBoard[0][1] && nBoard[0][2]) && AIboard[0][0] === false) {
move = "A-1";
} else if ((nBoard[1][0] && nBoard[1][1]) && AIboard[1][2] === false) {
move = "B-3";
} else if ((nBoard[1][1] && nBoard[1][2]) && AIboard[1][0] === false) {
move = "B-1";
} else if ((nBoard[2][0] && nBoard[2][1]) && AIboard[2][2] === false) {
move = "C-3";
} else if ((nBoard[2][1] && nBoard[2][2]) && AIboard[2][0] === false) {
move = "C-1";
} else if ((nBoard[0][0] && nBoard[1][0]) && AIboard[2][0] === false) {
move = "C-1";
} else if ((nBoard[1][0] && nBoard[2][0]) && AIboard[0][0] === false) {
move = "C-1";
} else if ((nBoard[0][1] && nBoard[1][1]) && AIboard[2][1] === false) {
move = "C-2";
} else if ((nBoard[1][1] && nBoard[2][1]) && AIboard[0][1] === false) {
move = "A-2";
} else if ((nBoard[0][2] && nBoard[1][2]) && AIboard[2][2] === false) {
move = "C-3";
} else if ((nBoard[1][2] && nBoard[2][2]) && AIboard[0][2] === false) {
move = "A-3";
} else if ((nBoard[2][0] && nBoard[1][1]) && AIboard[0][2] === false) {
move = "A-3";
} else if ((nBoard[1][1] && nBoard[0][2]) && AIboard[2][0] === false) {
move = "C-1";
} else if ((nBoard[0][0] && nBoard[1][1]) && AIboard[2][2] === false) {
move = "C-3";
} else if ((nBoard[1][1] && nBoard[2][2]) && AIboard[0][0] === false) {
move = "C-1";
} else if ((nBoard[0][0] && nBoard[2][2]) && AIboard[1][1] === false) {
move = "B-2";
} else if ((nBoard[0][2] && nBoard[2][0]) && AIboard[1][1] === false) {
move = "B-2";
} else if ((nBoard[0][0] && nBoard[0][2]) && AIboard[0][1] === false) {
move = "A-2";
} else if ((nBoard[1][0] && nBoard[1][2]) && AIboard[1][1] === false) {
move = "B-2";
} else if ((nBoard[2][0] && nBoard[2][2]) && AIboard[2][1] === false) {
move = "C-2";
} else if ((nBoard[0][0] && nBoard[2][0]) && AIboard[1][0] === false) {
move = "B-1";
} else if ((nBoard[0][1] && nBoard[2][1]) && AIboard[1][1] === false) {
move = "B-2";
} else if ((nBoard[0][2] && nBoard[2][2]) && AIboard[1][2] === false) {
move = "B-3";
} else {

var possibleMoves = ["x", "x", "x"];
if (true) {

var index = Math.floor(Math.random() * possibleMoves.length);

if (!nBoard[0][0] && !nBoard[1][0] && !nBoard[0][2]) {

possibleMoves = ["A-1", "B-1", "C-1"];
} else if (!nBoard[0][0] && !nBoard[1][0] && !nBoard[2][0]) {
possibleMoves = ["A-1", "B-1", "C-1"];
} else if (!nBoard[1][0] && !nBoard[1][1] && !nBoard[1][2]) {
possibleMoves = ["B-1", "B-2", "B-3"];
} else if (!nBoard[2][0] && !nBoard[2][1] && !nBoard[2][2]) {
possibleMoves = ["C-1", "C-2", "C-3"];
} else if (!nBoard[0][1] && !nBoard[1][1] && !nBoard[2][1]) {
possibleMoves = ["A-1", "B-1", "C-1"];
} else if (!nBoard[0][2] && !nBoard[1][2] && !nBoard[2][2]) {
possibleMoves = ["A-3", "B-3", "C-3"];
} else if (!nBoard[0][0] && !nBoard[1][1] && !nBoard[2][2]) {
possibleMoves = ["A-1", "B-2", "C-3"];
} else if (!nBoard[0][2] && !nBoard[1][1] && !nBoard[2][0]) {
possibleMoves = ["A-3", "B-2", "C-1"];
}
}

if (Acells.indexOf(possibleMoves[index]) === -1) {

possibleMoves.splice(index, 1);
index = Math.floor(Math.random() * possibleMoves.length);
}
move = possibleMoves[index];

}
}

if (Acells.indexOf(move) !== -1) {
return move;
}

}

function detectFork(Board) {

var move;
if (Board[0][0]) {
return true;
} else if (Board[0][2]) {
return true;
} else if (Board[2][2]) {
return true;
} else if (Board[2][0]) {
return true;
}

}

function AIplay() {
if (player === "O") {
ai = "X";
} else if (player === "X") {
ai = "O";
}

var play1;
var move = computeAImove(Playerboard, AIboard);

if (!move) {
var index = Math.floor(Math.random() * Acells.length);
play1 = Acells[index];
} else {
play1 = move;
}
$("#" + play1).text(ai);
updateBoard(play1, AIboard);
Acells.splice(Acells.indexOf(play1), 1);
Play = true;
}

function quit() {
player = undefined;
$("#Board").hide();
$("#Return").hide();
$("#Count").hide();
$("#Results").hide();
$("#Select").show();
}

function clearAll() {
$(".cell").html("");
}

function updateBoard(cellid, nBoard) {
switch (cellid) {
case "A-1":
nBoard[0][0] = true;
break;
case "A-2":
nBoard[0][1] = true;
break;
case "A-3":
nBoard[0][2] = true;
break;
case "B-1":
nBoard[1][0] = true;
break;
case "B-2":
nBoard[1][1] = true;
break;
case "B-3":
nBoard[1][2] = true;
break;
case "C-1":
nBoard[2][0] = true;
break;
case "C-2":
nBoard[2][1] = true;
break;
case "C-3":
nBoard[2][2] = true;
break;
}

if (detectWin(nBoard)) {
victoy();
return true;

} else if (Acells.length === 0) {
tie();
}
}

function detectWin(nBoard) {

if (
(nBoard[0][0] && nBoard[0][1] && nBoard[0][2]) ||
(nBoard[0][0] && nBoard[1][0] && nBoard[2][0]) ||
(nBoard[1][0] && nBoard[1][1] && nBoard[1][2]) ||
(nBoard[2][0] && nBoard[2][1] && nBoard[2][2]) ||
(nBoard[0][1] && nBoard[1][1] && nBoard[2][1]) ||
(nBoard[0][2] && nBoard[1][2] && nBoard[2][2]) ||
(nBoard[0][0] && nBoard[1][1] && nBoard[2][2]) ||
(nBoard[0][2] && nBoard[1][1] && nBoard[2][0])
) {
return true;
}
}

function tie() {
$("#Board").hide();
$("#ItsDraw").show();
DrawCount=DrawCount+1;
$("#Drawcount").html(DrawCount);
}

function victory() {

$("#Board").hide();


if (Play) {
$("#PlayerWins").show();
PlayerCount=PlayerCount+1;
$("#PlayerCount").html(PlayerCount);

} else {
$("#AIwins").show();
AICount=AICount+1;
$("#AIcount").html(AICount);

}

}

});