Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // By: Sammy Samkough
- // Craps
- // To simulate the popular game called, "Craps"
- import java.util.Random;
- public class Craps
- {
- private static Random r = new Random();
- private int numFaces;
- private int faceValue;
- public Craps()
- {
- numFaces = 6;
- faceValue = 1;
- }
- public Craps(int sides)
- {
- numFaces = sides;
- faceValue = 1;
- }
- public void roll()
- {
- faceValue = (int)(Math.random() * numFaces) + 1;
- }
- // get results of rolled die
- public int getFaceValue()
- {
- return faceValue;
- }
- public String toString()
- {
- // we put strings since faceValue is an int
- return "" + faceValue;
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // By: Sammy Samkough
- // Craps
- // To simulate the popular game called, "Craps"
- import java.util.Scanner;
- public class CrapsMain
- {
- public static void main(String args[])
- {
- // instance variables
- Craps d1 = new Craps();
- Craps d2 = new Craps(6);
- Scanner sc = new Scanner(System.in);
- // variables
- int sum = 0;
- int points = 0;
- int roll = 1;
- boolean keepPlaying = true;
- String s;
- while (keepPlaying)
- {
- // roll
- d1.roll();
- d2.roll();
- // get results
- d1.getFaceValue();
- d2.getFaceValue();
- sum = d1.getFaceValue() + d2.getFaceValue();
- // print outs
- System.out.println("D1 = " + d1);
- System.out.println("D2 = " + d2);
- System.out.println("Sum = " + sum);
- // RULES
- // #1 on the 1st roll if it rolls 7 or 11 you win
- if (roll == 1 && ((sum == 7)||(sum == 11)))
- {
- System.out.println("YOU WIN!");
- points = 0;
- roll = 1;
- }
- // #2 you lose if you roll a 2, 3, or 12 on the first roll
- if (roll == 1 && sum == 2 || sum == 3 || sum == 12)
- {
- System.out.println("CRAPS! YOU LOSE!");
- points = 0;
- roll = 1;
- }
- // #3 if you roll a 4, 5, 6, 8, 9, or 10 you get that amount of points
- if (sum == 4 || sum == 5 || sum == 6 || sum == 8 || sum == 9 || sum == 10)
- {
- // add the points to each other
- points += sum;
- System.out.println("You have " + points + " point(s)");
- roll++;
- }
- // #4 on any other roll besides the first and you get a 7, you lose
- if (roll != 1 && sum == 7)
- {
- System.out.println("CRAPS! YOU LOSE!");
- points = 0;
- roll = 1;
- }
- // #5 you roll again if you roll a 2, 3, 11, or 12 if it's not the fist roll
- if (roll != 1 && ((sum == 2)||(sum == 3)||(sum == 11)||(sum == 12)))
- {
- System.out.println("Roll again.");
- roll++;
- }
- System.out.println("Would you like to keep playing? Say yes to keep playing and no to stop.");
- // asking for input
- s = sc.nextLine();
- // .equalsIgnoreCase excepts the string in any way it's capitalized
- if (s.equalsIgnoreCase("yes"))
- {
- System.out.println("Alright back to the top!");
- }
- else
- {
- // game over
- System.out.println("Okay, thanks for playing!");
- keepPlaying = false;
- }
- }
- }
- }
- /*
- D1 = 3
- D2 = 2
- Sum = 5
- You have 5 point(s)
- Would you like to keep playing? Say yes to keep playing and no to stop.
- yes
- Alright back to the top!
- D1 = 2
- D2 = 1
- Sum = 3
- CRAPS! YOU LOSE!
- Would you like to keep playing? Say yes to keep playing and no to stop.
- yes
- Alright back to the top!
- D1 = 1
- D2 = 6
- Sum = 7
- YOU WIN!
- Would you like to keep playing? Say yes to keep playing and no to stop.
- yes
- Alright back to the top!
- D1 = 4
- D2 = 6
- Sum = 10
- You have 10 point(s)
- Would you like to keep playing? Say yes to keep playing and no to stop.
- yes
- Alright back to the top!
- D1 = 2
- D2 = 4
- Sum = 6
- You have 16 point(s)
- Would you like to keep playing? Say yes to keep playing and no to stop.
- yes
- Alright back to the top!
- D1 = 4
- D2 = 5
- Sum = 9
- You have 25 point(s)
- Would you like to keep playing? Say yes to keep playing and no to stop.
- yes
- Alright back to the top!
- D1 = 1
- D2 = 1
- Sum = 2
- Roll again.
- Would you like to keep playing? Say yes to keep playing and no to stop.
- yes
- Alright back to the top!
- D1 = 4
- D2 = 2
- Sum = 6
- You have 31 point(s)
- Would you like to keep playing? Say yes to keep playing and no to stop.
- yes
- Alright back to the top!
- D1 = 5
- D2 = 1
- Sum = 6
- You have 37 point(s)
- Would you like to keep playing? Say yes to keep playing and no to stop.
- yes
- Alright back to the top!
- D1 = 3
- D2 = 4
- Sum = 7
- CRAPS! YOU LOSE!
- Would you like to keep playing? Say yes to keep playing and no to stop.
- yes
- Alright back to the top!
- D1 = 5
- D2 = 2
- Sum = 7
- YOU WIN!
- Would you like to keep playing? Say yes to keep playing and no to stop.
- yes
- Alright back to the top!
- D1 = 4
- D2 = 4
- Sum = 8
- You have 8 point(s)
- Would you like to keep playing? Say yes to keep playing and no to stop.
- yes
- Alright back to the top!
- D1 = 2
- D2 = 4
- Sum = 6
- You have 14 point(s)
- Would you like to keep playing? Say yes to keep playing and no to stop.
- yes
- Alright back to the top!
- D1 = 4
- D2 = 4
- Sum = 8
- You have 22 point(s)
- Would you like to keep playing? Say yes to keep playing and no to stop.
- yes
- Alright back to the top!
- D1 = 6
- D2 = 5
- Sum = 11
- Roll again.
- Would you like to keep playing? Say yes to keep playing and no to stop.
- yes
- Alright back to the top!
- D1 = 1
- D2 = 5
- Sum = 6
- You have 28 point(s)
- Would you like to keep playing? Say yes to keep playing and no to stop.
- yes
- Alright back to the top!
- D1 = 5
- D2 = 6
- Sum = 11
- Roll again.
- Would you like to keep playing? Say yes to keep playing and no to stop.
- yes
- Alright back to the top!
- D1 = 6
- D2 = 3
- Sum = 9
- You have 37 point(s)
- Would you like to keep playing? Say yes to keep playing and no to stop.
- yes
- Alright back to the top!
- D1 = 3
- D2 = 2
- Sum = 5
- You have 42 point(s)
- Would you like to keep playing? Say yes to keep playing and no to stop.
- yes
- Alright back to the top!
- D1 = 1
- D2 = 5
- Sum = 6
- You have 48 point(s)
- Would you like to keep playing? Say yes to keep playing and no to stop.
- yes
- Alright back to the top!
- D1 = 3
- D2 = 5
- Sum = 8
- You have 56 point(s)
- Would you like to keep playing? Say yes to keep playing and no to stop.
- yes
- Alright back to the top!
- D1 = 3
- D2 = 4
- Sum = 7
- CRAPS! YOU LOSE!
- Would you like to keep playing? Say yes to keep playing and no to stop.
- no
- Okay, thanks for playing!
- Press any key to continue . . .
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement