New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: Java

maze

27 Views
Copy Code Show/Hide Line Numbers
   1:  package inlämning4;
   2:   
   3:  import se.lth.cs.pt.dots.*;
   4:  import se.lth.cs.pt.dots.events.*;
   5:  import se.lth.cs.pt.io.Keyboard;
   6:  import se.lth.cs.pt.maze.MazeGenerator;
   7:   
   8:   
   9:   
  10:   public class Main {
  11:   
  12:      static DotWindow w;
  13:      static Maze maze;
  14:      private static int xPos, yPos = 0;
  15:      private static int size;
  16:   
  17:     
  18:      public static void main(String[] args) {
  19:          setup();
  20:          while(true){
  21:          drawMaze();
  22:          eventLoop();
  23:          }
  24:      }
  25:   
  26:      private static void setup(){
  27:          size = Keyboard.nextInt("Ange labyrintens storlek: ");
  28:          w = new DotWindow(size, size, 600/size);
  29:          maze = new Maze(size);
  30:          maze.creatMaze(size);
  31:          maze.getStartPoint(xPos, yPos);
  32:          w.setDot(xPos, yPos, Color.RED);
  33:      }
  34:   
  35:   
  36:      private static void drawMaze(){
  37:          maze.display(w);
  38:      }
  39:   
  40:   
  41:      private static void eventLoop(){
  42:          while (true){ 
  43:              GameEvent e = w.getNextEvent();
  44:              keyPressed(e.getKey());
  45:          }
  46:      }
  47:   
  48:      private static void keyPressed(char ch) {
  49:          switch (ch){
  50:              case 'l':
  51:                  break;
  52:          }
  53:      }
  54:   
  55:  }
  56:   
  57:   
  58:   
  59:   
  60:   
  61:   class Maze {
  62:   
  63:     private int[][] mazeMatrix;
  64:     private int size;
  65:   
  66:     private final int BLACK = 1;
  67:     private final int WHITE = 0;
  68:   
  69:   
  70:     public Maze (int size){
  71:         this.size = size;
  72:         mazeMatrix = new int[size][size];
  73:     }
  74:   
  75:   
  76:     public void creatMaze(int n){
  77:           mazeMatrix = MazeGenerator.generate(n, n);
  78:      }
  79:   
  80:   
  81:     public void display(DotWindow w){
  82:         for(int x = 0; x < size; x++){
  83:             for(int y = 0; y < size; y++){
  84:                 w.setDot(x, y, getColor(mazeMatrix[x][y]));
  85:             }
  86:         }
  87:     }
  88:   
  89:   
  90:     private Color getColor(int dot){
  91:         if(dot == BLACK){
  92:             return Color.BLACK;
  93:         }
  94:         return Color.WHITE;
  95:     }
  96:   
  97:     public int getStartPoint(int xPos, int yPos){
  98:         for( int y = 0; y < size; y++){
  99:             if(y == WHITE){
 100:                 yPos = y;
 101:                 return yPos;
 102:             }
 103:         }
 104:         return xPos;
 105:     }
 106:   }
by
  February 08, 2010 @ 2:43pm

Add a comment


Report Abuse
brought to you by:
West Wind Techologies


If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate