1

Could somebody please help me solve the player movement of my 2d array grid.I'm able to print out the grid fine and display it. I'm also able to print out the player on the grid. I'm only having a hard time moving the player to the next position. I've tried looking at C++ code to try and replicate it but it hasn't been going well.

import java.util.Scanner;
import java.util.Random;
public class Components 
{
public Components()
{       
    Graph();
}

void Graph()
{
    int Row,Col,Num1,Num2;
    int p1 = 0;
    int p2 = 0;
    char Move;
    boolean Running = true;
    Scanner input = new Scanner(System.in);
    //Random random = new Random();
    System.out.println("Please Enter The Number Of Row:");
    Row = input.nextInt();
    System.out.println("Please Enter The Number Of Columns:");
    Col = input.nextInt();
    int Array[][] = new int [Row][Col];
    //Num1 = random.nextInt(Row) + 1;
    //Num2 = random.nextInt(Col) + 1;
    for(int i=0;i<Row;i++)
    {
    for(int x=0;x<Col;x++)
    {
    Array[i][x] = 0;
    Array[p1][p2] = 1;
    }
    }
    for(int i=0;i<Row;i++)
    {
    for(int x=0;x<Col;x++)
    {
    System.out.print(Array[i][x] + "\t");
    }
    System.out.println();
    }
    while(Running)
    {
    Move = input.next().charAt(0);
    switch(Move)
    {
    case 'w':
    case 'W':
    Array[p1][p2] = 0;
    Array[p1+=1][p2] = 1;
    break;
    case 's':
    case 'S':
    Array[p1][p2] = 0;
    Array[p1-=1][p2] = 1;
    break;
    case 'd':
    case 'D':
    Array[p1][p2] = 0;
    Array[p1][p2+=1] = 1;
    break;
    case 'a':
    case 'A':
    Array[p1][p2] = 0;
    Array[p1][p2-=1] = 1;
    break;
    case 'l':
    case 'L':
    Running = false;
    input.close();
    break;
    default:
    System.out.println("Please Press Proper Keys!");
    break;
    }
    }
    }
    }

  public class Main {

  public static void main(String[] args) {

  new Components();
   }

   }
2
  • Can you please explain a little more about what problem you are having or what is your aim in this program ? Commented Jan 15, 2019 at 17:36
  • My aim is to make a game where I can move around a player(Number). I'm currently stuck with the "movement of the player" on the 2d array grid. Commented Jan 16, 2019 at 12:19

2 Answers 2

1

I'm guessing that W key should appear to move up a row. As you print your rows from zero, the top row is zero, so you want to decrement p1 rather than increment it for case W, and similarly increment rather than decrement for case S.

You don't say exactly what you expect to happen when a player is moved, so I'm guessing here. Your code will only print out the grid when it is created. At this point the initial position will always be top left (p1=0. p2=0). If you print the grid after each move you would see it change.

e.g. if your code in Component class was:

public Components()
{       
    Graph();
}

    void Graph() {
        int Row, Col, Num1, Num2;
        int p1 = 0;
        int p2 = 0;
        char Move;
        boolean Running = true;
        Scanner input = new Scanner(System.in);
        // Random random = new Random();
        System.out.println("Please Enter The Number Of Row:");
        Row = input.nextInt();
        System.out.println("Please Enter The Number Of Columns:");
        Col = input.nextInt();
        int Array[][] = new int[Row][Col];
        // Num1 = random.nextInt(Row) + 1;
        // Num2 = random.nextInt(Col) + 1;
    // Don't need to do this : int[] will initialise to 0    
    //    for(int i=0;i<Row;i++)
    //    {
    //    for(int x=0;x<Col;x++)
    //    {
    //    Array[i][x] = 0;
    //    }
    //    }
        Array[p1][p2] = 1;
        print(Array);
        while (Running) {
            Move = input.next().charAt(0);
            switch (Move) {
            case 'w':
            case 'W':
                Array[p1][p2] = 0;
                Array[--p1][p2] = 1;
                print(Array);
                break;
            case 's':
            case 'S':
                Array[p1][p2] = 0;
                Array[++p1][p2] = 1;
                print(Array);
                break;
            case 'd':
            case 'D':
                Array[p1][p2] = 0;
                Array[p1][++p2] = 1;
                print(Array);
                break;
            case 'a':
            case 'A':
                Array[p1][p2] = 0;
                Array[p1][--p2] = 1;
                print(Array);
                break;
            case 'l':
            case 'L':
                Running = false;
                input.close();
                break;
            default:
                System.out.println("Please Press Proper Keys!");
                break;
            }
        }
    }

    private void print(int[][] grid) {
        for (int i = 0; i < grid.length; i++) {
            for (int x = 0; x < grid[0].length; x++) {
                System.out.print(grid[i][x] + "\t");
            }
            System.out.println();
        }
    }

}

Start the app and use 3 for rows and 4 for columns you will see:

1   0   0   0   
0   0   0   0   
0   0   0   0

If you then enter d you will see:

0   1   0   0   
0   0   0   0   
0   0   0   0

Then enter s you will see:

0   0   0   0   
0   1   0   0   
0   0   0   0

You need to explain what you expect to see, otherwise no one will be able to help.

Sign up to request clarification or add additional context in comments.

4 Comments

I've added the code that you suggested it hasn't changed anything Sir. Besides showing another way to increment.There's still no player movement. Please fill me in on what I'm missing regarding the "Movement" of the player. Thanks pcoates ...
@MastersProgression I've updated my answer - not sure if it'll help. You don't really say anywhere what you expect to happen.
You answered my question exactly how needed. I was just missing the part of putting the printing of the 2darray in a function. I'll post the working code above. ...
@MastersProgression glad it helped, Good luck if you're planning on learning more Java. It's normal to accept an answer on StackOverflow if you find it useful.
0

//2d array with player movement in random starting position

  import java.util.Scanner;
  import java.util.Random;
  public class Components 
  {
  public Components() 
  {     
    Graph();
  }

int Row,Col,Num1,Num2;
char Move;
boolean Running = true;
Scanner input = new Scanner(System.in);

void TwoArray(int gph [][])
{
    for(int i=0;i<Row;i++)
    {
    for(int x=0;x<Col;x++)
    {
    System.out.print(gph[i][x] + "\t");
    }
    System.out.println();
    }
}

void Graph() 
{
    Random random = new Random();
    System.out.println("Please Enter The Number Of Row:");
    Row = input.nextInt();
    System.out.println("Please Enter The Number Of Columns:");
    Col = input.nextInt();
    Num1 = random.nextInt(Row) + 1;
    Num2 = random.nextInt(Col) + 1;
    int Array[][] = new int [Row][Col];
    for(int i=0;i<Row;i++)
    {
    for(int x=0;x<Col;x++)
    {
    Array[i][x] = 0;
    }
    }
    Array[Num1][Num2] = 1;
    int p1 = Num1;
    int p2 = Num2;
    TwoArray(Array);
    while(Running)
    {
    Move = input.next().charAt(0);
    switch(Move)
    {
    case 'w':
    case 'W':
    Array[p1][p2] = 0;
    Array[p1-=1][p2] = 1;
    TwoArray(Array);
    break;
    case 's':
    case 'S':
    Array[p1][p2] = 0;
    Array[p1+=1][p2] = 1;
    System.out.flush();
    TwoArray(Array);
    break;
    case 'd':
    case 'D':
    Array[p1][p2] = 0;
    Array[p1][p2+=1] = 1;
    System.out.flush();
    TwoArray(Array);
    break;
    case 'a':
    case 'A':
    Array[p1][p2] = 0;
    Array[p1][p2-=1] = 1;
    System.out.flush();
    TwoArray(Array);
    break;
    case 'l':
    case 'L':
    Running = false;
    input.close();
    break;
    default:
    System.out.println("Please Press Proper Keys!");
    break;
    }
    }
    }
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.