Skip to content

Commit dec4f17

Browse files
committed
コード整理
1 parent 65ca501 commit dec4f17

File tree

1 file changed

+48
-57
lines changed

1 file changed

+48
-57
lines changed

Script/puzzle.js

Lines changed: 48 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -10,59 +10,50 @@ export default () => {
1010
const [ALT_ORB,ALT_OBJECT,ALT_FIELD] = ["□🔴🔵🟢🟡🟣","□🧱🌸","□🥬"];
1111
// SECTOR_2:変数群
1212
let [chain_now,chainable]=[false,false];
13-
let chain_info={color : null,count : 0};
13+
let chain_color=null;
1414
let chain_yx =new Array();//[i].(x | y)
1515
let adj_list = new Array();//[i].(y | x)
1616
//SECTOR_2.5:準const変数群
1717
let PUZ_BOARD_BONE=new Array();
1818
let DATA={};
1919
// SECTOR_3:関数マニュアル
20-
// fallable(obj_type) : 該当オブジェクトが落下物か確認
21-
// is_adj_break(obj_type) : 該当オブジェクトがadj_breakを持つか確認
22-
// update_cell(y,x) : 1マスだけ画面を更新
23-
// update_display() : 関数名通り
24-
// obj_erase(y,x,draw=false) : 指定したマスを消す。drawなら描画する。
25-
// load_board() : [TODO] ファイルからパズルの読み込みをする
26-
// falling_orb() : 関数名通り
2720
// onmouce_cell(cell) : チェイン中の処理とかやってます
28-
// chain_toggler(cell) : 関数名通り
29-
// board_init() : 関数名通り
30-
// startgame() : 関数名通り
31-
const fallable = obj_type => (obj_type>0)||[-2].includes(obj_type);
32-
const is_adj_break = obj_type => [-2].includes(obj_type);
33-
const dest_sync = field_type => [1].includes(field_type);
21+
const endscene = () => {
22+
DIV_PUZ_DISPLAY.style.display="none";
23+
document.querySelector("#move_START").onclick();
24+
}
25+
const getType = obj => obj[0];
26+
const isnullobj = obj => getType(obj) === 0;
27+
const fallable = obj => (getType(obj)>0)||[-2].includes(getType(obj));
28+
const is_adj_break = obj => [-2].includes(getType(obj));
29+
const dest_sync = field => [1].includes(getType(field));
3430
const alt_text = (type,isobj) => isobj?(type<0?ALT_OBJECT[-type]:ALT_ORB[type]):ALT_FIELD[type];
3531
const update_cell = (y,x) =>{
3632
const CELL=PUZ_BOARD_BONE[y][x];
3733
[CELL.querySelector("img.object").src,CELL.querySelector("img.field").src,
3834
CELL.querySelector("img.object").alt,CELL.querySelector("img.field").alt] =
39-
[`Pictures/Orbs/${DATA.board.obj[y][x][0]}.svg`,`Pictures/Fields/${DATA.board.field[y][x][0]}.svg`,
40-
alt_text(DATA.board.obj[y][x][0],true),alt_text(DATA.board.field[y][x][0],false)];
35+
[`Pictures/Orbs/${getType(DATA.board.obj[y][x])}.svg`,`Pictures/Fields/${getType(DATA.board.field[y][x])}.svg`,
36+
alt_text(getType(DATA.board.obj[y][x]),true),alt_text(getType(DATA.board.field[y][x]),false)];
4137
}
4238
const object_copy = x => JSON.parse(JSON.stringify(x));
43-
4439
const update_display = () => {
4540
for(let i=0;i<DATA.size.Height;i++)for(let j=0;j<DATA.size.Width;j++)update_cell(i,j);
4641
document.querySelector("#puz_info").innerText = `Score : ${DATA.target.score} Hand : ${DATA.target.hand}`;
4742
}
48-
const obj_erase = (y,x,isobj=true) => {
49-
(isobj?DATA.board.obj:DATA.board.field)[y][x] = [0,0];
50-
update_cell(y,x);
51-
}
43+
const obj_erase = obj => [obj[0],obj[1]] = [0,0];
5244
const break_obj = (y,x,ischain,isobj=true) => {
5345
const TARGET = isobj?DATA.board.obj[y][x]:DATA.board.field[y][x];
5446
if(--TARGET[1]<=0||ischain){
55-
if(!isobj&&TARGET[0] === 1)DATA.target.score+=BASE_SCORE;
56-
obj_erase(y,x,isobj);
57-
isobj && dest_sync(DATA.board.field[y][x][0]) && break_obj(y,x,false,false);
47+
if(!isobj&&getType(TARGET) === 1)DATA.target.score+=BASE_SCORE;
48+
obj_erase(TARGET);
49+
update_cell(y,x);
50+
isobj && dest_sync(DATA.board.field[y][x]) && break_obj(y,x,false,false);
5851
}
5952
}
60-
const fall_obj = (yfrom,xfrom,yto,xto) => {
61-
const [OBJ_TO,OBJ_FROM] = [DATA.board.obj[yto][xto],DATA.board.obj[yfrom][xfrom]];
62-
if(OBJ_TO[0] === 0 && fallable(OBJ_FROM[0]) ){
63-
[OBJ_TO[0],OBJ_TO[1]]=OBJ_FROM;
64-
update_cell(yto,xto);
65-
obj_erase(yfrom,xfrom);
53+
const fall_obj = (obj_from,obj_to) => {
54+
if(isnullobj(obj_to) && fallable(obj_from)){
55+
[obj_to[0],obj_to[1]]=obj_from;
56+
obj_erase(obj_from);
6657
return true;
6758
}
6859
else return false;
@@ -72,11 +63,11 @@ export default () => {
7263
const FALL_TIMER = setInterval(() => {
7364
let refall=false;
7465
for(let i=DATA.size.Height-1;i>0;i--)/*性質上、下から探索したほうがいい*/{
75-
for(let j=0;j<DATA.size.Width;j++)refall=fall_obj(i-1,j,i,j)||refall;//C-shift
76-
for(let j=1;j<DATA.size.Width;j++)refall=fall_obj(i-1,j,i,j-1)||refall;//L-shift
77-
for(let j=0;j<DATA.size.Width-1;j++)refall=fall_obj(i-1,j,i,j+1)||refall;//R-shift
66+
for(let j=0;j<DATA.size.Width;j++)refall=fall_obj(DATA.board.obj[i-1][j],DATA.board.obj[i][j])||refall;//C-shift
67+
for(let j=1;j<DATA.size.Width;j++)refall=fall_obj(DATA.board.obj[i-1][j],DATA.board.obj[i][j-1])||refall;//L-shift
68+
for(let j=0;j<DATA.size.Width-1;j++)refall=fall_obj(DATA.board.obj[i-1][j],DATA.board.obj[i][j+1])||refall;//R-shift
7869
}
79-
DATA.board.obj[0] = DATA.board.obj[0].map(x => (x[0]===0)?(refall = true , [~~(Math.random()*ORB_COLORS)+1,1]):x);
70+
DATA.board.obj[0] = DATA.board.obj[0].map(x => isnullobj(x)?(refall = true , [~~(Math.random()*ORB_COLORS)+1,1]):x);
8071
if(!refall){
8172
clearInterval(FALL_TIMER);
8273
chainable=true;
@@ -85,24 +76,22 @@ export default () => {
8576
},ANIM_SPEED);
8677
}
8778
const onmouce_cell = cell => {
79+
if(!chain_now) return;
8880
const [CELL_Y,CELL_X] = [cell.target.parentNode.rowIndex,cell.target.cellIndex];
89-
const CELL_COLOR = DATA.board.obj[CELL_Y][CELL_X][0];
90-
if(chain_now &&
91-
Math.abs(chain_yx.at(-1).y-CELL_Y)<=1&&Math.abs(chain_yx.at(-1).x-CELL_X)<=1 /*位置チェック*/ &&
92-
chain_info.color === CELL_COLOR&&!chain_yx.some(e => e.x === CELL_X && e.y === CELL_Y))/*条件チェック*/{
93-
cell.target.querySelector("img").classList.add("chaining");
94-
chain_yx.push({x : CELL_X,y : CELL_Y});
95-
chain_info.count++;
96-
}
81+
const CELL_COLOR = getType(DATA.board.obj[CELL_Y][CELL_X]);
82+
if(!(Math.abs(chain_yx.at(-1).y-CELL_Y) <= 1 && Math.abs(chain_yx.at(-1).x-CELL_X)<=1)) return; /*位置チェック*/
83+
if(chain_color !== CELL_COLOR || chain_yx.some(e => e.x === CELL_X && e.y === CELL_Y)) return;/*条件チェック*/
84+
cell.target.querySelector("img").classList.add("chaining");
85+
chain_yx.push({x : CELL_X,y : CELL_Y});
9786
}
9887
const chain_toggler = cell => {
9988
if(!chainable)return;
10089
const [CELL_Y,CELL_X] = [cell.target.parentNode.rowIndex,cell.target.cellIndex];
101-
const CELL_COLOR = DATA.board.obj[CELL_Y][CELL_X][0];
90+
const CELL_COLOR = getType(DATA.board.obj[CELL_Y][CELL_X]);
10291
if(chain_now){//チェイン終了時の処理
10392
chain_now=false;
104-
if(!(chain_info.count<SHORTEST_CHAIN)){
105-
DATA.target.score+=~~(chain_info.count**SCORE_EXPONENT*BASE_SCORE);
93+
if(!(chain_yx.length<SHORTEST_CHAIN)){
94+
DATA.target.score += ~~(chain_yx.length**SCORE_EXPONENT*BASE_SCORE);
10695
DATA.target.hand--;
10796
chain_yx.forEach(pos => {
10897
break_obj(pos.y,pos.x,true);
@@ -114,32 +103,34 @@ export default () => {
114103
}
115104
}
116105
});
117-
adj_list.forEach(pos => is_adj_break(DATA.board.obj[pos.y][pos.x][0]) && break_obj(pos.y,pos.x,false));
106+
adj_list.forEach(pos => is_adj_break(DATA.board.obj[pos.y][pos.x]) && break_obj(pos.y,pos.x,false));
118107
update_display();
119108
falling_orb();
120109
}
121110
chain_yx.forEach(pos => PUZ_BOARD_BONE[pos.y][pos.x].querySelector("img").classList.remove("chaining"));
122-
chain_info={count : 0,color : null};
123-
adj_list=chain_yx=[];
124-
if(DATA.target.hand<=0){
111+
chain_color = null;
112+
adj_list = chain_yx = [];
113+
if(DATA.target.hand <= 0){
125114
alert(`ゲームオーバー! スコアは${DATA.target.score}でした!`);
126-
DIV_PUZ_DISPLAY.style.display="none";
127-
document.querySelector("#move_START").onclick();
115+
endscene();
128116
}
129-
}else if(CELL_COLOR>0){//チェイン開始の処理
130-
chain_now=true;
131-
chain_info={count : 1,color : CELL_COLOR};
117+
}else if(CELL_COLOR > 0){//チェイン開始の処理
118+
chain_now = true;
119+
chain_color = CELL_COLOR;
132120
chain_yx.push({x : CELL_X,y : CELL_Y});
133121
cell.target.querySelector("img").classList.add("chaining");
134122
}
135123
}
136124
const load_board = () => {
137-
PUZ_BOARD_BONE=new Array(DATA.size.Height).fill().map(_=>Array(DATA.size.Width));
125+
const [HEIGHT,WIDTH] = [DATA.size.Height,DATA.size.Width];
126+
PUZ_BOARD_BONE=new Array(HEIGHT).fill().map(_=>Array(WIDTH));
138127
MAIN_BOARD.innerHTML = null;
139-
for (let i = 0; i < DATA.size.Height; i++) {
128+
if(HEIGHT <= 0){console.error(`GUARD! ${HEIGHT} is not positive`); return endscene();}
129+
if(WIDTH <= 0){console.error(`GUARD! ${WIDTH} is not positive`); return endscene();}
130+
for (let i = 0; i < HEIGHT; i++) {
140131
const TR = document.createElement("tr");
141132
TR.classList.add("puz_board_tr");
142-
for (let j = 0; j < DATA.size.Width; j++) {
133+
for (let j = 0; j < WIDTH; j++) {
143134
const TD = document.createElement("td");
144135
TD.classList.add("inboard");
145136
TD.onmouseover = onmouce_cell;

0 commit comments

Comments
 (0)