repl.it
@bikey/

JavaExam2019Dec-1

Java

No description

fork
loading
Files
  • Main.java
  • jdt.ls-java-project
  • CommandException.java
  • Dragon.java
  • Hero.java
  • Sword.java
  • Weapon.java
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class Main {
  public static void main(String[] args) {
    int command;  //入力したコマンド
    java.util.Scanner scan = new java.util.Scanner(System.in);  //キーボードからの入力
    
    Hero player = new Hero("ミナト");
    Dragon com = new Dragon();

    player.setTarget(com);
    com.setTarget(player);

    while(!(player.isDead() || com.isDead())) {
      player.showCommand();
      
      do {
        System.out.print("コマンド:");
        command = scan.nextInt();
        try {
          player.doCommand(command);
          break;
        } catch(Exception e) {
          System.out.println(e);
          continue;
        }
      } while(true);

      System.out.println();

      com.doRandomAction();

      System.out.println();
    }

    checkResult(player, com);
  }

  public static void checkResult(Hero player, Dragon com) {
    if(player.isDead()) {
      System.out.println("ざんねん!あなたの負けです...");
    } else if(com.isDead()) {
      System.out.println("おみごと!敵を倒しました!");
    } else {
      System.out.println("戦いはまだまだ続きます。");
    }
  }

  public static void gameEnd() {
    System.out.println("ゲームを終了します。");
    System.exit(0);
  }
}
?