Позиция игрока в массиве - PullRequest
       9

Позиция игрока в массиве

0 голосов
/ 24 октября 2019

Почему мой охранник ('S') находится в том же положении, что и Дональд ('D').

Карта должна распечатываться следующим образом

[D - - - -]

[- - - - -]

[- - -- -]

[- - S - -]

[- - P - -]

Но вместо этого это выглядит так

[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() {

    }

}

Итак, как вы можете видеть здесь, я поставил позицию безопасностикак [3,2], но по какой-то причине он не распознает его как [3,2] и помещает ЦБ в [0,0], где сидит Дональд.

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



    }

}

1 Ответ

1 голос
/ 24 октября 2019

Класс Security наследует атрибуты row и col от Position, но в конструкторе вы делаете это:

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

После конструктора Securityобъект остается с s.row и s.col равно 0.

Вы должны сделать

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

Вы делаете ту же ошибку в Donald: вы говорите Donald, чтов позиции (0,0), но затем вы указываете Security быть в позиции (0,0), поэтому Security появляется, а Donald нет, он перезаписывается на Security.

Player находится в строке 4 и столбце 2, так же, как вы установили.

...