Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
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 328 times)
0 Members and 1 Guest are viewing this topic.
Offline lordberzerker97

Senior Newbie





« Posted 2012-01-17 06: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);}
   }
}
Online ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #1 - Posted 2012-01-17 06: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 06: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!
Play the free demo of Revenge of the Titans!
Online ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #3 - Posted 2012-01-17 06: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.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (140 views)
2013-05-17 21:29:12

alaslipknot (149 views)
2013-05-16 21:24:48

gouessej (179 views)
2013-05-16 00:53:38

gouessej (173 views)
2013-05-16 00:17:58

theagentd (183 views)
2013-05-15 15:01:13

theagentd (167 views)
2013-05-15 15:00:54

StreetDoggy (212 views)
2013-05-14 15:56:26

kutucuk (236 views)
2013-05-12 17:10:36

kutucuk (235 views)
2013-05-12 15:36:09

UnluckyDevil (240 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.384 seconds with 20 queries.