Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gambling #21

Open
Rajiv-max-star opened this issue Jul 25, 2024 · 0 comments
Open

Gambling #21

Rajiv-max-star opened this issue Jul 25, 2024 · 0 comments

Comments

@Rajiv-max-star
Copy link

package com.example.gamblingapp;

import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Random;

public class SlotMachineActivity extends AppCompatActivity {

private ImageView slot1, slot2, slot3;
private TextView resultText;
private Button spinButton;
private int[] images = {R.drawable.slot1, R.drawable.slot2, R.drawable.slot3};

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

    slot1 = findViewById(R.id.slot1);
    slot2 = findViewById(R.id.slot2);
    slot3 = findViewById(R.id.slot3);
    resultText = findViewById(R.id.resultText);
    spinButton = findViewById(R.id.spinButton);

    spinButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            spinSlots();
        }
    });
}

private void spinSlots() {
    Random random = new Random();
    int slot1Result = random.nextInt(images.length);
    int slot2Result = random.nextInt(images.length);
    int slot3Result = random.nextInt(images.length);

    slot1.setImageResource(images[slot1Result]);
    slot2.setImageResource(images[slot2Result]);
    slot3.setImageResource(images[slot3Result]);

    if (slot1Result == slot2Result && slot2Result == slot3Result) {
        resultText.setText("You Win!");
    } else {
        resultText.setText("Try Again!");
    }
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant