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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
function _edit_view(div) {
_view.call(this, div);
var self = this;
var pick = this.get_pick();
var ctx = this.get_ctx();
var dai_left = (_val.VIEW_W - 1.5 * 7 * _val.KOMA_L) / 2;
var dai_right = (_val.VIEW_W + 1.5 * 7 * _val.KOMA_L) / 2;
var dai_top;
var dai_bottom;
var dai = null;
var b_pick_komabako = false;
this.clean_and_draw = function(b_clean) {
if (b_clean === true) {
pick.before = -1;
pick.after = -1;
this.set_komadai_mode(0);
this.get_teban();
} else if (pick.after != -1) {
if (pick.after < 81) // 盤面に駒を追加
this.set_banmen_koma(pick.after, pick.koma);
else if (pick.after == 81) // 持駒に駒を追加
dai[pick.koma % 8]++;
if (pick.before < 81) // 盤面から駒を削除
this.set_banmen_koma(pick.before, -1);
else if (pick.before == 81) // 持駒から駒を削除
dai[pick.koma % 8]--;
if (pick.before == 82 || pick.after == 82)
komabako_callback();
pick.before = -1;
pick.after = -1;
b_pick_komabako = false;
} else {
if (b_pick_komabako) komabako_callback();
b_pick_komabako = (pick.before == 82);
}
this.draw();
this.draw_komadai(this.get_komadai_mode());
}
var komabako_callback;
this.set_komabako_callback = function(f) { komabako_callback = f; }
var sasite_callback;
this.set_sasite_callback = function(f) { sasite_callback = f; }
this.banmen_edit = function(x, y) {
var ban_pos = this.get_banmen_pos(x, y);
var koma = this.get_banmen_koma(ban_pos);
if (ban_pos != -1) { // 盤面がクリックされた
if (pick.before == -1) { // 駒を掴んでいない
if (koma != -1) { // 駒がある
pick.koma = koma;
pick.before = ban_pos;
this.clean_and_draw();
}
return;
}
// 駒を掴んでいる
var pos = pick.before;
if (ban_pos == pos) { // 同じ位置
if (koma % 16 == _koma.Ou) return;
if (koma % 16 == _koma.Ki) koma += 8;
do {
koma = (koma + 8) % 32;
} while (this.get_kyokumen().is_kinjite(koma, ban_pos, pos));
pick.koma = koma;
this.set_banmen_koma(pos, koma);
this.clean_and_draw();
return;
}
if (koma != -1) { // 駒がある
pick.koma = koma;
pick.before = ban_pos;
this.clean_and_draw();
return;
}
// 駒がない
if (this.get_kyokumen().is_kinjite(pick.koma, ban_pos, pos)) return;
pick.after = ban_pos;
this.clean_and_draw();
return;
} // 盤面がクリックされた
var komadai_mode = this.get_komadai_mode();
var mochigoma_mode = this.get_mochigoma_mode();
if (komadai_mode != 1 && mochigoma_mode != 1 &&
this.in_sente_mochigoma(x, y)) { // 先手の持ち駒がクリックされた
if (pick.before == 81) pick.before = -1;
this.set_komadai_mode(1);
dai = this.get_sente_mochigoma();
this.clean_and_draw();
return;
} else if (komadai_mode != 2 && mochigoma_mode != 2 &&
this.in_gote_mochigoma(x, y)) { // 後手の持ち駒がクリックされた
if (pick.before == 81) pick.before = -1;
this.set_komadai_mode(2);
dai = this.get_gote_mochigoma();
this.clean_and_draw();
return;
} // 持駒がクリックされた
if (this.in_komadai(x, y)) { // 駒台がクリックされた
if (pick.before == -1) { // 駒を掴んでいない
koma = this.get_komadai_koma(x, y);
if (koma != -1) {
pick.koma = koma;
pick.before = 81;
this.clean_and_draw();
}
return;
}
// 駒を掴んでいる
koma = pick.koma;
if (koma % 8 == _koma.Ou) return; // 玉
if (pick.before == 81) { // 駒台の駒
koma = this.get_komadai_koma(x, y);
if (koma != -1) {
pick.koma = koma;
pick.before = 81;
} else {
pick.before = -1;
}
this.clean_and_draw();
return;
}
pick.after = 81;
this.clean_and_draw();
return;
} // 駒台がクリックされた
if (pick.before != -1) { // 盤の外
pick.before = -1;
this.clean_and_draw();
}
}
var b_teban = true;
this.get_teban = function() {
if (b_teban == null) return;
var sasite = this.get_sasite();
b_teban = (sasite.is_empty_sasite() || sasite.koma >= 16);
}
this.set_teban = function(b) {
if (b) {
b_teban = true; this.get_teban();
} else {
b_teban = null;
}
}
this.kifu_edit = function(x, y) {
var ban_pos = this.get_banmen_pos(x, y);
var koma = this.get_banmen_koma(ban_pos);
if (ban_pos != -1) { // 盤面がクリックされた
if (ban_pos == pick.before) return;
if (pick.before != -1) {
var kyokumen = self.get_kyokumen();
var option = kyokumen.can_put(ban_pos, pick.before, pick.koma);
if (option != -1) { // 駒が置ける場合
if ((option & _opt.NARAZU) != 0 && confirm(_msg.NARI))
option ^= _opt.NARI | _opt.NARAZU;
sasite_callback(new _sasite(ban_pos, pick.before, pick.koma, option));
return;
}
}
if (koma != -1 && b_teban != (koma >= 16)) { // 駒があり、かつ手番なら
pick.koma = koma;
pick.before = ban_pos;
this.clean_and_draw();
return;
}
return;
} // 盤面がクリックされた
var komadai_mode = this.get_komadai_mode();
if (komadai_mode != 1 && b_teban !== false &&
this.in_sente_mochigoma(x, y)) { // 先手の持ち駒がクリックされ、かつ手番なら
if (pick.before == 81) pick.before = -1;
this.set_komadai_mode(1);
dai = this.get_sente_mochigoma();
this.clean_and_draw();
return;
} else if (komadai_mode != 2 && b_teban !== true &&
this.in_gote_mochigoma(x, y)) { // 後手の持ち駒がクリックされ、かつ手番なら
if (pick.before == 81) pick.before = -1;
this.set_komadai_mode(2);
dai = this.get_gote_mochigoma();
this.clean_and_draw();
return;
}
if (komadai_mode != 0 && this.in_komadai(x, y)) { // 駒台がクリックされた
koma = this.get_komadai_koma(x, y);
if (koma != -1) {
pick.koma = koma;
pick.before = 81;
this.clean_and_draw();
}
return;
} // 駒台がクリックされた
if (pick.before != -1) { // 盤の外
pick.before = -1;
this.clean_and_draw();
}
}
var edit_mode = 0;
this.on_banmen_edit = function() { return edit_mode == 1; }
this.on_kifu_edit = function() { return edit_mode == 2; }
this.edit_end = function() { edit_mode = 0; }
this.banmen_edit_start = function() { edit_mode = 1 }
this.kifu_edit_start = function() { edit_mode = 2; }
_util.touched(this.get_canvas(), function(x, y) {
if (edit_mode == 0) return;
if (self.get_hanten()) {
x = _val.VIEW_W - x; y = _val.VIEW_H - y;
}
if (edit_mode == 1) self.banmen_edit(x, y);
else if (edit_mode == 2) self.kifu_edit(x, y);
});
this.draw_komadai = function(komadai_mode) {
if (komadai_mode == 0) return;
var sign;
var x, y;
if (komadai_mode == 1) {
sign = 1;
y = _val.VIEW_H - _val.KOMADAI_H;
} else {
sign = -1;
y = 0;
}
// 駒台領域に色を付ける
this.set_transform(1, 1, 0, y);
ctx.fillStyle = _color.KOMADAI;
ctx.fillRect(0, 0, _val.VIEW_W, _val.KOMADAI_H);
ctx.fillStyle = _color.KOMA;
y += _val.KOMADAI_H / 2;
dai_top = y - _val.KOMA_L / 2;
dai_bottom = y + _val.KOMA_L / 2;
// 駒の描画
var koma = (pick.before == 81) ? pick.koma % 8 : -1;
ctx.textAlign = "left";
ctx.textBaseline = "middle";
var n;
x = 0;
this.set_transform(sign, sign,
(sign > 0) ? dai_right : dai_left,
y);
for (var i = _koma.Fu; i <= _koma.Hi; i++) { // 歩から飛まで
n = dai[i];
if (n == 0) continue;
x -= 1.5 * _val.KOMA_L;
if (i == koma) {
ctx.fillStyle = _color.PICK;
ctx.fillRect((n > 1) ? x : x + _val.KOMA_L / 4, -_val.KOMA_L / 2,
_val.KOMA_L, _val.KOMA_L);
ctx.fillStyle = _color.KOMA;
}
ctx.fillText(_koma.MOJI[i], (n > 1) ? x : x + _val.KOMA_L / 4, 0);
}
// 漢数字の描画
x = 0;
this.set_transform(0.5 * sign, 0.5 * sign,
(sign > 0) ? dai_right + _val.KOMA_L : dai_left - _val.KOMA_L,
sign * _val.KOMA_L / 4 + y);
for (var i = _koma.Fu; i <= _koma.Hi; i++) { // 歩から飛まで
n = dai[i];
if (n == 0) continue;
x -= 3 * _val.KOMA_L;
if (n > 1)
ctx.fillText(this.get_kansuuji_string(n), x, 0);
}
}
this.get_komadai_koma = function(x, y) {
if (x < dai_left || x >= dai_right ||
y < dai_top || y >= dai_bottom) return -1;
x = Math.floor((x - dai_left) / (1.5 * _val.KOMA_L));
var komadai_mode = this.get_komadai_mode();
if (komadai_mode == 1) x = 6 - x;
for (var i = _koma.Fu; i <= _koma.Hi; i++) { // 歩から飛まで
if (dai[i] > 0) {
if (x == 0) return (komadai_mode == 1) ? i : i + 16;
x--;
}
}
return -1;
}
}