保守性の良いじゃんけんプログラムを考えてみた

image

はじめに

Taigaです。今年は「良いコード悪いコードで学ぶ設計入門」の輪読会に参加したり、アプリケーションの保守性について勉強した一年だったと思います。
そのまとめとして、じゃんけんプログラムを丁寧にクラス化して保守性を向上した過程をお見せします!

今回は、コンソールに1~3の数字を入力すると結果を出力するような形式のじゃんけんプログラムを考えました。

直感的に実装してみる

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Random random = new Random();
        
        System.out.println("出す手を決めて下さい");
        System.out.println("1: グー");
        System.out.println("2: チョキ");
        System.out.println("3: パー");
        
        System.out.print("あなたの手 > ");
        int player_hand = sc.nextInt();
        
        int opponent_hand = random.nextInt(3) + 1;
        System.out.println("相手の手 > " + opponent_hand);
        
        if(player_hand == 1){
            if(opponent_hand == 1) System.out.println("あいこ");
            if(opponent_hand == 2) System.out.println("勝ち!");
            if(opponent_hand == 3) System.out.println("負け...");
        }else if(player_hand == 2){
            if(opponent_hand == 1) System.out.println("負け...");
            if(opponent_hand == 2) System.out.println("あいこ");
            if(opponent_hand == 3) System.out.println("勝ち!");
        }else if(player_hand == 3){
            if(opponent_hand == 1) System.out.println("勝ち!");
            if(opponent_hand == 2) System.out.println("負け...");
            if(opponent_hand == 3) System.out.println("あいこ");
        }else{
            System.out.println("1, 2, 3のどれかを入力して下さい");
        }
    }
}

書いてみて気になるのは、勝敗を判定するロジックです。ではどんなところに問題があるでしょうか?

個人的にまず気になるのは、player_hand, opponent_handがint型になっている所です。
player_hand == 1 かつ opponent_hand == 2 の時が勝ちなのか負けなのか分かりにくいです。

また、ロジックがMainクラスにベタ書きになっている所も気になります。
mainクラスでは実行順だけわかるようになっているのが分かりやすいんじゃないかと思います。
じゃんけんの細かいルールについて、mainクラスが知っている必要はなさそうです。

手をクラス化する

Handクラスを作ります。
Handクラスはidを持っていて、fight_againstメソッドで他の手と戦った結果を出力します。

public interface Hand {
    public int id();
    public String name();
    public String fight_against(Hand opponent_hand);
}
public class Gu implements Hand {
    @Override
    public int id(){return 1;}
    @Override
    public String name(){return "グー";}
    @Override
    public String fight_against(Hand opponent_hand) {
        if(opponent_hand.id() == 1) return "あいこ";
        if(opponent_hand.id() == 2) return "勝ち!";
        if(opponent_hand.id() == 3) return "負け...";
        return "error: invalid hand";
    }
}
public class Main {
    public static void main(String[] args) {
        final Scanner sc = new Scanner(System.in);
        final Random random = new Random();
        
        final Map<Integer, Hand> handMap = Map.of(1, new Gu(), 2, new Choki(), 3, new Pa());
        
        final Hand player_hand = handMap.get(sc.nextInt());
        final Hand opponent_hand = handMap.get(random.nextInt(3) + 1);
        
        final String result = player_hand.fight_against(opponent_hand);
    }
}

Mapを使ってグー/チョキ/パーのインスタンスをそれぞれ1, 2, 3と紐付けることで、player_hand, opponent_handをHand型で表現することができました。
これで、mainクラスは細かいルールについて知る必要がなくなりました。

ただ、Handクラスの内部ではint型のidによる判断が残ってしまっています。
idで判断するのではなく、enumで判断するようにします。

HandTypeを作る

public enum HandType {
    GU,
    CHOKI,
    PA,
}
public interface Hand {
    public HandType type();
    public String name();
    public String fight_against(Hand opponent_hand);
}
public class Gu implements Hand {
    @Override
    public HandType type(){return HandType.GU;}
    @Override
    public String name(){return "グー";}
    @Override
    public String fight_against(Hand opponent_hand) {
        if(opponent_hand.type() == HandType.GU) return "あいこ";
        if(opponent_hand.type() == HandType.CHOKI) return "勝ち!";
        if(opponent_hand.type() == HandType.Pa) return "負け...";
        return "error: invalid hand";
    }
}

ぱっと見でわかりやすい実装になってきました。

しかしまだ気になる点があります。Handクラスが勝敗を判断している所です。
Handクラスがじゃんけんゲームのルールまで担ってしまっているのは微妙な感じがします。

例えば3人以上でのじゃんけんに対応しよう!となった時、Handクラスが勝敗を判断していると修正が大変になります。
Handクラスは他の手との相性を返すようにして、帰ってきた相性をもとに勝ち負けを判断するJudgeクラスを作るのが良さそうです。

相性を返すようにする

public enum Affinity {
    GOOD,
    EVEN,
    BAD,
}
public class Gu implements Hand {
    @Override
    public HandType type(){return HandType.GU;}
    @Override
    public String name(){return "グー";}
    @Override
    public Affinity fight_against(Hand opponent_hand) throws Exception {
        if(opponent_hand.type() == HandType.GU) return Affinity.EVEN;
        if(opponent_hand.type() == HandType.CHOKI) Affinity.GOOD;
        if(opponent_hand.type() == HandType.PA) return Affinity.BAD;
        throw new Exception("error: invalid hand");
    }
}

Judgeクラスを作る

public class Judge {
    static String rule_2players(Hand player_hand, Hand opponent_hand){
        try{
            final Affinity affinity = player_hand.fight_against(opponent_hand);
            if (affinity == Affinity.GOOD)  return "勝ち!";
            if (affinity == Affinity.EVEN)  return "あいこ";
            if (affinity == Affinity.BAD)   return "負け...";
            return "error: invalid affinity";
        }catch (Exception e){
            return e.toString();
        }
    }
}
public class Main {
    public static void main(String[] args) {
        final Scanner sc = new Scanner(System.in);
        final Random random = new Random();
        
        final Map<Integer, Hand> handMap = Map.of(1, new Gu(), 2, new Choki(), 3, new Pa());
        
        final Hand player_hand = handMap.get(sc.nextInt());
        final Hand opponent_hand = handMap.get(random.nextInt(3) + 1);
        
        final String result = Judge.rule_2players(player_hand, opponent_hand);
    }
}

Handクラスは相性を知っているだけで、勝ち負けについてはJudgeクラスが判断するようになりました。
もし3人以上のじゃんけんを実装しよう!となっても、Judgeクラスに新たな判断ロジックを書けば実装できそうです。

最後に

保守性の高いコードを書いていると、後で機能追加をするときに書き換える部分が少なくて済んだり、バグの発生を未然に防ぐ事ができたりするようです。
またこれは人によるかもしれませんが、きちんと整理されたコードの方が心理的安全性も高くなる気がします。 この記事が保守性について考えるきっかけになっていたら嬉しいです。最後まで読んで頂いてありがとうございました。

お知らせ

カリキュラムなし!学びたいことだけをプロと学べる『TechCampus』の詳細情報

カリキュラムなし!学びたいことだけをプロと学べる『TechCampus』の詳細情報

カリキュラムなし!学びたいことだけをプロと学べる『TechCampus』の詳細情報!興味のある人はぜひコミュニティに参加ください!

Read More
可茂IT塾ではFlutterインターンを募集しています!

可茂IT塾ではFlutterインターンを募集しています!

可茂IT塾ではFlutterインターンを募集しています!可茂IT塾のエンジニアの判断で、一定以上のスキルをを習得した方には有給でのインターンも受け入れています。

Read More

関連の記事

タグ

Flutter (100)初心者向け (26)イベント (17)Google Apps Script (14)Nextjs (11)可茂IT塾 (8)Firebase (7)riverpod (6)React (6)ChatGPT (4)新卒 (4)就活 (4)デザイン (4)Dart (4)JavaScript (4)FlutterWeb (3)vscode (3)Prisma (3)NestJS (3)Slack (3)TypeScript (3)ワーケーション (3)インターン (3)お知らせ (3)設計 (2)線型計画法 (2)事例 (2)Image (2)File (2)画像 (2)Figma (2)iOS (2)アプリ開発 (2)React Hooks (2)tailwindcss (2)社会人 (2)大学生 (2)RSS (1)CodeRunner (1)個人開発 (1)Android (1)Unity (1)WebView (1)Twitter (1)フルリモート (1)TextScaler (1)textScaleFactor (1)学生向け (1)supabase (1)Java (1)Spring Boot (1)shell script (1)正規表現 (1)パワーポイント (1)趣味 (1)モンスターボール (1)CSS (1)SCSS (1)Cupertino (1)ListView (1)就活浪人 (1)既卒 (1)保守性 (1)iPad (1)シェアハウス (1)スクレイピング (1)PageView (1)画面遷移 (1)flutter_hooks (1)Gmail (1)GoogleWorkspace (1)ShaderMask (1)google map (1)Google Places API (1)GCPコンソール (1)Google_ML_Kit (1)Vercel (1)Google Domains (1)Git (1)オンラインオフィス (1)LINE (1)Bitcoin (1)bitFlyer (1)コミュニティー (1)文系エンジニア (1)Freezed (1)markdown (1)GlobalKey (1)ValueKey (1)Key (1)アイコン (1)go_router (1)debug (1)Apple Store Connect (1)FlutterGen (1)デバッグ (1)Widget Inspector (1)flutter (1)検索機能 (1)Shader (1)Navigator (1)メール送信 (1)Dio (1)CustomClipper (1)ClipPath (1)Material Design (1)カスタム認証 (1)figma (1)アニメーション (1)Arduino (1)ESP32 (1)経験談 (1)フリーランス (1)mac (1)csv (1)Dialog (1)BI (1)LifeHack (1)ショートカット (1)Chrome (1)高校生 (1)キャリア教育 (1)非同期処理 (1)生体認証 (1)BackdropFilter (1)レビュー (1)getAuth (1)Algolia (1)コンサルティング (1)Symbol (1)

お知らせ

カリキュラムなし!学びたいことだけをプロと学べる『TechCampus』の詳細情報

カリキュラムなし!学びたいことだけをプロと学べる『TechCampus』の詳細情報

カリキュラムなし!学びたいことだけをプロと学べる『TechCampus』の詳細情報!興味のある人はぜひコミュニティに参加ください!

Read More
可茂IT塾ではFlutterインターンを募集しています!

可茂IT塾ではFlutterインターンを募集しています!

可茂IT塾ではFlutterインターンを募集しています!可茂IT塾のエンジニアの判断で、一定以上のスキルをを習得した方には有給でのインターンも受け入れています。

Read More