Skip to content

Gambling  #21

Description

@Rajiv-max-star

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

}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions