-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.js
68 lines (57 loc) · 1.66 KB
/
board.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
function Board(_onclickFunction){
this.spotClass = 'stone';
this.blankImage = 'blank.png';
this.blackImage = 'black.png';
this.whiteImage = 'white.png';
this.boardImage = 'board.png';
this.imageSize = 16;
this.initialize = function(onclickFunction){
document.write("<img src='"+this.boardImage+"' style='position:absolute; left:0px; top:0px'>");
for(x=0;x<19;x++){
for(y=0;y<19;y++){
id = this.coordsToId([x,y]);
xPixels = x*this.imageSize;
yPixels = y*this.imageSize;
onclick = onclickFunction + "(this.id)"
document.write("<img class='"+this.spotClass+"' id='"+id+"' onclick='"+onclick+"' style='z-index:1; position:absolute; left:"+xPixels+"px; top:"+yPixels+"px' src='"+this.blankImage+"'>");
}
}
}
this.loadBlank = function(){
for(x=0;x<19;x++){
for(y=0;y<19;y++){
this.updateSpot([x,y],'blank');
}
}
}
this.updateSpot = function(coords,player){
//alert([coords,player])
element = this.coordsToElement(coords);
image = this.playerToImage(player);
//alert(element.id);
element.src = image;
}
this.update = function(updateHash){
updateHash.each(function(pair){this.updateSpot(pair.key.split(','),pair.value);}.bind(this));
}
this.coordsToElement = function(coords){
return $(this.coordsToId(coords))
}
this.coordsToId = function(coords){
return ("board_"+coords[0]+"_"+coords[1])
}
this.idToCoords = function(idString){
splits = idString.substr(6).split('_')
return splits
}
this.playerToImage = function(player){
if(player == 0){
return this.blackImage;
} else if(player == 1){
return this.whiteImage;
} else{
return this.blankImage;
}
}
this.initialize(_onclickFunction);
}