/
main.js
252 lines (189 loc) · 6.42 KB
/
main.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Global variables
var adjacentSquare = {},
opacity = 0.3,
state
data = [];
// initialize data
for (i = 0; i < 25; i++) {
var s = 'c' + Math.floor(i/5) + '' + i%5;
data.push(s);
adjacentSquare[s] = [];
for (dx = -1; dx <= 1; ++dx) {
for (dy = -1; dy <= 1; ++dy) {
if (dx != 0 || dy != 0) {
var x = Math.floor(i/5)+dx, y = i%5 + dy;
adjacentSquare[s].push('c' + x + '' + y)
}
}
}
}
for (i = 0; i < 25; i++) {
s = data[i]
adjacentSquare[s] = adjacentSquare[s].filter(function(d){return data.includes(d) })
}
function loadBoard() {
// create board and sizing
d3.select("#board")
.selectAll("div")
.data(data)
.enter()
.append("div")
.attr('class', 'square')
.attr('id', function(d){return d})
.attr('level', '0')
.attr('worker', '')
.attr('selected', 'false')
.attr('possiblemove', '')
boardSizing();
window.addEventListener("resize", boardSizing);
}
function boardSizing() {
var windowHeight = window.innerHeight,
windowWidth = window.innerWidth,
titleHeight = d3.select('#header').node().getBoundingClientRect().height+d3.select('#bar').node().getBoundingClientRect().height,
boardSize = d3.min([windowHeight - titleHeight, windowWidth])*0.8;
d3.select("#board")
.style('width', boardSize+'px')
.style('height', boardSize+'px')
}
//
d3.select('#button').on('click', load)
d3.select('.navbar-btn').on('click', load)
function loadGame(){
console.log('load')
d3.select('#turn').html('Your turn')
d3.select('#button').html('▾')
d3.select('#shortInstructions').html('(Beginning)')
d3.select('#instructions').html('Select the square where to place your first worker')
state = 'init-1';
d3.selectAll('.square')
.on('click', onClick)
d3.select('#button')
.on('click', function(){
console.log('display instructions')
d3.select('#instructions')
.transition()
.duration(500)
.style('opacity', 1-d3.select('#instructions').style('opacity') )
})
}
function load(){
d3.selectAll('#board > *').remove()
loadBoard()
loadGame()
}
// main function
function onClick(){
if (state=='init-1'){
d3.select(this).attr('worker', 'Af').append('div').attr('class', 'worker human')
ai();
// Next state
state = 'init-2';
d3.select('#instructions').html('Select the square where to place your second worker')
return;
}
if (state=='init-2'){
d3.select(this).attr('worker', 'Ag').append('div').attr('class', 'worker human')
ai();
// Next state
state = 'player-select-to-move';
d3.select('#instructions').html('Select the worker you want to move')
d3.select('#shortInstructions').html('(Move)')
return;
}
if (state=='player-select-to-move'){
if( d3.select(this).attr('worker')=='' ){
console.log('There is no worker on this square !')
return;
}
if( ['Bg', 'Bf'].includes(d3.select(this).attr('worker')) ){
console.log('This is not your worker !')
return;
}
if( ['Ag', 'Af'].includes(d3.select(this).attr('worker')) ){
d3.select(this).attr('class', d3.select(this).attr('class')+ ' selected');
d3.select(this).attr('selected', 'true')
// identify possible square to move :
var a = adjacentSquare[d3.select(this).attr('id')];
for (i = 0; i < a.length; i++) {
if ( d3.select("#"+a[i]).attr('worker') == '' ){
if ( (parseInt(d3.select("#"+a[i]).attr('level')) - parseInt(d3.select(this).attr('level'))) < 2 ){
d3.select("#"+a[i]).attr('possiblemove', 'true')
}
}
}
d3.select(this).attr('possiblemove', 'true')
d3.selectAll("[possiblemove= '']").style('opacity', opacity)
d3.select(this).attr('possiblemove', '')
// Next state
state = 'player-move';
d3.select('#instructions').html('Click where you want your worker to go')
return;
}
}
if (state=='player-move'){
if(d3.select(this).attr('selected')=='true'){
d3.select(this).classed('selected', false).attr('selected', 'false')
// back
state = 'player-select-to-move';
d3.selectAll(".square").style('opacity', 1).attr('possiblemove', '')
return;
}
if(d3.select(this).attr('possiblemove')==''){
console.log('This is not a possible move !')
return;
}
if(d3.select(this).attr('possiblemove')=='true'){
d3.select(this)
.attr('worker', d3.select(".selected").attr('worker'))
.append('div')
.attr('class', 'worker human')
d3.selectAll(".square").style('opacity', 1)
d3.select(".selected > .worker").remove()
d3.select(".selected").classed('selected', false)
.attr('selected', 'false')
.attr('worker', '')
d3.selectAll(".square").attr('possiblemove', '')
if (d3.select(this).attr('level')=='3'){
console.log('You win !')
state='ended'
d3.select('#turn').html('You win 🎉 Want to play again ?')
d3.select('#instructions').html(' ')
d3.select('#shortInstructions').html(' ')
d3.select('#button').html('⚔').on('click', load)
return;
}
// identify possible square to build :
var a = adjacentSquare[d3.select(this).attr('id')];
for (i = 0; i < a.length; i++) {
if ( d3.select("#"+a[i]).attr('worker') == '' ){
if ( d3.select("#"+a[i]).attr('level') != '4' ){
d3.select("#"+a[i]).attr('possiblemove', 'true')
}
}
}
d3.select(this).attr('possiblemove', 'true')
d3.selectAll("[possiblemove= '']").style('opacity', opacity)
d3.select(this).attr('possiblemove', '')
// Next state
state = 'player-build';
d3.select('#instructions').html('Build in a square adjacent to your moved worker')
d3.select('#shortInstructions').html('(Build)')
return;
}
}
if (state=='player-build'){
if(d3.select(this).attr('possiblemove')==''){
console.log('You cannot build here')
return;
}
if(d3.select(this).attr('possiblemove')=='true'){
d3.select(this).attr('level', parseInt(d3.select(this).attr('level'))+1)
d3.select(this).append('div').attr('class', 'level'+d3.select(this).attr('level'))
d3.selectAll(".square").style('opacity', 1).attr('possiblemove', '')
state='ai-select-to-move';
ai();
return;
}
}
}