Skip to content

Instantly share code, notes, and snippets.

@jakl
Created September 28, 2014 03:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakl/68b2909710b1d0e0524d to your computer and use it in GitHub Desktop.
Save jakl/68b2909710b1d0e0524d to your computer and use it in GitHub Desktop.
example
<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