Results 1 to 5 of 5
Thread: Snake Game in Java
- 02-10-2011, 02:05 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
Snake Game in Java
Hello there how are you all. I am trying to make a Snake Game in Java and have managed to set up the cells required for the snake to move around in.
What i am trying to do now is place food randomly. This method should find a randomly chosen clear cell. I was hoping i'd use a do-while loop.
I understand to choose a cell, i will need to choose its X and Y values randomly an that these must lie in the range zero to one less than the size of grid.
To obtain two integers in range 0.0 to n-1, i was hoping i can use the following:
x = (int) (Math.random() * n);
y = (int) (Math.random() * n);
Any ideas on the do-while loop because i cannot get it to work.
Please Help :(
Kind regards
Shyam
- 02-10-2011, 02:11 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
You should generate a cell (x, y) and if it's not empty, try again:
Java Code:do { generate a cell (x,y) } while (cell[x][y] != empty);
JosBuild a wall around Donald Trump; I'll pay for it.
- 02-10-2011, 02:15 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
sanke
I don't seem to understand sorry. Here is my code, and i'm trying to create a method for placing the food:
Java Code:public class Game { // ---------------------------------------------------------------------- // Part a: the score message private static String scoreMessage; public String getScoreMessage() { return scoreMessage; } // getScoreMessage public void setScoreMessage(String message) { scoreMessage = message; } // setScoreMessage public String getAuthor() { return "Shyam Gorasia"; } // getAuthor // ---------------------------------------------------------------------- // Part b: constructor and grid accessors private final int gridSize; private final Cell[][] gridCell; public Game(int requiredGridSize) { gridSize = requiredGridSize; gridCell = new Cell[requiredGridSize][requiredGridSize]; for (int row = 0; row < gridSize; row++) for (int column = 0; column < gridSize; column++) gridCell[row][column] = new Cell(); } // Game public int getGridSize() { return gridSize; } // getGridSize public Cell getGridCell(int x, int y) { return gridCell[x][y]; } // getGridCell // ---------------------------------------------------------------------- // Part c: initial game state // Part c-1: setInitialGameState method public void setInitialGameState(int requiredTailX, int requiredTailY, int requiredLength, int requiredDirection) { for (int row = 0; row < gridSize; row++) for (int column = 0; column < gridSize; column++) gridCell[row][column].setClear(); } // setInitialGameState // ---------------------------------------------------------------------- // Part c-2 place food public void placeFood() { int gridSize; do { int randomXValue = (int) (Math.random() * gridSize); int randomYValue = (int) (Math.random() * gridSize); while // ---------------------------------------------------------------------- // Part c-3: place snake // ---------------------------------------------------------------------- // Part d: set snake direction public void setSnakeDirection(int requiredDirection) { } // setSnakeDirection // ---------------------------------------------------------------------- // Part e: snake movement // Part e-1: move method public void move(int moveValue) { } // move // ---------------------------------------------------------------------- // Part e-2: move the snake head // ---------------------------------------------------------------------- // Part e-3: move the snake tail // ---------------------------------------------------------------------- // Part e-4: check for and deal with crashes // ---------------------------------------------------------------------- // Part e-5: eat the food public int getScore() { return 99999999; } // getScore // ---------------------------------------------------------------------- // Part f: cheat public void cheat() { } // cheat // ---------------------------------------------------------------------- // Part g: trees public void toggleTrees() { } // toggleTrees // ---------------------------------------------------------------------- // Part h: crash countdown // ---------------------------------------------------------------------- // Part i: optional extras public String optionalExtras() { return " No optional extras defined\n"; } // optionalExtras public void optionalExtraInterface(char c) { if (c > ' ' && c <= '~') setScoreMessage("Key " + new Character(c).toString() + " is unrecognised (try h)"); } // optionalExtraInterface } // class Game
- 02-10-2011, 02:42 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Java Code:// ---------------------------------------------------------------------- // Part c-2 place food public void placeFood() { int gridSize= ...; // give it a value int randomXvalue; int randomYvalue; do { randomXValue = (int) (Math.random() * gridSize); randomYValue = (int) (Math.random() * gridSize); } while (/* cell at randomXvalue, randomYvalue isn't empty */); // place food at the generated cell position
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 02-10-2011, 02:49 PM #5
Crossposted: Snake Game In Java - Java Programming Forums
How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
Similar Threads
-
Snake Game Applet
By Growler in forum Java AppletsReplies: 6Last Post: 07-11-2010, 02:47 PM -
Snake game in java
By freaky in forum New To JavaReplies: 5Last Post: 04-20-2010, 06:34 PM -
Java Applet Help - Making "Snake" Game
By Alphimeda in forum Java AppletsReplies: 15Last Post: 04-04-2010, 05:39 PM -
Snake Game
By mustachMan in forum New To JavaReplies: 2Last Post: 12-10-2009, 10:35 PM -
Snake game movement
By BeerMonkey in forum New To JavaReplies: 9Last Post: 11-27-2008, 12:48 PM
Bookmarks