qiita の記事のコードは「初心者がゲームを作った」ということで結構ごちゃごちゃです。
初心者のコードを初心者が参考にするのはあまり宜しくない気がしますが、だからと言って初心者じゃない コードを参考にするのも大変とは思います。が、まぁ参考の参考というか、違うコードを見るのも何かになるかもということでちょっと(?)直して(??)みました。(以下の 5 ファイルだけで、パッケージは使っていません。)
どこかのフォルダに5つを作成し、 javac Main.java
→ java Main
で動作すると思います。(未確認)
Main.java
java
1 import java . io . * ;
2 import java . util . * ;
3
4 public class Main {
5 public static void main ( String [ ] args ) throws Exception {
6
7 DragonsNest nest = new DragonsNest ( ) ;
8
9 System . out . println ( "ドラゴンのバトルを始めます" ) ;
10
11 try ( BufferedReader br = new BufferedReader ( new InputStreamReader ( System . in ) ) ) {
12
13 System . out . println ( "ID:\tドラゴン名" ) ;
14 Attribute attrs [ ] = Attribute . values ( ) ;
15 for ( int i = 0 ; i < attrs . length ; i ++ ) System . out . println ( ( i + 1 ) + ":\t" + attrs [ i ] ) ;
16
17 System . out . print ( "自分が使用するドラゴンをidで選んでください>" ) ;
18 int choiceMyDragon = Integer . parseInt ( br . readLine ( ) ) - 1 ;
19 Dragon myDragon = nest . summon ( attrs [ choiceMyDragon ] ) ;
20
21 System . out . print ( "相手に使用させるドラゴンをidで選んでください>" ) ;
22 int choiceOpponentDragon = Integer . parseInt ( br . readLine ( ) ) - 1 ;
23 Dragon oppoDragon = nest . summon ( attrs [ choiceOpponentDragon ] ) ;
24
25 doBattle ( br , myDragon , oppoDragon ) ;
26
27 } catch ( Exception e ) {
28 System . out . println ( "深刻なエラーが出ました. e=" + e ) ;
29 }
30 }
31
32 static void doBattle ( BufferedReader br , Dragon myDragon , Dragon oppoDragon ) throws IOException {
33 System . out . println ( "ドラゴンバトルをスタートします。" ) ;
34
35 while ( true ) {
36 Action myAction = selectAction ( br , myDragon ) ;
37 System . out . println ( "こちらの攻撃!" ) ;
38 if ( attack ( myDragon , myAction , oppoDragon ) == 0 ) {
39 System . out . println ( "\n勝ちました!" ) ;
40 break ;
41 }
42
43 Action oppoAction = autoAction ( oppoDragon ) ;
44 System . out . println ( "相手の攻撃!" ) ;
45 if ( attack ( oppoDragon , oppoAction , myDragon ) == 0 ) {
46 System . out . println ( "\n負けました‥" ) ;
47 break ;
48 }
49 }
50
51 System . out . println ( "バトルが終了しました" ) ;
52 }
53
54 static Action selectAction ( BufferedReader br , Dragon dragon ) throws IOException {
55 while ( true ) {
56 System . out . println ( ) ;
57 System . out . println ( "コマンド: 技名 (残りポイント)" ) ;
58 for ( int i = 0 ; i < dragon . getActionCount ( ) ; i ++ ) {
59 System . out . println ( ( i + 1 ) + ": " + dragon . getAction ( i ) + " (" + dragon . getActionPoint ( i ) + ")" ) ;
60 }
61 System . out . println ( "技を選んでください>" ) ;
62 int actionNo = Integer . parseInt ( br . readLine ( ) ) - 1 ;
63
64 int actionPoint = dragon . getActionPoint ( actionNo ) ;
65 if ( actionPoint == 0 ) {
66 System . out . println ( "その技はもう残りポイントがありません" ) ;
67 continue ; //再選択
68 }
69
70 dragon . setActionPoint ( actionNo , actionPoint - 1 ) ;
71
72 return dragon . getAction ( actionNo ) ;
73 }
74 }
75
76 static Action autoAction ( Dragon dragon ) {
77 //ポイントが残っている技の中からランダムに選択
78 List < Integer > actionNoList = new ArrayList < > ( ) ;
79 for ( int i = 0 ; i < dragon . getActionCount ( ) ; i ++ ) if ( dragon . getActionPoint ( i ) >= 1 ) actionNoList . add ( i ) ;
80 Collections . shuffle ( actionNoList ) ;
81 int actionNo = actionNoList . get ( 0 ) ;
82
83 int actionPoint = dragon . getActionPoint ( actionNo ) ;
84 dragon . setActionPoint ( actionNo , actionPoint - 1 ) ;
85
86 return dragon . getAction ( actionNo ) ;
87 }
88
89 static int attack ( Dragon offence , Action action , Dragon defence ) {
90 System . out . println ( offence + "の" + action + "!!" ) ;
91
92 int leftHitPoint = Math . max ( defence . getHitPoint ( ) - action . power , 0 ) ;
93 defence . setHitPoint ( leftHitPoint ) ;
94 System . out . println ( defence + "は" + action . power + "のダメージで残り体力が" + leftHitPoint + "になった!" ) ;
95
96 return leftHitPoint ;
97 }
98 }
DragonsNest.java
java
1 4
5 class DragonsNest {
6
7 private static class DragonImpl implements Dragon {
8 private final Attribute attribute ;
9 private int hitPoint ;
10 private int [ ] actionPoint ;
11
12 private DragonImpl ( Attribute attribute ) {
13 this . attribute = attribute ;
14 this . hitPoint = attribute . hitPoint ;
15 this . actionPoint = new int [ attribute . actions . length ] ;
16 for ( int i = 0 ; i < actionPoint . length ; i ++ ) this . actionPoint [ i ] = attribute . actions [ i ] . actionPoint ;
17 }
18
19 @Override
20 public int getHitPoint ( ) { return hitPoint ; }
21 @Override
22 public void setHitPoint ( int hp ) { hitPoint = hp ; }
23
24 @Override
25 public int getActionCount ( ) { return actionPoint . length ; }
26 @Override
27 public Action getAction ( int i ) { return attribute . actions [ i ] ; }
28 @Override
29 public int getActionPoint ( int i ) { return actionPoint [ i ] ; }
30 @Override
31 public void setActionPoint ( int i , int ap ) { actionPoint [ i ] = ap ; }
32
33 @Override
34 public String toString ( ) { return attribute . name ( ) ; }
35 }
36 38 39 40
41 Dragon summon ( Attribute attr ) {
42 return new DragonImpl ( attr ) ;
43 }
44 }
Dragon.java
java
1
2 interface Dragon {
3 int getHitPoint ( ) ;
4 void setHitPoint ( int hp ) ;
5
6 int getActionCount ( ) ;
7 Action getAction ( int i ) ;
8 int getActionPoint ( int i ) ;
9 void setActionPoint ( int i , int ap ) ;
10 }
Action.java
java
1
2 enum Action {
3 アタック ( 2 , 20 ) , ホワイトブレス ( 4 , 2 ) , ブルーブレス ( 3 , 2 ) , レッドブレス ( 5 , 2 ) , つよいアタック ( 6 , 1 ) ;
4
5 final int power ;
6 final int actionPoint ;
7
8 private Action ( int power , int actionPoint ) {
9 this . power = power ;
10 this . actionPoint = actionPoint ;
11 }
12 }
Attribute.java
java
1
2 enum Attribute {
3 ホワイトドラゴン ( 20 , Action . アタック , Action . ホワイトブレス , Action . つよいアタック ) ,
4 ブルードラゴン ( 25 , Action . アタック , Action . ブルーブレス , Action . つよいアタック ) ,
5 レッドドラゴン ( 15 , Action . アタック , Action . レッドブレス , Action . つよいアタック ) ;
6
7 final int hitPoint ;
8 final Action [ ] actions ;
9
10 Attribute ( int hitPoint , Action . . . actions ) {
11 this . hitPoint = hitPoint ;
12 this . actions = actions ;
13 }
14 }