简化RPG战斗方法

时间:2014-03-18 16:25:36

标签: java methods simplification

我正在努力解决一大堆代码问题,它现在应该可以正常工作,但可能会有所改进吗?我正在为一个学校作业制作一个RPG游戏,我现在可以把它交给它了。但如果它可以改进(缩短),我会很高兴看到一些指示:)

对其他方法的所有调用都有效(例如player.isDead等)。

有问题的两种方法是:

我从Game类打电话给.fight开始战斗(带有怪物列表)。

public void fight() {
    monster = monsterList.get(random.nextInt (monsterList.size()));                
    System.out.println("A" + " " + monster.getName() + " " + monster.getEnter());
    //turn 0 = player 
    //turn 1 = monster       
    fight:
    while(!player.isDead() && !monster.isDead()) {
        while(turn == 0) {
            String input = inputreader.getCommand();            
            if(input.contains ("run")) {
                player.setGold(player.getGold()-50); 
                System.out.println("You throw 50 gold at the monster and escape in the confusion.");
                break fight;
            }
            else if(input.contains ("attack")) {                    
                break;
            }
            else if(input.contains ("help")) {
                printHelp();
            } 
            else if(input.contains ("inspect")) {
                printDescription(monster);
            }              
        }   
        gameAttack();
    }

}
/**
 * The fight between the monster and the player
 */
private void gameAttack() {                       
    System.out.println("You attack! ");
    int playerDamage = random.nextInt(21)+10;
    monster.changeHealth(-playerDamage);
    System.out.println(player.getName() + " hits " + monster.getName() + " for " + playerDamage + " damage! "
        + monster.getName() + " has " + monster.getHealth() + "/" + monster.getMaxHealth() + " hitpoints left! ");
    if(monster.isDead()) {
        System.out.println(player.getName() + " is victorious! " + "\n" + player.getName() + " looted the monster for " 
            + monster.getGold() + " gold ");
        player.setGold(player.getGold() + monster.getGold());
    }    
    else{    
        turn = 1;
        System.out.println("The monster attacks!");
        int monsterDamage = random.nextInt(21)+20;
        player.changeHealth(-monsterDamage);
        System.out.println(monster.getName() + " hits " + player.getName() + " for " + monsterDamage + " damage! "
            + player.getName() + " has " + player.getHealth() + "/" + player.getMaxHealth() + " hitpoints left!");
    }
    if(player.isDead()) {
        System.out.println(player.getName() + " has been slain by " + monster.getName() + ".\n" + "Game over, bro!");
    }
    else{
        turn = 0;
    }
}

0 个答案:

没有答案