Lab7-Maze


5530126221  นายฐิติภัทร์ อัศวรัตนาภรณ์ (COM22023) (2/20/2013 (10:15:32 AM))

MiniQuiz + TestScript
JLab>javac Maze.java
JLab>
JLab>java Selftest 

JLab> testMaze : ok ok ok ok ok ok ok ok ok ok  (10.0/10.0)
JLab> : -----------------------------
JLab> : ?????? 10.0 ????? (???? 10.0)
JLab> : -----------------------------
JLab> :<POINT>10.0</POINT> (<TOTAL>10.0</TOTAL> )
JLab>

ได้ 10 คะแนน
Source Code
import jlab.graphics.DWindow;
import java.util.*;

public class Maze {
  static boolean testMode = false;
  public static void main(String[] args) {    
    int width = 40, height = 30;
    DWindow w = new DWindow(width * 8, height * 8);
    w.setRepaintDuringSleep(true);
    int[][] maze = createMaze(width, height);
    findPath(w, maze);
    System.out.println("Done.");
  }
  //-----------------------------------------------------------------
  // เติมทางเดินเลียบผนังในห้องจากตำแหน่ง (xs, ys) ถึง (xt, yt)
  // ในกรณีที่ไม่มีทางเดินไปสู่จุดหมาย ให้จบเมื่อออกนอกห้อง
  public static int[][] findPath(DWindow w, int[][] m) {
    int x = 2, y = 2;
    m[x][y] = 2;
    show(w, m);
    while (!(x == 2 && y == 3)) {
     // เขียนคำสั่งเพื่อเพิ่มหรือลดค่าของ x หรือ y ไปหนึ่ง แทนทางเดินช่องถัดไป ในแต่ละรอบ
     if (m[x][y - 1] == 1 && m[x + 1][y] != 1) {
       x++;
     }
     else if (m[x - 1][y] == 1 && m[x][y - 1] != 1) {
       y--;
     }
     else if (m[x][y + 1] == 1 && m[x - 1][y] != 1) {
       x--;
     }
     else if (m[x + 1][y] == 1 && m[x][y + 1] != 1) {
       y++;
     }
     else if (m[x - 1][y] == 1 && m[x][y + 1] != 1) {
       y++;
     }
     else if (m[x][y + 1] == 1 && m[x - 1][y] != 1) {
       x--;
       }
       else if (m[x + 1][y + 1] == 1 && m[x][y + 1] != 1) {
         y++;
       }
      else if (m[x + 1][y - 1] == 1 && m[x + 1][y] != 1) {
        x++;
        }
        else if (m[x - 1][y - 1] == 1 && m[x][y - 1] != 1) {
          y--;
        }
       else if (m[x - 1][y + 1] == 1 && m[x - 1][y] != 1) {
         x--;
        }      
        
   







      m[x][y] = 2;  // เติมทางเดิน แล้วแสดงภาพห้องใหม่
      show(w, m);
    }
    return m;
  }
  //-----------------------------------------------------------------
  // แสดงห้องในวินโดว์ w  ผนังสีดำ (1) , ทางเดินสีแดง (2), ที่ว่างที่ยังไม่เดินผ่านสีขาว (0) 
  private static void show(DWindow w, int[][] m) {
    if (testMode) return;
    w.clearBackground();
    int width = w.getWidth() / m.length;
    for (int x = 0; x < m.length; x++) {
      for (int y = 0; y < m[x].length; y++) {
        if (m[x][y] == 1) w.fillRect(DWindow.BLACK, x * width, y * width, width, width);
        if (m[x][y] == 2) w.fillRect(DWindow.mixRGB(150,150,150), x * width, y * width, width, width);
      }
    }
    w.sleep(1);
  }
  //-----------------------------------------------------------------
  // สร้างห้องขนาด width x height ที่มีผนังกั้นสี่ด้าน มีผนังสุ่ม ๆ ภายในห้อง
  // ทุกผนังมีความกว้างอย่างน้อย 2 และทุกช่องว่าง (ที่ให้เดินได้) มีความกว้างอย่างน้อย 2 เช่นกัน
  // เติมช่องว่างที่ตำแหน่ง (xs, ys) และ (xt, yt)
  public static int[][] createMaze(int width, int height) {
    // ขอสร้าง maze ขนาดเป็นครึีงหนึ่งของที่ต้องการ แล้วค่อยขยายทีหลัง
    // เพื่อให้ทางเดินมีขนาดสองช่องเสมอ จะเดินเดินไปและกลับได้
    int w2 = width / 2, h2 = height / 2;
    int[][] m = new int[w2][h2];
    for (int y = 0; y < h2; y++) m[0][y] = m[w2 - 1][y] = 1; // เติมผนังด้านซ้ายและขวา
    for (int x = 0; x < w2; x++) m[x][0] = m[x][h2 - 1] = 1; // เติมผนังด้านบนและล่าง
    int t = Math.max(width, height) / 3; // จำนวนการเติมผนังในห้อง
    for (int k = 0; k < t; k++) {
      int n = (int) (Math.max(w2, h2)/3 * Math.random());    // ความยาวผนัง
      int x = (int)(w2 * Math.random());            // ตำแหน่งเริ่มต้น
      int y = (int)((h2 - n) * Math.random());
      for (int i = 0; i < n; i++) m[x][y + i] = 1;  // เติมผนังแนวตั้ง
      // เติมผนังแนวนอนในห้อง
      x = (int)((w2 - n) * Math.random());          // ตำแหน่งเริ่มต้น
      y = (int)(h2 * Math.random());
      for (int i = 0; i < n; i++) m[x + i][y] = 1;  // เติมผนังแนวนอน
    }
    m[1][1] = 0; // เติมช่องว่างที่มุมซ้ายบน (จุดเริ่ม)
    //-----------------------------------------
    // สร้างห้องเท่าของจริง ย้ายข้อมูลจากห้องเล็กมาเติมในห้องใหญ่
    int[][] maze = new int[width][height];
    for (int x = 0; x < width; x++) {
      for (int y = 0; y < height; y++) {
        maze[x][y] = m[x / 2][y / 2];
      }
    }
    return maze;
  }
}
SM=1, CM=120, ST=65, KY=2331, TR=60:00
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;