在嵌套for循环内,打印字符串“。”并在满足条件时将其替换为其他字符。 MOOC Java Week 10'Dungeon'

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在嵌套for循环内,打印字符串“。”并在满足条件时将其替换为其他字符。 MOOC Java Week 10'Dungeon'相关的知识,希望对你有一定的参考价值。

我正在研究这个项目MOOC Java Week 10 Exercise 33

当玩家/吸血鬼位置X和Y等于网格的X和Y时,我希望String .被玩家/吸血鬼符号(String @v)取代。 (网格的高度和宽度由字符串.打印)。问题出在类Dungeon,方法printMap()

班级球员

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

}

吸血鬼类

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

}

类地牢

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

}
}

班级主要

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

    }
}

输出是

@ 0 0(球员x,y) v 3 0(吸血鬼x,y)

@ ... in .. ..... ..... ..... .....

字符串@应该已经取代了第一个.,而v应该取代第三个.

预期产出

@ 0 0(球员x,y) v 3 0(吸血鬼x,y)

@ .v .. ..... ..... ..... .....

答案

问题是你要为playervampire占据的位置打印多个字符。你的代码检查给定的位置是否被player占用,如果是,打印@但是它仍会打印点。

您应该对此进行限制,以便每个位置只打印一个字符。例如像这样的东西。

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

有更好的方法来做这个像二维数组和hashmap。但这是快速解决方案。没有测试它,但它应该工作。

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 "@"


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

}
}
另一答案

我通过创建一个布尔方法解决了这个问题。

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

以上是关于在嵌套for循环内,打印字符串“。”并在满足条件时将其替换为其他字符。 MOOC Java Week 10'Dungeon'的主要内容,如果未能解决你的问题,请参考以下文章

JAVA里 FOR循环内 IF 与 ELSE的嵌套使用

for 循环与嵌套

循环练习编程题

for 语句执行顺序

0125 双重for循环:语法执行思路打印m行n列打印倒三角形正三角九九乘法表

如果满足条件,则移动到嵌套 for 循环中的下一次迭代