dice game - please help

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1905 Views - Last Post: 02 April 2010 - 11:04 AM Rate Topic: -----

#1 evedugz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-February 10

dice game - please help

Posted 29 March 2010 - 04:03 AM

I have to create a dice game using java and am finding it very difficult as I have very little knowledge of the language. basically the player can choose to roll between 2 and 5 die. if they roll a pair they get 10 point, triple 75 points, 2 pairs 50 points, full house 100, fours 200, fives 250 and a straight( all different numbers) 300 points.after the player takes their first turn they should have the choice of rolling some, all or none of the dice again. so far i have created 3 classes but i seem to have a few errors and cant figure out what im doing wrong. any help would be greatly appreciated.

package sm;

import java.util.Scanner;

import javax.xml.transform.Result;

import sm.DiceRoll;

public class Dice {
	
	private DiceRoll dice1;
	private DiceRoll dice2;
	private DiceRoll dice3;
	private DiceRoll dice4;
	private DiceRoll dice5;
	
	public Dice() {
		
		dice1 = new DiceRoll();
		dice2 = new DiceRoll();
		dice3 = new DiceRoll();
		dice4 = new DiceRoll();
		dice4 = new DiceRoll();
	}
	public void rolldice(){
			dice1.newFace();
			dice2.newFace();
			dice3.newFace();
			dice4.newFace();
			dice5.newFace();
		}
	int[]result = new result [7];
	for (int i = 1; i < 7; i ++){
		int count = 0;
		for (int j = 0; j < dicelength; j ++){
			if (dice [j] getFace()== i){
				count = count + 1;
				{
			}
		if (count == 2){
			result[0]++;
			points = 10;
		}
		if (count == 3){
			result[1]++;
			points = 75;
		}
		if (count == 4){
			result [2]++;
			points = 200;
		}
		if (count == 5){
			result [3]++;
			points = 250;
		
		
		}
	}
	if (result[0]==2){
		result [2]++;
		result [0]= result[0]-2;
		points = 50;
		
		if (result[1]==1){
			result(1)++;
			points = 100;
		}else{
			result[1] ++;
			result[1]= result [0]-1;
			points = 100;
		}
		
		boolean thumbsup;
		for (int i = 1 ; i > 7; i ++ ){
			thumbsup = false;
		for (int j = 0; j < Dice; ){
			
			if (dices [j].getface()== i){
				thumbsup = true;
			}
		
		}
		if(thumbs == true){
		}else{
			break;
		}
	}
		if (thumbsup == true){
			result [2] ++;
			points = 250;
		}
		
	}
	



			

			Scanner input = new Scanner(System.in);

			private int() {
				// TODO Auto-generated method stub
				return 0;
			}

		}



package sm;



import java.io.IOException;

public class DiceGame {
	public static void main(String[] args) throws IOException{
		runDiceGame();
	}	


	
	public static void runDiceGame() throws IOException {

		
		Stream stream = new Stream(System.in);
		Dice mydice = new Dice();
								
		

System.out.print("****Welcome to the Dice Game!!****\n\n"); 
System.out.println("There are 5 die available...\n");
System.out.println("How many die would you like to roll?");


	

			int points = 0;
			char cont = 'z';
			
				
			do {
				int DiceEntered = 0;
			
			
				while ( DiceEntered < 2 || DiceEntered > 5 ){
				if (DiceEntered > 5 )
					System.out.println("Invalid! You must choose a number between 2 and 5!!!\n");
				System.out.println("How many die would you like to play with?\n");
				
				DiceEntered = stream.readInt();
			}
				
				
			
		mydice.rolldice();
		
		System.out.println(mydice.toString());
		
		if (points == 10);{
			points = points + 10 ;
			System.out.println("\n**You rolled a pair!\n your score is" + points);
		
		} if (points == 75);{
			points = points + 75;
			System.out.println("you rolled a triple? your score is" + points);
			
		} if (points == 200);{
			points = points + 200 ;
			System.out.println("you rolled fours" + points);
		} if (points ==250);{
			points = points + 250;
			System.out.println("you rolled fives" + points);
		} if (points == 50);{
			points = points + 50;
			System.out.println("you rolled 2 pairs" + points);
		} if (points == 100);{
			points = points + 100;
			System.out.println("you rolled a triple and a pair" + points );
		} if (points == 300);{
			points = points + 300;
			System.out.println("you rolled a straight" + points);
		}
		

		
		cont = 'x';
		while ( cont != 'y'){
			if (cont == 'n') break;
			System.out.println("Continue? (y/n)");
			cont = Character.toLowerCase(stream.readChar());
			System.out.println(cont);
		
		}
	}while(cont == 'y');
	
	System.out.println("***bye bye***");
			}
}
		
			
		
		
	
	
package sm;



public class DiceRoll {
	private int face;


	public DiceRoll() {
		newFace();
	}
	

	public int getFace() {
		return this.face;
	}
	public void newFace() {
		this.face =1 + (int)(Math.random()*6);
	}
	
	public String toString() {
	
			String result = "";
			if (face == 1){
				result = "1";
			}else if (face == 2){
				result = "2";
			
			}else if (face == 3){
				result = "3";
			}else if (face == 4){
				result = "4";
			}else if (face == 5){
				result = "5";
			}else if (face == 6){
				result = "6";
				
			}
		System.out.println("you rolled" + result);
			
	
	}
	
		
}

EDIT: Inserted code tags, PennyBoki. Please :code: in future. Thanks. :)

This post has been edited by PennyBoki: 29 March 2010 - 04:09 AM


Is This A Good Question/Topic? 0
  • +

Replies To: dice game - please help

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: dice game - please help

Posted 29 March 2010 - 04:14 AM

from first glance, you dont initialize dice5, as you initiate dic4 twice:
dice4 = new DiceRoll(); 
dice4 = new DiceRoll(); 


anyway, it would be helpful if you posted the error messesges you get.
Was This Post Helpful? 2
  • +
  • -

#3 evedugz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-February 10

Re: dice game - please help

Posted 29 March 2010 - 04:22 AM

thanks! i'v corrected that, these are some of the error messages:

at sm.Dice.<init>(Dice.java:32)
at sm.DiceGame.runDiceGame(DiceGame.java:18)
at sm.DiceGame.main(DiceGame.java:9)

i seem to be having trouble printing the players results to the screen and calculation results, any ideas?
thanks again.
Was This Post Helpful? 0
  • +
  • -

#4 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: dice game - please help

Posted 29 March 2010 - 07:35 AM

 if (thumbsup == true)
{ 
   result [2] ++; 
    points = 250; 
}             
} 
Scanner input = new Scanner(System.in); 
 
                        private int() { 
                                // TODO Auto-generated method stub 
                                return 0; 
                        }

private int() {
That is an incorrect method heading, it has a type and a return but it has no name, our methods need names. They are grumpy, but work with ye if ye are nice enough to name them! ;)

Hope this helps!

This post has been edited by Fuzzyness: 29 March 2010 - 07:37 AM

Was This Post Helpful? 1
  • +
  • -

#5 evedugz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-February 10

Re: dice game - please help

Posted 02 April 2010 - 06:25 AM

thanks i think i'v resolved that
 private int getFace() {
				// TODO Auto-generated method stub
				return 0; 


make sense?
and also im having resolving the errors at
 	int[]result = new result[7];
	for (int i = 1; i < 7; i ++){
		int count = 0;
		int dicelength;
		for (int j = 0; j < Dice.length; j ++){
			if (Dice[j] getFace()== i){
				count = count + 1;  


any ideas that night point me in the right direction? would really appreciate it.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: dice game - please help

Posted 02 April 2010 - 06:34 AM

Here Dice[j] getFace(), you need to use the dot-notation to invoke getFace() from your Dice object. Like so: Dice[j].getFace(). Also, in the future, please specifically describe your errors. It's easier for us to help you if you tell us what's wrong and where it is occurring. :)
Was This Post Helpful? 1
  • +
  • -

#7 evedugz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-February 10

Re: dice game - please help

Posted 02 April 2010 - 07:17 AM

thanks here all the errors its throwing up,

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
result cannot be resolved to a type
Syntax error on token ";", { expected after this token
The primitive type int of Dice does not have a field length
The type of the expression must be an array type but it resolved to int
Duplicate local variable j
DiceEntered cannot be resolved
The type of the expression must be an array type but it resolved to int
Syntax error, insert "}" to complete Statement
Syntax error, insert "else Statement" to complete IfStatement
Syntax error, insert "}" to complete Statement
Syntax error, insert "else Statement" to complete IfStatement
Syntax error, insert "}" to complete Block
Syntax error on token "String", @ expected
Syntax error, insert "enum Identifier" to complete EnumHeader
Syntax error on token "}", delete this token

at sm.Dice.<init>(Dice.java:27)
at sm.DiceGame.runDiceGame(DiceGame.java:18)
at sm.DiceGame.main(DiceGame.java:9)

would appreciate any more help u can give! :)
Was This Post Helpful? 0
  • +
  • -

#8 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: dice game - please help

Posted 02 April 2010 - 07:19 AM

Could you post your new and revised code please so we know what to look at? Would help a lot.
Was This Post Helpful? 0
  • +
  • -

#9 evedugz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-February 10

Re: dice game - please help

Posted 02 April 2010 - 07:29 AM

dice class

 package sm;

public class Dice {
	int points = 0;
	int Dice = 0;
	private DiceRoll dice1;
	private DiceRoll dice2;
	private DiceRoll dice3;
	private DiceRoll dice4;
	private DiceRoll dice5;
	
	public Dice() {
		
		dice1 = new DiceRoll();
		dice2 = new DiceRoll();
		dice3 = new DiceRoll();
		dice4 = new DiceRoll();
		dice5 = new DiceRoll();
	}
	public void rolldice(){
			dice1.newFace();
			dice2.newFace();
			dice3.newFace();
			dice4.newFace();
			dice5.newFace();
		}
	int[]result = new result[7];
	for (int i = 1; i < 7; i ++){
		int count = 0;
		int dicelength;
		for (int j = 0; j < Dice.length; j ++){
			if (Dice[j].getFace()== i){
				count = count + 1;
				{
			}
		if (count == 2){
			result[0]++;
			points = 10;
		}
		if (count == 3){
			result[1]++;
			points = 75;
		}
		if (count == 4){
			result [2]++;
			points = 200;
		}
		if (count == 5){
			result [3]++;
			points = 250;
		
		
		}
	}
	if (result[0]==2){
		result [2]++;
		result [0]= result[0]-2;
		points = 50;
		
		if (result[1]==1){
			result[1]++;
			points = 100;
		}else{
			result[1] ++;
			result[1]= result [0]-1;
			points = 100;
		}
		
		boolean thumbsup;
		for (int i = 1 ; i > 7; i ++ ){
			thumbsup = false;
		for (int j = 0; j < DiceEntered; ){
			
			if (Dice [j].getface()== i){
				thumbsup = true;
			}
		
		}
		if(thumbsup == true){
		}else{
			break;
		}
	}
		if (thumbsup == true){
			result [2] ++;
			points = 250;
			}
	
		public String toString() {
			String name;
			name = (dice1.toString()) + "|" +(dice2.toString()) + "|" + (dice3.toString()) + "|" + (dice4.toString()) + "|" + (dice5.toString());		
			return name;
		}
	
	}
	
}	

			private int getFace() {
				// TODO Auto-generated method stub
				return 0;
			}

		}



  


DiceGame class
 package sm;



import java.io.IOException;

public class DiceGame {
	public static void main(String[] args) throws IOException{
		runDiceGame();
	}	


	
	public static void runDiceGame() throws IOException {

		
		Stream stream = new Stream(System.in);
		Dice mydice = new Dice();
								
		

System.out.print("****Welcome to the Dice Game!!****\n\n"); 
System.out.println("There are 5 die available...\n");
System.out.println("How many die would you like to roll?");


	

			int points = 0;
			char cont = 'z';
			
				
			do {
				int DiceEntered = 0;
			
			
				while ( DiceEntered < 2 || DiceEntered > 5 ){
				if (DiceEntered > 5 )
					System.out.println("Invalid! You must choose a number between 2 and 5!!!\n");
				System.out.println("How many die would you like to play with?\n");
				
				DiceEntered = stream.readInt();
			}
				
				
			
		mydice.rolldice();
		
		System.out.println(mydice.toString());
		
		if (points == 10);{
			points = points + 10 ;
			System.out.println("\n**You rolled a pair!\n your score is" + points);
		
		} if (points == 75);{
			points = points + 75;
			System.out.println("you rolled a triple? your score is" + points);
			
		} if (points == 200);{
			points = points + 200 ;
			System.out.println("you rolled fours" + points);
		} if (points ==250);{
			points = points + 250;
			System.out.println("you rolled fives" + points);
		} if (points == 50);{
			points = points + 50;
			System.out.println("you rolled 2 pairs" + points);
		} if (points == 100);{
			points = points + 100;
			System.out.println("you rolled a triple and a pair" + points );
		} if (points == 300);{
			points = points + 300;
			System.out.println("you rolled a straight" + points);
		}
		

		
		cont = 'x';
		while ( cont != 'y'){
			if (cont == 'n') break;
			System.out.println("Continue? (y/n)");
			cont = Character.toLowerCase(stream.readChar());
			System.out.println(cont);
		
		}
	}while(cont == 'y');
	
	System.out.println("***bye bye***");
			}
}
		
			
		
		
	
	
  


DiceRoll class
 package sm;



public class DiceRoll {
	private int face;


	public DiceRoll() {
		newFace();
	}
	

	public int getFace() {
		return this.face;
	}
	public void newFace() {
		this.face =1 + (int)(Math.random()*6);
	}
	
	

	
			String result = "";{
			if (face == 1){
				result = "1";
			}else if (face == 2){
				result = "2";
			
			}else if (face == 3){
				result = "3";
			}else if (face == 4){
				result = "4";
			}else if (face == 5){
				result = "5";
			}else if (face == 6){
				result = "6";
				
			}
		System.out.println("you rolled" + result);
			
	
	}
	
		
} 



all errors are within the Dice class
Was This Post Helpful? 0
  • +
  • -

#10 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: dice game - please help

Posted 02 April 2010 - 07:37 AM

int Dice = 0;

You need to change that to a lowercase D. Dice is the name of the class so it is bad programming to name it that. You gave the Dice object a value of int, but you treat it as an Array in the class? You should make it an array and not an int if you want to treat it like an array.

Edit - It seems as though you have an extra brace, hard to trace them in your code posted the gaps are way big. so check it in your Compiler.. but I believe it is this one...
                public String toString() { 
                        String name; 
                        name = (dice1.toString()) + "|" +(dice2.toString()) + "|" + (dice3.toString()) + "|" + (dice4.toString()) + "|" + (dice5.toString());            
                        return name; 
                } 
         
        } 
         
}// <-- Extra from the looks of it


This post has been edited by Fuzzyness: 02 April 2010 - 07:40 AM

Was This Post Helpful? 0
  • +
  • -

#11 evedugz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-February 10

Re: dice game - please help

Posted 02 April 2010 - 07:54 AM

i dont really understand what you mean by changing it to an array? sorry dont have much understanding of java.
Was This Post Helpful? 0
  • +
  • -

#12 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: dice game - please help

Posted 02 April 2010 - 08:10 AM

for (int j = 0; j < Dice.length; j ++){ 
                        if (Dice[j].getFace()== i){ 
                                count = count + 1;

You use Dice as if it is an array. You use array methods with it.

int Dice = 0;

You create it as a int, if make it an int but use it as an array. So you need to change Dice from an int, to an Array of int's. like you did with the result array.
Was This Post Helpful? 0
  • +
  • -

#13 evedugz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-February 10

Re: dice game - please help

Posted 02 April 2010 - 09:36 AM

 int[] result = new result[7];
		for (int i = 1; i < 7; i ++){
			int count = 0;
			int length;
		}
		int []Dice = new dice;
			for (int j = 0; j < Dice.length; j ++)
				if (Dice.lenght; j++){	 


i'v changed it a bit but it still wont work
Was This Post Helpful? 0
  • +
  • -

#14 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: dice game - please help

Posted 02 April 2010 - 10:53 AM

int []Dice = new dice;
the second dice needs to be capitalized. I am at work right now away from my Compiler, so when I get to my house, if it isnt solved i will load it up and check specific errors ect.
Was This Post Helpful? 0
  • +
  • -

#15 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: dice game - please help

Posted 02 April 2010 - 10:59 AM

I don't mean to be rude, but seriously, check your code for spelling before you submit questions. Seeing this thread has 14 posts so far and you still have the error,
Dice.lenght



only indicates to me that you're lazy.

Now, here are some more errors..

for (int i = 1; i < 7; i ++){ 
                        int count = 0; 
                        int length; 
                } 



I doubt that you want to declare the variable count inside the for-loop and I seriously doubt that you want to declare the variable length at all since it's an attribute that comes with the array class and you don't need to declare it as it's already there.

This post has been edited by Gloin: 02 April 2010 - 11:00 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2