Results 1 to 8 of 8
  1. #1
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default How do i place objects onto a grid?

    Hi all again, I've created a 2D grid for my snake game using the following code:
    Java Code:
    public static class Grid
    	{
    		Grid[][] grid = new Grid[31][31]; // 31x31 grid
    		static void fill(char grid[][], char _){}
    	}
    However, I need to create a new object called food and find a way of placing it in random places on the grid once the snake has ate it (Level 1) or when the time runs out(Level 2-5)

    I have also created a snake class that will have to be placed on the grid and eventually moved by user input.

    One thing at a time though, is there anyway i can place the food randomly on the grid for the snake to eat?
    by the way I'm not using swing or any GUI its just a game in a terminal.

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    You should be able to just use the random class to get an x,y position and then say like grid[x][y] = new Food(). You might need to type cast some stuff or change grid to type Object[][] in order to allow different object types into it

  3. #3
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    So basically i have to create an Object[][] grid instead of grid[][] grid? So then the Object grid can hold other objects such as food and snake etc. I should also use the random method to randomly put the food in a grid position?
    Is this right or have i got the wrong idea?

    Thanks.

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Do a quick search for array of arrays on this site. I asked a question similar to this and that topic went into a pretty good explanation.

  5. #5
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    Thanks for the post my friend. Really appreciated.

  6. #6
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    No problem. Did you get it working? I only redirected you because my first time attempting this was a week or two ago and I'm not comfortable enough explaining it and didn't want to lead you astray.

  7. #7
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    To place food randomly, you should search how to generate random numbers (I think that there is a Random class) and make sure you're picking out legal values within your grid dimensions as well as legal spots on your grid.

    I honestly don't think should use an array of actual Objects, as classes are pretty heavy duty for what you're trying to do. You can use characters much more easily, or you can also try to expand your Java knowledge database by learning enumerations. If this is a project with a deadline, do it however you want, but I suggest afterwards going back and trying it all three ways (Object[][], char[][], enum[][]). It'll just help you understand better how and when to use enums.

  8. #8
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    Quote Originally Posted by emceenugget View Post
    To place food randomly, you should search how to generate random numbers (I think that there is a Random class) and make sure you're picking out legal values within your grid dimensions as well as legal spots on your grid.

    I honestly don't think should use an array of actual Objects, as classes are pretty heavy duty for what you're trying to do. You can use characters much more easily, or you can also try to expand your Java knowledge database by learning enumerations. If this is a project with a deadline, do it however you want, but I suggest afterwards going back and trying it all three ways (Object[][], char[][], enum[][]). It'll just help you understand better how and when to use enums.
    I'll bare that in mind. I'm considering changing it to char[][] as it seems more simple, but we'll see. I'll post back how everything goes in the near future.

    Quote Originally Posted by xcallmejudasx
    No problem. Did you get it working? I only redirected you because my first time attempting this was a week or two ago and I'm not comfortable enough explaining it and didn't want to lead you astray.
    Don't worry you didn't. I've found someone on the Sun forums to who is helping me out. He's teaching me the things i need to know step by step working with me. kinda cool. :)
    Thanks all for your help so far, I'm sure I'll be posting back soon. ;)

Similar Threads

  1. how to get the grid values
    By jazz2k8 in forum Advanced Java
    Replies: 2
    Last Post: 11-06-2008, 02:11 PM
  2. How can i copy a folder from one place to another..
    By rajeshgubba in forum New To Java
    Replies: 4
    Last Post: 06-14-2008, 02:21 AM
  3. How to place panel into frame
    By vivek_9912 in forum AWT / Swing
    Replies: 2
    Last Post: 11-19-2007, 11:21 PM
  4. Help with Grid Layout
    By coco in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 08:03 PM
  5. how to place an image in an applet
    By paty in forum Java Applets
    Replies: 2
    Last Post: 08-04-2007, 05:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •