java - 在嵌套的 for 循环内,打印字符串“。” 如果满足条件,则将其替换为其他字符。MOOC Java 第 10 周“地牢”
问题描述
我正在做这个项目 MOOC Java 第 10 周练习 33
当玩家/吸血鬼位置 X 和 Y 等于 Grid 的 X 和 Y 时,我希望将字符串.
替换为玩家/吸血鬼符号(字符串@
和)。v
(网格的高度和宽度由字符串打印.
)。问题出在 Class Dungeon
, methodprintMap()
中。
类播放器
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("\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();
}
}
}
主类
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)
@...v
..
.....
.....
.....
String@
应该已经替换了第一个.
,并且v
应该替换了第三个.
。
预期产出
@ 0 0 (玩家 x, y)
v 3 0 (吸血鬼 x, y)
@ .v
…………………………………………
_
_
解决方案
有更好的方法来做到这一点,比如二维数组和哈希图。但这是快速修复。没有测试它,但它应该工作。
公共无效打印地图(){
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();
}
}
}
推荐阅读
- java - JGit - Re-order/remove commits in Master branch before push to Upstream
- android - 使用 RecyclerView.Adapter 项目单击的内容动态加载片段
- c# - 为什么服务中的 File.ReadAllText 会将其目录重置为“C:\Windows\System32”?
- macos - 容器一次又一次重启,无法停止/删除/杀死
- node.js - 在 SDK V4 中使用 Microsoft Bot Framework 创建 Alexa Bot
- java - Java中的内在锁和同步
- php - 带有php的MongoDB无法创建会话
- django - 如何使用 Django 将字符串添加到自动字段/主键
- php - 如何保护从其他文件夹中删除文件
- java - 线程时钟对象