对于我所在的课程,我们有一个程序要写,要求玩家猜测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;
}
}
--------------------------------------- AND ------- ------------------------------------------
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!”并在每次猜到第二个宝藏的坐标时将其更改为#away。谢谢你的帮助!
答案 0 :(得分:3)
尝试这样的事情(这与你的初始帖子差不多)。你需要更新你的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();
}
}
答案 1 :(得分:0)
我认为你可以使用一个标志(int值或boolean)值。并且您可以根据找到的宝藏状态或距离改变距离来更改旗帜的值。
我认为您可以声明一个变量,如
int flag=0;
一旦你发现宝藏标志= 1;
if( flag=0)
{
String pout="write distance value here"
}
if(flag==1)
{
String pout="found !!";
}
答案 2 :(得分:0)
你真的需要让found
成为treasureStatus方法中的永久停止点。现在你有一个"发现"变量,但除非你在代码中使它成为一个不可改变的停止点,否则它对你没有任何好处。
在宝藏状态方法中有类似的东西...
public int treasureStatus(int xStick , int yStick){
if ((xStick == xLocation && yStick == yLocation) || found == true){
return 0;
} else if ...
这将使方法if found == true
每次只需return 0
,将Treasure标记为已找到。
(作为一个注释,第一次回答Stack Overflow问题,我为我可怕的格式化道歉。)