Tuesday 16 May 2017

Strange Probelms with Connect4 AI

I've been working on a Connect 4 Robot Project, for which purpose I am trying to get this code to work on my PC with node.js, so that it can be run on a Raspberry Pi, with sensors and some buttons being the only input. Unfortunately I am not very experienced with Javascript, which is why I am having some issues with it. I commented out most of the lines that were incompatible with node.js, and I managed to get it to work for one round. However, as soon as I call the "act" function more than once:

Game.prototype.act = function(column  /*e*/) {

    // Human round
    if (that.round == 0) that.place(column    /*element.cellIndex*/);

    // Computer round
    if (that.round == 1) that.generateComputerDecision();

Game.prototype.place = function(column) {
    // If not finished
    if (that.board.score() != that.score && that.board.score() != -that.score && !that.board.isFull()) {
    if (!that.board.place(column)) {              //Place coin in seperate Board class
        return alert("Invalid move!");
    }

    that.round = that.switchRound(that.round);
    that.updateStatus();
}
}

things start getting weird. When I for example test it with "act(1)", immediately followed by "act(2)", the second coin comes out in the wrong column. I did some more testing with console logs and it appears that the programm does not run in the order that i want. It seems to start the next "act" function call before the last one has finished calculating the Computers move.

How can i solve this?

Thanks in advance for any advice and I apologize if my question wasn't very clear.

EDIT: At the top of the Game constructor, I commented out this:

//document.getElementById('game_board').innerHTML = game_board;
*/
// Action listeners
// var td = document.getElementById('game_board').getElementsByTagName("td");

/*for (var i = 0; i < td.length; i++) {
    if (td[i].addEventListener) {
        td[i].addEventListener('click', that.act, false);
    } else if (td[i].attachEvent) {
        td[i].attachEvent('click', that.act)
    }
}*/

Could that be a problem?



via Kaza123

No comments:

Post a Comment