• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

How to make objects move in an array?

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

I am attempting to create a maze for one of my exams at university. I am new to java so please excuse my code but I am struggling to find out a way of making the baby move around the maze and have spent the last two days looking for a solution, nothing has helped. My exam is due in this upcoming Sunday and I am still trying to solve this issue. Ideally, I am aiming to make the baby move one space at a time through my 2D array without being able to go through the brick wall. If anyone knows how to move the image through the array I would really appreciate the help. I was recommended by my tutor to use JButtons for the icons I'm not sure however on how to make progress from there. I have attached the java files of my maze so you can test it yourself (I don't know if it matters but I'm using Eclipse IDE to do my code), the focus is on the Maze Panel and Movement Panel as they are the two panels I am stuck on with functionality.

Any help would be appreciated! If you would prefer to leave your discord name so you can see it properly I will message you.

Many thanks,

Ben
code-1.PNG
[Thumbnail for code-1.PNG]
code-2.PNG
[Thumbnail for code-2.PNG]
code-3.PNG
[Thumbnail for code-3.PNG]
 
Master Rancher
Posts: 4188
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some comments re the code:
Those LONG lists of repetitive code needs to be in a loop.
The loops need to use {}s that enclose the code that they control.

Note you need to post the source code as text not as images.

way of making the baby move around the maze


Is the baby supposed to move itself
or is its motion controlled by a user?
 
Bartender
Posts: 7789
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...and when you post code as text please UseCodeTags (<--link).
 
Carey Brown
Bartender
Posts: 7789
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whether the user moves the baby or the baby moves itself you'll need logic to tell if the square it wants to move to is even on the board, and if so, does it have a brick on it.

If the baby moves itself there are two approaches (probably more) to solving a maze. One is the right-hand approach which is like if you were in the maze yourself and facing a direction and sticking out your right hand to touch a nearby wall and keep rotating to the right as long as there's a wall still touching your hand, if no wall, then walk forward one. Continue till you reach the end of the maze. A second approach is to use recursion to keep track of where you are and which cells you've visited and to visit the next unvisited cell and descend the recursion to the next level.
 
Carey Brown
Bartender
Posts: 7789
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Grabouski wrote:Any help would be appreciated! If you would prefer to leave your discord name so you can see it properly I will message you.

We'd prefer that you didn't use messages and that you continue posting back here to this thread so that everyone might learn.
 
Ben Grabouski
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the maze is supposed to be controlled by the user. also sorry for not putting the tags and the other stuff I'm new to the website and didn't realise.
 
Ben Grabouski
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the loop that you mentioned, would you be able to share an example? I'm very new to Java and find it quite confusing.
 
Carey Brown
Bartender
Posts: 7789
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Grabouski wrote:the maze is supposed to be controlled by the user.

So, are you going to have buttons or keystrokes for up/down/left/right?
 
Ben Grabouski
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Preferably buttons but either works.
 
Carey Brown
Bartender
Posts: 7789
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Whether the user moves the baby or the baby moves itself you'll need logic to tell if the square it wants to move to is even on the board, and if so, does it have a brick on it.

So what have you got for this so far?
 
Carey Brown
Bartender
Posts: 7789
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Grabouski wrote:For the loop that you mentioned, would you be able to share an example? I'm very new to Java and find it quite confusing.

Because all your coordinates are hand picked I don't think there is a simple way to make a loop to initialize the maze. On the other hand, what you've got is an accident waiting to happen. It's very likely that you'd make a typo error in all those coordinates. Here is "a" way around that by using an array of Strings to "draw" the maze and then initialize your img_array from that. I don't know if you'll find this confusing or not. If so, feel free to ignore it.
 
Ben Grabouski
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was making this change following a video as it was the only video I found that could create the functionality I needed. The issue was the video included a lot of stuff that I cannot include in my assignment like a maze solver. This made it difficult to follow. I understand that using that array should allow me to use X=1 and so on to move around but I'm not sure how to. Either way I understand what you have shown me and it seems quite helpful as long as I can get it to work.
 
Marshal
Posts: 72059
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please give us details about the video. We don't know whether it is any good.
 
Ben Grabouski
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so I fixed it using the method provided by Carey Brown. Once I had used her his method I was able to easily create the movement controls using x and y coordinates. thanks for the help!
 
Carey Brown
Bartender
Posts: 7789
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Ranch Hand
Posts: 51
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a side note: There's nothing like multi-dimensional arrays, only arrays of arrays. Also note that inner arrays don't have to share the same length. While one might fit the expected length another maybe doesn't. Always use <array>.length to get the actual length.
 
I can't take it! You are too smart for me! Here is the tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic