Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Nothing Happens in my code:P Maze Code Implementation  (Read 489 times)
0 Members and 1 Guest are viewing this topic.
Offline lordberzerker97

Senior Newbie





« Posted 2012-01-17 05:08:59 »

There seems to be a problem with my code. For some reason my window will not popup for some reason. I will only type in the code that seems to be wrong. No error pops up its just that nothing will happen. I just implemented a random maze generation code in the maps and mapdirector files but nothing happens after i implemented the new code. Please help or at least tell me whats wrong.

Maps.java
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
import java.util.*;

public class Maps{
   private int SizeX = 33;
   private int SizeY = 33;
   public short RandomMaze[][] = new short[SizeX][SizeY];
   private Random r = new Random();
   public Maps(){
      GenerateMaze(r.nextInt(70) + 30);
   }
   public void GenerateMaze(int StraightNess){
      int CurrentX = 50;
      int CurrentY = 50;
      boolean TimeUp = false;
      boolean tocheckDirection = false;
      boolean Moved = false;
      boolean ChangeDirection = false;
      boolean Done = false;
      int Dir = 0;
      int LastDirection = r.nextInt(4);
      int Open = 2;
      int NotOpen = 3;
      int North = Open;
      int South = Open;
      int East = Open;
      int West = Open;
      short Flr = 0;
      short Wall = 1;
      long LastDrawTimer = 0;
      for(int y = 0;y < RandomMaze.length;y++){
         for(int x = 0;x < RandomMaze[y].length;x++){
            RandomMaze[y][x] = 1;
         }
      }
      CurrentX = 50;
      CurrentY = 50;
      while(TimeUp != true){
         if(tocheckDirection != true){
            Moved = false;
            ChangeDirection = true;
           
            if(r.nextInt(100) < StraightNess){
               ChangeDirection = false;
               Dir = LastDirection;
            }
           
            while(Moved != true){
               if(ChangeDirection == true){
                  Dir = r.nextInt(4);
                  LastDirection = Dir;
               }
            }
           
            ChangeDirection = true;
           
            switch(Dir){
               case 0:
                  if(North == Open){
                     Moved = true;
                     CurrentY--;
                  }
                  break;
               case 1:
                  if(South == Open){
                     Moved = true;
                     CurrentY++;
                  }
                  break;
               case 2:
                  if(East == Open){
                     Moved = true;
                     CurrentX++;
                  }
                  break;
               case 3:
                  if(West == Open){
                     Moved = true;
                     CurrentX--;
                  }
                  break;
            }
         }
         RandomMaze[CurrentX][CurrentY] = Flr;
         LastDrawTimer = System.currentTimeMillis();
      }
      North = NotOpen;
      South = NotOpen;
      East = NotOpen;
      West = NotOpen;
     
      if(CurrentY - 2 < 0){
         North = NotOpen;
      }else if(RandomMaze[CurrentX][CurrentY - 1] == Wall && RandomMaze[CurrentX][CurrentY - 2] == Wall){
         if(RandomMaze[CurrentX - 1][CurrentY - 1] == Wall && RandomMaze[CurrentX + 1][CurrentY - 1] == Wall){
            North = Open;
         }else{
            North = NotOpen;
         }
      }
     
      if(CurrentY + 2 < 0){
         South = NotOpen;
      }else if(RandomMaze[CurrentX][CurrentY + 1] == Wall && RandomMaze[CurrentX][CurrentY + 2] == Wall){
         if(RandomMaze[CurrentX - 1][CurrentY + 1] == Wall && RandomMaze[CurrentX + 1][CurrentY + 1] == Wall){
            South = Open;
         }else{
            South = NotOpen;
         }
      }
     
      if(CurrentX + 2 < 0){
         East = NotOpen;
      }else if(RandomMaze[CurrentX + 1][CurrentY] == Wall && RandomMaze[CurrentX + 2][CurrentY] == Wall){
         if(RandomMaze[CurrentX + 1][CurrentY - 1] == Wall && RandomMaze[CurrentX + 1][CurrentY + 1] == Wall){
            East = Open;
         }else{
            East = NotOpen;
         }
      }
     
      if(CurrentX - 2 < 0){
         West = NotOpen;
      }else if(RandomMaze[CurrentX - 1][CurrentY] == Wall && RandomMaze[CurrentX - 2][CurrentY] == Wall){
         if(RandomMaze[CurrentX - 1][CurrentY - 1] == Wall && RandomMaze[CurrentX - 1][CurrentY + 1] == Wall){
            West = Open;
         }else{
            West = NotOpen;
         }
      }
     
      if(System.currentTimeMillis() - LastDrawTimer > 100){TimeUp = true;}
     
      if(North == NotOpen && South == NotOpen && East == NotOpen && West == NotOpen && TimeUp == false){
         Done = false;
         while(Done != true){
            CurrentX = 50;
            CurrentY = 50;
            if(RandomMaze[CurrentX][CurrentY] == Flr){Done = true;}
         }
      }
   }
}


MapDirector.java
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
import java.util.*;
import java.awt.*;

public class MapDirector{
   Maps M = new Maps();
   Prey prey;
   ArrayList<Floor> floor = new ArrayList<Floor>();
   ArrayList<Wall> wall = new ArrayList<Wall>();
   ArrayList<Wall> wallT = new ArrayList<Wall>();
   ArrayList<Exit> exit = new ArrayList<Exit>();
   private short Maze1[][] = M.RandomMaze;
   private Random r = new Random();
   public static boolean WIN = false;
   public MapDirector(){
      M.GenerateMaze(r.nextInt(70) + 30);
      prey = new Prey(50,50,500,270);
      for(int y = 0;y < Maze1.length;y++){
         for(int x = 0;x < Maze1[y].length;x++){
            if(Maze1[y][x] == 0){floor.add(new Floor(x*50,y*46));}
            if(Maze1[y][x] == 1){wall.add(new Wall(x*50,y*46,1));wall.add(new Wall(x*50,y*46-4,1));
            wall.add(new Wall(x*50,y*46-8,1));wallT.add(new Wall(x*50,y*46-12,1));}
            if(Maze1[y][x] == 2){floor.add(new Floor(x*50,y*46));}
         }
      }
   }
   public void WallCollision(Wall w){
      Rectangle wall = new Rectangle(w.x,w.y,50,50);
      Rectangle U = new Rectangle(Prey.x + 20,Prey.y + 10,13,5);
      Rectangle D = new Rectangle(Prey.x + 20,Prey.y + 20,13,5);
      Rectangle L = new Rectangle(Prey.x + 17,Prey.y + 20,5,5);
      Rectangle R = new Rectangle(Prey.x + 30,Prey.y + 20,5,5);
      if(U.intersects(wall)){Prey.y += 2;Prey.gy -= 2;}
      else if(D.intersects(wall)){Prey.y -= 2;Prey.gy += 2;}
      else if(L.intersects(wall)){Prey.x += 2;Prey.gx -= 2;}
      else if(R.intersects(wall)){Prey.x -= 2;Prey.gx += 2;}
   }
   public void Exit(Exit e){
      Rectangle exit = new Rectangle(e.x,e.y,50,50);
      Rectangle PREY = new Rectangle(Prey.x,Prey.y,50,50);
      if(PREY.intersects(exit)){WIN = true;}
   }
   public void paint(Graphics g,Graphics2D g2d){
      g2d = (Graphics2D) g;
      g2d.translate(Prey.gx,Prey.gy);
      for(Floor f : floor){g2d.drawImage(Floor.floor,f.x,f.y,50,50,null);}
      for(Wall w : wall){g2d.drawImage(Wall.wall,w.x,w.y,50,50,null);}
      prey.paint(g,g2d);
      for(Wall w : wallT){g2d.drawImage(Wall.wall,w.x,w.y,50,50,null);}
   }
}
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-01-17 05:19:57 »

Why do you think your map generation code is wrong? If you are getting no errors and the window is not popping up, then there is an error with your window initialization Smiley

Offline lordberzerker97

Senior Newbie





« Reply #2 - Posted 2012-01-17 05:27:37 »

Why do you think your map generation code is wrong? If you are getting no errors and the window is not popping up, then there is an error with your window initialization Smiley

Well before I added the Map generation code the program worked well but when i added it it wouldn't work for some reason. I tested it out both ways with and withought the map generation. The one withought map generation worked and not the one with it.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-01-17 05:38:01 »

Does the program just end? If it doesn't then you have an infinite loop somewhere in there. Try debugging it.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Grunnt (17 views)
2014-09-23 14:38:19

radar3301 (14 views)
2014-09-21 23:33:17

BurntPizza (31 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (29 views)
2014-09-20 20:14:06

BurntPizza (33 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (75 views)
2014-09-10 13:57:51

TehJavaDev (105 views)
2014-09-10 06:39:09
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!