-
Notifications
You must be signed in to change notification settings - Fork 0
/
go.js
186 lines (168 loc) · 4.34 KB
/
go.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
let goField = [];
// WHITE = 1, BLACK = 2, EMPTY = 3, OUT = 4
const goStatus = 'EMPTY';
//board size x by y
const boardSize = [4, 4];
let surrounds = [];
//setting field values (EXAMPLE)
// . #.
// # o #
// . #. <= bottom to top
let setup = [
{ x: 1, y: 2, value: 'BLACK' },
{ x: 2, y: 1, value: 'BLACK' },
{ x: 2, y: 2, value: 'WHITE' },
{ x: 2, y: 3, value: 'BLACK' },
{ x: 3, y: 2, value: 'BLACK' },
];
//setting field values (EXAMPLE)
// o #
// #. <= bottom to top
// board size 3x3
// let setup = [
// { x: 1, y: 2, value: 'BLACK' },
// { x: 1, y: 3, value: 'WHITE' },
// { x: 2, y: 3, value: 'BLACK' },
// ];
// oo.
// ## o
// o o #
// .o. <= bottom to top
// const setup = [
// { x: 2, y: 1, value: 'WHITE' },
// { x: 1, y: 2, value: 'WHITE' },
// { x: 2, y: 2, value: 'WHITE' },
// { x: 3, y: 2, value: 'BLACK' },
// { x: 1, y: 3, value: 'BLACK' },
// { x: 2, y: 3, value: 'BLACK' },
// { x: 3, y: 3, value: 'WHITE' },
// { x: 1, y: 4, value: 'WHITE' },
// { x: 2, y: 4, value: 'WHITE' },
// ];
let drawBoard = (_x, _y) => {
let tempX = -1;
function pushToGrid(_tempX, _i, _status) {
goField.push({ x: _tempX, y: _i + 1, value: _status });
}
for (let i = 0; i < _x + 1; i++) {
tempX++;
for (let i = -1; i < _y; i++) {
if (tempX === 0 || i + 1 === 0) {
pushToGrid(tempX, i, 'OUT');
} else if (i === _y - 1 || tempX === _x) {
pushToGrid(tempX, i, 'OUT');
} else {
pushToGrid(tempX, i, goStatus);
}
}
}
};
//initialising goban board
drawBoard(boardSize[0] + 1, boardSize[1] + 1);
let placeRock = (_setup) => {
setup.map((a) => {
goField.find((o, i) => {
if ((o.x === a.x) & (o.y === a.y)) {
goField[i] = { x: a.x, y: a.y, value: a.value };
return true; // stop searching
}
});
});
};
placeRock(setup);
//getting status of coordinates
let get_status = (_x, _y) => {
let obj = goField.find((o) => (o.x === _x) & (o.y === _y));
if (obj.value === 'EMPTY' || obj.value === 'OUT') {
console.log('EMPTY or OUT');
} else {
if (is_shape(obj)) {
// return shape cordinates, array
if (
checkSurrounds(obj.x, obj.y)[0] === 'EMPTY' ||
checkSurrounds(obj.x, obj.y)[1] === 'EMPTY' ||
checkSurrounds(obj.x, obj.y)[2] === 'EMPTY' ||
checkSurrounds(obj.x, obj.y)[3] === 'EMPTY'
) {
console.log('not take, there are moves');
} else {
console.log(' need to draw shape; ', drawShape(obj));
}
} else {
is_taken(obj, surrounds);
}
}
};
let checkSurrounds = (_x, _y) => {
surrounds = [
goField.find((o) => (o.x === _x - 1) & (o.y === _y)).value,
goField.find((o) => (o.x === _x) & (o.y === _y + 1)).value,
goField.find((o) => (o.x === _x + 1) & (o.y === _y)).value,
(obj_bottom = goField.find((o) => (o.x === _x) & (o.y === _y - 1)).value),
];
return surrounds;
};
let is_shape = (_obj) => {
console.log(
'Surrounds: ', '\n',
'\n',
' ',
checkSurrounds(_obj.x, _obj.y)[1],
' ',
'\n',
checkSurrounds(_obj.x, _obj.y)[0],
' ',
checkSurrounds(_obj.x, _obj.y)[2],
'\n',
' ',
checkSurrounds(_obj.x, _obj.y)[3], '\n'
);
if (
checkSurrounds(_obj.x, _obj.y)[0] === _obj.value ||
checkSurrounds(_obj.x, _obj.y)[1] === _obj.value ||
checkSurrounds(_obj.x, _obj.y)[2] === _obj.value ||
checkSurrounds(_obj.x, _obj.y)[3] === _obj.value
) {
//is shape
return true;
} else {
return false;
}
};
let drawShape = (_obj) => {
let shapeArray = [];
//shape colour
if (_obj.value === 'WHITE') {
shapeArray.push('WHITE');
} else {
shapeArray.push('BLACK');
}
shapeArray.push({ x: _obj.x, y: _obj.y });
// TO DO
//draw full shape
return shapeArray;
};
let is_taken = (_obj, _surrounds) => {
// console.log(_obj, _surrounds);
if (
_surrounds[0] === 'EMPTY' ||
_surrounds[1] === 'EMPTY' ||
_surrounds[2] === 'EMPTY' ||
_surrounds[3] === 'EMPTY'
) {
console.log(' is NOT taken, there are moves');
} else {
if (
(_surrounds[0] !== _obj.value) &
(_surrounds[1] !== _obj.value) &
(_surrounds[2] !== _obj.value) &
(_surrounds[3] !== _obj.value)
) {
console.log(' this', _obj.value, 'is taken');
} else {
console.log(' IS NOT TAKEN');
}
}
};
//get status
get_status(2, 2);