diff --git a/game/Console.java b/game/Console.java index c37722d..9b9fb1b 100644 --- a/game/Console.java +++ b/game/Console.java @@ -1,17 +1,24 @@ package game; +import java.io.*; import java.util.Locale; import static java.lang.System.err; +import static java.lang.System.in; import static java.lang.System.out; /** - * コンソールへの出力を行うユーティリティクラスです。 + * コンソール関連の処理を行うユーティリティクラスです。 * * @since 0.0.1p */ public final class Console { + /* 一文字入力に使用するInputStream */ + private static BufferedInputStream charInput; + /* 文字列取得に使用するReader */ + private static BufferedReader stringInput; + /* 特定の地域を表す 開発地域が日本なので(笑)日本にしておく */ private static Locale defaultLocale = Locale.JAPAN; @@ -166,4 +173,50 @@ public static void putcolrgb(String str, int r, int g, int b) { put("\\u001B[38;2;" + r + ";" + g + ";" + b + "m" + str); } + + /** + * 一文字だけ入力された値を取得します。 + * + * @return 入力された文字 エラーが発生した場合0(0x30ではなく0x00) + * + * @throws IOException 入出力例外が発生した場合 + */ + public static char getInputChar() throws IOException + { + /* 入力させる */ + charInput = new BufferedInputStream(in, 1); + + /* 読み込み */ + int read = charInput.read(); + + /* エラーでない */ + if(read != -1){ + /* 読み込めるバイトをすべて読み飛ばす */ + charInput.skip(charInput.available()); + return (char)read; + }else return (char)0; + } + + /** + * 文字列の入力された値を取得します。 + * もしかしたら200バイト以上の入力で例外が発生するかもしれません。 + * + * @return 入力された文字列 エラーが発生した場合null + * + * @throws IOException 入出力例外が発生した場合 + */ + public static String getInputString() throws IOException + { + /* 入力させる 基本的にこれ以上入れる人は少ないと思うから200バイト(全角100文字分) */ + stringInput = new BufferedReader(new InputStreamReader(in), 200); + + /* 読み込み */ + String read = stringInput.readLine(); + + /* エラーでない */ + if(read != null){ + /* 読み飛ばしは改行(読み込み開始)までを上で取得しているのでいらない */ + return read; + }else return null; + } } diff --git a/game/Game.java b/game/Game.java index 93c4a7a..e6b52e9 100644 --- a/game/Game.java +++ b/game/Game.java @@ -1,5 +1,7 @@ package game; +import java.io.IOException; + import game.ConsEsc; import static game.Console.*; @@ -22,6 +24,37 @@ public static void main(String[] args) write(" Mini Game++ ", ConsEsc.ESC_ULINE); write(" ver:0.0.1 "); putll("---------------"); + + write("1.セーブデータを作る/開く"); + write("x.終了する"); + + char c = input(); + + if(c == '1'){ + write("名前を入れてください!"); + String name = read(); + putf("%s lv.xxがログインしたよ!" + NEWLINE + NEWLINE, name); + maintown(name); + /* System.exitだとファイナライザが呼ばれない(らしい)ので */ + /* あえてreturnで終わらせてみる(意味があるかは知らない) */ + }else if(c == 'x') return; + } + + private static void maintown(String name) + { + write("初めの町 住宅街"); + putll(name + "はどうしようか?"); + write("x.ゲーム終了"); + char c = input(); + + if(c == 'x'){ + write("お疲れ様!ゲームを終了します!"); + write(name + "さんがログアウトしました!"); + return; + }else{ + write("関係ない入力がされました。", ConsEsc.ESC_TRED); + maintown(name); + } } /* ANSIエスケープシーケンスを使用するか */ @@ -53,6 +86,7 @@ public static boolean getAnsi() * * @param str 出力する文字列 * @param escapes 出力時に使用するエスケープ + * */ public static void write(String str, ConsEsc... escapes) { @@ -60,4 +94,42 @@ public static void write(String str, ConsEsc... escapes) if(useAnsi) pute(str + NEWLINE, escapes); else putl(str); } + + /** + * 一文字取得して返します。 + * Console.getInputChar に例外処理を追加しただけです。 + * + * @return 入力された文字 エラーの場合0が戻る(0x30でなく0x00) + * + * @see game.Console#getInputChar() + */ + public static char input() + { + try{ + char c = getInputChar(); + return c; + }catch(IOException ie){ + write("入出力でエラーが発生しました!", ConsEsc.ESC_TRED); + return (char)0; + } + } + + /** + * 文字列を取得して返します。 + * Console.getInputString に例外処理を追加しただけです。 + * + * @return 入力された文字列 エラーの場合nullが戻る + * + * @see game.Console#getInputString() + */ + public static String read() + { + try{ + String s = getInputString(); + return s; + }catch(IOException ie){ + write("入出力でエラーが発生しました!", ConsEsc.ESC_TRED); + return null; + } + } }