Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4012814
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:17:39+00:00 2026-05-20T09:17:39+00:00

So I am trying to create a real simple dice game…more like a dice

  • 0

So I am trying to create a real simple dice game…more like a dice roller game and I have encountered a problem for which I have yet to find an answer. So obviously I have 6 images for the die faces(1-6), but what I am missing somewhere is how to basically assign values to these images when the “dice are rolled”. So that when I begin to implement some game logic I can compare what the numbers of the die faces would be. Here is what I have so far, and as usual any assistance would be much appreciated.

public class CrapsGameActivity extends CrapsActivity {
    private final int rollAnimations = 50;
    private final int delayTime = 15;
    private Resources res;
    private final int[] diceImages = new int[]{R.drawable.die1, R.drawable.die2, 
            R.drawable.die3, R.drawable.die4, R.drawable.die5,R.drawable.die6};
    private Drawable dice[] = new Drawable[6];
    private final Random randomGen = new Random();

    private int diceSum;
    private int roll[] = new int[] {6,6};
    private ImageView die1;
    private TextView die1_Total;
    private int die1_number;
    private ImageView die2;
    private TextView die2_Total;
    private TextView diceTotal;
    private LinearLayout diceContainer;
    private Handler animationHandler;
    private long lastUpdate = -1;
    private float x, y, z;
    private float last_x, last_y, last_z;
    private boolean paused = false;
    private static final int UPDATE_DELAY = 50;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        paused = false;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);
        setTitle(getString(R.string.app_name));
        res = getResources();

        for (int i = 0; i<6; i++){
            dice[i] = res.getDrawable(diceImages[i]);
        }


        diceContainer = (LinearLayout) findViewById(R.id.diceContainer);
        diceContainer.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                try{
                    rollDice();
                    die1_Total.setText("" + roll[0]);
                    die2_Total.setText("" + roll[1]);
                    diceTotal.setText(""+ diceSum);
                }catch(Exception e) {}; 
            }//end of onClick
        });//end of onClick listener

        die1 = (ImageView) findViewById(R.id.die1);
        die1_Total = (TextView) findViewById(R.id.TV_die1);
        die2 = (ImageView) findViewById(R.id.die2);
        die2_Total = (TextView) findViewById(R.id.TV_die2);

        diceTotal = (TextView) findViewById(R.id.TV_diceTotal);


        animationHandler = new Handler(){
            public void handleMessage(Message msg){
                die1.setImageDrawable(dice[roll[0]]);
                die2.setImageDrawable(dice[roll[1]]);
            }//end of handle message
        };//end of Handler
    }


    private void rollDice() {
        if(paused) return;
        new Thread(new Runnable(){
            @Override
            public void run(){
                for(int i = 0; i < rollAnimations; i++){
                    doRoll();
                }//end of for statement
            }//end of run()
        }).start();//end of thread
    }//end of rollDice()


    private void doRoll(){//only does a single roll
        roll[0] = randomGen.nextInt(6);
        roll[1] = randomGen.nextInt(6);
        diceSum = roll[0] + roll[1] + 2;    


        synchronized(getLayoutInflater()){
            animationHandler.sendEmptyMessage(0);
        }
        try{//delay for smooth animations
            Thread.sleep(delayTime);
        }catch(final InterruptedException e){
            e.printStackTrace();
        }
    }//end of doRoll()


    public void onResume(){
        super.onResume();
        paused = false;
    }//end of onResume()


    public void onPause(){
        super.onPause();
        paused = true;
    }//end of onPause()

}//end of activity

I may not be doing it properly but the code (below). When I use this to see the numbers on the emulator, they are random and dont correspond to the face values of the dice. So like if I wanted to total the face values of the dice (just ran it again now) 0 is for 6 face, and 2 is for the 3 face, which should give me a total dice face of 9, but I am getting 2

public void onClick(View v) {
                try{
                    rollDice();
                    die1_Total.setText("" + roll[0]);
                    die2_Total.setText("" + roll[1]);
                    diceTotal.setText(""+ diceSum);
                }catch(Exception e) {}; 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:17:39+00:00Added an answer on May 20, 2026 at 9:17 am
    roll[0] = randomGen.nextInt(6);
    roll[1] = randomGen.nextInt(6);
    

    There are your values. You don’t want the image to contain the data (or at least, I wouldn’t bother). Instead, you render the images/animations based on these values, and can then work with those values directly later.

    If you wanted to store the values with the images, you could always make an object (GameDie) and store the images and current value in there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a simple login system. When I run the login
I'm trying to create a simple Java app that uses JNI to call some
I am trying to create a simple app for the iPad that with play
I am trying to create a very simple application (part of my learning program),
Any real world simple samples of using abstract class? I'm trying to get in
I'm trying to create a simple application that does a HTTP request/response on a
I'm trying to create a method which create the where predicate to use later
I am trying create a WCF service that leverages the WPF MediaPlayer on the
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation
Trying to create a user account in a test. But getting a Object reference

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.