Created
September 28, 2014 03:46
-
-
Save jakl/68b2909710b1d0e0524d to your computer and use it in GitHub Desktop.
example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<table> | |
<tr><td id="00"></td> <td id="01"> </td><td id="02"></td></tr> | |
<tr><td id="10"></td><td id="11"></td><td id="12"></td></tr> | |
<tr><td id="20"></td><td id="21"></td><td id="22"></td></tr> | |
</table> | |
<style> | |
table, th, td { | |
border: 1px solid black; | |
} | |
td { | |
width: 50px; | |
height: 50px; | |
} | |
</style> | |
<script> | |
board = [[0,0,0],[0,0,0],[0,0,0]] | |
turn = false | |
elems = document.getElementsByTagName('td') | |
for(i = 0; i < elems.length; i++){ | |
elem = elems[i] | |
elem.onclick = selectSpot | |
} | |
function selectSpot() { | |
id = this.id | |
if(empty(id)){ | |
spot(id, player()) | |
advanceTurn() | |
} | |
} | |
function advanceTurn() { | |
turn = !turn | |
} | |
function player() { | |
if(turn) | |
return 1 | |
else | |
return 2 | |
} | |
function empty(id) { | |
return spot(id) == 0 | |
} | |
function spot(id, value) { | |
x = id[0] | |
y = id[1] | |
if(value){ | |
board[x][y] = value | |
placeMark(id) | |
} else { | |
return board[x][y] | |
} | |
} | |
function placeMark(id) { | |
color = turn ? 'blue' : 'red' | |
document.getElementById(id).style.backgroundColor = color | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment