-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell.js
149 lines (143 loc) · 4.3 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
147
148
149
// Cell object
function Cell(x, y, color){
//console.log(x+', '+y + ', ' + color);
this.x = x;
this.y = y;
this.hasCoin = false;
this.coinColor = null;
this.r = 80;
this.color = color;
this.possible = false;
this.isEaten = false;
}
Cell.prototype.show = function() {
if (this.color === 'black') {
fill(0);
}if (this.color === 'white') {
fill(255);
}if (this.possible) {
fill('blue');
}
rect(this.x, this.y, this.r, this.r);
if (this.hasCoin) {
this.showCoin();
}
}
Cell.prototype.clicked = function(x, y){
return (x > this.x && x < this.x + this.r && y > this.y && y < this.y + this.r);
}
Cell.prototype.showCoin = function() {
if (this.coinColor === 'red') {
fill('red');
}else{
fill('green');
}
ellipseMode()
ellipse(this.x + this.r / 2, this.y + this.r / 2, this.r / 2);
}
Cell.prototype.possibleMoves = function(){
if (this.hasCoin && gameStart) {
var currI = this.x / 80;
var currJ = this.y / 80;
for (var i = 0; i < grid.length; i++) {
for (var j = 0; j < grid[i].length; j++) {
grid[i][j].possible = false;
if (!grid[i][j].hasCoin) {
if ((currI - 1 === i && (currJ - 1 === j || currJ + 1 === j)) ||
(currI + 1 === i && (currJ - 1 === j || currJ + 1 === j))) {
grid[i][j].possible = true;
}
if (currI - 2 === i && currJ - 2 === j && typeof(grid[i+1][j+1]) !== 'undefined') {
if (grid[i+1][j+1].hasCoin && grid[i+1][j+1].coinColor !== this.coinColor) {
grid[i][j].possible = true;
grid[i+1][j+1].isEaten = true;
}
}
if (currI - 2 === i && currJ + 2 === j && typeof(grid[i+1][j-1]) !== 'undefined') {
if (grid[i+1][j-1].hasCoin && grid[i+1][j-1].coinColor !== this.coinColor) {
grid[i][j].possible = true;
grid[i+1][j-1].isEaten = true;
}
}
if (currI + 2 === i && currJ - 2 === j && typeof(grid[i-1][j+1]) !== 'undefined') {
if (grid[i-1][j+1].hasCoin && grid[i-1][j+1].coinColor !== this.coinColor) {
grid[i][j].possible = true;
grid[i-1][j+1].isEaten = true;
}
}
if (currI + 2 === i && currJ + 2 === j && typeof(grid[i-1][j-1]) !== 'undefined') {
if (grid[i-1][j-1].hasCoin && grid[i-1][j-1].coinColor !== this.coinColor) {
grid[i][j].possible = true;
grid[i-1][j-1].isEaten = true;
}
}
}
}
}
}
}
Cell.prototype.moveToIt = function(from){
var fromI = from.x / 80;
var fromJ = from.y / 80;
grid[fromI][fromJ].hasCoin = false;
this.hasCoin = true;
this.coinColor = from.coinColor;
var i = this.x / 80;
var j = this.y / 80;
console.log("heloo")
if (i+1 === 8) {
}
if (!(i-1 === -1 || j+1 === 8)) {
if (grid[i-1][j+1].isEaten) {
try {
eat(grid[i-1][j+1]);
} catch (e) {
console.log("error");
}
//grid[i-1][j+1].hasCoin = false;
}
}
if (!(i-1 === -1 || j-1 === -1)) {
if (grid[i-1][j-1].isEaten) {
try {
eat(grid[i-1][j-1]);
} catch (e) {
console.log("error")
}
//grid[i-1][j-1].hasCoin = false;
}
}
if (!(i+1 === 8 || j+1 === 8)) {
if (grid[i+1][j+1].isEaten) {
try {
eat(grid[i+1][j+1]);
} catch (e) {
console.log("error");
}
//grid[i+1][j+1].hasCoin = false;
}
}
if (!(i+1 === 8 || j-1 === -1)) {
if (grid[i+1][j-1].isEaten) {
try {
eat(grid[i+1][j-1]);
} catch (e) {
console.log("error");
}
// grid[i+1][j-1].hasCoin = false;
}
}
//}//
for (var i = 0; i < grid.length; i++) {
for (var j = 0; j < grid[i].length; j++) {
grid[i][j].possible = false;
grid[i][j].isEaten = false;
}
}
if (turn === aiCoin) {
turn = userCoin;
}else{
turn = aiCoin;
}
checkWin();
}