-
Notifications
You must be signed in to change notification settings - Fork 2
/
toe.min.js
1 lines (1 loc) · 3.18 KB
/
toe.min.js
1
/* full source code at https://github.com/alexdantas/doge-toe/ */function onCanvasClick(a){var b=a.clientX-canvas.offsetLeft,c=a.clientY-canvas.offsetTop;onBoardClick(b,c)}function hiliteScore(a){var b=void 0;b=a===PLAYER_ONE?"#player-one":"#player-two",$(b).effect("highlight",{color:"green"},6e3)}function randomBetween(a,b){var c=Math.floor(Math.random()*(b-a+1)+a);return c}function drawBoard(){context.fillStyle=BOARD_COLOR_BG,context.fillRect(0,0,BOARD_WIDTH,BOARD_HEIGHT);for(var a=0;3>a;a+=1)for(var b=0;3>b;b+=1){var c=BOARD_OFFSET_X+a*TILE_WIDTH+a*TILE_SPACING,d=BOARD_OFFSET_Y+b*TILE_HEIGHT+b*TILE_SPACING,e=board[a][b];e===TILE_EMPTY?(context.fillStyle=TILE_COLOR_EMPTY,context.fillRect(c,d,TILE_WIDTH,TILE_HEIGHT)):e===TILE_X?(context.drawImage(player_one_image,c,d),context.fillStyle=BOARD_COLOR_BG,context.fillText(PLAYER_ONE_POINTS,c+10,d+10)):e===TILE_O&&(context.drawImage(player_two_image,c,d),context.fillStyle=BOARD_COLOR_BG,context.fillText(PLAYER_TWO_POINTS,c+10,d+10))}}function pixelToTileX(a){for(var b=0;3>b;b+=1)if(a>=BOARD_OFFSET_X+b*TILE_WIDTH+b*TILE_SPACING&&BOARD_OFFSET_X+TILE_WIDTH+b*TILE_WIDTH+b*TILE_SPACING>=a)return b;return-1}function pixelToTileY(a){for(var b=0;3>b;b+=1)if(a>=BOARD_OFFSET_Y+b*TILE_HEIGHT+b*TILE_SPACING&&BOARD_OFFSET_Y+TILE_HEIGHT+b*TILE_HEIGHT+b*TILE_SPACING>=a)return b;return-1}function wonGame(){var a=void 0,b=void 0,c=void 0;if(a=board[0][0],b=board[1][1],c=board[2][2],a===b&&b===c&&c!==TILE_EMPTY)return!0;if(a=board[0][2],b=board[1][1],c=board[2][0],a===b&&b===c&&c!==TILE_EMPTY)return!0;for(var d=0;3>d;d+=1){if(a=board[d][0],b=board[d][1],c=board[d][2],a===b&&b===c&&c!==TILE_EMPTY)return!0;if(a=board[0][d],b=board[1][d],c=board[2][d],a===b&&b===c&&c!==TILE_EMPTY)return!0}return!1}function onBoardClick(a,b){if(!gameOver){var c=pixelToTileX(a),d=pixelToTileY(b);if(!(0>c||0>d)&&board[c][d]===TILE_EMPTY)return board[c][d]=currentPlayer===PLAYER_ONE?TILE_X:TILE_O,drawBoard(),wonGame()?(currentPlayer===PLAYER_ONE?(PLAYER_ONE_POINTS+=1,hiliteScore(PLAYER_ONE)):(PLAYER_TWO_POINTS+=1,hiliteScore(PLAYER_TWO)),player_one_score.placeholder=PLAYER_ONE_POINTS,player_two_score.placeholder=PLAYER_TWO_POINTS,void(gameOver=!0)):void(currentPlayer=currentPlayer===PLAYER_ONE?PLAYER_TWO:PLAYER_ONE)}}function resetGame(){gameOver=!1,board=[[TILE_EMPTY,TILE_EMPTY,TILE_EMPTY],[TILE_EMPTY,TILE_EMPTY,TILE_EMPTY],[TILE_EMPTY,TILE_EMPTY,TILE_EMPTY]],drawBoard()}var canvas=$("#screen")[0];canvas.addEventListener("click",onCanvasClick,!1);var context=canvas.getContext("2d"),player_one_score=$("#player-one")[0],player_two_score=$("#player-two")[0],player_one_image=new Image;player_one_image.src="img/player-one.png";var player_two_image=new Image;player_two_image.src="img/player-two.png";var TILE_EMPTY=0,TILE_O=1,TILE_X=2,BOARD_OFFSET_X=10,BOARD_OFFSET_Y=10,BOARD_WIDTH=340,BOARD_HEIGHT=340,TILE_WIDTH=100,TILE_HEIGHT=100,TILE_SPACING=10,BOARD_COLOR_BG="#000",TILE_COLOR_EMPTY="#777",TILE_COLOR_X="red",TILE_COLOR_O="blue",board=[[TILE_EMPTY,TILE_EMPTY,TILE_EMPTY],[TILE_EMPTY,TILE_EMPTY,TILE_EMPTY],[TILE_EMPTY,TILE_EMPTY,TILE_EMPTY]],PLAYER_ONE=1,PLAYER_TWO=2,PLAYER_ONE_POINTS=0,PLAYER_TWO_POINTS=0,currentPlayer=randomBetween(PLAYER_ONE,PLAYER_TWO),gameOver=!1;$(function(){resetGame()});