1 2 Previous Next 29 Replies Latest reply: Dec 2, 2008 5:12 PM by 843785 RSS

    Getting an array to print into another array.

    843785
      Hi all. So after an illness and other things i had enough time to think about my project. I decided i was going about it a more complicated way than what was needed. So now i've began work again. I felt lik i was getting along nicely until i had trouble printing my Snake array into my Grid 2D array.
      If anyone can help me with the error and why i cannot print the snake onto the grid that would be great?
      Thanks.


      Grid Class:
      import java.lang.*;
      
      public class Grid
      {
           public static void main(String[] args)
           {
                //Final int of ROWS and COLS for the grid size
                final int ROWS = 33;
                final int COLS = 33;
                
                // int counter will be used to display the grid
                int counter = 0;
                
                //Create new String array called Grid
                String[][] Grid = new String[ROWS][COLS];
                
                //new Food, Snake and Catcher
                Food Fd = new Food();
                Snake Sn = new Snake();
                Catcher cat = new Catcher();
                
                //Nested for loops to display the grid
                for (int i =0; i < Grid.length; i++)
                {
                         for (int j = 0; j < Grid[0].length; j++)
                     {
                        Grid[i][j] = " ";
                          Grid[0] = "|";
                          Grid[i][32] = "|";
                          Grid[0][j] = "-";
                          Grid[32][j] = "-";
                          Grid[12][12] = Fd.food;//Print food F onto grid
                          Grid[15][15] = Sn.snake;//Print snake onto grid (NOT WORKING YET!!)
                          Grid[5][10] = cat.catcher;//print catcher on grid
                          System.out.print(Grid[i][j]);//Prints grid
                          counter = counter + 1;
                
                          if(counter == 33)
                          {
                               counter = 0;
                               System.out.println("");
                          }          

                }
                }
           }
      }
      Snake Class:
      import java.lang.*;

      public class Snake
      {
           public static void main(String[] args)
           {
                final int Snake = 20;
                
           
                String[] snake = new String[Snake];
                     for (int h =0; h < snake.length; h++)
                     {
                               snake[0] = "+";
                               snake[1] = "*";
                               snake[2] = "*";
                               snake[3] = "*";
                               
                               System.out.println(snake[h]);
                     }
           }
      }
      Error:
      Grid.java:28: cannot find symbol
      symbol : variable snake
      location: class Snake
      Grid[15][15] = Sn.snake;
      ^
      1 error
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
        • 1. Re: Getting an array to print into another array.
          800268
          Java is case sensitive, the class Snake has a variable Snake, not snake.
          Your next problem is going to be that Sn.Snake is an int, not a String.
          • 2. Re: Getting an array to print into another array.
            800323
            Your snake variable only exists inside the main() method of the Snake class - you cannot access this variable outside of the class.
            You could make the variable a public member; however, in general, directly accessing variables outside the class is considered a BadThink(TM). You should use something like a "getter" method.
            • 3. Re: Getting an array to print into another array.
              843785
              The variable Snake is to decalare the length of the array.
              If you look further down it does say:
              String[] snake = String[Snake];
              Which creates an array called snake with a length of Snake.

              So the array is a string but i don't know how to print that into my Grid.
              • 4. Re: Getting an array to print into another array.
                800323
                WalterLaan wrote:
                Java is case sensitive, the class Snake has a variable Snake, not snake.
                Your next problem is going to be that Sn.Snake is an int, not a String.
                That's what I thought at first; but, there is both a "Snake" and a "snake" varible.
                • 5. Re: Getting an array to print into another array.
                  843785
                  jbish wrote:
                  Your snake variable only exists inside the main() method of the Snake class - you cannot access this variable outside of the class.
                  You could make the variable a public member; however, in general, directly accessing variables outside the class is considered a BadThink(TM). You should use something like a "getter" method.
                  I removed the main method and put it all in public void Snake()
                  {

                  }
                  However i still get the same error. For some reason it cannot see there is a snake variable that i can use.

                  Also what do you mean getter method? Would i be able to use the geter method to print into onto my Grid[][] 2d array?

                  Edited by: Nakira on 02-Dec-2008 14:25

                  Edited by: Nakira on 02-Dec-2008 14:26
                  • 6. Re: Getting an array to print into another array.
                    800323
                    Nakira wrote:
                    jbish wrote:
                    Your snake variable only exists inside the main() method of the Snake class - you cannot access this variable outside of the class.
                    You could make the variable a public member; however, in general, directly accessing variables outside the class is considered a BadThink(TM). You should use something like a "getter" method.
                    I removed the main method and put it all in public void Snake()
                    {

                    }
                    However i still get the same error. For some reason it cannot see there is a snake variable that i can use.

                    Edited by: Nakira on 02-Dec-2008 14:25
                    Show the new code.
                    • 7. Re: Getting an array to print into another array.
                      843785
                      Very similar to before, i still haven't touched the Grid code yet so no need in putting it up?
                      If you do want it here though let me know i'll edit post.
                      import java.lang.*;
                      
                      public class Snake
                      {
                           public void Snake()
                           {
                                final int Snake = 20;
                                
                           
                                String[] snake = new String[Snake];
                                     for (int h =0; h < snake.length; h++)
                                     {
                                               snake[0] = "+";
                                               snake[1] = "*";
                                               snake[2] = "*";
                                               snake[3] = "*";
                                               
                                               System.out.println(snake[h]);
                                     }
                           }
                      }
                      • 8. Re: Getting an array to print into another array.
                        800323
                        Now the variable is only accessible in the Snake constructor.
                        Not the best way to do this; however, the below code should get you closer.
                        import java.lang.*;
                         
                        public class Snake
                        {
                             final static Snake = 20;
                             public String[] snake;
                        
                             public void Snake()
                             {
                                  snake = new String[Snake];
                                       for (int h =0; h < snake.length; h++)
                                       {
                                                 snake[0] = "+";
                                                 snake[1] = "*";
                                                 snake[2] = "*";
                                                 snake[3] = "*";
                                                 
                                                 System.out.println(snake[h]);
                                       }
                             }
                        }
                        • 9. Re: Getting an array to print into another array.
                          843785
                          Thanks for all the help so far. I'll Have a quick mess around with my code see if i can find any more stuff out and post back here soon.
                          • 10. Re: Getting an array to print into another array.
                            843785
                            Hey, I was wondering when you were going to come back around to work on this.

                            Honestly, I see why your new way appealed to you, but I think you are headed in the wrong direction.

                            Just some minor observations.

                            First and most importantly, take time to name your variables correctly. You don't want to make them super long so you have to type 25 characters every time you want to use that variable, but you don't want to make them so short you have no idea what they are doing either. Like for example your int Snake variable would have been much better named maxSnakeLength. Actually it should probably be a final as well which would make it named MAXSNAKELENGTH. Naming two objects the same but with different capitalization schemes is extremely undesirable, it always leads to confusion and errors, as seen here.

                            Your grid drawing is completely messed up. Let's take two steps through the j loop on i's first step and see what is happening.

                            So i = 0 and j = 0.
                            (0, 0) gets set to " "
                            (0, 0) now gets reset to "|"
                            (0, 32) gets set to "|"
                            (0, 0) now gets reset yet again to "_"
                            (32, 0) gets set to "_"
                            (12, 12) gets set to food
                            (15, 15) gets set to snake
                            (5, 10) gets set to catcher

                            Then you print (0, 0) which is "_".

                            Now i = 0 and j = 1.
                            (0, 1) gets set to " "
                            (0, 0) now gets reset (total of 4 sets already on second pass of inner loop) to "|"
                            (0, 32) now gets reset to "|" even though it was that already
                            (0, 1) now gets reset to "_"
                            (32, 1) gets set to "_"
                            (12, 12) gets reset to food even though it was that already
                            (15, 15) gets reset to snake even though it was that already
                            (5, 10) gets reset to catcher even though it was that already

                            Then you print (0, 1) which is "_".

                            I see what you are going for, it is sort of like a switch statement without the breaks where whatever it is when it falls out the bottom is what it was meant to be. But this is horribly inefficient and going to lead you to problems later. You are doing 8 array sets for each index in an array with 1089 indices. That means you are wasting 7623 sets in a game that is time sensitive.

                            Also, how exactly do you plan to get the snake, food, or catcher to move when every time you redraw the grid their positions are hard coded? This is solvable with your current scheme, but it is ugly. What isn't going to be easily solvable is collision detection, which is pretty much the center point of this game. You are going to be much better off where you were two weeks ago where you set the grid up, have it for manipulation, and have a seperate function to just print what the grid currently holds.

                            Also, you should just use "#" for the walls, it is going to make it much smoother at the corners and make the bottom row much less confusing to the player. I am fairly sure "#" is what was used in the original version of this game (been a while), so you can't really go wrong with it.

                            Also your counter and its code is completely unnecessary and only adding to the unnecessary computations you are doing each time through the loop. This is a really common mistake for new programmers, so don't sweat it. You just need to be more aware of how nested loops work. Take a look at this pseudo code.
                             
                            for (i = 0; i < 4; i++) 
                            { 
                                  for (j = 0; j < 3; j++) 
                                  { 
                                        Just print "hi " with no carriage return; 
                                  } 
                                   
                                  //Notice this is inside the first for loop (i) but not the nested one (j) 
                                  print "bye" with carriage return 
                            } 
                            The outcome of that will be:
                            hi hi hi bye
                            hi hi hi bye
                            hi hi hi bye
                            hi hi hi bye

                            See how nifty that works?

                            JSG

                            Edited by: JustSomeGuy on Dec 2, 2008 7:54 AM
                            • 11. Re: Getting an array to print into another array.
                              843785
                              I totally agree with what you said about the '#' It looks really nice compared to what i have, however the grid at the moment and the way it is coded seems to work really well.

                              http://i95.photobucket.com/albums/l157/Josh_burley/Grid.png

                              Looking at that it seems to work fine. I dont understand the need to change it so much. My snake is now an array so couldn't i print that array into this grid 2D array and when the user presses the 'w' key move it up one using the Grid co-ordinates.

                              Also the food shouldn't be to much of a problem once i figure out the random methods to use etc. but for now i've just displayed food as f in a fixed position.

                              Edited by: Nakira on 02-Dec-2008 17:03
                              • 12. Re: Getting an array to print into another array.
                                843785
                                Here is The Grid Snake and Food classes just to bring them all together for easier reading!

                                Grid:
                                import java.lang.*;
                                
                                public class Grid
                                {
                                     public static void main(String[] args)
                                     {
                                          //Final int of ROWS and COLS for the grid size
                                          final int ROWS = 33;
                                          final int COLS = 33;
                                          
                                          // int counter will be used to display the grid
                                          int counter = 0;
                                          
                                          //Create new String array called Grid
                                          String[][] Grid = new String[ROWS][COLS];
                                          
                                          //new Food
                                          Food Fd = new Food();
                                          Score myScore = new Score();
                                          
                                          //Nested for loops to display the grid
                                          for (int i =0; i < Grid.length; i++)
                                          {
                                                   for (int j = 0; j < Grid[0].length; j++)
                                               {
                                                  Grid[i][j] = " ";
                                                    Grid[0] = "#";
                                                    Grid[i][32] = "#";
                                                    Grid[0][j] = "#";
                                                    Grid[32][j] = "#";
                                                    Grid[Fd.fdx][Fd.fdy] = Fd.food;//Print food F onto grid
                                                    System.out.print(Grid[i][j]);//Prints grid
                                                    counter = counter + 1;
                                          
                                                    if(counter == 33)
                                                    {
                                                         counter = 0;
                                                         System.out.println("");
                                                    }          

                                          }
                                          }
                                          System.out.println("Score: "+myScore.Score);
                                     }
                                }
                                Snake:
                                import java.lang.*;

                                public class Snake
                                {
                                     public void Snake()
                                     {
                                          final int MAX_SNAKE_LENGHT = 20;
                                          
                                     
                                          String[] snake = new String[MAX_SNAKE_LENGHT];
                                               for (int h =0; h < snake.length; h++)
                                               {
                                                         snake[0] = "H";
                                                         snake[1] = "*";
                                                         snake[2] = "*";
                                                         snake[3] = "*";
                                                         
                                                         System.out.println(snake[h]);
                                               }
                                     }
                                }
                                Food:
                                import java.util.Random;

                                public class Food
                                {
                                     String food = "F";//Declaring String variable food "F"
                                     

                                     Random foodx = new Random();
                                     Random foody = new Random();

                                     int fdx = foodx.nextInt(31);
                                     int fdy = foody.nextInt(31);
                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                                • 13. Re: Getting an array to print into another array.
                                  843785
                                  Right, what i said is it would work, but is less than efficient.

                                  Have you managed to get your snake to move yet? Because I think that is when you will first encounter problems. But once you try to do collision detection you are going to be in real trouble. At least that is how it looks to me, I could be completely wrong.

                                  At least look at my pseudo code and get rid of that counter, if your teacher sees that he is going to mark you down for it for sure.

                                  JSG
                                  • 14. Re: Getting an array to print into another array.
                                    843785
                                    I have looked at it but im afraid i dont quite understand what your trying to say. I have edited my code a bit and removed the snake array, now its just a String "H"
                                    I've tried to get it to move but the code i entered doesn't seem quite right.

                                    Here is my new code so far.
                                    import java.lang.*;
                                    
                                    public class Grid
                                    {
                                         public static void main(String[] args)
                                         {
                                              //Final int of ROWS and COLS for the grid size
                                              final int ROWS = 33;
                                              final int COLS = 33;
                                              char move;
                                              
                                              // int counter will be used to display the grid
                                              int counter = 0;
                                              
                                              //Create new String array called Grid
                                              String[][] Grid = new String[ROWS][COLS];
                                              
                                              //new Food
                                              Food Fd = new Food();
                                              Score myScore = new Score();
                                              Snake sn = new Snake();
                                              Keyboard kb = new Keyboard();
                                              
                                              //Nested for loops to display the grid
                                              for (int i =0; i < Grid.length; i++)
                                              {
                                                       for (int j = 0; j < Grid[0].length; j++)
                                                   {
                                                      Grid[i][j] = " ";
                                                        Grid[0] = "#";
                                                        Grid[i][32] = "#";
                                                        Grid[0][j] = "#";
                                                        Grid[32][j] = "#";
                                                        Grid[Fd.fdx][Fd.fdy] = Fd.food;//Print food F onto grid
                                                        Grid[sn.snakeHeadX][sn.snakeHeadY] = sn.snakeHead;
                                                        System.out.print(Grid[i][j]);//Prints grid
                                                        counter = counter + 1;
                                              
                                                        if(counter == 33)
                                                        {
                                                             counter = 0;
                                                             System.out.println("");
                                                        }          

                                              }
                                              }
                                              System.out.println("Score: "+myScore.Score);
                                              System.out.println("Please Enter either w,a,s,d to move up, left, down, right accordingly: ");
                                              move = kb.readChar();
                                              if (move = w)
                                              {
                                                   sn.snakeHeadY++;
                                              }
                                              else if (move = s)
                                              {
                                                   sn.snakeHeadY--;
                                              }
                                              else if (move = a)
                                              {
                                                        sn.snakeHeadX--;
                                              }
                                              else if (move = d)
                                              {
                                                        sn.snakeHeadX++;
                                              }
                                              else
                                              {
                                                   System.out.println("Invalid move!");
                                              }
                                              
                                         }
                                    }import java.lang.*;

                                    public class Snake
                                    {
                                              String snakeHead = "H";
                                              int snakeHeadX = 15;
                                              int snakeHeadY = 15;
                                              
                                              char move;
                                              
                                    }
                                    They print out this error,
                                    Grid.java:50: cannot find symbol
                                    symbol : variable w
                                    location: class Grid
                                    if (move = w)
                                    ^
                                    Grid.java:50: incompatible types
                                    found : char
                                    required: boolean
                                    if (move = w)
                                    ^
                                    Grid.java:54: cannot find symbol
                                    symbol : variable s
                                    location: class Grid
                                    else if (move = s)
                                    ^
                                    Grid.java:54: incompatible types
                                    found : char
                                    required: boolean
                                    else if (move = s)
                                    ^
                                    Grid.java:58: cannot find symbol
                                    symbol : variable a
                                    location: class Grid
                                    else if (move = a)
                                    ^
                                    Grid.java:58: incompatible types
                                    found : char
                                    required: boolean
                                    else if (move = a)
                                    ^
                                    Grid.java:62: cannot find symbol
                                    symbol : variable d
                                    location: class Grid
                                    else if (move = d)
                                    ^
                                    Grid.java:62: incompatible types
                                    found : char
                                    required: boolean
                                    else if (move = d)
                                    ^
                                    8 errors
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                                    1 2 Previous Next