LoginSignup
2
1

More than 3 years have passed since last update.

タイピングゲーム-ver1.0

Last updated at Posted at 2020-01-31
Typing.java

class Typing {

    Word word = new Word();


    //フィールド
    private int score;      //スコアを計測
    private int mode;       //モード選択
    private int miss; //ミス回数
    private boolean logic = true; //論理値

    //スコアセット・ゲット
    public void setScore(int score) {
        this.score += score;        //ゲーム終了後にスコア取得
    }
    public int getScore() {
        return this.score;      //スコアリターン
    }

    //モードセット・ゲット
    public void setMode(int mode) {
        this.mode = mode;       //モードを取得
    }
    public int getMode() {
        return this.mode;       //モードリターン
    }

    //ミス回数セット・ゲット
    public void setMiss(int miss) {
        this.miss += miss;  //ミスをするたびに1加算
    }
    public int getMiss() {
        return this.miss;   //ミス回数リターン
    }

    //論理値
    public void setLogic(boolean logic) {
        this.logic = logic;
    }
    public boolean getLogic() {
        return this.logic;
    }


    //モード選択メソッド
    public void modeSelect() {
        System.out.println("【モードを数字で選択してください】");
        System.out.println("『ことわざ : 1』");

        try {
            @SuppressWarnings("resource")
            int p = new java.util.Scanner(System.in).nextInt(); //難易度セッターに渡す引数取得
            setMode(p);

            if(getMode()==1) {
                System.out.println("ことわざモードを選択しました\n------------------------------------");
            }
            else if(getMode()==2) {
                System.out.println("動物モードを選択しました\n------------------------------------");
            }
            else {
                System.out.println("『1,2』のどれかを入力してください");
                modeSelect();
            }
            }
        catch(Exception e) {
            System.out.println("数字で入力してください");
            modeSelect();
        }
    }


    //タイマーメソッド
    public void timer() {
        try {
            System.out.print("   3,");
            Thread.sleep(1000);
            System.out.print("  2,");
            Thread.sleep(1000);
            System.out.print("  1");
            Thread.sleep(1000);
            System.out.println("  『START!!!!』\n------------------------------------");

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    //リスタートメソッド
    public void reStart() {
        System.out.println("もう一度プレイしますか?【はい,いいえ : 1,2】");

        try {
            @SuppressWarnings("resource")
            int re = new java.util.Scanner(System.in).nextInt();
            if(re==1) {
                setScore(-(getScore()));
                setLogic(true);
            }
            else if(re==2){
                System.out.println("お疲れさまでした。");
                setLogic(false);
            }
            else {
                System.out.println("1か2で入力してください");
                setLogic(false);
                reStart();
            }
        }
        catch(Exception e) {
            System.out.println("数字の1か2を入力してください");
            setLogic(false);
            reStart();
        }
    }


    //ことわざ1タイピングメソッド

        public void kotowaza1() {

            for(int i=0; i<20; i++) {
                //Wordクラスから配列を呼び出しランダムで表示
                word.setWordKotowazaC();
                System.out.println("【"+word.getWordKotowazaC()+"】");

                //in変数にタイピング結果を代入
                @SuppressWarnings("resource")
                String in = new java.util.Scanner(System.in).nextLine();

                //結果判定
                if(in.equals(word.getWordKotowazaC())) {
                    setScore(10);
                    }

                else {
                    setMiss(1);
                    setScore(0);
                }
            }
            System.out.println("ゲーム終了です。\n[スコア]:"+getScore()+"\n[ミス数]:"+getMiss());
        }


    //ゲーム全体メソッド
    public void typingGame() {

        //論理値がtrueの限りループ
        while(getLogic()==true) {
        System.out.println("~タイピングゲームを開始します~");
        //levelSelect();    //難易度セレクト
        modeSelect();   //モード選択1
        timer();    //タイマー3秒前

        if(getMode()==1) {
            kotowaza1();
            reStart();
        }
        }
    }
}
Word.java

class Word {

    //入力ワ―ド

    //モード1ことわざ
    private String[] wordKotowaza
    = {"あうんの呼吸","足をすくわれる","石の上にも三年","一矢報いる","公開先に立たず","スズメの涙",
            "二回から目薬","猫にこばん","桃栗三年柿八年","焼け石に水","良薬は口に苦し","笑う門には福来る",
            "病は気から","目からうろこが落ちる","火のない所に煙は立たぬ"};




    //ワード出力用変数
    private String wordKotowazaC;

    //引数に乱数を取得し配列をランダムで表示
    public void setWordKotowazaC() {
        int num = new java.util.Random().nextInt(getWordKotowazaL());
        this.wordKotowazaC = wordKotowaza[num];
    }

    //配列値リターン
    public String getWordKotowazaC() {
        return wordKotowazaC;
    }

    //配列の長さ取得
    public int getWordKotowazaL() {
        return wordKotowaza.length;
    }

}

Game.java

public class Game {

    public static void main(String[] args) {
        Typing typ = new Typing();
        typ.typingGame();   //ゲーム開始
    }
}

<今後実装したいこと>
・難易度設定
・GUI化
・速度測定

2
1
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
1