当达到某个分数时,如何在骰子游戏中添加警告对话框?java、android studio

kuhbmx9i  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(319)

我已经做了一个简单的骰子游戏应用程序,用户点击一个骰子,它滚动一个计算机骰子和用户骰子。数字高的人得一分。
我怎样才能创造一个“你赢了!”或者“你输了!”当用户或计算机达到25分时弹出的警报对话框?有两个选择,一个是再次玩游戏,它会重置分数。另一个是关闭应用程序?
我试着乱搞一些对话框,但我有很多错误,它没有工作。
以下是我目前的主要活动:

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;
    }
}
}

下面是我的应用程序的一个示例屏幕截图,这样你就可以对它的工作原理有一个基本的了解。我真的是个新手,所以我很感激任何帮助。非常感谢!

xxhby3vn

xxhby3vn1#

我最后想到的是:

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);
            }
        }
    });
owfi6suc

owfi6suc2#

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`
    }
}

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

相关问题