• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

creating a maze in java

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys,

I'm trying to create a maze using java reading from a file,

The file is a text file with 1's and 0's. Each group of 4 numbers make a Square(a 1 means there is a wall), for example if I read from the file the sequence 1011, it means that I will have a wall in the north of the square, and in the south and west too.

I'm wondering if there is a way of creating a Square with edges (the walls), because I can't think of any.

Thanks!
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What context are you working in? Console or GUI?
 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are doing this by hand, not a problem, you just have to remember that where you have a 0, you have to have a corresponding 0 in any adjacent square, so to continue you have to have a opening in the south of the square on top or you will dead end there, and a opening in north of the square on the bottom or you will dead end there.

With this in mind, then a turn would be an opening in two directions say in the example you gave, an opening to the north and then an opening to the east representing a continuation of the corridor and the turn the right.

Johnny Januan wrote:Hello guys,

I'm trying to create a maze using java reading from a file,

The file is a text file with 1's and 0's. Each group of 4 numbers make a Square(a 1 means there is a wall), for example if I read from the file the sequence 1011, it means that I will have a wall in the north of the square, and in the south and west too.

I'm wondering if there is a way of creating a Square with edges (the walls), because I can't think of any.

Thanks!

 
Johnny Januan
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm working in GUI

But how do I "paint" the walls?? Is there any method?

I've tried to create the walls as if they were rectangles (Rectangle2D.Float), but it doesn't work.

Any ideas on how to do it?
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I can't visualize what you want to display. Can you make an image and post it?

You might look at custom painting to draw the shapes you want. That's done by using a component like a JPanel and overriding its paintComponent method which would contain your drawing code.
 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Johnny,
It's like Norm said to get your graphic to display. You need to render to a Buffered Image and use Graphics Class' drawImage to render to your Graphic context of your display object. There is a nice discussion here that took place a couple weeks ago in the forums, pretty much showing how to do animation in Java. Take a look, the examples are commented on almost every line.
Les
 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a code snippet to supplement the example in the other thread. It should be the paintComponent method of your class used in displaying your animation--usually JPanel.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Johnny,

in addition to all the advices given above, I would like to point you to the Path2D class, and its two concrete implmenetations -.Double and -.Float.
These are ideally suited for the Squares requirements. It lets you define the outline of any shape you want, by having methods like moveTo, lineTo and even curveTo.

Your sixteen combinations of zeroes and ones enable you to set up 16 of these Shapes, and that makes creating Les' Image very easy.
 
Johnny Januan
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I attach an example of what I want to do.

Thanks
-271BAC226A4AE1BAF243C55B9B00D263C2C01EC93F0E8C835E-pimgpsh_thumbnail_win_distr.jpg
[Thumbnail for -271BAC226A4AE1BAF243C55B9B00D263C2C01EC93F0E8C835E-pimgpsh_thumbnail_win_distr.jpg]
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you trying to write code that will create an image like in your last post from the groups of 4 digits read from a file?
Other questions about it:
how large are the squares (in pixels)
how many squares go on each row?
 
Johnny Januan
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help, but I've already solved the problem.

Now I'm going to try to create a piece and move inside the maze.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How did you solve it? How would you feel about posting the code here?
 
Johnny Januan
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can try to explain it.
However, I'm spanish and the name of the variables are in spanish.



How do I put the piece in the maze, and move it with the keyboard? Do I need to create a separate class called Piece or something like that? And how does the piece know where are the walls, in order to avoid them?

Thanks
 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Johnny,
Your override of the paintComponent is NOT right. Get all the IO and other things out of there, it should be as tight as possible, probably 3 or 4 lines at most. Do your rendering to a BufferedImage and then paint it an your Graphics context--too much done in the paintComponent method is a great way to get your game to be slow and the graphics to flicker.

To get your game piece on the screen, you do it like in the examples I gave, you either use a pre-existing class, like oval, or make your own.

To do movement, you need to have 2 arrays: your graphics, which you will have rendered to a BufferedImage and render to your Graphics Context in your display object paintComponent override, and an array that holds what type of room you are in. So following along with your original 4 bit design and bit masks:

Walls:
NORTH: 1000 = 8
EAST: 0100 = 4
SOUTH: 0010 = 2
WEST: 0001 = 1

These are bit masks, so each represent a power of 2. So if you have a opening to the North and no where else, the value would be 0111 or 7, bit masks are significant because now if you want to move North, you do a bitwise "&" with the North big mask and the room value:


So use 2 arrays--one to display (your BuferedImage) and one to check movement your bit masked room configurations.

Actual movement is covered in the examples and discussion I already gave you a link to.

Johnny Januan wrote:I can try to explain it.
However, I'm spanish and the name of the variables are in spanish.



How do I put the piece in the maze, and move it with the keyboard? Do I need to create a separate class called Piece or something like that? And how does the piece know where are the walls, in order to avoid them?

Thanks

 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is another way to proceed:
Define an image to draw the maze on and get a Graphics object for it:
Rename your current paintComponent() method to something like: buildMazeImage() and call it with the Graphics from above:
Now mazeImage has the maze drawn on it.

Create a paintComponent method from what Les posted.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
*Since I am Turkish, Mods are in Turkish
*Here I drew the maze, no player mechanics
* I just showed we how to build walls, roads and players.





import javax.swing.JOptionPane;

/**
* If the color of the walls is red 2
* If roads are 0 color white
* If the player is blue number -1
*/
public class Labirent {
 int lab[][] = {
   {2,2,2,2,2,2,2,2,2,2},
   {0,0,0,0,2,0,0,0,0,2},
   {2,2,2,0,2,0,2,2,0,2},
   {2,0,0,0,2,0,0,2,0,2},
   {2,0,0,0,0,0,2,2,0,2},
   {2,0,2,2,2,0,0,2,2,2},
   {2,0,0,0,0,0,2,0,0,2},
   {2,2,0,2,2,2,2,0,0,2},
   {2,0,0,0,0,0,0,0,0,2},
   {2,2,2,2,2,2,2,2,0,2}
 };
 int oyuSatir = 1, oyuSütun = 0;
 
 public String ekranAl() {    
   String ekran = "<html><table>";
   for(int satir=0; satir < lab.length; satir++) {
     ekran += "<tr>";
     for(int sütun=0; sütun < lab[satir].length; sütun++) {
       if(lab[satir][sütun] == 2) {
         ekran += "<td bgcolor='red' width=25></td>";
       }
       if(lab[satir][sütun] == 0) {
         ekran += "<td bgcolor='white' width=25></td>";
       }
       if(lab[satir][sütun] == -1) {
         ekran += "<td bgcolor='blue' width=25></td>";
       }
     }
     ekran += "</tr>";
   }
   ekran += "</table></html>";
   System.out.println(ekran);
   return ekran;
 }
 public void oyunBasla() {
   lab[oyuSatir][oyuSütun] = -1;
   JOptionPane.showMessageDialog(null, ekranAl());

 }
 public static void main(String[] args) {
   Labirent L = new Labirent();
   L.oyunBasla();
 }
}
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
What's wrong? Where are you going? Stop! Read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic