#!/usr/local/bin/node /* * ======================================== * Super Science High Schools 科学探求 * 数学課題研究で作ったもの * * Version 10-2017 * * Licence * * Copyright 2017 2017年度2-1 SSH数学班 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * ライセンスは変更することがあります。このバージョンでは上記のライセンスが有効です。 * * なるべくわかり易く書くつもり。 * ======================================== */ //環境変数(スネークケース...) const X_SIZE = 31; const Y_SIZE = 31; const NUM_OBSTACLES = 23; const STEP_OBSTACLE = 5;//障害物乗り越えにかかるステップ //estateクラス class Estate{ /* *statusの数値の意味 *0:道路 *1:住宅 *2:障害物 *3:行方不明者の位置 */ constructor(x,y,numOfObstacles){ this.size = new Object(); this.size.x = ++x;//0~xまでのマスを作る(わかりやすくするため) this.size.y = ++y;//0~yまでのマスを作る(わかりやすくするため) this.status = new Array(x*y);//二次元配列(配列の中に配列入れることで実現)は生成がしんどいのとめんどい(ついでに少し遅い) for(var i=0;i=0)?{x: addr % this.estateSize.y, y: Math.floor(addr / this.estateSize.y)}:{x:-1,y:-1}; } _fillBlock(offset_x,offset_y,size_x,size_y,num){ if((offset_x + size_x > this.size.x)||(offset_y + size_y > this.estateSize.y))return; for(var y = offset_y;y < offset_y + size_y ;y++){ for(var x = offset_x;x < offset_x + size_x;x++){ this.estate[this.posToAddr({x:x,y:y})] = num; } } } _makeHouses(){ //住宅生成(2x2で今は固定) for(var y = 1;y0)for(var i=0;i=10)this._put(" "); this._put(estate.estate[estate.posToAddr({x:cnt_x,y:cnt_y})]+" "); } } } log(estate){ var pos = estate.lostAddr; this._put("Lost:("+pos.x+","+pos.y+");"); this._put("Obstacles:"); var oblist = estate.obstacles; for(var i = 0,len = oblist.length;i < len;i++)this._put((i?",":"")+"("+oblist[i].x+","+oblist[i].y+")"); this._put(";"); this._flush(); } } var estate = new Estate(X_SIZE,Y_SIZE,NUM_OBSTACLES); var printer = new EstatePrinter(); var walker = new Walker(); printer.printAscii(estate); printer.log(estate); //Test var rule = function(estate,x,y,desk){ var pos; //初期化用 if(desk.cnt_step === undefined)desk.cnt_step = estate.estateSize.x; if(desk.direction === undefined)desk.direction = 0; //方向転換 if(!--desk.cnt_step)desk.cnt_step = (++desk.direction > 2)?((estate.estateSize.x -1) - 3*Math.floor((desk.direction -1) / 2 )):(estate.estateSize.x - 1); switch (desk.direction % 4) { case 0: pos = {x:1,y:0}; break; case 1: pos = {x:0,y:1}; break; case 2: pos = {x:-1,y:0}; break; case 3: pos = {x:0,y:-1}; break; default: break; } /* if((x == (estate.estateSize.x-1) - y)&&y x + 3)&& ((estate.estateSize.y - 1) - y) >=x)pos = {x:0,y:-1}; else if( x < (estate.estateSize.x -1) - y)pos = {x:1,y:0}; else if((x == y)&&(x>(estate.estateSize.x -1)/2)/*&&(y>(estate.estateSize.y -1)/2)/)pos = {x:-1,y:0}; else if((((estate.estateSize.y - 1) - y) x)pos = {x:-1,y:0}; else if(y == x + 3)pos = {x:1,y:0}; else{ console.log("Dead!!!!"); pos = {x:0,y:0}; }*/ return pos; }; var step = walker.search(estate,rule); console.log(step==-1?"Not found.":"Found when step is "+step+".");