Question

Please write in JAVA code: Create a simple dice game that rolls 2 die at the...

Please write in JAVA code:

Create a simple dice game that rolls 2 die at the press of a button. Append the results of each roll to a text area. If the values rolled are the same(i.e., you've rolled a double), indicate that next to the dice values.

Homework Answers

Answer #1
import java.io.*; 
import java.util.Scanner;
import java.io.File;
import java.io.IOException;  
import java.io.FileWriter; 
import java.util.Random; 

public class gam{
    // Open file in append mode.
        public static void appendStrToFile(String fileName, 
                                       String str) 
    { 
        try { 
  
            // Open given file in append mode. 
            BufferedWriter out = new BufferedWriter( 
                   new FileWriter(fileName, true)); 
            out.write(str+"\n"); 
            out.close(); 
        } 
        catch (IOException e) { 
            System.out.println("exception occoured" + e); 
        } 
    } 
    // perform game using random numbers.
         static void func(){

                String s="", d1="",d2="";
                Random rand = new Random();
                int rand_int1 = rand.nextInt(6)+1; 
        int rand_int2 = rand.nextInt(6)+1;
        d1=Integer.toString(rand_int1);
        d2=Integer.toString(rand_int2); 
        if(rand_int1==rand_int2){
                s=d1+", "+d2+" you've rolled a double.";
        }
        else{
                s=d1+", "+d2;
        }

        appendStrToFile("result.txt", s);
                
        }
         public static void main(String []args){
                // crate a file
                File file = new File("C:\\Users\\LENOVO\\Desktop\\game\\result.txt");
                int t=1;
                boolean result;
                try   
                {  
                        result = file.createNewFile();  //creates a new file  
                        if(result)      // test if successfully created a new file  
                        {  
                        System.out.println("file created "+file.getCanonicalPath()); //returns the path string  
                        }  
                        else  
                        {  
                        System.out.println("File already exist at location: "+file.getCanonicalPath());  
                        }  
                }   
                catch (IOException e)   
                        {  
                        e.printStackTrace();    //prints exception if any  
                        } 
                // Take input        
                Scanner myObj = new Scanner(System.in);
                while(t ==1){
                        System.out.println("Press 1 to role dice or press 0 to exit game.");
                        String option = myObj.nextLine();
                        t=Integer.parseInt(option); 
                        if(t==1){
                         func();// perform the game
                        }
                        else{
                                System.out.println("Exiting game...");
                        }

                }

                

        
     }
}

The final results of the game are written to "result.txt" file.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
JAVA: MUST BE DONE IN JAVA Assignment: Write algorithms and programs to play “non-betting” Craps. Craps...
JAVA: MUST BE DONE IN JAVA Assignment: Write algorithms and programs to play “non-betting” Craps. Craps is a game played with a pair of dice. In the game, the shooter (the player with the dice) rolls a pair of dice and the number of spots showing on the two upward faces are added up. If the opening roll (called the “coming out” roll) is a 7 (“natural”) or 11 (“yo-leven”), the shooter immediately wins the game. If the coming out...
Implement the following problem as a self-contained Python program (module). Please write in beginner level code!...
Implement the following problem as a self-contained Python program (module). Please write in beginner level code! Thanks! The game of Pig is a simple two-player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn, a player rolls a six-sided die. After each roll: a) If the player rolls a 1 then the player gets no new points and it becomes the other player’s turn. b) If the player rolls...
In java create a dice game called sequences also known as straight shooter. Each player in...
In java create a dice game called sequences also known as straight shooter. Each player in turn rolls SIX dice and scores points for any sequence of CONSECUTIVE numbers thrown beginning with 1. In the event of two or more of the same number being rolled only one counts. However, a throw that contains three 1's cancels out player's score and they mst start from 0. A total of scores is kept and the first player to reach 100 points,...
Classes/ Objects(programming language java ) in software (ATOM) Write a Dice class with three data fields...
Classes/ Objects(programming language java ) in software (ATOM) Write a Dice class with three data fields and appropriate types and permissions diceType numSides sideUp The class should have A 0 argument (default) constructor default values are diceType: d6, numSides: 6, sideUp: randomValue 1 argument constructor for the number of sides default values are diceType: d{numSides}, sideUp: randomValue 2 argument constructor for the number of sides and the diceType appropriate accessors and mutators *theoretical question: can you change the number of...
Bunco is a group dice game that requires no skill. The objective of the game is...
Bunco is a group dice game that requires no skill. The objective of the game is to accumulate points by rolling certain combinations. The game is played with three dice, but we will consider a simpler version involving only two dice. How do you play two dice Bunco? There are six rounds, one for each of the possible outcomes in a die, namely the numbers one through six. Going clockwise, players take turns rolling two dice trying to score points....
   Dice Game – Accumulator Pattern and Conditionals (40 pts) IN PYTHON Write a program dicegame.py...
   Dice Game – Accumulator Pattern and Conditionals (40 pts) IN PYTHON Write a program dicegame.py that has the following functions in the following order: in python Write a function roll that takes an int n and returns a random number between 1 and n inclusive. Note: This function represents a n sided die so it should produce pseudo random numbers. You can accomplish this using the random module built into python.         (3 pts) Write a function scoreRound that...
For this assignment, design and implement a class to represent a die (singular of "dice"). A...
For this assignment, design and implement a class to represent a die (singular of "dice"). A normal bag of dice for playing Dungeons and Dragons, for example, contains dice with the following numbers of sides: 4, 6, 8, 10, 12, 20, and 100. In this program, the sides (or faces) of the die are numbered, starting with one. The current face value of the die corresponds to the side that is currently facing upward. By default, the face value is...
One of the most popular games of chance is a dice game known as “craps”, played...
One of the most popular games of chance is a dice game known as “craps”, played in casinos around the world. Here are the rules of the game: A player rolls two six-sided die, which means he can roll a 1, 2, 3, 4, 5 or 6 on either die. After the dice come to rest they are added together and their sum determines the outcome. If the sum is 7 or 11 on the first roll, the player wins....
Using C++ / provide code comments so I can understand. Create a simple linked list program...
Using C++ / provide code comments so I can understand. Create a simple linked list program to create a class list containing class node { void *info; node *next; public: node (void *v) {info = v; next = 0; } void put_next (node *n) {next = n;} node *get_next ( ) {return next;} void *get_info ( ) {return info;} }; Be able to initially fill the list. Provide functions to insert/append nodes and remove nodes from the linked list. Be...
Write a program that does the following. *Java Code* • Create an instance of the Hashtable...
Write a program that does the following. *Java Code* • Create an instance of the Hashtable class from the Java API • Make the initial table size of the hash table 1000 and the load factor 0.75 (which has been shown experimentally to be optimal). • Create an instance of the AVLtree class attached to this homework. • Measure the running time of adding various numbers of entries (as required by the table below) to the hash table and the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT