Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!-- HTML -->
<div id="app">
  <h1>{{ title }}</h1>
  <p>START: <input v-model="start"></p>
  <p>GOAL: <input v-model="goal"></p>
  <p>
    <input type="radio" value="heuristic" v-model="search_type">ヒューリスティック</input>
<input type="radio" value="a-star" v-model="search_type">A*</input>
  </p>
  <p><button v-on:click="reset()">RESET</button></p>
<!--   <p>OPEN: {{ open_list }}</p> -->
<!--   <p>CLOSED: {{ closed_list }}</p> -->
  <p>SEARCH COST: {{ counter }} </p>
  <p>CLOSED STATES: {{ closed_list }}</p>
  <vue-p5
    @setup="setup"
    @draw="draw"
    @mousePressed="mousePressed">
  </vue-p5>
</div>
              
            
!

CSS

              
                
              
            
!

JS

              
                let state_list = {
  "S1": [1, 1],
  "S2": [5, 1],
  "S3": [2, 2],
  "S4": [3, 2],
  "S5": [4, 2],
  "S6": [5, 2],
  "S7": [1, 3],
  "S8": [3, 3],
  "S9": [4, 3],
  "S10": [5, 3],
  "S11": [2, 4],
  "S12": [3, 4],
  "S13": [4, 4],
  "S14": [1, 5],
  "S15": [3, 5],
  "S16": [4, 5],
  "S17": [5, 5]};

let cost_list = {
  "S1": 0,
  "S2": 0,
  "S3": 0,
  "S4": 0,
  "S5": 0,
  "S6": 0,
  "S7": 0,
  "S8": 0,
  "S9": 0,
  "S10": 0,
  "S11": 0,
  "S12": 0,
  "S13": 0,
  "S14": 0,
  "S15": 0,
  "S16": 0,
  "S17": 0};

let link_list = {
  "S1": ["S2", "S7"],
  "S2": ["S1", "S6"],
  "S3": ["S4"],
  "S4": ["S3", "S5", "S8"],
  "S5": ["S4"],
  "S6": ["S2"],
  "S7": ["S1", "S8", "S14"],
  "S8": ["S4", "S7", "S12"],
  "S9": ["S10", "S13"],
  "S10": ["S9", "S17"],
  "S11": ["S12"],
  "S12": ["S8", "S11", "S13"],
  "S13": ["S9", "S12"],
  "S14": ["S7", "S15"],
  "S15": ["S14"],
  "S16": ["S17"],
  "S17": ["S10", "S16"]};

let open_list = []
let closed_list = []

function getPosition(i, j){
  let x = 50 + 100 * (i - 1);
  let y = 50 + 100 * (j - 1);
  return [x, y]
}

function openState(state){
  if(open_list.includes(state)){
    let index = open_list.indexOf(state);
    open_list.splice(index, 1)
  }
  
  closed_list.push(state);
  
  let links = link_list[state];
  for(let link of links){
    if(!closed_list.includes(link)){
      open_list.push(link);
      
      let [x, y] = state_list[state];
      let [link_x, link_y] = state_list[link];
      let cost = Math.abs(x - link_x) + Math.abs(y - link_y);
      cost_list[link] = cost_list[state] + cost;
    }
   }
  
  console.log(`open: ${open_list}`);
  console.log(`close: ${closed_list}`);
 
}

// JavaScript
new Vue({
  el: '#app',
  data: {
    title: "迷路問題(ヒューリスティック)",
    start: "S1",
    goal: "S17",
    state_list: state_list,
    cost_list: cost_list,
    open_list: open_list,
    closed_list: closed_list,
    counter: 0,
    search_type: "heuristic",
  },
  methods: {
    setup(sketch){
      sketch.createCanvas(500, 500); // キャンバスの大きさ
      sketch.background("black"); // キャンバスの背景色
      openState(this.start);  
      this.counter += 1;
    },
    draw(sketch){
      sketch.background("black");
      
      for(let state in this.state_list){
        let [i, j] = this.state_list[state];
        let [x, y] = getPosition(i, j);
        let cost = this.cost_list[state];
        
        let [goal_i, goal_j] = this.state_list[this.goal];
        let heuristic = Math.abs(i - goal_i) + Math.abs(j - goal_j);
        
        if(this.open_list.includes(state)){
          sketch.fill("yellow");
          sketch.noStroke();
          sketch.circle(x, y, 80);  
          
          sketch.fill("black");
          sketch.stroke("black");
          if(this.search_type == "heuristic"){
            sketch.text(`${state}(${heuristic})`, x-20, y+5);
          }
          else{
            sketch.text(`${state}(${cost}+${heuristic})`, x-25, y+5);
          }
        }
        else if(this.closed_list.includes(state)){
          
          if(state == this.goal){
            sketch.fill("red");
          }
          else{
            sketch.fill("green");
          }
          sketch.noStroke();
          sketch.circle(x, y, 80);  
          
          sketch.fill("black");
          sketch.stroke("black");          
          if(this.search_type == "heuristic"){
            sketch.text(`${state}(${heuristic})`, x-20, y+5);
          }
          else{
            sketch.text(`${state}(${cost}+${heuristic})`, x-25, y+5);
          }
        }
        else{
          if(state == this.goal){
            sketch.fill("skyblue");
            sketch.noStroke();
            sketch.rect(x-40, y-40, 80, 80);
            sketch.stroke("black");
            sketch.fill("black");
            sketch.text(`${state}`, x-15, y+5);
          }
          else{
            sketch.fill("gray");
            sketch.noStroke();
            sketch.rect(x-40, y-40, 80, 80);       
          }
        }
      }
    },
    mousePressed(sketch){
      for(let state in this.state_list){
        let [i, j] = this.state_list[state];
        let [x, y] = getPosition(i, j);  
        
        if((x-40) < sketch.mouseX && sketch.mouseX < (x + 40)){
          if((y-40) < sketch.mouseY && sketch.mouseY < (y + 40)){
            if(this.open_list.includes(state)){
              if(!this.closed_list.includes(this.goal)){
                openState(state);
                this.counter += 1;
                
                // 目的状態を発見したら終了
                if(this.open_list.includes(this.goal)){
                  let index = this.open_list.indexOf(this.goal);
                  this.open_list.splice(index, 1) // オープンリストから削除
                  this.closed_list.push(this.goal); // クローズリストに追加
                }
                
                break;
              }
            }
          }
        }
        
      }
    },
    reset(){
      this.open_list.length = 0;
      this.closed_list.length = 0;
      this.counter = 1;
      
      for(let state in this.cost_list){
        this.cost_list[state] = 0;
      }
      
      openState(this.start);
    }
  }
});
              
            
!
999px

Console