繁体   English   中英

为什么我的程序不知道敌人和英雄对象存在? [等候接听]

[英]Why doesnt my program know that enemy and Hero object exist? [on hold]

我正在从事地下城爬行游戏。 要求之一是使Character和Enemy类从一个公共基类派生类(或实现一个公共接口)。 该基类将包含两者的共同点,例如位置,能够移动并具有某种在屏幕上显示的方式。 我通过创建一个角色类来做到这一点,该角色类是敌人和英雄的父类。 在执行继承要求之前,我的玩家可以移动并做所有事情,现在它什么也没做,只是跳到成功消息。 我怎样才能解决这个问题? 在底部,我将只发布一半的代码,请告诉我是否正确进行了继承。 如果没有,我该如何解决。

人物类代码:

abstract class Character{

private String name;
private int health;


public Character(String name, int health){
    this.name = name;
    this.health = health;

}
public abstract void damage(int hit);
public abstract String getname();
public abstract boolean isAlive();
public abstract void girlTaunt();
public abstract void boyTaunt();



}

敌人的代码:

import java.util.*;

class Enemy extends Character {

String name;
int health;
Item weapon;

public  Enemy(String name, int health, Item Weapon) {
    super(name, health);
    this.weapon = Weapon;
}

public int health(){
    return this.health;
}

public boolean isAlive() {
    if (this.health <= 0) {
        return false;
    }
    return true;
}

public void damage(int hit){
    this.health -= hit;
}

public int power(){
    return this.weapon.getPower();
    //return health - damage;
}

public String equippedWeapon(){
    return this.weapon.toString();
}//ending bracket for equip

public void girlTaunt(){

    Random rand = new Random();

    int i = rand.nextInt()%3;

    switch (i){
        case 0:
            System.out.println("ARI: ahhh booo wahh wahhh I don't know what to do!!");
            break;
        case 1:
            System.out.println("ARI: i'll fake a pregnancy test...!");
            break;
        case 2:
            System.out.println("ARI: I will CONSENSUALLY  beat the cashews out of you.");
            break;
        default:
            break;

    }
}//end of girlTaunt

public void boyTaunt(){
    Random rand = new Random();
    int i = rand.nextInt()%3;

    switch (i){
        case 0:
            System.out.println("GARRET: Oh no why don't you love me???????");
            break;
        case 1:
            System.out.println("GARRET: You better watch yourself, i'll threaten you with a marriage propsal!!");
            break;
        case 2:
            System.out.println( "GARRET: If I beat you up, my wife's boyfriend said that he'll buy me a nintendo switch");
            break;
        default:
            break;

    }
    System.out.println(" ");
}
public String getname(){
    return name;
}
}

英雄等级:

  import java.util.Scanner;
  import java.io.PrintWriter;

class Hero extends Character{
public int health;
public boolean isalive;
private String name;
private String Class;

Hero(String name, int health, String Class){
    super(name, health);
    this.Class = Class;
    this.health = 100;
    this.isalive = true;
}
public String getname(){
    return name;
}


void save(PrintWriter pw){
    pw.println(name);
    //       Hero.save(pw);
}


public void damage(int hit){
    this.health -= hit;
}

public boolean isAlive(){// Check my parameters and the code in the method.
    if(this.health > 0){
        return true;
    }
    else{
        return false;
    }
}
public void kill(){
    this.health = 0;
}

public void girlTaunt(){
    System.out.println(this.name + ":YOU'RE SO BOGUS! YOU SHOULD BE LIKE ME, A STRONG INDEPDENT SEXY MAN");
}

public void boyTaunt(){
    System.out.println(this.name + "I am SO glad I dumped you. Whew. Dodged a bullet with that one!");
}

public void Soy(){
    for(int i = 0; i < 1; i++){
        System.out.println("Lets do this");
    }
}


}

主类:

 import java.util.*;
 import java.util.ArrayList;
 import java.io.ObjectOutputStream;
 import java.io.ObjectInputStream;
 import java.io.PrintWriter;
 import java.io.FileOutputStream;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileWriter;
 /**
 * a main class that makes a scanner and a inventory object
 * it prints out the options and then asks what the player wants to do
* each option utilizes the methods in the other classes, it quites if you press 6 
* it's in a do while so it never stops unless you wnat to stop
*/
public class Main{
  public static void main(String[] args){
    Scanner bob = new Scanner(System.in);
    Inventory stuff = new Inventory(100000000); 
    Room room = new Room(20,20);
    Room room2 = new Room(20,20);
    Room room3 = new Room(20,20);

    System.out.println(" ");
    System.out.println("                    ~  WELCOME TO ONE ROOM DUNGEON GAME ~");
    System.out.println("      ");
    System.out.println("                 YOU, THE HERO HAS ENTERED THE DEMON'S KINGS ");
    System.out.println("                 CASTLE. SADLY, HIS CASTLE ONLY HAS ONE ROOM"); 
    System.out.println("               BECAUSE HE HAS A SMALL BRAIN AND GOT RIPPED OFF");
    System.out.println("            BY THE MOST FAMOUS AND HANDSOME CON-ARTIST, SUAD PARVEZ.");
    System.out.println("        YOU ARE HERE TO KILL THE DEMON KING, GARRETTE DAYKWON AND ARI.");
    System.out.println("       BECAUSE THEY HAVE STOLEN ALL THE CURRY IN THE LAND OF 'SCHOP SCHOP' ");
    System.out.println("              RESTORE THIS LANDS GLORY AND DEFEAT THE DEMON KING!");
    System.out.println("   ");
    System.out.println("    ");
    System.out.println(" How to play:");
    System.out.println("              You, the Hero, will be represented on the board by the '+' symbol.");
    System.out.println("              Your enemy, the Demon King Garret, will be represented by the ':(' symbol.");
    System.out.println("              He is known for being thirsty and pacing back and forth. Watch out, he is tough as fudge");
    System.out.println("              Ari THE WOMAN will be represented by the '?' symbol.");   
    System.out.println("              She has gone crazy and roams aimlessly around the room, you may need to chase her.");
    System.out.println("       ");  
    System.out.println("              Move throughout the board collecting as many items '<$>' as you can to defeat the game.");
    System.out.println("              If you do happen to run into either enemy, you must dual it out to see who wins!");
    System.out.println("              Preferably you please, bob already went in there and we have no idea where he is..");
    System.out.println("    ");
    System.out.println("                                 Press 'w' to move UP. ");
    System.out.println("                                 Press 's' to move DOWN. ");
    System.out.println("                                 Press 'a' to move LEFT. ");
    System.out.println("                                 Press 'd' to move RIGHT. ");
    System.out.println("                                 Press 'i' to access inventroy ");
    System.out.println("                                 Press 'q' to activate not alive mode");
    System.out.println("  ");

    System.out.println(" ");
    System.out.println("                       WHAT IS YOUR NAME HERO??");
    System.out.println(" ");
    System.out.print("                                  ");
    String Name = bob.next();

    System.out.println(" ");
    System.out.println("                          WHAT IS YOUR CLASS??");
    System.out.println("              (FratBoi)  (JucciBoi)  (NerdBoi)  (Arsalan) ");
    System.out.println(" ");
    System.out.print("                                    ");
    String Class = bob.next();

    Hero bro = new Hero(Name, 100, Class);
    stuff.noobGear();//to give the hero a default gear weapon and armour


    System.out.println(" ");
    System.out.println("             Remember " + bro.getname() + ", to see the instrucitons, press 'p'.");
    System.out.println(" ");
    System.out.println("           Now remember not to ask any questions becuase this is a video game");
    System.out.println("   ");
    System.out.println("   ");

    Enemy Garret =new Enemy("Garret THE DEMON KING", 200, new Item(ItemType.weapon,"BasketBall", 4, 70, 70));//makes the enemy garret and gives him a weapon
    Enemy Ari = new Enemy("Ari THE WOMAN", 200, new Item(ItemType.weapon,"Cellphone", 345,543,60));//creates the enemy ari and gives her a weapon

    String input = " ";//declares and initializes the input variable

    //fully creates the rooms
    room.create();
    room2.create2();
    room3.create3();


    while(bro.isAlive()){//keeps going until player is dead 
        System.out.println(" ");

        //prints out board depending on location
        if(room.isHere()){//if in the first room
            room.print();//prints out the room
        }//end of if
        else if(room2.isHere()){//if in the second room
            if(Ari.isAlive()){//checks to see if ari is alive
                room2.printA();//if so print out room with her in it
            }
            else{
                room2.print();//if not, print out room without her cuz she dead
            }
        }//end of else if
        else if(room3.isHere()){//if in the third room
            if(Garret.isAlive()){//check to see if garret is alive
                room3.printG();//if so print out the room wit hhim in it
            }
            else{
                room3.print();//if not print out room without him cuz he dead
            }
        }//end of else if

        System.out.println(" ");
        System.out.println("              ");
        System.out.print("                      ");
        input = bob.next();//input from user

        //if statment for movment 
        if(input.equals("w") || input.equals("a") || input.equals("s") || input.equals("d")){
            room.MOVE(input);//to move the character in the room

            if(room3.isHere() && room3.garretFight() && Garret.isAlive()){
                System.out.println(" WHAT? YOUR FACING " + Garret.getname() + "!! YOU BETTER WATCH OUT.");
                System.out.println(" HE WAS LOOKING AT YOU THROUGH HIS TELESCOPE BEFORE YOU CAME HERE!");
                System.out.println(" AND HE LIKED WHAT HE SAW. PROBABLY ENOUGH TO WANT A BABY WITH YOU!");
                System.out.println(" DON'T ASK HOW I KNOW THAT WE DON'T HAVE TIME");
                System.out.println(" ");
                while(bro.isAlive() && Garret.isAlive()){
                    //enemy damage - the armor is the damage the enemy can deal
                    int enemyhit = Garret.power() - stuff.protection();
                    //your weapons damage is the damage you can do
                    int brohit = stuff.power();
                    System.out.println("Press 'k' to attack");
                    String attack = bob.next();
                    if(attack.equals("k")){
                        //the enemy gets damaged by the hero's hit
                        Garret.damage(brohit);//the hero's hit is in the parameter
                        System.out.println(" ");
                        System.out.println("You attacked Garret with your " + stuff.currentWeapon() + ". You did " + brohit + " damage to him!");
                        System.out.println(" His health is now " + Garret.health());
                        System.out.println(" ");
                    }
                    else{
                        System.out.println(" ");
                        System.out.println("your so dumb that wasn't the letter k");
                        System.out.println("you deserve to perish!");
                        System.out.println(" ");
                        bro.kill();
                    }
                    if(Garret.isAlive() && bro.isAlive()){
                        bro.damage(enemyhit);
                        System.out.println(" ");
                        System.out.println("Garret hit you with his " + Garret.equippedWeapon() + ". It did " + enemyhit + " damage!");
                        System.out.println(" ");
                    }
                }//end of while fight
                if(!bro.isAlive()){
                    System.out.println(" ");
                    System.out.println(bro.getname() + "!?!?!?! YOU CAN'T DIE NOW!!!");
                    for(int i = 0 ; i < 6; i++){
                        System.out.println("AAAAAAHHHHHHHHHHHHHHH");
                    }
                    Garret.boyTaunt();
                }//end of losing message
                if(bro.isAlive()){
                    System.out.println(" ");
                    System.out.println(" You have defeated " + Garret.getname() + "!!!!!");
                    System.out.println(" ");
                    bro.boyTaunt();
                }
            }//end of if for garret interaction  interaction

            if(room2.isHere() && room2.ariFight() && Ari.isAlive()){
                System.out.println(" ");
                System.out.println(" HOLY COW IT'S " + Ari.getname() + "!! SHE IS WELL KNOWN FOR COMPLAINING");
                System.out.println(" NO MAN CAN HANDLE THE PURE TEENAGE DRAMA SHE IS CAPABLE OF SUMMONING");
                System.out.println(" IF YOU DON'T BEAT HER QUICK SHE'LL DROWN YOU IN BORING SPEECHES");
                System.out.println(" ");
                while(bro.isAlive() && Ari.isAlive()){
                    //enemy damage - the armor is the damage the enemy can deal
                    int enemyhit = Ari.power() - stuff.protection();
                    //your weapons damage is the damage you can do
                    int brohit = stuff.power();
                    System.out.println(" ");
                    System.out.println("Press 'k' to attack");
                    System.out.println(" ");
                    String attack = bob.next();
                    if(attack.equals("k")){
                        //the enemy gets damaged by the hero's hit
                        Ari.damage(brohit);//the hero's hit is in the parameter
                        System.out.println(" ");
                        System.out.println("You attacked " + Ari.getname() + "  with your " + stuff.currentWeapon() + ". You did " + brohit + " damage to her!");
                        System.out.println(" Her health is now " + Ari.health());
                        System.out.println(" ");
                    }
                    else{
                        System.out.println(" ");
                        System.out.println("your so dumb that wasn't the letter k");
                        System.out.println("you deserve to perish");
                        System.out.println(" ");
                        bro.kill();
                    }
                    if(Ari.isAlive()&& bro.isAlive()){
                        bro.damage(enemyhit);
                        System.out.println(" ");
                        System.out.println("Ari hit you with her " + Ari.equippedWeapon() + ". It did " + enemyhit + " damage!");
                        Ari.girlTaunt();
                        System.out.println(" ");
                    }
                    System.out.println(" ");
                    bro.girlTaunt();
                }//end of while fight
                if(!bro.isAlive()){
                    System.out.println(" ");
                    System.out.println(bro.getname() + "!?!?!?! YOU CAN'T DIE NOW!!!");
                    for(int i = 0 ; i < 8; i++){
                        System.out.println("AAAAAAHHHHHHHHHHHHHHH");
                    }
                }//end of losing message
                if(bro.isAlive()){
                    System.out.println(" ");
                    System.out.println(" You have defeated " + Ari.getname() + "!!!!!");
                    System.out.println(" ");
                    bro.girlTaunt();
                }
            }//end of if for ari fight interaction

            if(Garret.isAlive()){//if garret is alive, make him move
                room3.enemymoveGarret();
            }
            if(Ari.isAlive()){//if ari is alive , make her move
                room2.enemymoveAri();
            }   
        }//end of if for movement

        //if statment to see if there is a door or staris leading to other area
        if(room.isHere()){//if the player is in the first room
            if(room.onDoor()){//and they are at a door
                System.out.println(" ");
                System.out.println("            NANI?!?!? Your near a door, it leads to the next room..." );
                System.out.println("            Want to go through it? 'yes' 'no' ");
                System.out.println(" ");
                //ask them if they want to enter and go to next room
                String response = bob.next();
                if(response.equals("yes")){
                    System.out.println(" ");
                    System.out.println("        What a brave person you are");
                    for(int i = 0; i < 300 ; i++){
                        System.out.println(" YYYYYYYYYYYYYYYYYYYYYYYYYYEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEETTTTTTTTTTTTTTTTTTTTTTTTTTTTT");
                    }
                    System.out.println(" ");
                    room.ghost();//clear out the players position in the current room
                    room2.ghost();//clear our the room leading up just in case
                    room2.teleportTo(1,1);//add the players position in the coming room
                    System.out.println(" ");
                    System.out.println("        Welcome to the second room");
                    System.out.println(" ");
                }
                else{
                    System.out.println("        um because you didn't say 'yes' i cannot consensually let you go through");
                    System.out.println(" ");
                }
            }//end of if
        }
        else if(room2.isHere()){
            if(room2.onStairs()){//and they are at a door
                System.out.println(" ");
                System.out.println("                    NANI?!?!? Your near somes stairs, it leads to the next room... up above...spoooooookyyyyyy" );
                System.out.println("                    Want to go up it? 'yes' 'no' ");
                System.out.println(" ");
                //ask them if they want to enter and go to next room
                String response = bob.next();
                if(response.equals("yes")){
                    System.out.println(" ");
                    System.out.println("            Get ready for a lot of walking");
                    for(int i = 0; i < 300 ; i++){
                        System.out.println(" YYYYYYYYYYYYYYYYYYYYYYYYYYEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEETTTTTTTTTTTTTTTTTTTTTTTTTTTTT");
                    }
                    System.out.println(" ");
                    room2.ghost();//clear the players position in the current room 
                    room3.ghost();//clear the players position in the coming room just in case
                    room3.teleportTo(1,1);//put the player in the next room
                    System.out.println(" ");
                    System.out.println("            Welcome to the third room");
                    System.out.println(" ");
                }
                else{
                    System.out.println("            that doens't sound like a 'yes'");
                    System.out.println(" ");
                }
            }//end of if for if the player is at the stairs that lead up
            else if(room2.onDoor()){//and they are at a door to go back down 
                System.out.println(" ");
                System.out.println("                    HHHHHHHHHHMMMMMMMMMMMMM  that's the door that leads back to the other room." );
                System.out.println("                    Want to go through it? 'yes' 'no' ");
                System.out.println(" ");
                //ask them if they want to enter and go to next room
                String response = bob.next();
                if(response.equals("yes")){
                    System.out.println(" ");
                    System.out.println("            okaaayyyyy");
                    for(int i = 0; i < 300 ; i++){
                        System.out.println(" YYYYYYYYYYYYYYYYYYYYYYYYYYEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEETTTTTTTTTTTTTTTTTTTTTTTTTTTTT");
                    }
                    System.out.println(" ");
                    room.ghost();//clears the player's position in the current room
                    room2.ghost();//clears the player's position in the room they're going to just in case
                    room.teleportTo(9,13);//put the players position in the room down below
                    System.out.println(" ");
                    System.out.println("            Welcome to the first room");
                    System.out.println(" ");
                }
                else{
                    System.out.println("            Come back when your answer is 'yes'");
                    System.out.println(" ");
                }
            }//end of if
        }
        else if(room3.isHere()){//if they are in the third room
            if(room2.onStairs()){//and they are at some stairs that leads back down
                System.out.println(" ");
                System.out.println("                    These are the same stairs you went up from, taking these again will take you downy" );
                System.out.println("                    Want to go down it? 'yes' 'no' ");
                System.out.println(" ");
                //ask them if they want to enter and go to last room
                String response = bob.next();
                if(response.equals("yes")){
                    System.out.println(" ");
                    System.out.println("            Get ready for a lot of walking");
                    for(int i = 0; i < 300 ; i++){
                        System.out.println(" YYYYYYYYYYYYYYYYYYYYYYYYYYEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEETTTTTTTTTTTTTTTTTTTTTTTTTTTTT");
                    }
                    System.out.println(" ");
                    room2.ghost();//clears the players position in the room they are going to just in case
                    room3.ghost();//clears the players position in the room they are currently in
                    room2.teleportTo(18,18);//adds the players location in the laast room where the stairs end
                    System.out.println(" ");
                    System.out.println("            Welcome to the second room");
                    System.out.println(" ");
                }
                else{
                    System.out.println("            that doens't sound like a 'yes'");
                    System.out.println(" ");
                }
            }//end of if for if the player is at the stairs that lead down
        }//end of if there is a door if statment

        //for the input if you need to look at directions again
        if(input.equals("p")){
            System.out.println(" ");
            System.out.println(" How to play:");
            System.out.println("                You, the Hero, will be represented on the board by the '+' symbol.");
            System.out.println("                Your enemy, the Demon King Garret, will be represented by the ':(' symbol. He is super sad so he walks in a path");
            System.out.println("                Ari THE WOMAN will be represented by the '?' symbol. She is CRAZY so you need to chase her");
            System.out.println("       ");
            System.out.println("                Move throughout the board collecting as many items '<$>' as you can to find a powerful weapon.");
            System.out.println("                If you do happen to run into either enemy, you must dual it out to see who wins!");
            System.out.println("    ");
            System.out.println("                                   Press 'w' to move UP. ");
            System.out.println("                                   Press 's' to move DOWN. ");
            System.out.println("                                   Press 'a' to move LEFT. ");
            System.out.println("                                   Press 'd' to move RIGHT. ");
            System.out.println("                                   Press 'i' to access inventroy ");
            System.out.println("                                   Press 'q' to activate not alive mode");
            System.out.println(" ");
        }//end of if for printing controls

        if(input.equals("q")){
            bro.kill();
        }//end of if for quit

        //input for inventory
        if(input.equals("i")){
            boolean go = true;//variable to make loop go forever until it's false

            do{

                System.out.println(" ");
                System.out.println("Your current weapon is " + stuff.currentWeapon());
                System.out.println("Your current Armor is " + stuff.currentArmor());
                System.out.println(" ");
                System.out.println(" 1. Print the inventory");
                System.out.println(" 2. Drop an item");
                System.out.println(" 3. Equip weapon");
                System.out.println(" 4. Equip armor");
                System.out.println(" 5. quit");
                System.out.println(" ");
                int choice = bob.nextInt();
                switch(choice){
                    case 1://print inventory
                        stuff.print();
                        break;
                    case 2://drop item
                        stuff.drop();   
                        break;
                    case 3://equp weapon
                        stuff.equipWeapon();
                        break;
                    case 4://equp armor
                        stuff.equipArmor();
                        break;
                    case 5://exit
                        try{
                            FileOutputStream file = new FileOutputStream("data.txt");
                            PrintWriter pw = new PrintWriter(file);
                            bro.save(pw);
                            pw.close();
                        }catch(FileNotFoundException e){
                            System.out.println("not found");
                        }
                        go = false;
                        break;

                }//end of swtich
            }while(go);//end of do while
        }//end of if of inventory choice


        if(room.itemonfloor()){
            stuff.add(room.pickup());
        }//end of if for checking if item is on the floor.

        if(!Garret.isAlive() && !Ari.isAlive()){
            for(int i = 0 ; i < 5 ; i++){
                System.out.println("     YYYYYYYYAAAAAAAAAAAAAAAAAAAAAAAAYYYYYYYYYYY");
                System.out.println("                     YOU HAVE WON ");
            }
            System.out.println(" ");
            System.out.println("      YOU HAVE DONE IT, YOU HAVE KILLED THE DEMON KING AND HER QUEEN");
            System.out.println("      The curry can now be restored to the land of 'SCHOP SCHOP'");
            System.out.println("             Now you can finally rest in peace and die. ");
            System.out.println("                     Are you ready to die? ");
            System.out.println(" ");
            String response = bob.next();
            System.out.println(" ");
            System.out.println("       What's that?? I can't hear you because I am a virtual machine. Oh well.. lolz You probably said 'yes' ");
            System.out.println("      Thank you for your service " + bro.getname() + ". Good game.");
            System.out.println(" ");
            bro.kill();
        }//end of if for winning message 


    }//end of while alive check
}//end of main

}//end of Main

请告诉我我继承权是否正确

你没有

您在基类中有私有变量namehealth ,没有getter或setter。 然后,在子类中声明了具有相同名称的变量。

这些是不同的变量。

另外,变量的可见性不一致。 在某些情况下,它们是private ,在其他情况下则是私有的,而其他则是public

你需要:

  1. 摆脱实例变量
  2. 将它们全部声明为private
  3. 根据需要声明( publicprojected )吸气剂和吸气剂。
  4. 使用getter和setter访问cchild类中的私有变量。

目前尚不清楚这是否可以解决您的所有问题,但这只是一个开始。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM