简体   繁体   English

在嵌套的for循环内,打印字符串“。”,如果满足条件,则将其替换为其他字符。 MOOC Java第10周“地牢”

[英]Inside nested for loop, print string “.” and replace it to other character if the condition are met. MOOC Java Week 10 'Dungeon'

I'm working on this project MOOC Java Week 10 Exercise 33 我正在从事这个项目MOOC Java Week 10练习33

I want String . 我想要String . replaced by player/vampire symbol (String @ and v ), when the player/vampire position X and Y are equal to X and Y of Grid. 当玩家/吸血鬼位置X和Y等于Grid的X和Y时,由玩家/吸血鬼符号(String @v )替换。 (Height and Width of Grid are printed by string . ). (网格的高度和宽度由字符串.打印. The problem is in Class Dungeon , method printMap() . 问题出在类Dungeon printMap() ,方法为printMap()

Class Player 班级球员

public class Player {

private final String symbol;
private int posX, posY, width, height;

public Player(String symbol, int width, int height) {
    this.symbol = symbol;
    this.posX = 0;
    this.posY = 0;
    this.width = width - 1;
    this.height = height - 1;
}

public String getSymbol() {

    return symbol;
}

public int getPosX() {

    return posX;

}

public int getPosY() {

    return posY;

}

public void setPosX(int x) {

    posY = x;

}

public void setPosY(int y) {

    posY = y;
}

public String getPos() {

    return posX + " " + posY;
}

public void keyMap(String keyPressed) {
    boolean y = posY > 0;
    boolean h = posY < height;
    boolean x = posX > 0;
    boolean w = posX < width;

    if (keyPressed.equalsIgnoreCase("w")) {
        if (y) {
            posY--;
        }
    } else if (keyPressed.equalsIgnoreCase("s")) {
        if (h) {
            posY++;
        }
    } else if (keyPressed.equalsIgnoreCase("a")) {
        if (x) {
            posX--;
        }
    } else if (keyPressed.equalsIgnoreCase("d")) {
        if (w) {
            posX++;
        }
    }

}

@Override
public String toString() {

    return symbol + " " + posX + " " + posY;
}

}

Class Vampire 吸血鬼

package dungeon;

import java.util.Random;


public class Vampire {

    private String symbol;
    private int posX, posY, width, height;
    private final Random rand = new Random();

public Vampire(String symbol, int width, int height) {
    this.symbol = symbol;
    this.posX = rand.nextInt(width);
    this.posY = rand.nextInt(height);
    this.width = width - 1;
    this.height = height - 1;

}

public String getSymbol() {

    return symbol;
}

public int getPosX() {

    return posX;

}

public int getPosY() {

    return posY;

}

public String getPos() {

    return posX + " " + posY;
}

public void setPosX(int x) {

    posX = x;

}

public void setPosY(int y) {

    posY = y;
}

public void resetPos() {
    posX = rand.nextInt(width);
    posY = rand.nextInt(height);
    checkStartPos();
}

public void checkStartPos() {

    while (posX == 0 || posY == 0) {
        if (posX == 0) {
            posX = rand.nextInt(width);
        } else if (posY == 0) {
            posY = rand.nextInt(height);
        }
    }

}

public void move() {
    boolean y = posY > 0;
    boolean h = posY < height;
    boolean x = posX > 0;
    boolean w = posX < width;

    int direction = rand.nextInt(4);
    switch (direction) {
        case 0:
            if (y) {
                posY--;
                break;
            }
        case 1:
            if (h) {
                posY++;
                break;
            }
        case 2:
            if (x) {
                posX--;
                break;
            }
        case 3:
            if (w) {
                posX++;
                break;

            }

    }
}

@Override
public String toString() {

    return symbol + " " + posX + " " + posY;
}

}

Class Dungeon 类地牢

package dungeon;

import java.util.ArrayList;
import java.util.List;

public class Dungeon {

private Player player;
private List<Vampire> vampires = new ArrayList<Vampire>();
private int width;
private int height;
private int BP; // lamp battery point
private boolean canVampireMove;

public Dungeon(int width, int height, int vampires, int moves, boolean vampiresMove) {

    this.player = new Player("@", width, height);
    this.width = width;
    this.height = height;
    this.BP = moves;
    this.canVampireMove = vampiresMove;

    for (int i = 0; i < vampires; i++) {
        this.vampires.add(new Vampire("v", width, height));
    }

}


public void printCoordinate() {
    System.out.println(BP);
    System.out.println("\n" + player);
    for (Vampire each : vampires) {
        System.out.println(each);
    }
}

public void printMap() {

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {


                if (player.getPos().equals(x + " " + y)) {
                    System.out.print(player.getSymbol()); // print "@"


                }
                for (int v = 0; v < vampires.size(); v++) {
                    if (vampires.get(v).getPos().equals(x + " " + y)) {
                        System.out.print(vampires.get(v).getSymbol()); // print "v"

                    }
                }

                System.out.print(".");


        }
        System.out.println();
    }

}
}

Class Main 主班

    package dungeon;

    public class Main {

    public static void main(String[] args) {

        Dungeon d = new Dungeon(5, 5, 1, 14, true); // width of grid, height of grid (grid printed by string "."), vampires, moveRemaining, canVampireMove 

        d.printCoordinate();
        d.printMap();

    }
}

The Output Was 输出是

@ 0 0 (player x, y) @ 0 0(玩家x,y)
v 3 0 (vampire x, y) v 3 0(吸血鬼x,y)

@...v.. @ ...诉。
..... .....
..... .....
..... .....
..... .....

String @ should have replaced the first . 字符串@应该替换了第一个. , and v should replaced the third . v应该替换第三个. .

Expected Output 预期产量

@ 0 0 (player x, y) @ 0 0(玩家x,y)
v 3 0 (vampire x, y) v 3 0(吸血鬼x,y)

@.v.. @ .V ..
..... .....
..... .....
..... .....
..... .....

Problem is that you are printing multiple characters for positions occupied by player and vampire . 问题是您要为playervampire占据的位置打印多个字符。 Your code checks whether the given positions is occupied by player and if it is, prints @ but then it will print the dot anyway. 您的代码检查给定位置是否被player占据,如果是,则打印@但无论如何它将打印点。

You should restrict this so that only one character is printed per position. 您应该对此进行限制,以便每个位置仅打印一个字符。 For example something like this. 例如这样的事情。

if (positions is occupied by player) {
    print player
} else if (position is occupied by a vampire) {
    print vampire
} else {
    else print dot
}

There is better way to do this like two dimensional array and hashmap. 有更好的方法来执行此操作,例如二维数组和哈希图。 But this is quick fix. 但这是快速修复。 Did not test it but it should work. 没有测试,但它应该工作。

public void printMap() { 公共无效printMap(){

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {


                if (player.getPos().equals(x + " " + y)) {
                    System.out.print(player.getSymbol()); // print "@"


                } else if {
                    for (int v = 0; v < vampires.size(); v++) {
                        if (vampires.get(v).getPos().equals(x + " " + y)) {
                            System.out.print(vampires.get(v).getSymbol()); // print "v"

                    }
                }
               } else {
                    System.out.print(".");


        }
        System.out.println();
    }

}
}

I have solved this by creating a boolean method. 我已经通过创建布尔方法解决了这个问题。

public void printMap() {

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {

            if (player.getPos().equals(x + " " + y)) {
                System.out.print(player.getSymbol());

            } else if (getVampires(x, y)) {
                System.out.print("v");

            } else {
                System.out.print(".");
            }
        }
        System.out.println();
    }

}

public boolean getVampires(int x, int y) {
    for (int v = 0; v < vampires.size(); v++) {
        if (vampires.get(v).getPos().equals(x + " " + y)) {
            return true;
        }
    }

    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM