mercredi 16 décembre 2020

How to grab a case in switch if its in another class using random [duplicate]

My issue is having a class with a switch in it, my objective is to randomly pick an int depending on the size of the switch. Which ever random number of (3) is picked the case corresponding to that switch is picked and a new object is built and added to the current weapon class.

This issue is based on not knowing how to work objects fully through different classes.

So lets begin with my player class which is Singleton...

package PlayerSingleton;

import PlayerState.*;
import Accessories.*;
import Guns.*;

public final class PlayerSingleton {

    private static PlayerSingleton player;
    private Weapon weapon;
    
    private final String playerName;
    public static Integer MAX_PLAYER_HEALTH = 200;
    public static Integer DEFAULT_PLAYER_LIVES = 2;
    
    private Integer health = MAX_PLAYER_HEALTH;
    private int lives = DEFAULT_PLAYER_LIVES;
    private PlayerState playerState;

    private PlayerSingleton(Weapon weapon, String pName) {
        this.weapon = weapon;
        this.playerName = pName;
        setState(new AliveState(this));
    }

    /**
     *
     * @param choice String choice of weapon
     * @param name String player name
     * @return PlayerSingleton the player instance
     */
    public static PlayerSingleton getInstance(String choice, String name) {

        Weapon weapon = PlayerSingleton.chooseWeapon(choice);
        weapon.getDescription();

        if (player == null) {
            player = new PlayerSingleton(weapon, name);
        }
        return player;
    }

    public void sufferDamage(int takeDamage) {
        playerState.takeDamage(takeDamage);
        if(takeDamage == 0){
            System.out.println("\tEnemy missed!");
        }
    }

    public int getDamage() {
         return weapon.damage(); 
    }
    
    public void respawn() {
        playerState.respawn();
    }

    public int getLives() {
        return lives;
    }

    public void setLives(int lives) {
        this.lives = lives;
    }

    public int getHealth() {
        return health;
    }

    public void setHealth(Integer health) {
        this.health = health;
    }

    public void setState(PlayerState playerState) {
        this.playerState = playerState;
    }

    public void chosenWeapon() {
        System.out.println("Player Info: " + playerName + " " + "has: " + health + " health and "
                + lives + " lives" + " : " + weapon.getDescription() + ":" + " base damage: " + weapon.damage());
        System.out.println();
    }

    public static Weapon chooseWeapon(String choice) {
        switch (choice) {
            case "MP5":
                System.out.println("You have chosen MP5!");
                return new MP5Weapon();
            case "SNIPER":
                System.out.println("You have chosen Sniper!");
                return new SniperWeapon();
            case "SHOTGUN":
                System.out.println("You have chosen Shotgun!");
                return new ShotgunWeapon();
            default:
                System.out.println("No gun by that name found!");
                return null;
        }
    }

    public void addBasicAttachment(int attachment) {
        switch (attachment) {
            case 0:
                weapon = new BasicSight(weapon);
                break;
            case 1:
                weapon = new BasicSilencer(weapon);
                break;
            case 2:
                weapon = new BasicStock(weapon);
                break;
            default:
                System.out.println("No Attachment found!");
        }
    }

    public void addGoodAttachment(int attachment) {
        switch (attachment) {
            case 0:
                weapon = new GoodSight(weapon);
                break;
            case 1:
                weapon = new GoodSilencer(weapon);
                break;
            case 2:
                weapon = new GoodStock(weapon);
                break;
            default:
                System.out.println("No Attachment found!");
        }
    }

    public void addGreatAttachment(int attachment) {
        switch (attachment) {
            case 0:
                weapon = new GreatSight(weapon);
                break;
            case 1:
                weapon = new GreatSilencer(weapon);
                break;
            case 2:
                weapon = new GreatStock(weapon);
                break;
            default:
                System.out.println("No Attachment found!");
        }
    }
}

As you can see at the bottom of the code, the player has addBasic,Good,GreatAttachments.

The objective is to be able to call this using a random function in the main.

The main class code...

public class FireingRange {

    Weapon weapon;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println(" -------------------------------------- ");
        System.out.println("        Text based Shooting Game        ");
        System.out.println(" -------------------------------------- ");
        System.out.println("Please Enter your name before Beginning*");

        String name = scanner.nextLine().toUpperCase();

        System.out.println("Thank you for joining this Adventure!: " + name);
        System.out.println();
        System.out.println(name + " to begin the game, first type a weapon you would like to play with:");
        System.out.println();
        System.out.println("'Shotgun' for Shotgun");
        System.out.println("'Sniper' for Sniper");
        System.out.println("'MP5' for MP5");
        String chooseWeapon = scanner.next().toUpperCase();

        while (!chooseWeapon.equals("MP5") && !chooseWeapon.equals("SHOTGUN") && !chooseWeapon.equals("SNIPER")) {
            System.out.println("Incorrect input, try again...");
            chooseWeapon = scanner.next().toUpperCase();
        }

        System.out.println();
        PlayerSingleton player = PlayerSingleton.getInstance(chooseWeapon, name);
        player.chosenWeapon();

        String[] enemies = {"Cowboy", "Wild Dog", "Thief", "Bounty Hunter", "Snake", "Killer Moth"};
        int MAX_ENEMY_HEALTH = 10;
        int MAX_ENEMY_ATTACK = 25;
        int enemyHP = MAX_ENEMY_HEALTH;
        int enemyDamage = MAX_ENEMY_ATTACK;

        int healthPotHealAmount = 50;
        int healthPotDropChance = 50;
        int numOfHealthPot = 3;

        Scanner in = new Scanner(System.in);
        Random rand = new Random();

        int playerHP = player.getHealth();

        int basicAttachmentDropChance = 40;

        boolean isRunning = true;

        GAME:
        while (isRunning) {
            int enemyHealth = rand.nextInt(enemyHP);
            String enemy = enemies[rand.nextInt(enemies.length)];
            System.out.println("\t# " + enemy + " appeared! #\n");

            while (enemyHealth > 0) {
                System.out.println("\tYour HP:" + playerHP);
                System.out.println("\t" + enemy + "'s HP: " + enemyHealth);
                System.out.println("\tWhat would you like to do?");
                System.out.println("\t1.Attack");
                System.out.println("\t2.Use health potion");
                System.out.println("\t3.Run");
                System.out.println();

                String input = in.nextLine();
                if (input.equals("1")) {
                    int damageDealt = rand.nextInt(player.getDamage());
                    int damageTaken = rand.nextInt(enemyDamage);
                    if (damageDealt == 0) {
                        System.out.println("\tYou missed!");
                    } else {
                        System.out.println("\tHit " + enemy + " " + "Attacked for " + damageDealt);
                        playerHP -= damageTaken;
                    }
                    enemyHealth -= damageDealt;
                    player.sufferDamage(damageTaken);

                    if (playerHP == 0) {
                        player.respawn();
                        continue GAME;
                    }

                } else if (input.equals("2")) {
                    if (numOfHealthPot > 0) {
                        playerHP += healthPotHealAmount;
                        numOfHealthPot--;
                        System.out.println("\t You drink a health potion, healing you for " + healthPotHealAmount);
                        System.out.println("\t You now have " + playerHP + " HP");
                        System.out.println("\t  You have " + numOfHealthPot + " left");
                    } else {
                        System.out.println("\t No more health potions! Defeat an enemy to have a chance of getting another one...");
                    }
                } else if (input.equals("3")) {
                    System.out.println("\tYou run away from the " + enemy + "!");
                    continue GAME;
                } else {
                    System.out.println("\tInvalid command!");
                }
            }

            System.out.println(" -------------------------------------- ");
            System.out.println(" # " + enemy + " was defeated! #");
            System.out.println(" # You have " + playerHP + " HP left. #");
            if (rand.nextInt(100) < healthPotDropChance) {
                numOfHealthPot++;
                System.out.println(" # The " + enemy + " dropped a health potion! # ");
                System.out.println(" # You now have " + numOfHealthPot + " health potion(s). # ");
                if (rand.nextInt(100) < basicAttachmentDropChance) {

                }
            }

            System.out.println(" -------------------------------------- ");
            System.out.println("What would you like to do?");
            System.out.println("1. Continue playing?");
            System.out.println("2. Exit dungeon");
            String input = in.nextLine();
            while (!input.equals("1") && !input.equals("2")) {
                System.out.println("Invalid command");
                input = in.nextLine();
            }
            if (input.equals("1")) {
                System.out.println("You continue the adventure");
            } else if (input.equals("2")) {
                System.out.println("You exit the dungeon, successful form your adventure");
                break;
            }
        }
        System.out.println("#########################################");
        System.out.println("           THANKS FOR PLAYING!           ");
        System.out.println("#########################################");
    }

The snippet code where I am not able to figure it out is:

if (rand.nextInt(100) < healthPotDropChance) {
                numOfHealthPot++;
                System.out.println(" # The " + enemy + " dropped a health potion! # ");
                System.out.println(" # You now have " + numOfHealthPot + " health potion(s). # ");
                if (rand.nextInt(100) < basicAttachmentDropChance) {
                    //Here I am not sure how to pull the addAttachment from PlayerSingleton
                }
            }

How would I go about it? I have spent a few days on this with searching Google to find help or some assistance in this, now I am coming to Stack to ask you.

Any help would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire