This is the click function. I've inserted a computer player and need it to replicate what the users click does. It is a connect four game.
click: function(ev) {
var component = this;
if(component.get('playing') && !component.get('winner')) {
if(ev.target.tagName.toLowerCase() == 'canvas' && ev.offsetX < 360 && ev.offsetY < 360) {
var x = Math.floor((ev.offsetX) / 51.5);
var y = Math.floor((ev.offsetY));
var state = component.get('state');
var gridHeight = state[0].length;
for(y = 1; y < gridHeight; y++ ) {
if(state[x][y]) {
//this cell is occupied, so choose the cell above y
break;
}
}
y = y - 1;
This is what makes the users counter go up one when the desired spot is already occupied
if (!state[x][y]) {
var move_count = component.get('moves')['x'];
var marker = component.get('markers')['x'][move_count];
state[x][y] = 'x';
marker.visible = true;
marker.x = 25 + x * 51.5;
marker.y = 20 + y * 60;
component.check_winner();
component.get('moves')['x'] = move_count + 1;
setTimeout(function() {
if(!component.get('winner')&& !component.get('draw')) {
var move = computer_move(state);
move_count = component.get('moves')['o'];
state[move.x][move.y] = 'o';
marker = component.get('markers')['o'][move_count];
marker.visible = true;
marker.x = 25 + move.x * 51.5;
marker.y = 20 + move.y * 60 ;
component.get('moves')['o']= move_count + 1;
component.get ('stage').update();
component.check_winner();
}
}, 200);
}
console.log(state);
}
} },
I need the computer to be able to drop the counters in the same fashion as the user
Aucun commentaire:
Enregistrer un commentaire