简体   繁体   中英

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

I've made a simple dice game app where the user clicks a dice and it rolls a computer dice and the users dice. Whoever has the higher number scores one point.

How can I create a "You Won?" or "You Lost," alert dialog box that pops up when either the user or the computer reaches a score of 25. With two options? one being to play the game again and it resets the scores. The other being to close the app?

I tried messing around with some dialog boxes but I had lots of errors and it didn't work.

Here's my main activity so far:

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

And here's an example screenshot of my app so you can get a basic idea of how it works. I'm really new to this so I'd appreciate any help. Thanks so much!

在此处输入图像描述

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

You would have to write a couple of classes here to handle the game being over but I hope I helped you out a little here

Here's what I came up with in the end:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM