-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell.js
146 lines (96 loc) · 2.81 KB
/
cell.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
;(function() {
'use strict';
function Cell(x,y) {
this.x=x;
this.y=y;
}
Cell.prototype = {
// the div for this cell, with it's classes for colour
cellHtml:'',
// dev output to show cell state
devState:false,
// cells colour
colour:'white',
// the piece to which this cell currently belongs
piece:{},
// is this cell filled or not, filled cells restrict moves
state:0,
// co-ordinates
x:0,
y:0,
/**
* appendCell() - adds a cell to the grid div
* @param gridDiv - the div which contains the playing grid
*/
appendCell: function(gridDiv)
{
gridDiv.appendChild(this.cellHtml);
},
/**
* buildCellHtml() - builds some html for this cell
*/
buildCellHtml: function()
{
this.cellHtml=document.createElement("div");
this.addDevOutput();
this.setHtmlClass();
},
/**
* markCell() - marks a cell as filled and coloured
*
* @param colour - colour to mark the cell with
*/
markCell: function(colour)
{
this.setColour(colour);
this.setState(1);
},
/**
* unmarkCell() - unmarks a cell, back to white and empty
*/
unmarkCell: function()
{
this.setColour('white');
this.setState(0);
},
/**
* setColour() - sets the colour for this cell, html and
*/
setColour: function(colour)
{
this.colour=colour;
this.setHtmlClass();
},
/**
* setState() - sets a cells state
*
* @param state - Boolean part of a piece or not
*/
setState: function(state)
{
this.state=state;
if (true === this.devState)
{
document.getElementById('js-state-dev-'+this.x+'-'+this.y).innerHTML=state;
}
},
/**
* setHtmlClass() - sets the class for this cell
*/
setHtmlClass: function()
{
this.cellHtml.setAttribute("class","cell "+this.colour);
},
/**
* addDevOutput() - adds x, y and state values to cells for dev work
*/
addDevOutput: function()
{
if (true === this.devState)
{
this.cellHtml.innerHTML="<span id='js-x-dev' class='cell-dev x-dev'>"+this.x+"</span><span id='js-y-dev y-dev' class='cell-dev'>"+this.y+"</span><span id='js-state-dev-"+this.x+"-"+this.y+"' class='cell-dev state-dev'>"+this.state+"</span>";
}
}
};
window.Cell = Cell;
}());