对于我上的一堂课,我们有一个编写程序,要求玩家猜测x和y坐标,直到找到宝藏为止。该项目包含2个文件,分别称为Treasure.java和TreasureHunt.java,它们使用Treasure类玩游戏。到目前为止,我的代码是...
package treasurehunt;
public class Treasure {
public int POSITIONS_PER_ROW = 20;
public int MAX_DISTANCE = 401;
private String name;
private int xLocation;
private int yLocation;
private Boolean found;
public Treasure(){
name = "";
xLocation = 0;
yLocation = 0;
found = false;
}
public Treasure(String name){
this.name = name;
xLocation = 0;
yLocation = 0;
found = false;
}
public int getXLocation(){
return xLocation;
}
public int getYLocation(){
return yLocation;
}
public boolean isFound(){
return found;
}
public void hideTreasure(){
xLocation = 1+(int)(Math.random()*POSITIONS_PER_ROW);
yLocation = 1+(int)(Math.random()*POSITIONS_PER_ROW);
}
public int treasureStatus(int xStick , int yStick){
if (xStick == xLocation && yStick == yLocation){
return 0;
} else if (xStick != xLocation || yStick != yLocation) {
return Math.abs((yStick-yLocation)+(xStick-xLocation));
} else return MAX_DISTANCE;
}
}
- - - - - - - - - - - - - - - - - - - -和 - - - - - ---------------------------------------
package treasurehunt;
import java.util.Scanner;
public class TreasureHunt {
public static final int MAX_DISTANCE = 401;
public static final int POSITIONS_PER_ROW = 20;
public static void main(String[] args) {
Treasure gold = new Treasure();
Treasure diamond = new Treasure();
char playAgain = 0;
gold.hideTreasure();
diamond.hideTreasure();
Scanner keyboard = new Scanner(System.in);
System.out.println("Treasures have been hidden.");
do{
System.out.println("");
System.out.print("Enter x and y coordinates to search: ");
int xStick = keyboard.nextInt();
int yStick = keyboard.nextInt();
int dist1 = diamond.treasureStatus(xStick, yStick);
int dist2 = gold.treasureStatus(xStick , yStick);
if (dist1 == 0 || dist1 == MAX_DISTANCE){
System.out.println("Diamonds: FOUND!");
}else{
System.out.println("Diamonds: " + dist1 + " away");
}
if (dist2 == 0 || dist2 == MAX_DISTANCE){
System.out.println("Gold: FOUND!");
}else{
System.out.println("Gold: " + dist2 + " away");
}
}while(!diamond.isFound() && !gold.isFound());
do{
System.out.println("Play again? y/n:");
playAgain = keyboard.next().charAt(0);
}while(diamond.isFound() && gold.isFound());
}
}
当程序正确运行时,应该看起来像这样。
Treasures have been hidden.
Enter x and y coordinates to search: 17 11
Diamonds: 2 away
Gold: 9 away
Enter x and y coordinates to search: 17 13
Diamonds: FOUND!
Gold: 7 away
Enter x and y coordinates to search: 21 13
Diamonds: FOUND!
Gold: 9 away
Enter x and y coordinates to search: 17 16
Diamonds: FOUND!
Gold: 4 away
Enter x and y coordinates to search: 17 19
Diamonds: FOUND!
Gold: 1 away
Enter x and y coordinates to search: 18 19
Diamonds: FOUND!
Gold: FOUND!
It took you 6 tries to find 2 treasures.
Play again? y/n: n
我的问题是,一旦您找到了第一宝藏,便如何防止其改变“发现!”当您猜测第二个宝藏的坐标时找不到。我现在拥有的代码删除了“ FOUND!”。并在您每次猜出第二个宝藏的坐标时将其更改为#。谢谢你的帮助!
最佳答案
尝试类似的事情(与您最初的帖子几乎一样)。您需要更新Treasure类,添加这样的setFound方法并按如下所示更改hideTreasure方法。
// Set's the found
public void setFound(boolean found) {
this.found = found;
}
// Setting found to false on hide!
public void hideTreasure() {
this.found = false;
xLocation = 1 + (int) (Math.random() * POSITIONS_PER_ROW);
yLocation = 1 + (int) (Math.random() * POSITIONS_PER_ROW);
}
// Finally update the main method to use the changes above.
public static void main(String[] args) {
Treasure gold = new Treasure();
Treasure diamond = new Treasure();
char playAgain = 0;
gold.hideTreasure();
diamond.hideTreasure();
Scanner keyboard = new Scanner(System.in);
System.out.println("Treasures have been hidden.");
try {
do {
System.out.println("");
System.out
.print("Enter x and y coordinates to search: ");
int xStick = keyboard.nextInt();
int yStick = keyboard.nextInt();
if (!diamond.isFound()) {
int dist1 = diamond.treasureStatus(xStick,
yStick);
if (dist1 == 0 || dist1 == MAX_DISTANCE) {
diamond.setFound(true);
System.out.println("Diamonds: FOUND!");
} else {
System.out.println("Diamonds: " + dist1
+ " away");
}
}
if (!gold.isFound()) {
int dist2 = gold.treasureStatus(xStick,
yStick);
if (dist2 == 0 || dist2 == MAX_DISTANCE) {
gold.setFound(true);
System.out.println("Gold: FOUND!");
} else {
System.out.println("Gold: " + dist2
+ " away");
}
}
if (diamond.isFound() && gold.isFound()) {
System.out.println("Play again? y/n:");
playAgain = keyboard.next().charAt(0);
if (playAgain != 'Y' && playAgain != 'y') {
System.exit(0);
} else {
diamond.hideTreasure();
gold.hideTreasure();
}
}
} while (true);
} finally {
keyboard.close();
}
}