I'm trying to make this freeCodeCamp challenge.. i have a problem with my computer AI
a little explanation--
every time is computer turn i push every item of my array disponibili (available ) in a mossa_calcolata array.. then i cancat this with computer_ moves array and i get my comp_possible array..the same things is done for player1;
after this i use my checkwin function with my comp_possible and if its true i use my disegnaSegno() function for put the moves in the right place...but if checkWin its not true i check the player possibilities...
but the problem is:sometimes computer prefer to block the players winning move insted of closing the game..especially if player1 start the game..
else if (computer_moves.length >= 1 && player1_moves.length >= 1) {
dance: for (var j = 1; j < disponibili.length; j++) {
mossa_calcolata = [];
comp_possible = [];
player_possible = [];
mossa_calcolata.push(disponibili[j]);
comp_possible = computer_moves.concat(mossa_calcolata);
player_possible = player1_moves.concat(mossa_calcolata);
for (var x = 0; x < possible_wins.length; x++) {
if (checkWin(comp_possible, possible_wins[x])) {
console.log("primo caso");
console.log(possible_wins[x]);
disegnaSegno(disponibili[j]);
svuotaDisponibili(disponibili[j]);
computer_moves.push(disponibili[j]);
setTimeout(function() {
segnalaVittoria(possible_wins[x]);
}, 1500);
setTimeout(function() {
popUpVittoria("computer");
}, 2000);
player2_counter++;
$("#player2-counter").text(player2_counter);
//break dance;
} else if (checkWin(player_possible, possible_wins[x])) {
console.log("secondo caso");
console.log(possible_wins[x]);
disegnaSegno(disponibili[j]);
computer_moves.push(disponibili[j]);
svuotaDisponibili(disponibili[j]);
break dance;
}
}
}
}
this is my codepen link for complete code http://ift.tt/2mWNNQI
Aucun commentaire:
Enregistrer un commentaire