当达到特定分数时,如何将警告对话框添加到我的骰子游戏中? Java、Android 工作室

How do I add an Alert Dialog to my Dice Game when a certain score is reached? Java, Android Studio

我制作了一个简单的骰子游戏应用程序,用户点击一个骰子,它会掷出一个计算机骰子,然后由用户掷骰子。谁的数字高一分。

如何创建“你赢了!”或“你输了!”当用户或计算机达到 25 分时弹出警告对话框?有两种选择,一种是重新玩游戏并重置分数。另一个是关闭应用程序?

我试着弄乱了一些对话框,但我遇到了很多错误而且没有用。

这是我目前的主要activity:

    package com.example.dicegame;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

ImageView computer, user;
TextView computerroll, userroll;
Random r;
int computerscore = 0, userscore = 0;

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

    computer = (ImageView) findViewById(R.id.computer);
    user = (ImageView) findViewById(R.id.user);

    computerroll = (TextView) findViewById(R.id.computerroll);
    userroll = (TextView) findViewById(R.id.userroll);

    r = new Random();

    user.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int computerThrow = r.nextInt(6) + 1;
            int userThrow = r.nextInt(6) + 1;

            setImageComputer(computerThrow);
            setImageUser(userThrow);

            if (computerThrow > userThrow){
                computerscore++;
            }

            if (userThrow > computerThrow){
                userscore++;
            }

            if (userThrow == computerThrow){
                Toast.makeText(MainActivity.this, "It's a Draw!", Toast.LENGTH_SHORT).show();
            }

            computerroll.setText("Computer: " + computerscore);
            userroll.setText("You: " + userscore);

            Animation rotate = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate);
            user.startAnimation(rotate);
            computer.startAnimation(rotate);
        }
    });
}

    public void setImageComputer(int num){
    switch (num){
        case 1:
            computer.setImageResource(R.drawable.dice_1);
            break;
        case 2:
            computer.setImageResource(R.drawable.dice_2);
            break;
        case 3:
            computer.setImageResource(R.drawable.dice_3);
            break;
        case 4:
            computer.setImageResource(R.drawable.dice_4);
            break;
        case 5:
            computer.setImageResource(R.drawable.dice_5);
            break;
        case 6:
            computer.setImageResource(R.drawable.dice_6);
            break;
    }
}

public void setImageUser(int num){
    switch (num){
        case 1:
            user.setImageResource(R.drawable.dice_1);
            break;
        case 2:
            user.setImageResource(R.drawable.dice_2);
            break;
        case 3:
            user.setImageResource(R.drawable.dice_3);
            break;
        case 4:
            user.setImageResource(R.drawable.dice_4);
            break;
        case 5:
            user.setImageResource(R.drawable.dice_5);
            break;
        case 6:
            user.setImageResource(R.drawable.dice_6);
            break;
    }
}
}

下面是我的应用程序的示例屏幕截图,您可以大致了解它的工作原理。 我真的很陌生,所以我很感激任何帮助。非常感谢!

public void onClick(View v) {
    int computerThrow = r.nextInt(6) + 1;
    int userThrow = r.nextInt(6) + 1;

    setImageComputer(computerThrow);
    setImageUser(userThrow);

    if (computerThrow > userThrow){
        computerscore++;
    }

    if (userThrow > computerThrow){
        userscore++;
    }

    if (userThrow == computerThrow){
        Toast.makeText(MainActivity.this, "It's a Draw!", Toast.LENGTH_SHORT).show();
    }

    // Pseudo Code, Maybe to help you out a bit here

    if (userscore == 25 or computerscore == 25) {
        if userscore == 25{
            alert(you won) // alert the user with a win 
        } else {
            alert(you lost) // alert the user with a loss
        }

        // class where you handle the reset of the game
        handleReset()

    } else {
        // continue game as normal 
        computerroll.setText("Computer: " + computerscore);
        userroll.setText("You: " + userscore);

        Animation rotate = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate);
        user.startAnimation(rotate);`enter code here`
        computer.startAnimation(rotate);`enter code here`
    }
}

你必须在这里写一些 类 来处理游戏结束的问题,但我希望我在这里能帮助你一点点

这是我最后得出的结论:

user.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int computerThrow = r.nextInt(6) + 1;
            int userThrow = r.nextInt(6) + 1;

            setImageComputer(computerThrow);
            setImageUser(userThrow);

            if (computerThrow > userThrow){
                computerscore++;
            }

            if (userThrow > computerThrow){
                userscore++;
            }

            if (userThrow == computerThrow){
                Toast.makeText(MainActivity.this, "It's a Draw!", Toast.LENGTH_SHORT).show();
            }

            if (userscore == 15){
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("You Won!")
                        .setMessage("Congratulations! You won the game!")
                        .setCancelable(false)
                        .setPositiveButton("Play Again", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent i = new Intent(MainActivity.this, MainActivity.class);
                                startActivity(i);
                            }
                        })
                        .setNegativeButton("Close App", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }
                        }).show();
            }
            else if (computerscore == 15){
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("You Lost!")
                        .setMessage("Unlucky! You lost the game!")
                        .setCancelable(false)
                        .setPositiveButton("Play Again", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent i = new Intent(MainActivity.this, MainActivity.class);
                                startActivity(i);
                            }
                        })
                        .setNegativeButton("Close App", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }
                        }).show();
            }
            else {
            computerroll.setText("Computer: " + computerscore);
            userroll.setText("You: " + userscore);

            Animation rotate = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate);
            user.startAnimation(rotate);
            computer.startAnimation(rotate);
            }
        }
    });