Javaでナベアツゲーム

プログラミングのお題を共有していこうかと。
今回はJavaでコンソールのゲームを作ってみた。

【ルール】
  • 3の倍数と3の付く数字の時は x を入力
  • それ以外は c を入力する
  • 間違えたらゲームオーバー
  • 数える数字はモードで切り替えられる(Short, Midium, Long)

Nabeatsu.java
/**
3の倍数と3の付く数字の時はx
それ以外はcを押す
違ったらゲームオーバー
*/
import java.io.*;
class Nabeatsu {
public static void main(String[] args) throws IOException {
// モード選択
System.out.print("【ナベアツゲーム】\n3の倍数もしくは3が含まれる数の時は x を。\nその他は c を入力して下さい。\n");
System.out.print("Short(max:30) :1\nMidium(max:50) :2\nLong(max:100) :3\nSelect Mode => ");
BufferedReader modeIn = new BufferedReader(new InputStreamReader(System.in));
int modeNum = Integer.parseInt(modeIn.readLine());
// ゲーム開始
System.out.println("\n= START =");
long start = System.currentTimeMillis();
game(modeNum);
long stop = System.currentTimeMillis();
// クリアタイム表示
System.out.println("[クリアタイム:" + (stop - start) + " ms]");
}
// Game本体
public static void game(int modeNum) throws IOException {
// Modeごとの回数設定
int maxNum = 0;
switch (modeNum) {
case 1:
maxNum = 30;
break;
case 2:
maxNum = 50;
break;
case 3:
maxNum = 100;
break;
default:
System.out.println("1~3 の数字をひとつ入力して下さい。");
System.exit(0);
}
// コア
for (int i = 1; i <= maxNum; i++) {
System.out.print(i + "-> ");
// 入力受付
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
String input = r.readLine();
String numstr = Integer.toString(i);
// 入力判定
if (i % 3 == 0 && input.equals("x") || numstr.indexOf("3") != -1 && input.equals("x")) {
System.out.println("アホ");
} else if (numstr.indexOf("3") != -1 && ! input.equals("x")) {
System.out.println("== GAME OVER ==");
System.exit(0);
} else if (i % 3 != 0 && input.equals("c")) {
} else {
System.out.println("== GAME OVER ==");
System.exit(0);
}
}
// Clearメッセージ
System.out.println("== CLEAR! ==");
}
}
view raw Nabeatsu.java hosted with ❤ by GitHub

実行結果

[C:\Java\source]$ javaexec Nabeatsu.java
【ナベアツゲーム】
3の倍数もしくは3が含まれる数の時は x を。
その他は c を入力して下さい。
Short(max:30)   :1
Midium(max:50)  :2
Long(max:100)   :3
Select Mode => 1

= START =
1-> c
2-> c
3-> x
アホ
4-> c
5-> c
6-> x
アホ
7-> c
8-> c
9-> x
アホ
10-> c
11-> c
12-> x
アホ
13-> x
アホ
14-> c
15-> x
アホ
16-> c
17-> c
18-> x
アホ
19-> c
20-> c
21-> x
アホ
22-> c
23-> x
アホ
24-> x
アホ
25-> c
26-> c
27-> x
アホ
28-> c
29-> c
30-> x
アホ
== CLEAR! ==
[クリアタイム:27066 ms]

コメント

このブログの人気の投稿

Vue.jsでimgタグのsrcをバインドさせる際の注意点

Windowsコマンドプロンプトをかっこよくする

vscodeとPlantUMLでロバストネス図を書く