public static void reset() {
//goblin
stats gob = new stats();
gob.setName("Goblin");
gob.setHP(15);
gob.setATK(3);
//END-goblin
//orc
stats orc = new stats();
orc.setName("Orc");
orc.setHP(25);
orc.setATK(5);
//END-orc
stats[] enemies = new stats[3];
enemies[1] = new stats();
enemies[1] = gob;
enemies[1].getName();
enemies[1].getHP();
enemies[1].getATK();
enemies[2] = new stats();
enemies[2] = orc;
enemies[2].getName();
enemies[2].getHP();
enemies[2].getATK();
}
public static void fight() {
//user
player user = new player();
user.setP_Name(username);
user.setP_ATK(atk);
user.setP_HP(hp);
//END-user
//goblin
stats gob = new stats();
gob.setName("Goblin");
gob.setHP(15);
gob.setATK(3);
//END-goblin
//orc
stats orc = new stats();
orc.setName("Orc");
orc.setHP(25);
orc.setATK(5);
//END-orc
stats[] enemies = new stats[3];
enemies[1] = new stats();
enemies[1] = gob;
enemies[1].getName();
enemies[1].getHP();
enemies[1].getATK();
enemies[2] = new stats();
enemies[2] = orc;
enemies[2].getName();
enemies[2].getHP();
enemies[2].getATK();
System.out.println("\u001B[32m" + "\nWelcome to Gilenor, " + user.getP_Name() + "." + "\u001B[39m"
+ "\u001B[32m" + "\nIn this world, you will fight monsters consecutively," + "\u001B[39m"
+ "\u001B[32m" + "\nattempting to get through all of them." + "\u001B[39m"
+ "\u001B[32m" + "\nFor each enemy you kill, you will grow stronger." + "\u001B[39m"
+ "\u001B[32m" + "\nFlee to go back to the past enemy, but do not flee on your first enemy!" + "\u001B[39m"
+ "\u001B[32m" + "\nA fair warning to you, " + user.getP_Name() + ":" + "\u001B[39m"
+ "\u001B[32m" + "\nEach monster gets stronger after every win. Good luck!" + "\u001B[39m");
int answer = 0;
int d;
int e;
boolean alive;
int i;
for (i = 1; i < 3; i++) {
System.out.print("\n");
System.out.println(enemies[i].getName() + " has appeared!");
System.out.print("\n");
alive = true;
while (alive == true) {
reset();
System.out.println(enemies[i].getName() + " stats:");
System.out.println("Hitpoints[HP]: " + enemies[i].getHP());
System.out.println("Attack[ATK] : " + enemies[i].getATK());
System.out.print("\n");
if (user.getP_HP() > 0) {
System.out.println("\u001B[34m" + "[1 - Attack]" + "\u001B[39m");
System.out.println("\u001B[34m" + "[2 - Flee]" + "\u001B[39m");
answer = In.getInt();
clrscr();
System.out.print("\n");
} else if (user.getP_HP() <= 0) {
System.out.println(user.getP_Name() + " has been slain by the " + enemies[i].getName() + "!");
return;
}
if (answer == 1) {
d = enemies[i].getHP() - user.getP_ATK();
enemies[i].setHP(d);
System.out.println(user.getP_Name() + " strikes the " + enemies[i].getName() + " for " + user.getP_ATK() + " damage.");
System.out.println(enemies[i].getName() + " has " + enemies[i].getHP() + " HP remaining.");
System.out.print("\n");
if (enemies[i].getHP() > 0) {
e = user.getP_HP() - enemies[i].getATK();
user.setP_HP(e);
System.out.println("The " + enemies[i].getName() + " attacked " + user.getP_Name() + "for " + enemies[i].getATK() + " damage.");
System.out.println(user.getP_Name() + " has " + user.getP_HP() + " HP remaining.");
System.out.println("\n");
} else if (enemies[1].getHP() <= 0) {
System.out.println("\u001B[32m" + user.getP_Name() + " has slain the " + enemies[i].getName() + "!" + "\u001B[39m"
+ "\u001B[32m" + "\nYou have gained " + (i * 1) + " attack and " + i * 10 + " HP!" + "\u001B[39m");
user.setP_ATK(user.getP_ATK() + (i * 1));
user.setP_HP(user.getP_HP() + (i * 10));
alive = false;
}
} else if (answer == 2) {
if (i == 1) {
System.out.println("You ran from your first enemy, tripped, then died! How embarassing!");
alive = false;
return;
} else {
System.out.println("You ran away, back to the last monster you saw!");
System.out.print("\n");
i = (i - 1);
}
}
}
}
} //fight method
在这段代码中,我为对象设置值,然后将它们放入一个名为
的数组中敌人。然后,我使用
更改对象元素的值敌人阵列。在&#34; HP&#34;之后元素数组的值达到零,它变为
到下一个元素。我做到了这样,用户可以回到上一个元素
并重新执行此操作,但该元素仍具有与用户第一个
时相同的值改变了他们。我把所有的&#34;设置&#34;和最初定义的敌人阵列
将值转换为方法并尝试调用它以便重置,但我不是
运气。有什么方法可以做到这一点吗?
修改
对于那些想知道的人,这是我的廉价解决方法:
public static void fight() {
//user
player user = new player();
user.setP_Name(username);
user.setP_ATK(atk);
user.setP_HP(hp);
//END-user
System.out.println("\u001B[32m" + "\nWelcome to Gilenor, " + user.getP_Name() + "." + "\u001B[39m"
+ "\u001B[32m" + "\nIn this world, you will fight monsters consecutively," + "\u001B[39m"
+ "\u001B[32m" + "\nattempting to get through all of them." + "\u001B[39m"
+ "\u001B[32m" + "\nFor each enemy you kill, you will grow stronger." + "\u001B[39m"
+ "\u001B[32m" + "\nFlee to go back to the past enemy, but do not flee on your first enemy!" + "\u001B[39m"
+ "\u001B[32m" + "\nA fair warning to you, " + user.getP_Name() + ":" + "\u001B[39m"
+ "\u001B[32m" + "\nEach monster gets stronger after every win. Good luck!" + "\u001B[39m");
int answer = 0;
int d;
int e;
boolean alive;
int i;
for (i = 1; i < 6; i++) { //CHEAP WORKAROUND
//goblin1
stats gob = new stats();
gob.setName("Goblin");
gob.setHP(15);
gob.setATK(3);
//END-goblin
//orc2
stats orc = new stats();
orc.setName("Orc");
orc.setHP(25);
orc.setATK(5);
//END-orc
//pheonix3
stats pheonix = new stats();
pheonix.setName("Pheonix");
pheonix.setHP(75);
pheonix.setATK(5);
//END-pheonix
//ancient dragon4
stats ancdr = new stats();
ancdr.setName("Ancient Dragon");
ancdr.setHP(220);
ancdr.setATK(13);
//END-ancient dragon
//hades5
stats hades = new stats();
hades.setName("Hades");
hades.setHP(500);
hades.setATK(25);
//END-hades
stats[] enemies = new stats[6];
enemies[1] = new stats();
enemies[2] = new stats();
enemies[3] = new stats();
enemies[1] = gob;
enemies[2] = orc;
enemies[3] = pheonix;
enemies[4] = ancdr;
enemies[5] = hades;
enemies[1].getName();
enemies[2].getName();
enemies[3].getName();
enemies[4].getName();
enemies[5].getName();
enemies[1].getHP();
enemies[2].getHP();
enemies[3].getHP();
enemies[4].getHP();
enemies[5].getHP();
enemies[1].getATK();
enemies[2].getATK();
enemies[3].getATK();
enemies[4].getATK();
enemies[5].getATK(); //CHEAP WORKAROUND
System.out.print("\n");
System.out.println(enemies[i].getName() + " has appeared!");
System.out.print("\n");
alive = true;
while (alive == true) {
System.out.println(enemies[i].getName() + " stats:");
System.out.println("Hitpoints[HP]: " + enemies[i].getHP());
System.out.println("Attack[ATK] : " + enemies[i].getATK());
System.out.print("\n");
if (user.getP_HP() > 0) {
System.out.println("\u001B[34m" + "[1 - Attack]" + "\u001B[39m");
System.out.println("\u001B[34m" + "[2 - Flee]" + "\u001B[39m");
answer = In.getInt();
clrscr();
System.out.print("\n");
} else if (user.getP_HP() <= 0) {
System.out.println(user.getP_Name() + " has been slain by the " + enemies[i].getName() + "!");
return;
}
if (answer == 1) {
d = enemies[i].getHP() - user.getP_ATK();
enemies[i].setHP(d);
System.out.println(user.getP_Name() + " strikes the " + enemies[i].getName() + " for " + user.getP_ATK() + " damage.");
System.out.println(enemies[i].getName() + " has " + enemies[i].getHP() + " HP remaining.");
System.out.print("\n");
if (enemies[i].getHP() > 0) {
e = user.getP_HP() - enemies[i].getATK();
user.setP_HP(e);
System.out.println("The " + enemies[i].getName() + " attacked " + user.getP_Name() + " for " + enemies[i].getATK() + " damage.");
System.out.println(user.getP_Name() + " has " + user.getP_HP() + " HP remaining.");
System.out.println("\n");
} else if (enemies[1].getHP() <= 0) {
System.out.println("\u001B[32m" + user.getP_Name() + " has slain the " + enemies[i].getName() + "!" + "\u001B[39m"
+ "\u001B[32m" + "\nYou have gained " + (i * 1) + " attack and " + i * 10 + " HP!" + "\u001B[39m");
user.setP_ATK(user.getP_ATK() + (i * 1));
user.setP_HP(user.getP_HP() + (i * 10));
alive = false;
}
} else if (answer == 2) {
if (i == 1) {
System.out.println("You ran from your first enemy, tripped, then died! How embarassing!");
alive = false;
return;
} else {
System.out.println("You ran away, back to the last monster you saw!");
System.out.print("\n");
i = (i - 1);
}
}
}
}
} //fight method
答案 0 :(得分:1)
数组或任何集合都有一个UNDO方法,你需要设计自己的数据容器的最后状态,并在需要时根据需要恢复它
答案 1 :(得分:0)
这里有很多事情没有意义:
在重置功能中:
// This stats[] array is local, it won't effect the one out side of the function...
stats[] enemies = new stats[3]; // Why size 3 if only use 2? first index is 0.
enemies[1] = new stats(); // This new stats instance will be override next line, so it have no effect.
enemies[1] = gob; // here you share reference to the gob instance, not copying it. so any change on 'enemies[1]' will in fact be apply on gob.
enemies[1].getName(); // Do nothing...
enemies[1].getHP(); // Do nothing...
enemies[1].getATK(); // Do nothing...
enemies[2] = new stats();
enemies[2] = orc;
enemies[2].getName();
enemies[2].getHP();
enemies[2].getATK();
这只是通过浏览第一个函数,检查其余功能甚至更少。抱歉,如果你有更具体的问题,我很乐意回答。