Skip to content

Instantly share code, notes, and snippets.

@mohsenheydari
Created January 8, 2019 01:37
Show Gist options
  • Save mohsenheydari/3fbc6e9b1a97951418f9691382ec4a53 to your computer and use it in GitHub Desktop.
Save mohsenheydari/3fbc6e9b1a97951418f9691382ec4a53 to your computer and use it in GitHub Desktop.
Converts clicked coordinates to cell index in Tic Tac Toe board
getCellIndex(position){
for (let x = 0;x < 3;x++) {
for (let y = 0;y < 3;y++) {
let xCordinate = x * this.cellSize;
let yCordinate = y * this.cellSize;
if (
position.x >= xCordinate && position.x <= xCordinate + this.cellSize &&
position.y >= yCordinate && position.y <= yCordinate + this.cellSize
) {
return y * 3 + x;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment