2017-06-07 64 views
1
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 

在此代碼後,「設置」的價值觀,我設置對象的值,然後把它們放到叫做的Java如何恢復被改變

敵人的數組。之後,我使用敵人陣列更改對象元素的值。使用敵人數組。在元素陣列的「HP」值達到零之後,它將

轉移到下一個元素上。我把它使用戶可以返回到前一個元素

,並重新做,但該元素仍然有當用戶第一次

改變他們爲相同的值。我把所有最初定義

值到一個方法,並試圖調用它,所以它可以重置「設置」和敵人數組,但我不是

運氣。有什麼辦法可以正確地做到這一點?

編輯:

對於那些想知道,這是我的便宜解決方法:

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

您需要不可變的數據結構,以便您可以快照您的狀態! – Sikorski

回答

1

陣列也沒有任何收藏有UNDO方法,你需要設計你自己的,你保持最後的狀態的數據容器,並在需要時按需恢復它

+0

我該如何製作這個容器? –

+1

紀念圖案! –

+0

@RaminAmiri您需要兩個集合,一個「當前」和一個「舊」。當有人對當前進行更改時,首先更新舊收藏。 –

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

而這僅僅是通過掠中的第一功能,檢查剩下的讓更少的意義。對不起,如果你有更具體的問題,我會很樂意回答。

+0

是的,在我提供的代碼的這一點是當我絕望。不介意這一點,我已經知道這是行不通的。我發現了一個不同的解決方案,即在for循環之後複製並粘貼所有套牌和敵人,以便自動重置。它便宜,但有效。 –