Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //this is Michelle's code
- import java.util.*;
- import javax.swing.JFrame;
- import java.awt.*;
- public class dicegame extends JFrame
- {
- static int win; //initializing my variables I'll use throughout the entire program
- static int dice1;
- static int dice2;
- public void paint (Graphics g) //paint class
- {
- super.paint(g);
- drawdice(g,dice1,dice2);
- }
- public static void drawdice(Graphics g,int dice1,int dice2) //method used for drawing the two dice
- {
- if (dice1==1) //different if statements for each value of dice1 and dice2; dice1 is the die in the upper left hand corner, and dice2 is lower right hand corner die
- {
- g.drawRect(30,30,150,150);
- g.fillOval(105,105,20,20);
- }
- if (dice1==2)
- {
- g.drawRect(30,30,150,150);
- g.fillOval(60,60,20,20);
- g.fillOval(150,150,20,20);
- }
- if(dice1==3)
- {
- g.drawRect(30,30,150,150);
- g.fillOval(60,60,20,20);
- g.fillOval(105,105,20,20);
- g.fillOval(150,150,20,20);
- }
- if(dice1==4)
- {
- g.drawRect(30,30,150,150);
- g.fillOval(60,60,20,20);
- g.fillOval(60,150,20,20);
- g.fillOval(150,60,20,20);
- g.fillOval(150,150,20,20);
- }
- if(dice1==5)
- {
- g.drawRect(30,30,150,150);
- g.fillOval(60,60,20,20);
- g.fillOval(105,105,20,20);
- g.fillOval(150,150,20,20);
- g.fillOval(150,60,20,20);
- g.fillOval(60,150,20,20);
- }
- if(dice1==6)
- {
- g.drawRect(30,30,150,150);
- g.fillOval(60,60,20,20);
- g.fillOval(60,90,20,20);
- g.fillOval(60,120,20,20);
- g.fillOval(150,60,20,20);
- g.fillOval(150,90,20,20);
- g.fillOval(150,120,20,20);
- }
- if(dice2==1)
- {
- g.drawRect(200,200,150,150);
- g.fillOval(275,275,20,20);
- }
- if(dice2==2)
- {
- g.drawRect(200,200,150,150);
- g.fillOval(230,230,20,20);
- g.fillOval(320,320,20,20);
- }
- if(dice2==3)
- {
- g.drawRect(200,200,150,150);
- g.fillOval(230,230,20,20);
- g.fillOval(275,275,20,20);
- g.fillOval(320,320,20,20);
- }
- if(dice2==4)
- {
- g.drawRect(200,200,150,150);
- g.fillOval(230,230,20,20);
- g.fillOval(230,320,20,20);
- g.fillOval(320,230,20,20);
- g.fillOval(320,320,20,20);
- }
- if(dice2==5)
- {
- g.drawRect(200,200,150,150);
- g.fillOval(230,230,20,20);
- g.fillOval(275,275,20,20);
- g.fillOval(320,320,20,20);
- g.fillOval(320,230,20,20);
- g.fillOval(230,320,20,20);
- }
- if(dice2==6)
- {
- g.drawRect(200,200,150,150);
- g.fillOval(230,230,20,20);
- g.fillOval(230,260,20,20);
- g.fillOval(230,290,20,20);
- g.fillOval(320,230,20,20);
- g.fillOval(320,260,20,20);
- g.fillOval(320,290,20,20);
- }
- }
- public static void main(String [] args) //main method of the game; calls all methods related to the game
- {
- win = 500; //initializing set amount of points that the game starts out with
- Random rand = new Random(); //making random numbers from 1-6 for dice1 and dice2
- dice1 = rand.nextInt(6)+1;
- dice2 = rand.nextInt(6)+1;
- Scanner prompt = new Scanner(System.in); //scanner that allows user to play
- System.out.println("Welcome to Michelle's dice game! Do you want to play? Type Y or N. ");
- String answer = prompt.next();
- if(answer.equals("Y")||answer.equals("y"))
- {
- System.out.println("Welcome! You start off with 500 points. Every roll you make with these two");
- System.out.println("dice with impact your fortune.");
- System.out.println("To win the game, you must get 1000 points or more.");
- System.out.println("Every time you roll the dice, the dice will appear in another window.");
- Scanner scan = new Scanner(System.in); //scanner allowing to roll the dice
- System.out.println("Type ROLL to roll the two dice.");
- String roll1 = scan.nextLine();
- if(roll1.equals("roll")||roll1.equals("ROLL"))
- {
- points(win); //calls points method
- }
- else
- {
- System.out.println("That's not the word roll. Goodbye.");
- }
- }
- else
- {
- System.out.print("Okay, goodbye.");
- }
- }
- public static void points(int win) //method containing switch case, controls amount of points made per roll
- {
- if(win<1000 && win>0)
- {
- switch (dice1+dice2)
- {
- case 2:
- win = win+200; //if sum of dice1 and dice2 (the roll) = 2, the user gains 200 pts
- System.out.println("You rolled a sum of 2! You have won 200 points. Your total is now " + win + ".");
- break;
- case 3:
- win = win-300; //if sum = 3, user loses 300
- System.out.println("You rolled a sum of 3! You have lost 300 points. Your total is now " + win + ".");
- break;
- case 4:
- win = win+400; //if sum = 4, user wins 400
- System.out.println("You have rolled a sum of 4! You have won 400 points. Your total is now " + win + ".");
- break;
- case 5:
- win = win-100; //if sum = 5, user loses 100
- System.out.println("You have rolled a sum of 5! You have lost 100 points. Your total is now " + win + ".");
- break;
- case 6:
- win = win+150; //if sum = 6, user wins 150
- System.out.println("You have rolled a sum of 6! You have won 150 points. Your total is now " + win + ".");
- break;
- case 7:
- win = win-50; //if sum = 7, user loses 50
- System.out.println("You have rolled a sum of 7! You have lost 50 points. Your total is now " + win + ".");
- break;
- case 8:
- win = win+250; //if sum = 8, user wins 250
- System.out.println("You have rolled a sum of 8! You have won 250 points. Your total is now " + win + ".");
- break;
- case 9:
- win = win+100; //if sum = 9, user wins 100
- System.out.println("You have rolled a sum of 9! You have won 100 points. Your total is now " + win + ".");
- break;
- case 10:
- win = win+100; //if sum = 10, user wins 100
- System.out.println("You have rolled a sum of 10! You have won 100 points. Your total is now " + win + ".");
- break;
- case 11:
- win = win-200; //if sum = 11, user loses 200
- System.out.println("You have rolled a sum of 11! You have lost 200 points. Your total is now " + win + ".");
- break;
- case 12:
- win = win+250; //if sum = 12, user wins 250
- System.out.println("You have rolled a sum of 12! You have won 250 points. Your total is now " + win + ".");
- break;
- }
- dicegame app = new dicegame(); //causes the frame with the 2 dice to show up every time this method runs
- app.setSize(500,500);
- app.setVisible(true);
- ask(win); //calls ask method
- }
- else
- {
- System.out.println("You lost all of your points and lost the game. Game over.");
- }
- }
- public static void ask(int win) //method asking user if they want to roll again
- {
- if(win<1000)
- {
- Scanner prompt1 = new Scanner(System.in);
- System.out.println("Wanna go again? Type Y or N.");
- String ans = prompt1.next();
- if (ans.equals("y") || ans.equals("Y") || ans.equals("yes"))
- {
- Random rand = new Random(); //randomizes value of dice1 and dice2 again for each time the user wants to roll again
- dice1 = rand.nextInt(6)+1;
- dice2 = rand.nextInt(6)+1;
- points(win); //calling the points method again
- }
- else
- {
- System.out.println("Okay, goodbye.");
- }
- }
- if(win>=1000)
- {
- System.out.println("You won 1000 points or more and won the game!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment