-3

How would I go about creating the board given any x and y coordinates for the start nodes. For example, if x=3 and y=2 the board should like:

1 2 3
4 5 x
6 7 8

An example in java or pseudocode would be extremely useful.

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
mcseth antwi
  • 131
  • 1
  • 1
  • 9
  • 5
    What have you already tried? Show your code please. – DimaSan Nov 28 '16 at 21:09
  • 4
    You have to print the numbers 1 through 8, and you need to print the character 'x'. You also need to start a new line after every third character until you reach the last line. Surely you can do *some* of this yourself, and then ask for help when you're stuck. – RaminS Nov 28 '16 at 21:10

2 Answers2

0

hope this helps. Let me know if you have any questions.

int x_lim = 2;
int y_lim = 3;

int count=1;
for(int x=1;x<3+1;x++)
{
    for(int y=1;y<3+1;y++)
    {
        if(x_lim==x && y_lim==y)  //skip case (blank tile)
        {
            System.out.println("x"+" ");
        }
        else  //other numbers
        {
            System.out.println(count+" ");
            count++;            
        }

    }
}
GC_
  • 448
  • 4
  • 23
-1

Before posting the code, I would recommend only one thing. In programming you have to start thinking in a zero-based indexing. Also, if the format you posted doesn't have any typos in indexing (because rationally after printing the 'x' you must print 7 and not 6, so that the 3x3 board ends up in the index 9) maybe the code below will help you.

    int coord_x = 3;
    int coord_y = 2;

    int rows = 3;
    int columns = 3;

    int counter = 1;
    for (int i = 1; i <= rows; i++){
        for (int j = 1; j <= columns; j++){
            if (i == coord_y && j == coord_x){
                System.out.print("x ");
                continue;
            }
            System.out.print(counter + " ");
            counter++;
        }
        System.out.println();
    }
RiPs7
  • 29
  • 7