• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Help needed on a Dice game I am creating!

Hey everyone,
I am trying to make a dice game called pig where you roll a dice and if you get anything from 2 to 6 you will score. If you get 1 your chance is gone. The first one to get to 100 wins.

I have a problem where I need to shift the control from player 1 to player 2. That is after player 1 rolls and the points are added to the score, the next roll should add the points to player 2's scores. I tried but I couldn't find a way to do this.
 
Do you have any code for this? Then we can possibly see what your problem is, and where you're going wrong.

package com.avalanchestudios.dice;

import android.app.Activity;
import android.content.DialogInterface;
import android.media.Image;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Random;


public class Dice extends Activity implements View.OnClickListener {

TextView player1;
TextView player2;
Button roll;
Button hold;
Random diceGen;
int generator;
ImageView diceImage;
int score1;
int score2;
int i;

@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dice);

player1 = (TextView) findViewById(R.id.textView1);
player2 = (TextView) findViewById(R.id.textView2);
roll = (Button) findViewById(R.id.roll);
hold = (Button) findViewById(R.id.hold);
diceImage = (ImageView) findViewById(R.id.diceImage);
roll.setOnClickListener(this);
hold.setOnClickListener(this);

diceGen = new Random();
generator = diceGen.nextInt(5);



if(i%2!=0){
for (; ; ) {
if (diceImage.getDrawable().equals(R.drawable.dice_1)) {
score1 = score1 + 0;
player1.setText("Player 1: " + score1);
} else if (diceImage.getDrawable().equals(R.drawable.dice_2)) {
score1 = score1 + 2;
player1.setText("Player 1: " + score1);
} else if (diceImage.getDrawable().equals(R.drawable.dice_3)) {
score1 = score1 + 3;
player1.setText("Player 1: " + score1);
} else if (diceImage.getDrawable().equals(R.drawable.dice_4)) {
score1 = score1 + 4;
player1.setText("Player 1: " + score1);
} else if (diceImage.getDrawable().equals(R.drawable.dice_5)) {
score1 = score1 + 5;
player1.setText("Player 1: " + score1);
} else if (diceImage.getDrawable().equals(R.drawable.dice_6)) {
score1 = score1 + 6;
player1.setText("Player 1: " + score1);
}
}
}else if(i%2==0) {
for (; ; ) {
if (diceImage.getDrawable().equals(R.drawable.dice_1)) {
score2 = score2 + 0;
player2.setText("Player 1: " + score2);
} else if (diceImage.getDrawable().equals(R.drawable.dice_2)) {
score2 = score2 + 2;
player2.setText("Player 1: " + score2);
} else if (diceImage.getDrawable().equals(R.drawable.dice_3)) {
score2 = score2 + 3;
player2.setText("Player 1: " + score2);
} else if (diceImage.getDrawable().equals(R.drawable.dice_4)) {
score2 = score2 + 4;
player2.setText("Player 1: " + score2);
} else if (diceImage.getDrawable().equals(R.drawable.dice_5)) {
score2 = score2 + 5;
player2.setText("Player 1: " + score2);
} else if (diceImage.getDrawable().equals(R.drawable.dice_6)) {
score2 = score2 + 6;
player2.setText("Player 1: " + score2);
}
}
}

}
@override
public void onClick (View v){
switch (v.getId()) {
case R.id.roll:
if (generator == 0) {
diceImage.setImageResource(R.drawable.dice_1);
} else if (generator == 1) {
diceImage.setImageResource(R.drawable.dice_2);
} else if (generator == 2) {
diceImage.setImageResource(R.drawable.dice_3);
} else if (generator == 3) {
diceImage.setImageResource(R.drawable.dice_4);
} else if (generator == 4) {
diceImage.setImageResource(R.drawable.dice_5);
} else if (generator == 5) {
diceImage.setImageResource(R.drawable.dice_6);
}
break;
case R.id.hold:

break;
}
}
}
 
Upvote 0
Question: What do you think happens when this bit of code is executed?

Code:
if(i%2!=0){
for (; ; ) {
  if (diceImage.getDrawable().equals(R.drawable.dice_1)) {
    score1 = score1 + 0;
    player1.setText("Player 1: " + score1);
  } 
  else if (diceImage.getDrawable().equals(R.drawable.dice_2)) {
    score1 = score1 + 2;
    player1.setText("Player 1: " + score1);
  } 
  else if (diceImage.getDrawable().equals(R.drawable.dice_3)) {
    score1 = score1 + 3;
    player1.setText("Player 1: " + score1);
  } 
  else if (diceImage.getDrawable().equals(R.drawable.dice_4)) {
    score1 = score1 + 4;
    player1.setText("Player 1: " + score1);
  } 
  else if (diceImage.getDrawable().equals(R.drawable.dice_5)) {
    score1 = score1 + 5;
    player1.setText("Player 1: " + score1);
  } 
  else if (diceImage.getDrawable().equals(R.drawable.dice_6)) {
    score1 = score1 + 6;
    player1.setText("Player 1: " + score1);
  }
}
 
Upvote 0
It checks whether the roll is odd or even and if odd, adds the respective score to player 1.
The if statement checks if the roll is odd or even. The for statement(I think this is the problem) sets it in a loop for checking the roll.

Any other way I can make this code better? I want it to check the button click to see if it is clicked odd or even times. If it is clicked for an odd number of times, the score adds to the player1 score and if even it adds it to the player 2 score.
Help!
 
Upvote 0
I'll be honest with you, I'm not going to do your homework, but I'll give you a couple of pointers

1. You don't need the for( ; ; ) loops in your onCreate() method.
2. The code for adjusting player scores is in the wrong place. All this stuff should be in your roll button click listener.

Thanks man, I did a thorough breakdown and started from scratch. And I have got the game running, I made quite a few changes by removing the extra code and not using any loops. Mostly if-else and switch case was enough.

Thanks though!
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones