Welcome to Dream.In.Code
Become a Java Expert!

Join 149,608 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,887 people online right now. Registration is fast and FREE... Join Now!




Dice game

 
Reply to this topicStart new topic

Dice game

Bluekattz
18 Aug, 2007 - 01:02 AM
Post #1

New D.I.C Head
*

Joined: 18 Aug, 2007
Posts: 1


My Contributions
Hi, I'm Bluekattz... smile.gif I don't really have any programming experience about Java.

We have to make a gaming software which includes a dice game and paper-rock-scissor games.

In the dice game, the user will chose between 1-6, then the computer will roll three dice by randomly generating 3 numbers from 1-6.If the user's number does not appear in any of the three numbers, then he player will lose 2 points while the computer will gain 2 points. If the user's number appears once, twice or thrice, then the user will gain 1 pt, 2pt., or 3 points respectively, while the computer lose the corresponding points.. The computer and the player will both have 1- points each. The game will continue until one gets 0, or until the user would want to quits the game. the running total of the points should always be shown.

In the Paper- rock- Scissor game, the computer will ask the player to choose among the three choices(paper, rock or scissor)and then the computer will randomly choose among the three. The winner will get 1 point, while the points of the loser will get subtracted by 1 pt. In case of tie, no point will be given or subtracted. The game will continue until one gets 0, or until the user would want to quits the game. the running total of the points should always be shown.


Here is my code in for the dice game:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Random;
public class DiceGame{

/**
* An object of Dice Game represents three dice,
* where each die shows a number between 1 and 6. The dice
* can be rolled, which randomizes the numbers showing on the
* dice.
*/

public static int enterValue(){
BufferedReader dataln = new BufferedReader(new InputStreamReader(System.in));
String Choices="";
int numChoices;
System.out.print("Please enter a number:");
try{
Choices = dataln.readLine();
}
catch(IOException e){
System.out.println("Error!");
}
numChoices = Integer.parseInt(Choices);
return numChoices;
}
public static void main (String[]args){
int choices, n;
choices = enterValue();


Random random = new Random();
n = random.nextInt(2);

}
}

Here is my code for the Paper-Rock-scissor Game:


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Random;
public class Game1{
public static int enterValue(){
BufferedReader dataln = new BufferedReader(new InputStreamReader(System.in));
String Choices="";
int numChoices;
System.out.print("Please enter a number:");
try{
Choices = dataln.readLine();
}
catch(IOException e){
System.out.println("Error!");
}
numChoices = Integer.parseInt(Choices);
return numChoices;
}
public static void main (String[]args){
int choices, n;
choices = enterValue();
if(choices==1)
System.out.println("You chose: ***** PAPER *****");
else if (choices==2)
System.out.println("You chose: ()()() ROCK ()()()");
else if (choices==3)
System.out.println("You chose: >>>>> SCISSOR <<<<<");
else
System.out.println("Choose between the numbers 1, 2 and 3 only");
Random random = new Random();
n = random.nextInt(2);
if(n==0)
System.out.println("The computer chose: ***** PAPER *****");
else if (n==1)
System.out.println("The computer chose: ()()() ROCK ()()()");
else
System.out.println("The computer chose: >>>>> SCISSOR <<<<<");
if(n==0&&choices==2)
System.out.println("Computer wins");
else if(n==0&&choices==3)
System.out.println("You win");
else if(n==1&&choices==1)
System.out.println("You win");
else if(n==1&&choices==3)
System.out.println("Computer wins");
else if(n==2&&choices==1)
System.out.println("Computer wins");
else if(n==2&&choices==2)
System.out.println("You win");
else
System.out.println("It's a tie!");
}
}


I don't have any idea how to continue it.... So please help me.. I needed your response immediately... Thanks!
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Dice Game
18 Aug, 2007 - 10:52 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Hi,

Well for your dice game all you need to do now is get the computer to generate three random numbers and store them into three variables (or an array if you like). You then take the player's number and compare it against the three. Each time you do a comparison, you award or remove the points to/from the appropriate player. So you are going to need two variables to hold the score (initialized to 1). You can also use this method in your scissor game.

I would make this comparison its own function so that if later a programmer wanted to alter the gaming points or rules they can do it within one function. Below is a program I wrote for you to show you a bit of what I am talking about. Of course you will have to adapt it to your program, but it shows you how to pull the random numbers in a range, how to compare them to the computer's choice and how to award points. You can use much of this in your scissor game as well.

roll.java

CODE

import java.util.Random;

public class roll {
    // Setup initial scores for our players.
    static int playerpoints = 1;
    static int computerpoints = 1;

    public static void main(String args[]) {
        // Here I just giving the player a number. You would take
        // take the player's input and pass it to the function instead of
        // using a hardcoded 3.

        awardPoints(3);
        
        System.out.println("Score is (Player) : " + playerpoints + " and (Computer): " + computerpoints);

    }

    // Get the next roll of the Die
    private static int rollDie(int min, int max) {
        Random randomNumber = new Random();
        double nextNumber = randomNumber.nextDouble();

        // This formula will return our range between min and max
        return (int) (nextNumber * (max - min + 1) ) + min;
    }

    // Compare our number to the three dice rolled.
    private static void awardPoints(int PlayerNum) {

        // Load up three random numbers.
        int ComputerNums[] = new int[3];

        for (int i = 0; i < ComputerNums.length; i++) {
            ComputerNums[i] = rollDie(1, 6);
            System.out.println("Computer Number: " + ComputerNums[i]);
        }

        // This determines if any matches were made
        boolean found = false;

        // Compare player's number to those of the computer's.
        for (int i = 0; i < ComputerNums.length; i++) {
            if (PlayerNum == ComputerNums[i]) {
                playerpoints++;
                computerpoints--;
                found = true;
            }
        }

        // If the player matched no dice.
        if (!found) {
            playerpoints -= 2;
            computerpoints += 2;
        }
    }
}


Read the comments to know what is going on and what I am doing with various functions. It should make sense if you take it one step at a time.

Hope this helps you out. On Dream.in.Code.net we are coding ninjas! ph34r.gif





User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:06AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month