LoginSignup
2
6

More than 5 years have passed since last update.

初心者がJavaでじゃんけんゲーム

Posted at

勉強始めて1ヶ月くらいなので復習ついでにじゃんけんゲーム書いてみました。
メインメソッドだけのプログラムは割とすぐ書けたんですが、
オブジェクト指向らしきものを作ろうとしたら結構難航しました。
独学なので色々とツッコミどころあるんじゃないかと思います。

コード

本体

Main.java
package janken;

public class Main {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        final String errorException = "正しい数値を入力してください。";
        final String errorMessage = "0~2、もしくは99の数値を入力してください。";
        final String exitMessage = "終了します";
        final String menuMessage = "0:グー 1:チョキ 2:パー 99:終了";
        Player you = new Player();
        Player enemy = new Player();

        for(;;) {
            System.out.println(menuMessage);
            try {
                you.jankenNum = new java.util.Scanner(System.in).nextInt(); 
                enemy.jankenNum = new java.util.Random().nextInt(3);
                if(you.jankenNum == 99) {
                    //終了
                    System.out.println("");
                    System.out.println(exitMessage);
                    System.out.println("");
                    break;
                } else if(you.jankenNum < 0 | you.jankenNum > 2) {
                    //正しくない数値が入力されたら…
                    System.out.println("");
                    System.out.println(errorMessage);
                    System.out.println("");
                } else {
                    //バトルメソッド呼び出し。
                    Battle result = new Battle();
                    result.battle(you, enemy);
                }

            } catch(java.util.InputMismatchException error) {
                //数値以外が入力されたら…
                System.out.println("");
                System.out.println(errorException);
                System.out.println("");
            }
        }
    }
}

プレイヤー

Player.java
package janken;

public class Player {
    //プレイヤーの勝敗数を保持。じゃんけんの手、勝敗などを定義。
    int jankenNum;
    final String goo = "グー";
    final String choki = "チョキ";
    final String par = "パー";
    int win; int lose; int draw;

    String Hand() {
        //プレイヤーの数字をじゃんけんの手に変換。
        switch(this.jankenNum) {
        case 0:
            return this.goo;
        case 1:
            return this.choki;
        case 2:
            return this.par;
        default:
            return null;
        }
    }

    void board(int win, int lose, int draw) {
        //勝敗数を表示する。
        System.out.println("Win:" + this.win + " Lose:" + this.lose + " Draw:" + this.draw);
    }
}

バトル

Battle.java
package janken;

public class Battle {
    //勝敗を決める。
    final String aiko = "あいこ";
    final String kachi = "勝ち";
    final String make = "負け";

    void battle(Player pc1, Player pc2) {
        //プレイヤー同士での勝敗を決定する。
        int result = (pc1.jankenNum - pc2.jankenNum +3) % 3;
        switch(result) {
        //お互いの手から勝敗を決定し、スコアとして保管。結果を表示。
        case 0:
            System.out.println("");
            System.out.println(pc1.Hand() + " VS " + pc2.Hand());
            System.out.println(aiko);
            pc1.draw += 1; pc2.draw += 1;
            pc1.board(pc1.win, pc1.lose, pc1.draw);
            System.out.println("");
            break;
        case 1:
            System.out.println("");
            System.out.println(pc1.Hand() + " VS " + pc2.Hand());
            System.out.println(make);
            pc1.lose += 1; pc2.win += 1;
            pc1.board(pc1.win, pc1.lose, pc1.draw);
            System.out.println("");
            break;
        case 2:
            System.out.println("");
            System.out.println(pc1.Hand() + " VS " + pc2.Hand());
            System.out.println(kachi);
            pc1.win += 1; pc2.lose += 1;
            pc1.board(pc1.win, pc1.lose, pc1.draw);
            System.out.println("");
            break;
        }
    }
}

とりあえずそれっぽく動いたので満足。
どう分割していくかとか考えるとなかなか難しいですね。

2
6
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
6