Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Dice game need help

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Dice game need help

    Hello everybody

    i'm making a dice game where you roll a dice and get a number between 1 and 6. if you hit 5 you can either choose to keep going og say you want to keep your score. because if you hit one you will loose your temp score. which is my problem i can't get my program to keep running. everytime i get 1 my programs just ends. i would like it to keep running but i just lost my temp score but still had my score i had from the other times.
    hope you understand what i mean
    here is my code
    import java.util.*;
    public class terning2_Christian_Sorensen {
    	public static void main (String[] args) {
    		Scanner input= new Scanner(System.in);
    		int oppenetScore = input.nextInt();
    		Random kast = new Random(); // random generator
     
    		System.out.println("************************* DICE GAME *************************");
    		System.out.println("*** You score on roll 2, 3, 4, 5 or 6 and loose on roll 1 ***");
     		System.out.println("*************************************************************");
     
    		int kk = 0;
    		int totalScore = 0;
    		int tempScore = 0;
     
    		while (kk <2){
    			kk = kast.nextInt(6)+1;
    			tempScore = tempScore + kk;
    			totalScore = tempScore;
     
    			if (kk > 1 && totalScore > oppenetScore) {
    				System.out.println("Roll= " + kk + "\n your total temp score = " + tempScore);
    			}
     
    			else if (kk == 1) {
    				System.out.println("you lost you score from this round");
    			}
    		}
     
    	} // end of main
     
    } // end of program


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Dice game need help

    Maybe this has something to do with it?

    while (kk < 2) {

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Dice game need help

    ye ^^
    my bad been looking at for too long time to see it xD
    but how do i make a temp score and a total score?
    so i have 15 point in my total score for the previous round, and then i have 10 point in my temp score, and then i roll and 1 and i only loose my point in my temp score?
    i have tried to do that but i can't get it to working :S
    can anybody help me?

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Dice game need help

    How would you keep track of that without the computer?
    Perhaps write down the score for each player on a sheet of paper. Then in a different place store the current player's temporary score. If the points are lost, move on to the next person. Otherwise give the temporary score to the current player, and then move to the next player.

    Get a firm system down first, then write code to match it. Post questions if you have problems.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Dice game need help

    How would you keep track of that without the computer?
    Perhaps write down the score for each player on a sheet of paper. Then in a different place store the current player's temporary score. If the points are lost, move on to the next person. Otherwise give the temporary score to the current player, and then move to the next player.

    Get a firm system down first, then write code to match it. Post questions if you have problems.
    the only input is the oppents score, so the computer decide how many times it want to roll.
    but i need to make a tempscore, so it can use the point is has lost that round and keep the points from the previous times.
    but i have no idea how i can do that :/

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Dice game need help

    Forgive me, in the interest of time, I tried to nudge you in the right direction without looking at your code.



    Quote Originally Posted by lf2killer View Post
    the only input is the oppents score, so the computer decide how many times it want to roll...
    I see... What I don't see is the code where you are calling this class and giving that value. Please explain exactly how the program is to be set up and how the game works. If I understand it correctly, player1 goes first. The turn lasts until player1's total score is > opponentScore. I am guessing you supply some default value on the call for the first turn to determine when the first turn is over. Then it is player2's turn. Player2's turn lasts as long as it takes to roll either a 1, (thus losing the current points and the turn), or roll until player2's current score is > opponentScore, (thus adding current points to player2's total, and losing the turn). After that it is on to the next player, which could be player1 again or player 3-n.




    Quote Originally Posted by lf2killer View Post
    ...but i need to make a tempscore, so it can use the point is has lost that round and keep the points from the previous times...
    I can answer this after you explain how the program should work and include all of the code. Unless this is all of it, in which case there is some work to be done.

  7. #7
    Member
    Join Date
    Sep 2012
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Dice game need help

    Thanks but i got it to work late but if you wanted to know what i tired to create here is the code
    import java.util.*;
    public class dicegame11_0_gruppe_5 {
    	public static void main (String[] args) {
    		int diceRoll = 0, tempScore = 0, totalScore = 0, maxScore = 200; 			// de forskellige variabler
    		String turn = ""; 																		// bestemmer hvem der har turen
    		Scanner input = new Scanner(System.in); 											// scanner hedder input
    		System.out.println("Velkommen til Dice Game 11.4 \n ENJOY :D");			// velkommen tekst
     
     
    		turn = "enemyTurn";
     
    		while (maxScore > totalScore && turn.equals("enemyTurn")){ 					//sætter det hele i en while løkke så det bliver ved med at køre og modstanders tur
    			System.out.println("indtast modstanders score: ");							
    			int enemyScore = input.nextInt();												// indtast modstanderens score
    				turn = "computerTurn";
     
    			while (turn.equals("computerTurn")){											// det er computerens tur
    				Random kast = new Random(); 													// random generator
    				diceRoll = kast.nextInt(6)+1;
     
    				if (diceRoll == 1){																// hvis man slår 1 er det modstandens tur og man får ikke nogen point
    					tempScore = 0;
    					turn = "enemyTurn";
    					System.out.println("Computer slog 1 og fik derfor ingen point");
     
    				}
     
    				else if (enemyScore - totalScore >= 12) { 								// laver en for løkke hvis forskellen mellem enemy og total er større en 12 
     
    					for (int i=1; i<=3; i++){ 													// kører 3 gange
    						diceRoll = kast.nextInt(6)+1; 										// laver en diceRoll hvergang
    						if (diceRoll == 1){ 														// hvis den slår 1 er det modstanderens tur
    							tempScore = -1;
    							turn = "enemyTurn";
    							i = 3;
    						}
    						tempScore += diceRoll; 													// lægger dem sammen
    //						System.out.println(tempScore + "--------" + diceRoll); // udskriver værdierne                    Cheat tester
    					}
    					totalScore +=tempScore; 													// lægger tempScore til totalScore
    					tempScore = 0; 																// resetter tempScore
    					System.out.println("Computeren scorede: " + totalScore);
    					turn = "enemyTurn"; 															// det bliver modstandens tur
    				}
     
    				else {
    					tempScore += diceRoll;														// tilføjer diceroll til tempScore
     
    						if (totalScore + tempScore >= enemyScore){						//tjekker om computerens Score er større end modstanderens og prøver at slå den
    							totalScore += tempScore;
    							System.out.println("Computeren scorede: " + totalScore);
    							tempScore = 0;															// resetter tempScore så den er klar til næste runde
    							turn = "enemyTurn";													// det bliver modstanderens tur
    						}
     
    				} // slut på else if 
     
     
     
    			}// slut på while computerTurn
     
     
    		}	// slut på første while
     
    	}//slut på main
    } // slut på progr

    not it is with danish comments ^^

Similar Threads

  1. Java Programming - Dice Game Help!
    By blackvelvet in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 19th, 2012, 11:47 AM
  2. Couple compile errors for dice game
    By smithmar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2012, 01:16 PM
  3. Dice game code creating help!
    By kodols in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 22nd, 2011, 07:01 PM
  4. Dice Game help and Arrays
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 9th, 2011, 10:08 PM
  5. Dice game help, a little cleanup
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2011, 12:28 PM