首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Java中的策略RPG

Java中的策略RPG
EN

Code Review用户
提问于 2018-03-09 01:56:23
回答 1查看 342关注 0票数 5

最近,我开始编写代码,并在Java中构建了一个小游戏来练习和测试自己。当我开始拔掉它的时候,它很快就变得很小了。到目前为止,一切都按我所希望的方式工作,但它有点笨重,我在内容扩展方面遇到了困难,即将其全部迁移到GUI中。有没有人对我如何优化我的代码有任何建议/想法,以便在我进行过程中更容易更新?

游戏的理念是,你是一个外星人,能够利用资源、营养和金属制造无人机。这些资源是通过派遣你的无人机去探索不同的区域而获得的,目前为止:山脉、平原、海岸和森林。现在,这是我编写的全部代码,但我知道,当我添加不同的生物遭遇、更多的区域等时,代码可能会更高效地布局。

这里有一个指向Github的链接:https://github.com/FormOther/Hive-Quest

这里是主要的蜂巢游戏:

代码语言:javascript
运行
复制
 import java.util.Scanner;
 import java.util.Random;

 public class Hive {
 //Class Instances are defined here.
     Random random = new Random();
     Events event = new Events();
     static Scanner myScanner = new Scanner(System.in);
     static Scanner enterScanner = new Scanner(System.in);

 //Nutrients in the main menu
    static int nutrients = 50;
    static int metals = 0;

//Nutrients gained while exploring
    public int missionNutrients;
    public int missionMetals;

//Drones available in the menu
    static int warriorDrones; 
    static int workerDrones;
    static int thinkerDrones;
    static int totalDrones = warriorDrones + workerDrones + thinkerDrones;

//Drones available while exploring
    public int missionWorkerDrones;
    public int missionWarriorDrones;
    public int missionDrones;

//Integers for battles
    public int critterHealth;
    int d2Result;
    int d3Result;
    int d10Result;
    int d20Result;
    int d30Result;
    int d50Result;
    int d100Result;

//Name Strings
    static String playerName;
    public String critterName;

//booleans for battle locations
    boolean mountBattle;
    boolean forestBattle;
    boolean plainBattle;
    boolean coastBattle; 

//booleans for player actions
    boolean dronesOffensive;
    boolean dronesDefensive;
    boolean dronesEvasive;

//booleans for creature actions
    boolean critterOffensive;
    boolean critterDefensive;
    boolean critterEvasive;

int mainMenuChoice;
int exploreMenuChoice;
int eggMenuChoice;
int droneRecruit;
int battleChoice;

//Opening Scene is here
    public void openingScene() {
        myScanner = new Scanner(System.in);
        System.out.println("What color is your carapace?");
        playerName = myScanner.nextLine();
    }

//Hive Main Menu Here
    public void mainMenu() {
        d2Result = 0;
        d3Result = 0;
        d10Result = 0;
        d20Result = 0;
        d30Result = 0;
        d50Result = 0;
        d100Result = 0;
        System.out.println("\n----------------------------------------------
----------------------");
    System.out.println("Your are the " + playerName + " Queen.");
    System.out.println("You are currently resting in your newly formed 
hive.\n");
    System.out.println("You have " + nutrients + " nutrients.");
    System.out.println("You have " + metals + " metals.\n");
    System.out.println("You have " + workerDrones + " worker drones.");
    System.out.println("You have " + warriorDrones + " warrior drones.");
    System.out.println("You have " + thinkerDrones + " thinker drones.\n");
    System.out.println("What would you like to do?\n");
    System.out.println("1) Lay Egg");
    System.out.println("2) Explore Outside");
    System.out.println("3) Check Hive"); 
    /*if (spaceshipMine = true) {
        System.out.println("4) Mine the derelict");
    }*/
    System.out.println("\n--------------------------------------------------------------------");
    mainMenuChoice = myScanner.nextInt();


    switch(mainMenuChoice) {
    case 1: makeDroneMenu();
    break;
    case 2: exploreBarracks();
    break;
    case 3: checkHiveMenu();
    break;
    default: System.out.println("That's not a choice!");
    mainMenu();
    break;
    }

}

//Check Hive Menu
    public void checkHiveMenu() {
    System.out.println("You are the " + playerName + " Queen.\n");
    System.out.println("You have " + nutrients + " nutrients.");
    System.out.println("You have " + metals + " metals.\n");
    System.out.println("You have " + workerDrones + " worker drones.");
    System.out.println("You have " + warriorDrones + " warrior drones.");
    System.out.println("You have " + thinkerDrones + " thinker drones.\n");
    enterScanner.nextLine();
    mainMenu();
}

//Lay Egg Main Menu
    public void makeDroneMenu() {
        if (nutrients >= 5) {
            System.out.println("\n------------------------------------------
--------------------------\n");
            System.out.println("What kind of child will be born to the hive?
\n");
            System.out.println("You have " + nutrients + " nutrients 
available.");
            System.out.println("You have " + metals + " metals 
available.\n");         
            System.out.println("1) Worker Drone | Cost: -5 Nutrients");
            System.out.println("2) Warrior Drone | Cost: -10 Nutrients, -5 
Metals");
            System.out.println("3) Thinker Drone | Cost: -6 Nutrients");
            System.out.println("4) Back");
            eggMenuChoice = myScanner.nextInt();
            System.out.println("\n------------------------------------------
 --------------------------\n");

            switch(eggMenuChoice) {
            case 1:
                addWorker();
                makeDroneMenu();
                break;  
            case 2:
                addWarrior();
                makeDroneMenu();
                break;  
            case 3:
                addThinker();
                makeDroneMenu();
                break;  
            case 4:
                mainMenu();
                break;
            }
        } else if (nutrients < 4) {
            System.out.println("You don't have enough nutrients to make 
drones!");
            mainMenu();
        }
    }

//Explore Recruitment Menu
    public void exploreBarracks() {
    System.out.println("------------------------------------------------------------------");
    System.out.println("Which drones do you send to explore?\n");

    System.out.println("You have " + workerDrones + " worker drones available.");
    System.out.println("You have " + warriorDrones + " warrior drones available.\n");

    System.out.println("You assigned " + missionWorkerDrones + " worker drones to explore.");
    System.out.println("You have " + missionWarriorDrones + " warrior drones to explore.\n");

    System.out.println("1) Add worker drones");
    System.out.println("2) Add warrior drones");
    System.out.println("3) Clear exploration queue");
    System.out.println("4) Continue");
    System.out.println("5) Back");  
    droneRecruit = myScanner.nextInt();
    System.out.println("------------------------------------------------------------------");
    switch (droneRecruit) {
        case 1:
            if (workerDrones == 0) {
                System.out.println("You don't have enough worker drones to explore for you.");
                exploreBarracks();
            } else {
            recruitWorker();
            exploreBarracks();
            }
            break;
        case 2:
            if (warriorDrones == 0) {
                System.out.println("You don't have enough warrior drones to explore for you.");
                exploreBarracks();
            } else {
            recruitWarrior();
            exploreBarracks();
            }
            break;
        case 3:
            resetDrones();
            exploreBarracks();
        case 4:
            if (missionWorkerDrones == 0 && missionWarriorDrones == 0) {
                System.out.println("You haven't assigned any drones to explore yet!");
                exploreBarracks();
            } else {
            exploreMenu(); 
            }
            break;
        case 5: 
            resetDrones();
            mainMenu();
            break;
        default:
            System.out.println("That's not an option.");
            exploreMenu();
            break;
}
}

//Exploration Location Menu
public void exploreMenu() {
    System.out.println("\n--------------------------------------------------------------------\n");
    System.out.println("Where would you like your children to explore?\n");
    System.out.println("1) The mountains to the north.\n");
    System.out.println("2) The forest to the south.\n");
    System.out.println("3) The plains to the east.\n");
    System.out.println("4) The coastline to the west.\n");
    System.out.println("5) Back");
    System.out.println("\n--------------------------------------------------------------------\n");

    exploreMenuChoice = myScanner.nextInt();


    switch (exploreMenuChoice) {
    case 1: 
        exploreMountains();
        break;
    case 2: 
        exploreForest();
        break;
    case 3: 
        explorePlains();
        break;
    case 4: 
        exploreCoast();
        break;
    case 5: 
        resetDrones();
        mainMenu();
        break; 
    default: 
        System.out.println("That isn't a place you can explore.");
        enterScanner.nextLine();
        exploreMenu();
        break;
    }
}

//Battle encounter Menu
public void battleMenu() {
    System.out.println("\n------------------------------------------------------------------\n");
    System.out.println("You are currently fighting a " + critterName + ".");
    System.out.println("The " + critterName + " has " + critterHealth + " health.");
    if (missionWorkerDrones > 0) {
    System.out.println("\nYou have " + missionWorkerDrones + " worker drones available to fight.");
    }
    if (missionWarriorDrones > 0) {
    System.out.println("You have " + missionWarriorDrones + " warrior drones available to fight.");
    }
    System.out.println("");
    System.out.println("How should your drones fight this enemy?\n");
    System.out.println("1) Offensively");
    System.out.println("2) Defensively");
    System.out.println("3) Evasively");
    battleChoice = myScanner.nextInt();
    System.out.println("\n------------------------------------------------------------------\n");   
    switch (battleChoice) {
    case 1:
    droneOffensive();
    break;
    case 2:
    droneDefensive();
    break;
    case 3:
    droneEvasive(); 
    break;
    default:
    System.out.println("That is not an option.");
    battleMenu();
    break;
    }
}

//Option for if the player chooses the OFFENSIVE option
public void droneOffensive() {
System.out.println("Your drones charge the " + critterName + "!");
dronesOffensive = true;
dronesDefensive = false;
dronesEvasive = false;

//This is where the critter attack goes
/*
 * d100();
 * 
 * if (d100Result >= 0 && d100Result <= 20) {
 * 
 * } else if (d100Result >= 21 && d100Result <= 40) {
 * 
 * } else if (d100Result >= 41 && d100Result <= 100) {
 * 
 * } else {
 * System.out.println(there is an error here!");
 * }
 * 
 */
d3();
switch (d3Result) {
case 1:
    critterOffensive = true;
    critterDefensive = false;
    critterEvasive = false;
    System.out.println("The " + critterName + " charges!");
    System.out.println("Your children and the " + critterName + " are evenly matched!");
    break;
case 2:
    critterDefensive = true;
    critterOffensive = false;
    critterEvasive = false;
    System.out.println("The " + critterName + " protects itself!");
    System.out.println("Your children crush the " + critterName + "'s defenses!");
    System.out.println("The " + critterName + " takes damage!");
    critterHealth -= 1 * missionWorkerDrones + (missionWarriorDrones * 2);
    break;
case 3:
    critterEvasive = true;
    critterDefensive = false;
    critterOffensive = false;
    System.out.println("The " + critterName + " moves quickly!");
    System.out.println("Your children are outflanked by the " + critterName + "!");
    System.out.println("One of your drones falls from it's wounds!");
    d2();
    switch (d2Result) {
        case 1:
            if (missionWarriorDrones > 0) {
                killWarrior();
            } else if (missionWarriorDrones <= 0) {
                killWorker();
            }
            break;
        case 2:
            if (missionWorkerDrones > 0) {
                killWorker();
            } else if (missionWorkerDrones <= 0) {
                killWarrior();
            }
            break;
        default:
            System.out.println("There is an error here");
            break;
    }
    break;
default:
    System.out.println("Here is where the error is.");
    }
    d3Result = 0;
    d2Result = 0;

//End loop or continue battle
if (missionWorkerDrones <= 0 && missionWarriorDrones <= 0) {
    System.out.println("Your drones were wiped out by the " + critterName);
    resetDrones();
    enterScanner.nextLine();
    mainMenu();
} else if (critterHealth <= 0) {
    System.out.println("Your drones brought down the " + critterName);
    getSpoils();
    addSpoils();
    resetDrones();
    enterScanner.nextLine();
    mainMenu();
} else {
    battleMenu();
}

}

//Option for if the player chooses the DEFENSIVE option
public void droneDefensive() {
System.out.println("Your drones shield eachother from the " + critterName + "!");
dronesDefensive = true;
dronesOffensive = false;
dronesEvasive = false;

//This is where the critter attack goes
d3();
switch (d3Result) {
case 1:
    critterOffensive = true;
    critterDefensive = false;
    critterEvasive = false;
    System.out.println("The " + critterName + " charges!");
    System.out.println("Your children's defenses are crushed by the " + critterName + "!");
    System.out.println("One of your drones falls from it's wounds!");
    d2();
    switch (d2Result) {
        case 1:
            if (missionWarriorDrones > 0) {
                killWarrior();
            } else if (missionWarriorDrones <= 0) {
                killWorker();
            }
            break;
        case 2:
            if (missionWorkerDrones > 0) {
                killWorker();
            } else if (missionWorkerDrones <= 0) {
                killWarrior();
            }
            break;
        default:
            System.out.println("There is an error here");
            break;
    }
    break;

case 2:
    critterDefensive = true;
    critterOffensive = false;
    critterEvasive = false;
    System.out.println("The " + critterName + " protects itself!");
    System.out.println("The " + critterName + " and your drones poke at eachothers defenses to no avail!");
    break;
case 3:
    critterEvasive = true;
    critterDefensive = false;
    critterOffensive = false;
    System.out.println("The " + critterName + " moves quickly!");
    System.out.println("YOur children hold their ground and lay into the " + critterName + "!");
    System.out.println("The " + critterName + " takes damage!");
    critterHealth -= 1 * missionWorkerDrones + (missionWarriorDrones * 2);
    break;

default:
    System.out.println("Here is where the error is.");
    break;
}
d3Result = 0;
d2Result = 0;
//End loop or continue battle
if (missionWorkerDrones <= 0 && missionWarriorDrones <= 0) {
    System.out.println("Your drones were wiped out by the " + critterName);
    resetDrones();
    enterScanner.nextLine();
    mainMenu();
} else if (critterHealth <= 0) {
    System.out.println("Your drones brought down the " + critterName);
    getSpoils();
    addSpoils();
    resetDrones();
    enterScanner.nextLine();
    mainMenu();
} else {
    battleMenu();
    }
    }

//Option for if the player chooses the EVASIVE option
public void droneEvasive() {
System.out.println("Your drones rush around in a dizzying flurry of chitin!");
dronesEvasive = true;
dronesDefensive = false;
dronesOffensive = false;

//This is where the critter attack goes
d3();
switch (d3Result) {
case 1:
    critterOffensive = true;
    critterDefensive = false;
    critterEvasive = false;
    System.out.println("The " + critterName + " charges!");
    System.out.println("Your children easily ourflank the " + critterName + "!");
    System.out.println("The " + critterName + " takes damage!");
    critterHealth -= 1 * missionWorkerDrones + (missionWarriorDrones * 2);
    break;

case 2:
    critterDefensive = true;
    critterOffensive = false;
    critterEvasive = false;
    System.out.println("The " + critterName + " protects itself!");
    System.out.println("The " + critterName + "holds it's ground and lays into your children!");
    System.out.println("One of your drones falls from it's wounds!");
    d2();
    switch (d2Result) {
        case 1:
            if (missionWarriorDrones > 0) {
                killWarrior();
            } else if (missionWarriorDrones <= 0) {
                killWorker();
            }
            break;
        case 2:
            if (missionWorkerDrones > 0) {
                killWorker();
            } else if (missionWorkerDrones <= 0) {
                killWarrior();
            }
            break;
        default:
            System.out.println("There is an error here");
            break;
    }
    break;

case 3:
    critterEvasive = true;
    critterDefensive = false;
    critterOffensive = false;
    System.out.println("The " + critterName + " moves quickly!");
    System.out.println("Your children and the " + critterName + " dance around eachother, looking for an opening.");
    break;

default:
    System.out.println("Here is where the error is.");
    break;
}
d3Result = 0;
d2Result = 0;
//End loop or continue battle
if (missionWorkerDrones <= 0 && missionWarriorDrones <= 0) {
    System.out.println("Your drones were wiped out by the " + critterName + "!");
    resetDrones();
    enterScanner.nextLine();
    mainMenu();
} else if (critterHealth <= 0) {
    System.out.println("Your drones brought down the " + critterName + "!");
    getSpoils();
    addSpoils();
    resetDrones();
    enterScanner.nextLine();
    mainMenu();
} else {
    battleMenu();
}
}

//Mountain Exploration Roll Table
public void exploreMountains() {
    System.out.println("Your drones explore the mountainous region.");
    d100();
    //System.out.println("Rolled a " + d100Result);
    if (d100Result <= 0) {
        event.mountEvent1();
        missionWarriorDrones = 0;
        missionWorkerDrones = 0;
        enterScanner.nextLine();
        mainMenu();
    } else if (d100Result >= 0 && d100Result <= 10) {
        System.out.println("Your drones don't find anything in the mountains.");
        enterScanner.nextLine();
        resetDrones();
        mainMenu();
    } else if (d100Result >= 11 && d100Result <= 20) {
        event.mountEvent2();
        //mountBattle = true;
        hillCritter();
        enterScanner.nextLine();
        battleMenu();
    } else if (d100Result >= 21 && d100Result <= 30) {
        event.mountEvent3();
        //mountBattle = true;
        hillCritter();
        enterScanner.nextLine();
        battleMenu();
    } else if (d100Result >= 31 && d100Result <= 40) {
        event.mountEvent4();
        //mountBattle = true;
        hillCritter();
        enterScanner.nextLine();
        battleMenu();
    } else if (d100Result >= 41 && d100Result <=50) {
        event.mountEvent5();
        //mountBattle = true;
        hillCritter();
        enterScanner.nextLine();
        battleMenu();
    } else if (d100Result >= 51 && d100Result <= 60) {
        event.mountEvent6();
        d10();
        missionMetals = d10Result * missionWorkerDrones + missionWarriorDrones;
        System.out.println("\nYou gain " + missionMetals + " metals!");
        addSpoils();
        resetDrones();
        enterScanner.nextLine();
        mainMenu();
    } else if (d100Result >= 61 && d100Result <= 70) {
        event.mountEvent7();
        d20();
        missionMetals = d20Result * missionWorkerDrones + missionWarriorDrones;
        System.out.println("\nYou gain " + missionMetals + " metals!");
        addSpoils();
        resetDrones();
        enterScanner.nextLine();
        mainMenu();
    } else if (d100Result >= 71 && d100Result <= 80) {
        event.mountEvent8();
        d20();
        d10();
        missionMetals = d20Result * missionWorkerDrones + missionWarriorDrones;
        missionNutrients = d10Result * missionWorkerDrones + missionWarriorDrones;
        System.out.println("Your gain " + missionMetals + " metals and " + missionNutrients + " nutrients!");
        addSpoils();
        resetDrones();
        enterScanner.nextLine();
        mainMenu();
    } else if (d100Result >= 81 && d100Result <= 90) {
        event.mountEvent9();
        d30();
        missionMetals = d30Result * missionWorkerDrones + missionWarriorDrones;
        System.out.println("\nYou gain " + missionMetals + " metals!");
        addSpoils();
        resetDrones();
        enterScanner.nextLine();
        mainMenu();
    } else if (d100Result >= 91 && d100Result <= 99) {
        event.mountEvent9a();
        d50();
        missionMetals = d50Result * missionWorkerDrones + missionWarriorDrones;
        System.out.println("\nYou gain " + missionMetals + " metals!");
        addSpoils();
        resetDrones();
        enterScanner.nextLine();
        mainMenu();
    } else if (d100Result == 100) {
        System.out.println("While exploring the mountains your drones find a scrap of rectancular metal embedded in a stone");
        System.out.println("They follow the trail of discarded metal until they come to a valley and crashed within it is a colossal spaceship!");
        System.out.println("Because you now know the way to this site, you can come here to mine metals for free!");
        //spaceshipMine = true;
        resetDrones();
        enterScanner.nextLine();
        mainMenu();
    } else {
        System.out.println("There is an error here");
    }
}

//Forest Exploration Roll Table
public void exploreForest() {
    System.out.println("Your drones explore the forested region.");
    d100();
    //System.out.println("Rolled a " + d100Result);
    if (d100Result <= 0) {
        event.forestEvent1();
        missionWarriorDrones = 0;
        missionWorkerDrones = 0;
        enterScanner.nextLine();
        mainMenu();
    } else if (d100Result >= 1 && d100Result <= 10) {
        System.out.println("Your drones don't find anything in the forest.");
        enterScanner.nextLine();
        resetDrones();
        mainMenu();
    } else if (d100Result >= 11 && d100Result <= 20) {
        event.forestEvent2();
        woodsCritter();
        enterScanner.nextLine();
        battleMenu();
    } else if (d100Result >= 21 && d100Result <= 30) {
        event.forestEvent3();
        d10();
        missionNutrients = d10Result * missionWorkerDrones + missionWarriorDrones;
        System.out.println("\nYou gain " + missionNutrients + " nutrients!");
        addSpoils();
        resetDrones();
        enterScanner.nextLine();
        mainMenu();
    } else if (d100Result >= 31 && d100Result <= 40) {
        event.forestEvent4();
        woodsCritter();
        enterScanner.nextLine();
        battleMenu();
    } else if (d100Result >= 41 && d100Result <=50) {
        event.forestEvent5();
        woodsCritter();
        enterScanner.nextLine();
        battleMenu();
    } else if (d100Result >= 51 && d100Result <= 60) {
        event.forestEvent6();
        d10();
        missionNutrients = d10Result * missionWorkerDrones + missionWarriorDrones;
        System.out.println("\nYou gain " + missionNutrients + " nutrients!");
        addSpoils();
        resetDrones();
        enterScanner.nextLine();
        mainMenu();
    } else if (d100Result >= 61 && d100Result <= 70) {
        event.forestEvent7();
        d20();
        missionNutrients = d20Result * missionWorkerDrones + missionWarriorDrones;
        System.out.println("\nYou gain " + missionNutrients + " nutrients!");
        addSpoils();
        resetDrones();
        enterScanner.nextLine();
        mainMenu();
    } else if (d100Result >= 71 && d100Result <= 80) {
        event.forestEvent8();
        d30();
        missionNutrients = d30Result * missionWorkerDrones + missionWarriorDrones;
        System.out.println("\nYou gain " + missionNutrients + " nutrients!");
        addSpoils();
        resetDrones();
        enterScanner.nextLine();
        mainMenu();
    } else if (d100Result >= 81 && d100Result <= 90) {
        event.forestEvent9();
        d50();
        missionNutrients = d50Result * missionWorkerDrones + missionWarriorDrones;
        System.out.println("\nYou gain " + missionNutrients + " nutrients!");
        addSpoils();
        resetDrones();
        enterScanner.nextLine();
        mainMenu();
    } else if (d100Result >= 91 && d100Result <= 99) {
        event.forestEvent9a();
        woodsCritter();
        enterScanner.nextLine();
        battleMenu();
    } else if (d100Result == 100) {
        event.forestEvent10();
        enterScanner.nextLine();
        mainMenu();
    } else {
        System.out.println("There is an error here");
    }
}

//Plains Exploration Roll Table
public void explorePlains() {
    System.out.println("Your drones explore the flat expanse.");
    d100();
    //System.out.println("Rolled a " + d100Result);
    if (d100Result <= 0) {
        event.plainsEvent1();
        missionWarriorDrones = 0;
        missionWorkerDrones = 0;
        enterScanner.nextLine();
        mainMenu();
    } else if (d100Result >= 1 && d100Result <= 10) {
        System.out.println("Your drones trek through the plains is surprisingly uneventful.");
        enterScanner.nextLine();
        resetDrones();
        mainMenu();
    } else if (d100Result >= 11 && d100Result <= 20) {
        event.plainsEvent2();
        grassCritter();
        enterScanner.nextLine();
        battleMenu();
    } else if (d100Result >= 21 && d100Result <= 30) {
        event.plainsEvent3();
        grassCritter();
        enterScanner.nextLine();
        battleMenu();
    } else if (d100Result >= 31 && d100Result <= 40) {
        event.plainsEvent4();
        grassCritter();
        enterScanner.nextLine();
        battleMenu();
    } else if (d100Result >= 41 && d100Result <=50) {
        event.plainsEvent5();
        grassCritter();
        enterScanner.nextLine();
        battleMenu();
    } else if (d100Result >= 51 && d100Result <= 60) {
        event.plainsEvent6();
        grassCritter();
        enterScanner.nextLine();
        battleMenu();
    } else if (d100Result >= 61 && d100Result <= 70) {
        event.plainsEvent7();
        d20();
        missionNutrients = d20Result * (missionWorkerDrones + missionWarriorDrones);
        System.out.println("\nYou gain " + missionNutrients + " nutrients!");
        addSpoils();
        resetDrones();
        enterScanner.nextLine();
        mainMenu();
    } else if (d100Result >= 71 && d100Result <= 80) {
        event.plainsEvent8();
        d20();
        missionMetals = d20Result * (missionWorkerDrones + missionWarriorDrones);
        System.out.println("\nYou gain " + missionMetals + " metals!");
        addSpoils();
        resetDrones();
        enterScanner.nextLine();
        mainMenu();
    } else if (d100Result >= 81 && d100Result <= 90) {
        event.plainsEvent9();
        d50();
        missionNutrients = d50Result * (missionWorkerDrones + missionWarriorDrones);
        System.out.println("\nYou gain " + missionMetals + " nutrients!");
        addSpoils();
        resetDrones();
        enterScanner.nextLine();
        mainMenu();
    } else if (d100Result >= 91 && d100Result <= 99) {
        event.plainsEvent9a();
        d50();
        missionMetals = d50Result * (missionWorkerDrones + missionWarriorDrones);
        System.out.println("\nYou gain " + missionMetals + " metals!");
        addSpoils();
        resetDrones();
        enterScanner.nextLine();
        mainMenu();
    } else if (d100Result == 100) {
        event.plainsEvent10();
    }
}

//Coast Exploration Roll Table
public void exploreCoast() {
    System.out.println("Your drones explore the coastal region.");
    d100();

    if (d100Result == 0) {
        event.coastEvent1();
        missionWarriorDrones = 0;
        missionWorkerDrones = 0;
        enterScanner.nextLine();
        mainMenu();
    } else if (d100Result >= 1 && d100Result <= 10) {
        System.out.println("Your drones don't find anything on the coast.");
        enterScanner.nextLine();
        resetDrones();
        mainMenu();
    } else if (d100Result >= 11 && d100Result <= 20) {
        event.coastEvent2();
        seaCritter();
        enterScanner.nextLine();
        battleMenu();
    } else if (d100Result >= 21 && d100Result <= 30) {
        event.coastEvent3();
        seaCritter();
        enterScanner.nextLine();
        battleMenu();
    } else if (d100Result >= 31 && d100Result <= 40) {
        event.coastEvent4();
        d10();
        missionMetals = d10Result * (missionWorkerDrones + missionWarriorDrones);
        d10Result = 0;
        d10();
        missionNutrients = d10Result * (missionWorkerDrones + missionWarriorDrones);
        System.out.println("Your gain " + missionMetals + " metals and " + missionNutrients + " nutrients!");
        addSpoils();
        resetDrones();
        enterScanner.nextLine();
        mainMenu();
    } else if (d100Result >= 41 && d100Result <=50) {
        event.coastEvent5();
        seaCritter();
        enterScanner.nextLine();
        battleMenu();
    } else if (d100Result >= 51 && d100Result <= 60) {
        event.coastEvent6();
        seaCritter();
        enterScanner.nextLine();
        battleMenu();
    } else if (d100Result >= 61 && d100Result <= 70) {
        event.coastEvent7();
        d20();
        missionMetals = d20Result * (missionWorkerDrones + missionWarriorDrones);
        d10();
        missionNutrients = d10Result * (missionWorkerDrones + missionWarriorDrones);
        System.out.println("Your gain " + missionMetals + " metals and " + missionNutrients + " nutrients!");
        addSpoils();
        resetDrones();
        enterScanner.nextLine();
        mainMenu();
    } else if (d100Result >= 71 && d100Result <= 80) {
        event.coastEvent8();
        d10();
        missionMetals = d10Result * (missionWorkerDrones + missionWarriorDrones);
        d20();
        missionNutrients = d20Result * (missionWorkerDrones + missionWarriorDrones);
        System.out.println("Your gain " + missionMetals + " metals and " + missionNutrients + " nutrients!");
        addSpoils();
        resetDrones();
        enterScanner.nextLine();
        mainMenu();
    } else if (d100Result >= 81 && d100Result <= 90) {
        event.coastEvent9();
        seaCritter();
        enterScanner.nextLine();
        battleMenu();
    } else if (d100Result >= 91 && d100Result <= 99) {
        event.coastEvent9a();
        d30();
        missionMetals = d10Result * (missionWorkerDrones + missionWarriorDrones);
        d30Result = 0;
        d30();
        missionNutrients = d10Result * (missionWorkerDrones + missionWarriorDrones);
        System.out.println("Your gain " + missionMetals + " metals and " + missionNutrients + " nutrients!");
        addSpoils();
        resetDrones();
        enterScanner.nextLine();
        mainMenu();
    } else if (d100Result == 100) {
        event.coastEvent10();
    } else {
        System.out.println("Here there be errors.");
    }
}

//Spawn Worker Drone Method
public int addWorker() {
    if (nutrients >= 5) {
        System.out.println("You lay a small egg which quickly hatches ino a worker drone!");
        System.out.println("'Mother! Ready to work! Work hard for Mother!'");
        workerDrones += 1;
        nutrients -= 5;
    } else {
        System.out.println("You do not have enough resources for a worker drone.");
        makeDroneMenu();
    }
    return workerDrones;
}

//Spawn Warrior Drone Method
public int addWarrior() {
    if (nutrients >= 10 && metals >= 5) {
        System.out.println("You lay a large egg that quickly hatches into a warrior drone!");
        System.out.println("'Ready to fight for Mother!'");
        warriorDrones += 1;
        nutrients -= 10;
        metals -= 5;
    } else {
        System.out.println("You do not have enough resources for a warrior drone.");
        makeDroneMenu();
    }
    return warriorDrones;
}

//Spawn Thinker Drone Method
public int addThinker() {
    if (nutrients >= 6) {
        System.out.println("You lay a translucent egg that quickly hatches into a thinker drone!");
        System.out.println("'Have ideas! Many thoughts for Mother!'");
        thinkerDrones += 1;
        nutrients -= 6;
    }else {
        System.out.println("You do not have enough resources for a thinker drone.");
        makeDroneMenu();
    }
    return thinkerDrones;
}

//Recruit Worker Drone 
public void recruitWorker() {
    workerDrones -= 1;
    missionWorkerDrones += 1; 
    missionDrones += 1;
}

//Recruit Warrior Drone
public void recruitWarrior() {
    warriorDrones -= 1;
    missionWarriorDrones += 1;
    missionDrones += 1;
}

 //Kill Worker Drone
public void killWorker() {
        missionWorkerDrones -= 1;
    }

//Kill Warrior Drone
public void killWarrior() {
        missionWarriorDrones -= 1;
    }   

//Basic mountain critter
public void hillCritter() {
    critterName = ("mountain creature");
    critterHealth = 20;
}

//Basic forest critter
public void woodsCritter() {
    critterName = ("forest creature");
    critterHealth = 5;
}

//Basic plains critter
public void grassCritter() {
    critterName = ("grass creature");
    critterHealth = 15;
}

//Basic coast critter
public void seaCritter() {
    critterName = ("sea creature");
    critterHealth = 15;
}

//Add explore spoils to total nutrients/metals
public void addSpoils() {
nutrients += missionNutrients;
metals += missionMetals;
missionNutrients = 0;
missionMetals = 0;
}

//Battle win spoils
public void getSpoils() {
//TO DO:
//set it up so that the spoils are random integers depending on the region
missionMetals += 5;
missionNutrients += 20;
System.out.println("You gain " + missionMetals + " metals and " + missionNutrients + " nutrients.");
}

//Returns drones from missions back to the main menu
public void resetDrones () {
workerDrones += missionWorkerDrones;
warriorDrones += missionWarriorDrones;
missionWorkerDrones = 0;
missionWarriorDrones = 0;
missionDrones = 0;

}

//D2 dice roller 
public int d2() {
d2Result += random.nextInt(2) +1;
return d2Result;
}

//D3 dice roller
public int d3() {
    d3Result += random.nextInt(3) +1;
    return d3Result;
    }

//D10 dice roller 
public int d10() {
d10Result += random.nextInt(10) +1;
return d10Result;
}

//D20 dice roller 
public int d20() {
d20Result += random.nextInt(20) +11;
return d20Result;
}

//D30 dice roller 
public int d30() {
d30Result += random.nextInt(30) +21;
return d30Result;
}

//D50 dice roller 
public int d50() {
d50Result += random.nextInt(50) +41;
return d50Result;
}

//D100 dice roller 
public int d100() {
d100Result += random.nextInt(100);
return d100Result;
}

//THE MAIN METHOD ALL THE FUN STARTS HERE!!!
public static void main(String[] args) {
    Hive hive = new Hive();
    hive.openingScene();
    hive.mainMenu();
    }
}

下面是这些事件的代码:

代码语言:javascript
运行
复制
public class Events {

//MOUNTAIN EVENTS START

//Event 1: Lose all drones
public void mountEvent1() {
    System.out.println("------------------------------------------------------------------------------------------------------------------");
    System.out.println("Your drones pick through the crags and stones of the mountain, searching for resources for your growing hive.");
    System.out.println("There is a rumble and your drones get one good look at the wave of stone");
    System.out.println("headed toward them before your link to them is severed.");
    System.out.println("------------------------------------------------------------------------------------------------------------------");
}

//Event 2: Combat Encounter
public void mountEvent2() {
System.out.println("Your drones make their way through the mountain when they are suddenly ambushed!");
}

//Event 3: Combat Encounter
public void mountEvent3() {
System.out.println("While out exploring the mountainous wilderness your drones encounter a hostile creature!");
}

//Event 4: Combat Encounter
public void mountEvent4() {
System.out.println("Your children don't get far from the hive before they encounter a hostile creature.");
}

//Event 5: Combat Encounter
public void mountEvent5() {
System.out.println("While out patrolling the mountains, your drones encounter a hostile creature!");
}

//Event 6: Resource Gain, Metals
public void mountEvent6() {
System.out.println("While out patrolling the mountains, your drones find a small deposit of metal ore.");
}

//Event 7: Resource Gain, Metals
public void mountEvent7() {
System.out.println("While out patrolling the mountains, your drones find a respectable deposit of metal ore.");
}

//Event 8: Resource Gain, Metals + Nutrients
public void mountEvent8() {
System.out.println("Your drones find the carcass of a large mountainous creature.");
}

//Event 9: Resource Gain Metals
public void mountEvent9() {
System.out.println("While out patrolling the mountains, your drones find a large deposit of metal ore.");
}

//Event 9a: Large Resource Gain Metals
public void mountEvent9a() {
System.out.println("While out patrolling the mountains, your drones find an enormous deposit of metal ore.");
}

/**In which the hive discovers a colossal derelict spaceship and has free access to a 
 * random value of metals depending on how many workers they send to mine it. Basically
 * a metal farm. Unlocks once on the main menu.
 */ 
public void mountEvent10() {
System.out.println("");
}
//MOUNTAIN EVENTS END

//FOREST EVENTS START
//Event 1: All drones killed
public void forestEvent1() {
System.out.println("Your drones scout out the forest before your connection is severed from them one by one.");
System.out.println("Whatever is picking them off is too fast to see, and you only catch a blur of movement");
System.out.println("before the last of your children vanishes from your concience.");   
}

//Event 2: Combat Encounter
public void forestEvent2() {
System.out.println("Your drones encounter a creature while out patrolling the forest"); 
}

//Event 3: Find pathetic amount of nutrients
public void forestEvent3() {
System.out.println("Your drones harvest some of the local flora and bring it back to the hive!");   
}

//Event 4: Combat Encounter
public void forestEvent4() {
System.out.println("Your drones stumble across a hostile creature while in the forest!");   
}

//Event 5: Combat Encounter 
public void forestEvent5() {
System.out.println("Your drones are ambushed by a hostile creature in the forest!");        
}

//Event 6: Find acceptable amount of nutrients
public void forestEvent6() {
System.out.println("Your drones capture a small den of creatures that they break down into nutrients.");    
}

//Event 7: find acceptable amount of nutrients
public void forestEvent7() {
System.out.println("Your drones notice several creatures eating berries off of a bush.");
System.out.println("They cheerfully uproot the bush and bring it back to the hive!");   
}

//Event 8: find good amount of nutrients
public void forestEvent8() {
System.out.println("Your drones encounter the corpse of a creature cooling in the forest!");    
}

//Event9: find excellent amount of nutrients
public void forestEvent9() {
System.out.println("Your drones encounter the corpse of a large creature cooling in the forest!");
}

//Event 9a: difficult encounter for VERY high yield of nutrients (maybe a danger trigger or something)
public void forestEvent9a() {
System.out.println("Your drones hear a screech and before they can react a 
very large creature attacks them!");
}

/**In which the Hive discovers a giant mushroom that they can harvest for free nutrients. 
* Unlocks the mushroom on the Main menu.
*/
public void forestEvent10() {
System.out.println("Your drones notice a river running red with what looks like a viscous sap.");
System.out.println("When one of yur drones tries this fluid, you notice that it is very nutritious!");
System.out.println("Your drones follow the flow of the river to it's source and find themselves before a gigantic mostly submeged mushroom!");
System.out.println("The water that flows around the mushroom is converted into this nutritious red jelly!");
System.out.println("Elated, you order your drones to collect as much of this foodstuff as possible before returning!");
System.out.println("And now that you know where it is, your drones can always come back to collect more with ease!");

}
//FOREST EVENTS END

//PLAINS EVENTS START
//Event 1: All drones killed
public void plainsEvent1() {
System.out.println("As your drones venture along the grassy plains, you hear a sieries of sonic shrieks from above.");
System.out.println("Without warning, hordes of blurry shapes burst from the cloudy skies and snatch all of drones before retreating back into the clouds.");
System.out.println("All of your drones have been killed!");
}

//Event 2: Combat Encounter
public void plainsEvent2() {
System.out.println("While venturing through the plains, your children encounter a hostile creature!");
}

//Event 3: Combat Encounter
public void plainsEvent3() {
System.out.println("Your children are exploring the vast plains when the hear a screech from above!");
}

//Event 4: Combat Encounter
public void plainsEvent4() {
System.out.println("Your children ecounter one of the many hostile creatures in the plains!");
}

//Event 5: Combat Encounter
public void plainsEvent5() {
System.out.println("Your drones don't have to wander in the plains long before they encounter a hostile creature!");
}

//Event 6: Combat Encounter
public void plainsEvent6() {
System.out.println("Your children are immediately attacked by a hostile creature.");

}

//Event 7: Nutrient Gain
public void plainsEvent7() {
System.out.println("Your drones find the still warm corpse of one of the many hostile creatures in the area.");
System.out.println("They gather what remains they can and return to the hive.");
}

//Event 8: Metals gain
public void plainsEvent8() {
System.out.println("While out in the plains your drones come across a deposit of surface metals!");

}

//Event 9 Large Nutrient gain
public void plainsEvent9() {
System.out.println("While out exploring, your drones come into contact with one of the enormous armored insectoids that roam the plains.");
System.out.println("It is heavily wounded, though you find no trace of what brought it to this state.");
System.out.println("You witness the creature's last heaving breaths before it goes still.");
System.out.println("Quickly as to avoid other predators, your drones strip as much meat off the creature as possible and return to the hive.");
}

//Event 9a Large Metals gain
public void plainsEvent9a() {
System.out.println("Your drones are exploring the plains when a line of fire scorches across the sky and lands deeper into the plains.");
System.out.println("Carefully your children approach the crash site, and chitter with glee when they peer over the lip of the impact crater.");
System.out.println("Resting at the bottom of the pit is a massive meteor, shimmering with heat and the glow of raw metals.");
System.out.println("After waiting for the meteor to cool, your drones carefully extract what metals they can and make their way back to the hive.");

}

/**
* In which the hive can fight some gigantic bug thing and if they win get research for 
* VERY powerful chitin armor, and unlock the titantic creature class. 
*/
public void plainsEvent10() {
System.out.println("Your drones encounter one of the heavily armored insectoids that roam the plains!");
System.out.println("Placeholder text");

}
//PLAINS EVENTS END

//COAST EVENTS START
//Event 1: All drones die
public void coastEvent1() {
System.out.println("Your drones pick across the beach carefully, scanning for useful things for the hive.");
System.out.println("Suddenly massive barbed tentacle erupts from the water and sweeps all of your drones beneath the waves.");
System.out.println("The last thing you can see from their perspective is a truly massive beast snapping up all your drones");
System.out.println("before your connection to them is cut.");
}
//Event 2:  Combat Encounter
public void coastEvent2() {
System.out.println("Your drones encounter a hostile creature on the beach!");
}
//Event 3:  Combat Encounter 
public void coastEvent3() {
System.out.println("While travelling along the coast, your drones encounter a hostile creature");
}
//Event 4:  Find small metals and nutrients
public void coastEvent4() {
System.out.println("Your drones recover the remains of some form of crustacean whose shell is oddly metallic.");
}
//Event 5:  Combat Encounter
public void coastEvent5() {
System.out.println("Your children's search for materials on the beach is interrupted when a hostile creature");
System.out.println("attacks!");
}
//Event 6:  Combat Encounter
public void coastEvent6() {
System.out.println("The drones you sent to explore the coast are attacked by a hostile creature!");
}
//Event 7:  Find acceptable metals and nutrients
public void coastEvent7() {
System.out.println("Your drones find a plantlike growth growing on a metallic outcropping.");
System.out.println("They bring samples of both back to the hive.");
}
//Event 8:  Find good metals and nutrients
public void coastEvent8() {
System.out.println("Your drones find an empty metallic shell while exploring the coast.");
System.out.println("The ragged chunks of meat lining the inside indicate that it was emptied by force.");
System.out.println("Your children bring samples of both back to the hive.");
}
//Event 9: Combat Encounter
public void coastEvent9() {
System.out.println("As your drones explore the breadth of the coast, a large hostile creature attacks!");
}
//Event 9a: Find excellent metals and nutrients
public void coastEvent9a() {
System.out.println("Your drones find the beached remains of an larged crustacean while exploring the coast.");
System.out.println("It's hardly been touched by any passing creatures, so your drones collect as much as they.");
System.out.println("can and return to the hive.");
}
/**
* In which the hive finds....something cool I dont know yet. 
*/      
public void coastEvent10() {
System.out.println("YOU ROLLED A 100 MO FUCKAAAAA!");
System.out.println("This is a placeholder for something truly spectacular");
}
//COAST EVENTS END

}
EN

回答 1

Code Review用户

回答已采纳

发布于 2018-03-11 01:22:23

1 Scanner /流

static Scanner myScanner = new Scanner(System.in); static Scanner enterScanner = new Scanner(System.in);

System.in是公开的。因此,没有理由让Scanner屏蔽它比公开(您使它包-私有,默认)。

代码语言:javascript
运行
复制
     public final static Scanner myScanner = new Scanner(System.in);

对于Scanner,您也不需要两个System.in对象。一个是丰富的,不太可能导致混乱。

由于Scanner将在整个程序的生命周期中保持不变,所以我们可以使它成为final

如前所述,每个流只需要一个Scanner。我们只需要多个Scanner对象,如果我们有多个流,例如文件或其他。

结构

Hive是巨大的。如果我要编辑这段代码而不是简单的更改,那么我要做的第一件事就是将它重构成更小的部分。这将帮助我弄清楚什么应该和不应该一起进行。

在其中包含main的类做任何事情都会让人感到困惑。第一步可能是将main移到自己的类中。然后,您可以开始考虑其他元素是如何交互的。他们应该一起上课吗?有单独的课程?

结果存储在Hive中。为什么不为此提供一个Results类呢?

同样,为什么不提供一个单独的类来处理输入和输出呢?或者提供一个单独的类来管理资源之类的事情。

您可以创建一个Critter接口或类,而不是更改Hive字段。

迁移到GUI时遇到困难的原因之一是您的输入和输出与其他所有内容混合在一起。尝试将游戏逻辑从输入和输出中分离出来。然后,您就不必更改逻辑以迁移到GUI。

票数 4
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/189176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档