Results 1 to 5 of 5
  1. #1
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default 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

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    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);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default 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

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    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
    I changed your 'place food' section (see above) and added a bit of comment for you to work out.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

Similar Threads

  1. Snake Game Applet
    By Growler in forum Java Applets
    Replies: 6
    Last Post: 07-11-2010, 02:47 PM
  2. Snake game in java
    By freaky in forum New To Java
    Replies: 5
    Last Post: 04-20-2010, 06:34 PM
  3. Java Applet Help - Making "Snake" Game
    By Alphimeda in forum Java Applets
    Replies: 15
    Last Post: 04-04-2010, 05:39 PM
  4. Snake Game
    By mustachMan in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 10:35 PM
  5. Snake game movement
    By BeerMonkey in forum New To Java
    Replies: 9
    Last Post: 11-27-2008, 12:48 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
  •