penfew
penfew

Reputation: 55

Player position in array

Why is my Security ('S') in the same position as Donald ('D').

The map should be printing out like this

[D - - - -]

[- - - - -]

[- - - - -]

[- - S - -]

[- - P - -]

But instead it shows up like this

[S - - - -]

[- - - - -]

[- - - - -]

[- - - - -]

[- - P - -]

public class Main {

    public static void main(String[] args) {

        Map m = new Map();
        Player p = new Player();
        Donald d = new Donald();
        Security s = new Security();

while(true) {

            m.updateMap(p.row, p.col, p.character);
            m.printMap();
            m.updateMap(p.row, p.col, '-');
            m.updateMap(d.row, d.col, d.character);
            m.updateMap(s.row, s.col, s.character);
            p.move();

        }

    }

}

public class Map {

    char map[][];

    Map() {
        map = new char[5][5];


        for(int i = 0; i<5; i++) {

            for(int j = 0; j<5; j++) {
                map[i][j] = '-';
            }
        }
    }


    void updateMap(int row, int col, char data) {

        map[row][col] = data;
    }


    //prints map on the screen. 
    void printMap() {
        for(int i = 0; i<5; i++) {
            for (int j = 0; j<5; j++) {
                System.out.print(map[i][j] + " ");
            }

            System.out.println();
        }
    }

}

public abstract class Position {

    int row;
    int col;
    char character;
    abstract void move();

}

public class Donald extends Position {

    //Doanld Trump's Position on the Array is [0,0]
    Donald() {
        int row = 0;
        int col = 0;
        character = 'D';
    }

    void move() {

    }

}

So as you can see here, i've put Security position as [3,2] but for some reason it's not recognizing it as [3,2] and places the Security at [0,0] where Donald is sitting.

public class Security extends Position {

    Security() {
        int row = 3;
        int col = 2;
        character = 'S';
    }

    void move() {

    }

}

import java.util.Scanner;

public class Player extends Position{

    //players position starts at [4,2] on the array
    Player() {
        row = 4;
        col = 2;
        character = 'P';
    }

    void move() {

        Scanner scanner = new Scanner(System.in);
        boolean move = false;
        while(!move) {
            System.out.println("up: w | down: s | left: a | right: d | quit: q");

            char userInput = scanner.next().toLowerCase().charAt(0);


            //moving forward
            if(userInput == 'w') {
                if(row>0) {
                    row--;
                    move = true;
                }
            }

            //moving left
            if(userInput == 'a') {
                if(col>0) {
                    col--;
                    move=true;
                }
            }

            //moving right
            if(userInput == 'd') {
                if(col<4) {
                    col++;
                    move=true;
                }
            }

            //moving down
            if(userInput == 's') {
                if(row<8) {
                    row++;
                    move=true;
                }
            }


            if(move == false) {
                System.out.println("You can't move here");
            }
        }



    }

}

Upvotes: 0

Views: 543

Answers (1)

Daniel
Daniel

Reputation: 7724

The class Security inherits the attributes row and col from Position, but in the constructor, you are doing this:

Security() {
    int row = 3;          //you are basically creating a new variable called row
    int col = 2;          //which is NOT the attribute (that is this.row)
    character = 'S';
}

After the constructor, the Security object stays with s.row and s.col equals to 0.

You should do

Security() {
    this.row = 3;          //you can also do        row = 3;
    this.col = 2;          //and the compiler will understand
    this.character = 'S';
}

You are making the same mistake in Donald: you tell Donald to be in position (0,0) but then you tell Security to be in position (0,0), that's why Security appears but Donald doesn't, he is overwritten by Security.

The Player is in row 4 and column 2, just as you set.

Upvotes: 1

Related Questions