Search code examples
javaandroidandroid-studiodice

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!

enter image description here


Solution

  • 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