2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

JAVAでHigh&Lowゲームを作ってみた

Last updated at Posted at 2025-12-19

はじめに

 今回は乱数を使用したトランプゲームを作成してみました。ドラクエのカジノにあるポーカーのダブルアップから思いつきました。カジノのように掛け金なども設定して遊べるので多少暇つぶしにはなると思います。

ゲームの仕様について

  1. どのくらいのコインを賭けるのかを入力(最初の所持金は1000)
  2. 一つのトランプが表示されそのトランプより次のトランプが大きい数字か小さい数字かを予想し入力
  3. 不正解なら賭け金没収で1に戻る、正解なら倍率が×2される
  4. まだ続けるかを選択する。終えるなら賭け金に倍率がかけられ、加算され1に戻る。続けるなら2に戻る

コインが0になればゲームオーバーになります。

JAVAコード

game.rb
package kare;
import java.util.Scanner;
import java.util.Random;


public class game {
public static void main(String[] args){
    Random rand = new Random();
    Scanner scanner = new Scanner(System.in);
    int money=1000; //最初の所持金
    int i=1;
    int kake=0;
    while (true) {
        System.out.print("現在の所持金 =>"+money+"\n掛け金を入力してください =>");
        kake = scanner.nextInt();
        if(kake<=money) break;
        System.out.println("お金が足りません");
    }
    money=money-kake;
    int tp1 = rand.nextInt(13)+1; //最初のトランプ
    tp(tp1); //トランプ表示関数

    while(true){
        int c=0,out=0;
        System.out.println("?");
        int tp2 = rand.nextInt(13)+1; //隠されてるトランプ
        System.out.print("強制終了=その他の値\nup=1  down=2    =>");
        int x = scanner.nextInt();
        System.out.println("\n===========================================================\n");
        
        switch (x) { //結果発表
            case 1:
                tp(tp1); tp(tp2); //トランプオープン 
                if(tp1<tp2){System.out.println("正解!"); c=1;}
                else if(tp1==tp2) System.out.println("draw");
                else {System.out.println("不正解"); out=1;};        
            break;

            case 2:
                tp(tp1); tp(tp2); //トランプオープン
                if(tp1>tp2){System.out.println("正解!"); c=1;}
                else if(tp1==tp2) System.out.println("draw");
                else {System.out.println("不正解"); out=1;}   
            break;

            default:
                return;
                
        }
        System.out.println("\n===========================================================\n");
        if(out==1){ //不正解の時
            if(money==0){ 
                System.out.println("ゲームオーバー");
                scanner.close();    
                return;
            }
            while (true) {
                System.out.print("exit=>-1\n現在の所持金 =>"+money+"\n掛け金を入力してください =>");
                kake = scanner.nextInt();
                if(kake==-1){System.out.println("最終結果=>"+money);scanner.close(); return;}
                if(kake<=money) break;
                System.out.println("お金が足りません");
            }

            money=money-kake;
            tp1 = rand.nextInt(13)+1;
            tp(tp1);
            i=1;
        }
        else if(c==1){
            kake=kake*2;
            i=i*2;
            System.out.println("現在の倍率=>"+i+"   貰える金額=>"+kake);
            System.out.print("強制終了=その他の値\n続ける=>1 やめる=>2   =>");
            x = scanner.nextInt();
            System.out.println("\n===========================================================\n");
            switch (x) {
                case 1:
                    tp1=tp2;
                    tp(tp1);
                    break;
                case 2:
                    money=money+kake;
                    while (true) {
                        System.out.print("exit=>-1\n現在の所持金 =>"+money+"\n掛け金を入力してください =>");
                        kake = scanner.nextInt();
                        if(kake==-1){System.out.println("最終結果=>"+money);scanner.close();return;}
                        if(kake<=money) break;
                        System.out.println("お金が足りません");
                    }

                    money=money-kake;
                    tp1 = rand.nextInt(13)+1;
                    tp(tp1);
                    break;
                default:
                    scanner.close();    
                    return;
            }
        }
        else{ //drawの場合何も金額は処理しない
            tp1=tp2;
            tp(tp1);
        }
    } 
}


public static void tp(int x){ //トランプ表示関数
    if (x==1) System.out.print("A   ");
    else if(x==11) System.out.print("J   ");
    else if(x==12) System.out.print("Q   ");
    else if(x==13) System.out.print("K   ");
    else System.out.print(x+"   ");
    }
    
}

終わりに

 今回は基礎的な知識のみでゲームを制作しました。結果として自分が考えていた通りの動作になりました。一人で一からこの量を考え打ち込んだのは初めてだったためかなりコードが汚くなってしまったため、改善点を探せば出てくるかと思います。今後もプログラミングの腕を高めていきたいと感じました。

2
0
0

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?