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 8448247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:22:32+00:00 2026-06-10T10:22:32+00:00

I’ve been stuck on this for a whole day, please help me. So I

  • 0

I’ve been stuck on this for a whole day, please help me. So I have 2 arrays filled with five dice rolls from two players and I want to set them to the according dice image that I put in my drawable. This is the original code that I have, displaying the arrays in TextView.

int player1[] = new int[5];
int player2[] = new int[5];
TextView p1Dice, p2Dice;

private void displayDice() {
    StringBuffer sbfNumbers = new StringBuffer();
    for (int i = 0; i < 5; i++) {
        sbfNumbers.append(player1[i] + " ");
    }
    StringBuffer sbfNumbers2 = new StringBuffer();
    for (int i = 0; i < 5; i++) {
        sbfNumbers2.append(player2[i] + " ");
    }
    p1Dice.setText(sbfNumbers.toString());
    p2Dice.setText(sbfNumbers2.toString());

I can’t figure out how to get it to display the ImageView instead.

private final static int[] diceImages = new int[] { R.drawable.d1, 
R.drawable.d2, R.drawable.d3, R.drawable.d4, R.drawable.d5, R.drawable.d6 };
ImageView p1Dice1, p1Dice2, p1Dice3, p1Dice4, p1Dice5;

for (int i = 0; i < 5; i++) {
    p1Dice[i].setImageResource(diceImage[player1[i]]);
}

What I am missing?

Here’s my full code, sorry its a mess. I just started to learn programming on my own and this is my first program. Any advice on making it better is appreciated too.

package com.kelvinblade.liardice;

import java.util.Random;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

int player1[] = new int[5];
int player2[] = new int[5];
TextView p1Dice, p2Dice, result, whosTurn, tvLog, score;
ImageView p1Dice1, p1Dice2, p1Dice3, p1Dice4, p1Dice5;
Button openDice, callDice;
EditText NumDice, DiceNum;
int NumOfDice, DiceNumber, turn;
int currentDiceQuantity, currentDiceFace;
boolean isOneWildCard = true;
int callLog[] = new int[70];
int playerOneEnergy, playerTwoEnergy;
boolean playerOneStart = true;

private final int[] diceImages = new int[] { R.drawable.d1, R.drawable.d2, R.drawable.d3, R.drawable.d4, R.drawable.d5, R.drawable.d6 };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initializePlaceHolders();
    startGame();
}

// Initialize the Place Holders
private void initializePlaceHolders() {
    p1Dice = (TextView) findViewById(R.id.tvP1Dice);
    p2Dice = (TextView) findViewById(R.id.tvP2Dice);
    openDice = (Button) findViewById(R.id.bOpenDice);
    callDice = (Button) findViewById(R.id.bCallDice);
    whosTurn = (TextView) findViewById(R.id.tvWhosTurn);
    tvLog = (TextView) findViewById(R.id.tvLog);
    score = (TextView) findViewById(R.id.tvScore);
    result = (TextView) findViewById(R.id.tvResult);
    NumDice = (EditText) findViewById(R.id.etNumDice);
    DiceNum = (EditText) findViewById(R.id.etDiceNum);
    p1Dice1 = (ImageView) findViewById(R.id.ivDice1);
    p1Dice2 = (ImageView) findViewById(R.id.ivDice2);
    p1Dice3 = (ImageView) findViewById(R.id.ivDice3);
    p1Dice4 = (ImageView) findViewById(R.id.ivDice4);
    p1Dice5 = (ImageView) findViewById(R.id.ivDice5);
    openDice.setOnClickListener(this);
    callDice.setOnClickListener(this);
}

// Game starts here
public void startGame() {
    playerOneEnergy = 4;
    playerTwoEnergy = 4;
    playGame();
}

// Starts level
public void playGame() {
    if ((playerOneEnergy != 0) && (playerTwoEnergy != 0)) {
        initialize();
        player1 = rollDice();
        player2 = rollDice();
        displayDice();
        displayTurn();
    } else if (playerTwoEnergy == 0) {
        result.setText("Loading Next Stage!");
        startGame();
    } else
        result.setText("Game Over!");
}

// Initialize the Variables
private void initialize() {
    turn = 0;
    currentDiceQuantity = 0;
    currentDiceFace = 0;
    result.setText("New Game.");
    tvLog.setText("Game Log:");
}

// Rolls the Dice
private int[] rollDice() {
    int[] diceArray = new int[5];
    Random randomDice = new Random();
    for (int i = 0; i < 5; i++) {
        diceArray[i] = randomDice.nextInt(6) + 1;
    }
    return diceArray;
}

// Displays the Dice for Player 1 & 2
private void displayDice() {
    StringBuffer sbfNumbers = new StringBuffer();
    for (int i = 0; i < 5; i++) {
        sbfNumbers.append(player1[i] + " ");
    }
    StringBuffer sbfNumbers2 = new StringBuffer();
    for (int i = 0; i < 5; i++) {
        sbfNumbers2.append(player2[i] + " ");
    }
    p1Dice.setText(sbfNumbers.toString());
    p2Dice.setText(sbfNumbers2.toString()); 

// try to display the dice array as image   <here's the problem>
    for (int i = 0; i < 5; i++) {
    p1Dice[i].setImageResource(diceImage[player1[i]]);
    }
    }

// Button actions
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.bCallDice:
        try {
            getCall();
        } catch (Exception e) {
            Dialog d = new Dialog(this);
            d.setTitle("Invalid call. Please try again.");
            d.show();
        }
        if ((validInput()) && (validCall())) 
            runCall();          
        else
            result.setText("Invalid call");
        break;
    case R.id.bOpenDice:
        checkDice();
        break;
    }
}

private void runCall() {
    currentDiceQuantity = NumOfDice;
    currentDiceFace = DiceNumber;
    result.setText("Valid call");
    writeLog();
    displayLog();
    turn++;
    displayTurn();
}

private void startAImove() {
    Random randomAction = new Random();
    int randomCall = randomAction.nextInt(1);
    if ((randomCall == 0) && (!isFirstMove()))
        checkDice();
    else {
        while (!validCall()) {
            NumOfDice = randomAction.nextInt(5) + 1;
            DiceNumber = randomAction.nextInt(5) + 1;
        }
        runCall();
    }
}

// Gets the Call from Player 1
private void getCall() {
    String s = NumDice.getText().toString();
    NumOfDice = Integer.parseInt(s);
    String s1 = DiceNum.getText().toString();
    DiceNumber = Integer.parseInt(s1);
    if (DiceNumber == 1) {
        isOneWildCard = false;
    }
}

// Checks to see if the call is a valid input
private boolean validInput() {
    int MaxNumOfDice = 5;
    int MaxDiceQuantity = 6;
    if ((NumOfDice <= MaxNumOfDice * 2) && (DiceNumber <= MaxDiceQuantity)) {
        return true;
    } else
        return false;
}

// Checks to see if Valid Call
private boolean validCall() {
    if (NumOfDice > currentDiceQuantity) {
        return true;
    } else if (((NumOfDice == currentDiceQuantity) && (currentDiceFace != 1))
            && ((DiceNumber == 1) || (DiceNumber > currentDiceFace))) {
        return true;
    } else {
        return false;
    }
}

// Writes to Log
private void writeLog() {
    callLog[turn * 2] = currentDiceQuantity;
    callLog[turn * 2 + 1] = currentDiceFace;
}

// Display Log
private void displayLog() {
    StringBuffer sbfNumbers = new StringBuffer();
    sbfNumbers.append("Game Log:\n");
    for (int i = 0; i < turn + 1; i++) {
        sbfNumbers.append((i + 1) + ": Player" + (i % 2 + 1) + " "
                + callLog[i * 2] + "x" + callLog[i * 2 + 1] + "\n");
    }
    tvLog.setText(sbfNumbers.toString());
}

// Display who's turn
public void displayTurn() {
    if (whichPlayersTurn() == 1)
        whosTurn.setText("Player 1's Turn...");
    else {
        whosTurn.setText("Player 2's Turn...");
    //  startAImove();
    }
}

// Checks who's turn
private int whichPlayersTurn() {
    boolean isTurnEven = false;
    if (turn % 2 == 0)
        isTurnEven = true;
    if (((playerOneStart) && (isTurnEven))
            || ((!playerOneStart) && (!isTurnEven))) {
        return 1;
    } else
        return 2;
}

// Checks if it's the first move
private boolean isFirstMove() {
    if (currentDiceQuantity == 0)
        return true;
    else
        return false;
}

// Checks the Player 1 & 2 for the Dice
private void checkDice() {
    if (!isFirstMove()) {
        int DiceCount = 0;
        for (int i = 0; i < 5; i++) {
            if (player1[i] == DiceNumber)
                DiceCount++;
            if (player2[i] == DiceNumber)
                DiceCount++;
            if ((player1[i] == 1) && (isOneWildCard))
                DiceCount++;
            if ((player2[i] == 1) && (isOneWildCard))
                DiceCount++;
        }
        if (((DiceCount >= NumOfDice) && (whichPlayersTurn() != 1))
                || ((DiceCount < NumOfDice) && (whichPlayersTurn() == 1))) {
            result.setText("Player 1 Wins!");
            playerTwoEnergy--;
            playerOneStart = false;
        } else {
            result.setText("Player 1 Loses!");
            playerOneEnergy--;
            playerOneStart = true;
        }
        displayWinLost();
        playGame();
    } else
        result.setText("Can not open on first move!");
}

// Display Win / Lose
private void displayWinLost() {
    StringBuffer sbfNumbers = new StringBuffer();
    sbfNumbers.append("Player One Energy : " + playerOneEnergy
            + "\nPlayer Two Energy: " + playerTwoEnergy);
    score.setText(sbfNumbers.toString());
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
}

}

Thank you so much in advanced. Any help is highly appreciated. Thanks again!!

  • 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-06-10T10:22:33+00:00Added an answer on June 10, 2026 at 10:22 am

    In your code imageViews are not in an array, so you cannot do p1Dice[i] .

    So please change to

    private ImageView[] p1Dice = new ImageView[5];
    

    or

    p1Dice1.setImageResource(diceImage[player1[0]]);
    p1Dice2.setImageResource(diceImage[player1[1]]);    
    p1Dice3.setImageResource(diceImage[player1[2]]);    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I have a view passing on information from a database: def serve_article(request, id): served_article

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.