困惑的学生编码

时间:2019-12-12 22:51:02

标签: java

对于Java编码类中的最终项目,我们告诉他们制作文本视频游戏。我们有能力做到这一切,现在我们只需要帮助,因为我们的游戏淘汰了具有相同生命值并受到相同伤害的怪物和玩家。有关如何解决此问题的任何想法?感谢高级的帮助!

package finalproject;
import java.util.Scanner;
import java.util.Random;

/**
 *
 * @author sternker31
 */
public class Main {




/**
 *
 * @author NightingVE08
 */
static boolean playerDead = false;
public static void main(String[] args) {
        System.out.println("Welcome to the game");
        //file io for high score shenanigans
        int counter = 0;
       '''
        player randomplayer = new player();
        //boolean playerDead = false;
        System.out.println("Players health is at start: " + randomplayer.getHealth());
        while (counter < 10 & playerDead == false){
          decisionMethod(randomplayer);
          counter++;
        }

    if(playerDead){
    System.out.println("You have died");}
    else {
    BossMethod(randomplayer); 

    if (playerDead)
    {
        System.out.println("You have died");
    }
    else{
        System.out.println("Hello");    //print player has won and set high score
    }

    }//end main method
}
    public static void decisionMethod(player p){
        Random rng = new Random();

        int random = rng.nextInt(5)+ 1;//generate random number here
        //System.out.println(random);
        if(random == 1 | random == 2)
        {
            System.out.println("You keep walking...");
        }
        else if (random == 3 | random == 4){
            System.out.println("You encountered a monster!");
           engageFight(p);      
        }
        else if (random == 5){
            System.out.println("A treasure chest appeared");
            int i = TreasureChest();
        }
    }



    public static player engageFight(player p){

    Scanner sc = new Scanner(System.in);
    monster randommonster = new monster();
    int monsterhp = randommonster.getHealth();
    int sleepCounter = 0;
    int test = 0;

    while (monsterhp > 0 & playerDead == false) {

        System.out.println("Monster’s health is " + randommonster.getHealth());
        System.out.println("Player’s health is " + p.getHealth());

    if(randommonster.sleep){
        sleepCounter++;
        }
        else{
            System.out.println("Choose your attack");
            System.out.println("1. Basic Attack");
            System.out.println("2. Sleep Attack, 50% chance to make target go to bed");
        }

     test = sc.nextInt();  


    if (test == 1){
        System.out.println("Basic attack was chosen");
        System.out.println("Player attacked!");
    randommonster.setHealth(randommonster.getHealth() - p.basicAttack());
    }

    if (test == 2){
        System.out.println("Sleep attack was chosen");
        if(p.sleepChance()) {
            randommonster.setSleep(true); 
            randommonster.setHealth(randommonster.getHealth() - p.sleepAttack()); 
        }
        else {
    randommonster.setHealth(randommonster.getHealth() - p.sleepAttack()); 
        }
    }


    if (sleepCounter == 1) {
    System.out.println ("The monster is awake");
    sleepCounter = 0;
    }

    if(!randommonster.getSleep()){
        System.out.println("The monster attacked!");
    p.setHealth(p.getHealth() - randommonster.basicAttack());  
    }
    else{
        System.out.println("The monster fell asleep.");
    sleepCounter++;
    }

    if (p.getHealth() <= 0) {
    playerDead = true;
    }     
    }//end while loop

    return p;

    }

此图像已输出,因为您可以看到怪物和玩家的健康状况相同。  :(

enter image description here

生物代码(超类)

package finalproject;
import java.util.Random;
/**
 *
 * @author sternker31
 */
    public class creature {
    protected static int Health;
    protected static int MaxAttackPoints;
    Random rng = new Random();

    public static void setHealth(int h)
    {
        Health = h;
    }
    public static int getHealth()
    {
        return Health;
    }
    public static void setMaxAttackPoints(int ap)
    {
        MaxAttackPoints = ap;
    }
    public static int getMaxAttackPoints()
    {
        return MaxAttackPoints;
    }

    public int basicAttack()
    {
        int basicAttack = rng.nextInt(MaxAttackPoints) + 1;
        System.out.println(basicAttack);
        return basicAttack;
    }

}//end class 

怪物代码

package finalproject;

/**
 *
 * @author sternker31
 */
public class monster extends creature{
    public static boolean sleep = false;
    public monster(){
      Health = 15;
      MaxAttackPoints = 5;
    }
public static void setSleep(boolean s)
    {
        boolean sleep = s;
    }        
    public static boolean getSleep()
    {
        return sleep;
    }

}//end class monster

玩家代码

package finalproject;

/**
 *
 * @author sternker31
 */
public class player extends creature{

    public player(){
    Health = 80;
    MaxAttackPoints = 20;
    }

    public int sleepAttack(){
        int sleepAttack = 5;
        System.out.println(sleepAttack);
        return sleepAttack;
    }

    public boolean sleepChance(){
      boolean sleep = false;
      int x = rng.nextInt(1);
      System.out.println(x);
      if (x == 0){
          sleep = false; 
      }
      else if (x == 1){
          sleep = true;
      }
      return sleep;
    }
}//end class player

0 个答案:

没有答案