From 394194a5177e8c380c8da648b7dd141994698d66 Mon Sep 17 00:00:00 2001 From: Dylan Lloyd Date: Wed, 17 Jun 2015 16:10:26 -0700 Subject: [PATCH] added cursor styling --- ai.js | 60 +++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/ai.js b/ai.js index 8fba9d6..d8660b6 100644 --- a/ai.js +++ b/ai.js @@ -91,33 +91,53 @@ $(document).ready(function(){ } return serial; } + this.checkBoardPosition = function(x,y) { + for (var i = 0; i < tictactoe.cells.length; i++) { + for (var ii = 0; ii < tictactoe.cells[i].length; ii++) { + var cell = tictactoe.cells[i][ii]; + if (x > cell.x && x < cell.x + cell.size + && y > cell.y && y < cell.y + cell.size) { + return cell; + } + } + } + return false; + } + this.play = function(player, cell) { + this.turn = 'ai'; + cell.play(player); + endCondition(tictactoe.serialize(), 'paint'); + if (!this.gameOver) { + var bestMove = minimax(this.serialize(), 'o').move; + tictactoe.cells[bestMove[1]][bestMove[0]].play('o'); + endCondition(tictactoe.serialize(), 'paint'); + tictactoe.turn = 'player'; + } else + $('#board').css('cursor', 'auto'); + } } + + $('#board').mousemove(function(e){ + if (!tictactoe || tictactoe.gameOver || tictactoe.turn != 'player') return; + event = e || window.event; + x = event.pageX - canvas.offsetLeft, + y = event.pageY - canvas.offsetTop; + var cell = tictactoe.checkBoardPosition(x,y); + if (cell && !cell.state) + $('#board').css('cursor', 'pointer'); + else + $('#board').css('cursor', 'auto'); + }); + $('#board').on('click', function(e){ if (!tictactoe || tictactoe.gameOver || tictactoe.turn != 'player') return; event = e || window.event; x = event.pageX - canvas.offsetLeft, y = event.pageY - canvas.offsetTop; - for (var i = 0; i < tictactoe.cells.length; i++) { - for (var ii = 0; ii < tictactoe.cells[i].length; ii++) { - var cell = tictactoe.cells[i][ii]; - if (x > cell.x && x < cell.x + cell.size - && y > cell.y && y < cell.y + cell.size) { - if (!cell.state) { - tictactoe.turn = 'ai'; - cell.play('x'); - endCondition(tictactoe.serialize(), 'paint'); - if (!tictactoe.gameOver) { - var aiMove = minimax(tictactoe.serialize(), 'o').move; - tictactoe.cells[aiMove[1]][aiMove[0]].play('o'); - endCondition(tictactoe.serialize(), 'paint'); - tictactoe.turn = 'player'; - } - return; - } - } - } - } + var cell = tictactoe.checkBoardPosition(x,y); + if (cell && !cell.state) + tictactoe.play('x', cell); }); $('#restart').on('click', function(e){ -- 2.30.2