Results 1 to 7 of 7
  1. #1
    josh2992 is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default Making a grid in a string

    Hi Java Forums,

    Before i explain my issue, i will say i have posted something similar a few weeks back and got some help which helped me solve my problem. The current problem i have is similar but a bit more advanced (it builds on what i did before). I have also conducted another search to find help with no luck. Anyway, here goes.

    I am making a console based game where the objective is to shoot an unseen target using X and Y co ordinates on a 10 by 10 grid (starting at 0 and ending at 9). Since this is console based, the grid shows up as a string. I have constructed the following code, which makes my grid perfectly for me. *Assume 'size' is set at 9*

    Java Code:
    public String displayGrid() {
    		int gridSize = size + 1;
    		int gridLine = gridSize - 1;
    		String grid = "";
    
    		for (int i = gridLine; i >= 0; i--) {
    			grid = grid + i;
    
    			for (int z = 0; z < gridSize; z++) {
    				grid = grid + " .";
    			}
    			grid = grid + "\n";
    		}
    
    		grid = grid + " ";
    
    		for (int i = 0; i < gridSize; i++)
    			grid = grid + " " + i;
    		return grid;
    	}
    My problem now is that as a more advanced feature of the game i want an X to show at the spot where the player has chosen to fire at. I can get the coordinates from another method in terms of two separate integers (x and y), but i don't know how to use them in my code to place an X on the spot where the player has chosen.

    I hope i have made my problem clear, if not, please do not hesitate to ask for clarification. Any help at all is, as always, much appreciated.

    Thanks,

    Josh.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    I suggest using a 2D array to represent the grid. Then all you need to do is have a nested loop iterate over the 2D array and print the contents. Elsewhere you have code that determines/changes the contents of the 2D array.

  3. #3
    josh2992 is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Thanks for the reply Junky,

    I am not too familiar with arrays yet, could you show me how i would do this or link me to a good arrays guide? I have used VB a long time ago and know what arrays are but don't have a clue on how to use them in java :(

  4. #4
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    Quote Originally Posted by josh2992 View Post
    could you show me how i would do this or link me to a good arrays guide?
    Suggested reading : Arrays And 2D-Arrays

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java Code:
    import java.util.Scanner;
    
    class Grid {
        private final int len = 10;
        char[][] grid = new char[len][len];
    
        Grid() {
            init();
        }
    
        private void init() {
            for(int row = 0; row < len; row++) {
                for(int col = 0; col < len; col++) {
                    grid[row][col] = '.';
                }
            }
        }
    
        private void display() {
            for(int row = 0; row < len; row++) {
                System.out.print((len - row - 1) + " ");
                for(int col = 0; col < len; col++) {
                    
                    System.out.print(grid[row][col] + " ");
                }
                System.out.println();
            }
            for(int index = 0; index < len; index++) {
                System.out.print((index == 0 ? "  " : " ") + index);
            }
            System.out.println();
        }
        
        public void run() {
            display();
            Scanner reader = new Scanner(System.in);
            System.out.print("Enter the x coordinate: ");
            int x = reader.nextInt();
            System.out.print("Enter the y coordinate: ");
            int y = reader.nextInt();
            if(x >= len || y >= len) {
                System.out.println("Invalid coordinates.");
            } else {
                y = len - 1 - y;
                grid[y][x] = 'X';
                display();
            }
        }
        
        public static void main(String[] args) {
            new Grid().run();
        }
    }
    I don't normally provide code as extensive as this. Since humans expect [0,0] to be bottom left corner I did some jiggery to reflect this, which may or may not be what you want.

  6. #6
    josh2992 is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Junky, you are a legend...that does practically exactly what i require, thanks so much!
    and thanks to Goldest i understand how it works too :D

    Thanks for the help guys.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,238
    Rep Power
    19

    Default

    Quote Originally Posted by Junky View Post
    humans expect [0,0] to be bottom left corner
    Humans do. Programmers don't :p

    I think it's better that a newbie get used to that early on.

    db

Similar Threads

  1. Help making a grid inside of a JPanel
    By satanfx55 in forum AWT / Swing
    Replies: 3
    Last Post: 05-13-2011, 07:18 PM
  2. Making a grid using a String.
    By josh2992 in forum New To Java
    Replies: 5
    Last Post: 05-10-2011, 02:11 PM
  3. Grid Painting?
    By iCon09 in forum New To Java
    Replies: 2
    Last Post: 02-26-2011, 07:04 AM
  4. Making a String Array from a text file
    By MrJimmah in forum New To Java
    Replies: 5
    Last Post: 11-16-2010, 11:19 PM
  5. Making grid using pictures
    By racewithferrari in forum New To Java
    Replies: 1
    Last Post: 11-01-2009, 12:59 AM

Posting Permissions

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