MastersProgression
MastersProgression

Reputation: 37

Two Dimensional Array Grid Player Movement Java

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();
   }

   }

Upvotes: 1

Views: 2857

Answers (2)

MastersProgression
MastersProgression

Reputation: 37

//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;
    }
    }
    }
   }

Upvotes: 0

pcoates
pcoates

Reputation: 2307

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.

Upvotes: 1

Related Questions