Help us understand the problem. What is going on with this article?

計算ゲーム作成中 メモ(追記あり)

追記あります。
一番下に最新のソースコードがあります。

計算ゲームを教材通りに作ったもの↓

https://twitter.com/i/status/1228128293783666688

コード↓

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

Button goButton;
ArrayList<Integer> answers = new ArrayList<Integer>();
int locationOfCorrectAnswer;
TextView resultTextView;
int score = 0;
int numberOfQusetions = 0;
TextView scoreTextView;
TextView sumTextView;
Button button0;
Button button1;
Button button2;
Button button3;
TextView timerTextview;
Button playAgainButton;
ConstraintLayout gameLayout;

public void playAgain(View view) {
    score = 0;
    numberOfQusetions = 0;
    timerTextview.setText("30s");
    scoreTextView.setText("0/0");
    newQuetion();
    playAgainButton.setVisibility(View.INVISIBLE);
    resultTextView.setText(""
    );

    new CountDownTimer(30100, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            timerTextview.setText(String.valueOf(millisUntilFinished / 1000) + "s");

        }

        @Override
        public void onFinish() {
            resultTextView.setText("Done!");
            playAgainButton.setVisibility(View.VISIBLE);

        }
    }.start();

}

public void chooseAnswer(View view) {
    if (Integer.toString(locationOfCorrectAnswer).equals(view.getTag().toString())) {
        resultTextView.setText("Correct!");
        score++;

    } else {
        resultTextView.setText("Wrong! :(");

    }

    numberOfQusetions++;
    scoreTextView.setText(Integer.toString(score) + "/" + Integer.toString(numberOfQusetions));

    newQuetion();
}

public void start(View view) {
    goButton.setVisibility(View.INVISIBLE);
    gameLayout.setVisibility(View.VISIBLE);
    playAgain(findViewById(R.id.timerTextView));

}

public void newQuetion() {
    //インスタンス生成  
    //クラス名 変数 = new クラス名();
    Random rand = new Random();

    //整数の変数a,bを定義  0~20の21通りの整数をランダムに代入
    int a = rand.nextInt(21);
    int b = rand.nextInt(21);

    sumTextView.setText(Integer.toString(a)+ " + " + Integer.toString(b));

    //0~3の4通りの整数をランダムに代入 Tagも0~3の4通り
    locationOfCorrectAnswer = rand.nextInt(4);
    //answersリストの中身(4つの答え)を消去 
    answers.clear();

    for(int i=0; i<4; i++) {
        if(i==locationOfCorrectAnswer) {
            answers.add(a+b);
        } else {
            //0~40の41通りの整数をランダムに代入(a,bにはそれぞれ0~20の整数が入るため 最小0+0 最大20+20)
            int wrongAnswer = rand.nextInt(41);

            while(wrongAnswer == a+b) {
                wrongAnswer = rand.nextInt(41);

            }

            answers.add(wrongAnswer);
        }

    }

    button0.setText(Integer.toString(answers.get(0)));
    button1.setText(Integer.toString(answers.get(1)));
    button2.setText(Integer.toString(answers.get(2)));
    button3.setText(Integer.toString(answers.get(3)));
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    goButton = findViewById(R.id.goButton);
    sumTextView = findViewById(R.id.sumTextView);

    button0 = findViewById(R.id.button0);
    button1 = findViewById(R.id.button1);
    button2 = findViewById(R.id.button2);
    button3 = findViewById(R.id.button3);
    resultTextView = findViewById(R.id.resultTextView);
    scoreTextView = findViewById(R.id.scoreTextView);
    timerTextview = findViewById(R.id.timerTextView);
    playAgainButton = findViewById(R.id.playAgainButton);
    gameLayout = findViewById(R.id.gameLayout);

    goButton.setVisibility(View.VISIBLE);
    gameLayout.setVisibility(View.INVISIBLE);

これを少しアレンジして

・点数によって3種類のテキストが表示され、効果音が鳴る

@Override
public void onFinish() {
MediaPlayer mediaPlayer;
buttonHide();

            if(score > 9) {
                resultTextView.setText("ほ、ほめてあげるわ//");
                mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shine1);
                mediaPlayer.start();

            } else if(score > 5) {
                resultTextView.setText("ふん、まあまあね");
                mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shakin1);
                mediaPlayer.start();

            } else {
                resultTextView.setText("あんたやる気あるの?");
                mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.tin1);
                mediaPlayer.start();
            }

            playAgainButton.setVisibility(View.VISIBLE);

        }

・タイマーが0になるとボタンが消え、後ろにある猫の画像を表示

public void buttonShow() {
button0.setVisibility(View.VISIBLE);
button1.setVisibility(View.VISIBLE);
button2.setVisibility(View.VISIBLE);
button3.setVisibility(View.VISIBLE);
}

public void buttonHide(){
    button0.setVisibility(View.INVISIBLE);
    button1.setVisibility(View.INVISIBLE);
    button2.setVisibility(View.INVISIBLE);
    button3.setVisibility(View.INVISIBLE);
}

という機能をプラスしたもの↓

https://twitter.com/i/status/1228339059128356864

コード全体↓

package com.example.braintrainer;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Layout;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Random;

import static android.media.MediaPlayer.create;

public class MainActivity extends AppCompatActivity {

Button goButton;
ArrayList<Integer> answers = new ArrayList<Integer>();
int locationOfCorrectAnswer;
TextView resultTextView;
int score = 0;
int numberOfQusetions = 0;
TextView scoreTextView;
TextView sumTextView;
Button button0, button1, button2, button3;
TextView timerTextview;
Button playAgainButton;
ConstraintLayout gameLayout;
ImageView imageView0;



public void playAgain(View view) {
    score = 0;
    numberOfQusetions = 0;
    timerTextview.setText("10s");
    scoreTextView.setText("0/0");
    newQuetion();
    playAgainButton.setVisibility(View.INVISIBLE);
    resultTextView.setText("");
    buttonShow();


    new CountDownTimer(5100, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            timerTextview.setText(String.valueOf(millisUntilFinished / 1000) + "s");

        }

        @Override
        public void onFinish() {
            MediaPlayer mediaPlayer;
            buttonHide();

            if(score > 9) {
                resultTextView.setText("ほ、ほめてあげるわ//");
                mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shine1);
                mediaPlayer.start();

            } else if(score > 5) {
                resultTextView.setText("ふん、まあまあね");
                mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shakin1);
                mediaPlayer.start();

            } else {
                resultTextView.setText("あんたやる気あるの?");
                mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.tin1);
                mediaPlayer.start();
            }

            playAgainButton.setVisibility(View.VISIBLE);

        }
    }.start();

}

public void chooseAnswer(View view) {
    if (Integer.toString(locationOfCorrectAnswer).equals(view.getTag().toString())) {
        resultTextView.setText("正解にゃ");
        score++;

    } else {
        resultTextView.setText("間違いにゃ");

    }

    numberOfQusetions++;
    scoreTextView.setText(Integer.toString(score) + "/" + Integer.toString(numberOfQusetions));

    newQuetion();
}

public void start(View view) {
    goButton.setVisibility(View.INVISIBLE);
    gameLayout.setVisibility(View.VISIBLE);
    playAgain(findViewById(R.id.timerTextView));

}

public void newQuetion() {

    //インスタンス生成  
    //クラス名 変数 = new クラス名();
    Random rand = new Random();

    //整数の変数a,bを定義  0~20の21通りの整数をランダムに代入
    int a = rand.nextInt(21);
    int b = rand.nextInt(21);

    sumTextView.setText(Integer.toString(a)+ " + " + Integer.toString(b));

    //0~3の4通りの整数をランダムに代入 Tagも0~3の4通り
    locationOfCorrectAnswer = rand.nextInt(4);
    //answersリストの中身(4つの答え)を消去 
    answers.clear();

    for(int i=0; i<4; i++) {
        if(i==locationOfCorrectAnswer) {
            answers.add(a+b);
        } else {
            //0~40の41通りの整数をランダムに代入(a,bにはそれぞれ0~20の整数が入るため 最小0+0 最大20+20)
            int wrongAnswer = rand.nextInt(41);

            while(wrongAnswer == a+b) {
                wrongAnswer = rand.nextInt(41);

            }

            answers.add(wrongAnswer);
        }

    }

    button0.setText(Integer.toString(answers.get(0)));
    button1.setText(Integer.toString(answers.get(1)));
    button2.setText(Integer.toString(answers.get(2)));
    button3.setText(Integer.toString(answers.get(3)));
}

public void buttonShow() {
    button0.setVisibility(View.VISIBLE);
    button1.setVisibility(View.VISIBLE);
    button2.setVisibility(View.VISIBLE);
    button3.setVisibility(View.VISIBLE);
}

public void buttonHide(){
    button0.setVisibility(View.INVISIBLE);
    button1.setVisibility(View.INVISIBLE);
    button2.setVisibility(View.INVISIBLE);
    button3.setVisibility(View.INVISIBLE);
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    goButton = findViewById(R.id.goButton);
    sumTextView = findViewById(R.id.sumTextView);

    button0 = findViewById(R.id.button0);
    button1 = findViewById(R.id.button1);
    button2 = findViewById(R.id.button2);
    button3 = findViewById(R.id.button3);
    resultTextView = findViewById(R.id.resultTextView);
    scoreTextView = findViewById(R.id.scoreTextView);
    timerTextview = findViewById(R.id.timerTextView);
    playAgainButton = findViewById(R.id.playAgainButton);
    gameLayout = findViewById(R.id.gameLayout);
    imageView0 = findViewById(R.id.nirami);


    goButton.setVisibility(View.VISIBLE);




}

}

(追記1)

タイマーが0になった時
 ・点数によって3種類の猫画像のどれかが表示されるように変更
  

     @Override
        //カウントダウンが0になった時の挙動
        public void onFinish() {
            MediaPlayer mediaPlayer;

            //4つのボタン(選択肢)を見えなくする
            buttonHide();

            resultTextView.setVisibility(View.INVISIBLE);
            commentTextView.setVisibility(View.VISIBLE);

            if(score > 9) {
                commentTextView.setText("ほ、ほめてあげるわ//");
                imageKakure.setVisibility(View.VISIBLE);

                //効果音キラキラが鳴る
                mediaPlayer = create(getApplicationContext(), R.raw.shine1);
                mediaPlayer.start();



            } else if(score > 5) {
                commentTextView.setText("ふん、まあまあね");
                imageYousumi.setVisibility(View.VISIBLE);

                //効果音シャキーン!が鳴る
                mediaPlayer = create(getApplicationContext(), R.raw.shakin1);
                mediaPlayer.start();

            } else {
                commentTextView.setText("あんたやる気あるの?");
                imageNirami.setVisibility(View.VISIBLE);

                //効果音チーンが鳴る
                mediaPlayer = create(getApplicationContext(), R.raw.tin1);
                mediaPlayer.start();

            }

            //playAgainButton出現
            playAgainButton.setVisibility(View.VISIBLE);

        }

    }.start();

また
 ・playAgainButtonを押したとき、出現した4つのボタンにより猫の画像が見えなくなるのではなく、
  猫画像3種類とも消えるように設定(表示されていたかどうか二関わらず)。

//猫の画像見えなくする
imageNirami.setVisibility(View.INVISIBLE);
imageYousumi.setVisibility(View.INVISIBLE);
imageKakure.setVisibility(View.INVISIBLE);

ゲームの様子をキャプチャしたもの↓
https://twitter.com/i/status/1231845874810015744

ソースコード全体↓  コメントあまり書いてなかったので、あちこち書きました。

package com.example.braintrainer;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Layout;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Random;
import java.util.zip.InflaterInputStream;

import static android.media.MediaPlayer.create;

public class MainActivity extends AppCompatActivity {

Button goButton;

//整数の入るanswersリスト(インスタンス)を生成
ArrayList<Integer> answers = new ArrayList<Integer>();

int locationOfCorrectAnswer;
TextView resultTextView;
TextView commentTextView;
int score = 0;
int numberOfQusetions = 0;
TextView scoreTextView;
TextView sumTextView;
Button button0, button1, button2, button3;
TextView timerTextview;
Button playAgainButton;
ConstraintLayout gameLayout;
ImageView imageNirami;
ImageView imageKakure;
ImageView imageYousumi;




//playaAgainボタンを押したときの挙動を定義
public void playAgain(View view) {
    score = 0;
    numberOfQusetions = 0;
    timerTextview.setText("15s");
    scoreTextView.setText("0/0");

    //新しい問題と選択肢を表示
    newQuetion();

    //resultTextViewを見えるようにする
    resultTextView.setVisibility(View.VISIBLE);

    //commentTextViewを見えなくする
    commentTextView.setVisibility(View.INVISIBLE);

    //playAgainButtonを見えなくする
    playAgainButton.setVisibility(View.INVISIBLE);

    //猫の画像見えなくする
    imageNirami.setVisibility(View.INVISIBLE);
    imageYousumi.setVisibility(View.INVISIBLE);
    imageKakure.setVisibility(View.INVISIBLE);

    resultTextView.setText("");

    //4つのボタン(選択肢)出現
    buttonShow();



    //インスタンス.start()     インスタンスのメソッド呼び出し
    //CountDownTimerクラスから生成したインスタンスにstart()メソッドという指示を与える
    new CountDownTimer(15100, 1000) {

        //CountDownTimerクラスのメソッドを上書き
        @Override
        //カウントダウンしている間の挙動
        public void onTick(long millisUntilFinished) {

            //残り何秒か表示
            timerTextview.setText(String.valueOf(millisUntilFinished / 1000) + "s");

        }


        @Override
        //カウントダウンが0になった時の挙動
        public void onFinish() {
            MediaPlayer mediaPlayer;

            //4つのボタン(選択肢)を見えなくする
            buttonHide();

            resultTextView.setVisibility(View.INVISIBLE);
            commentTextView.setVisibility(View.VISIBLE);

            if(score > 9) {
                commentTextView.setText("ほ、ほめてあげるわ//");
                imageKakure.setVisibility(View.VISIBLE);

                //効果音キラキラが鳴る
                mediaPlayer = create(getApplicationContext(), R.raw.shine1);
                mediaPlayer.start();



            } else if(score > 5) {
                commentTextView.setText("ふん、まあまあね");
                imageYousumi.setVisibility(View.VISIBLE);

                //効果音シャキーン!が鳴る
                mediaPlayer = create(getApplicationContext(), R.raw.shakin1);
                mediaPlayer.start();

            } else {
                commentTextView.setText("あんたやる気あるの?");
                imageNirami.setVisibility(View.VISIBLE);

                //効果音チーンが鳴る
                mediaPlayer = create(getApplicationContext(), R.raw.tin1);
                mediaPlayer.start();

            }

            //playAgainButton出現
            playAgainButton.setVisibility(View.VISIBLE);

        }

    }.start();

}

//4種類のボタンを押したときの挙動(メソッド)を定義
public void chooseAnswer(View view) {

    //正解のタグと押したタグが同じとき(4つのボタンに0~3の4種類のタグを設定している)
    if (Integer.toString(locationOfCorrectAnswer).equals(view.getTag().toString())) {
        resultTextView.setText("正解にゃ");
        score++;

    } else {
        resultTextView.setText("間違いにゃ");

    }

    numberOfQusetions++;

    //scoreTevtView 何問中何問正解しているか表示
    scoreTextView.setText(Integer.toString(score) + "/" + Integer.toString(numberOfQusetions));

    //新しい問題と選択肢を表示
    newQuetion();
}

//Goボタンを押したときの挙動(メソッド)を定義
public void start(View view) {
    goButton.setVisibility(View.INVISIBLE);

    //残り時間、問題、得点/問題数、4つの選択肢(ボタン)を出現させる
    gameLayout.setVisibility(View.VISIBLE);
    playAgain(findViewById(R.id.timerTextView));

}

public void newQuetion() {

    //インスタンス生成  
    //クラス名 変数 = new クラス名();
    Random rand = new Random();

    //整数の変数a,bを定義  0~20の21通りの整数をランダムに代入
    int a = rand.nextInt(21);
    int b = rand.nextInt(21);

    sumTextView.setText(Integer.toString(a)+ " + " + Integer.toString(b));

    //0~3の4通りの整数をランダムに代入 Tagも0~3の4通り
    locationOfCorrectAnswer = rand.nextInt(4);
    //answersリストの中身(4つの答え)を消去 
    answers.clear();

    for(int i=0; i<4; i++) {
        if(i==locationOfCorrectAnswer) {
            answers.add(a+b);
        } else {
            //0~40の41通りの整数をランダムに代入(a,bにはそれぞれ0~20の整数が入るため 最小0+0 最大20+20)
            int wrongAnswer = rand.nextInt(41);

            //wrongAnswerが正解と一緒の場合、再び0から40の41通りの整数をランダムに代入
            while(wrongAnswer == a+b) {
                wrongAnswer = rand.nextInt(41);

            }

            //answersリストに間違った答えを追加
            answers.add(wrongAnswer);
        }

    }

    button0.setText(Integer.toString(answers.get(0)));
    button1.setText(Integer.toString(answers.get(1)));
    button2.setText(Integer.toString(answers.get(2)));
    button3.setText(Integer.toString(answers.get(3)));
}

//4つのボタン(選択肢)を見えるようにするメソッドを定義
public void buttonShow() {
    button0.setVisibility(View.VISIBLE);
    button1.setVisibility(View.VISIBLE);
    button2.setVisibility(View.VISIBLE);
    button3.setVisibility(View.VISIBLE);
}

//4つのボタン(選択肢)を見えなくするメソッドを定義
public void buttonHide(){
    button0.setVisibility(View.INVISIBLE);
    button1.setVisibility(View.INVISIBLE);
    button2.setVisibility(View.INVISIBLE);
    button3.setVisibility(View.INVISIBLE);
}




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //それぞれlayoutのViewと紐づけ
    goButton = findViewById(R.id.goButton);
    sumTextView = findViewById(R.id.sumTextView);
    button0 = findViewById(R.id.button0);
    button1 = findViewById(R.id.button1);
    button2 = findViewById(R.id.button2);
    button3 = findViewById(R.id.button3);
    resultTextView = findViewById(R.id.resultTextView);
    commentTextView = findViewById(R.id.commentTextView);
    scoreTextView = findViewById(R.id.scoreTextView);
    timerTextview = findViewById(R.id.timerTextView);
    playAgainButton = findViewById(R.id.playAgainButton);
    gameLayout = findViewById(R.id.gameLayout);
    imageNirami = findViewById(R.id.nirami);
    imageKakure = findViewById(R.id.kakure);
    imageYousumi = findViewById(R.id.yousumi);
    goButton.setVisibility(View.VISIBLE);

}

}

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした