Почему мой охранник ('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");
}
}
}
}