-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.js
60 lines (59 loc) · 1.34 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
function Grid(val, x, y) {
this.val = val;
this.x = x;
this.y = y;
this.s;
}
Grid.prototype.isClicked = function() {
if(mouseX - b.x >= this.x && mouseX - b.x < this.x + this.s)
if(mouseY - b.y >= this.y && mouseY - b.y < this.y + this.s) return true;
else return false;
}
function Board(x, y, s) {
this.x = x;
this.y = y;
if(s == undefined) this.s = 230;
else this.s = s;
this.mark = [];
this.init();
}
Board.prototype.init = function() {
for (var i = 0; i < 3; i++) {
this.mark[i] = [];
for (var j = 0; j < 3; j++) {
this.mark[i][j] = new Grid(0, i*75 + 5, j*75 + 5);
this.mark[i][j].s = 70;
this.mark[i][j].x = i*75+5;
this.mark[i][j].y = j*75+5;
}
}
}
Board.prototype.render = function() {
img.board(this.x, this.y);
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
let x = this.x + i*75 + 15;
let y = this.y + j*75 + 15;
switch(this.mark[i][j].val) {
case 1 : img.markx(x, y); break;
case 2 : img.marko(x, y); break;
}
}
}
}
Board.prototype.set = function(m, i, j) {
constrain(m, 0, 2);
if(j == undefined)
this.mark[i][j].val = m;
else this.mark[i][j].val = m;
}
Board.prototype.getBoard = function() {
let arr = [];
for (let i = 0; i < this.mark.length; i++) {
arr[i] = [];
for (let j = 0; j < this.mark[i].length; j++) {
arr[i][j] = this.mark[i][j].val;
}
}
return arr;
}