Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.lang.*;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- class Animation{
- private Thread currentThread;
- public void animateFrame(JFrame window, int[] faceValues, boolean dispose){
- try{
- currentThread.stop();
- }catch(Exception e){};
- currentThread=new Thread(new Runnable(){
- Graphics g = window.getContentPane().getGraphics();
- private int random(int min, int max) {
- return (min + (int)(Math.random() * ((max - min) + 1)));
- };
- public int[][][] arcPoints(int numberOfArcs, int maxHeight){
- int[][][] arcs=new int[numberOfArcs][10][];
- double min=maxHeight*.8;//loses .2 of total maxHeight
- double split=min/numberOfArcs;//number of change in each arc
- for(int arc=0; arc<numberOfArcs; arc++){
- int[][] points=new int[32][2];//array of coords (x,y)
- for(int index=0; index<32; index++){
- int heightForArc=(int)(maxHeight-(split*arc));
- points[index][0]=(int)((arc*32+index)*1.25);// x-coordinate
- points[index][1]=150-(int)(Math.abs(Math.sin(index*.1)*heightForArc));// y-coordinate
- };
- arcs[arc]=points;
- };
- return(arcs);
- };
- public void drawDice(int x,int y,int faceValue){
- if (faceValue != 0)
- g.drawRect(x,y,20,20);
- if(faceValue==1){
- g.fillOval(x+10,y+10,2,2);
- }else if(faceValue==2){
- g.fillOval(x+5,y+10,2,2);
- g.fillOval(x+15,y+10,2,2);
- }else if(faceValue==3){
- g.fillOval(x+10,y+5,2,2);
- g.fillOval(x+10,y+10,2,2);
- g.fillOval(x+10,y+15,2,2);
- }else if(faceValue==4){
- g.fillOval(x+5,y+5,2,2);
- g.fillOval(x+5,y+15,2,2);
- g.fillOval(x+15,y+5,2,2);
- g.fillOval(x+15,y+15,2,2);
- }else if(faceValue==5){
- g.fillOval(x+5,y+5,2,2);
- g.fillOval(x+5,y+15,2,2);
- g.fillOval(x+10,y+10,2,2);
- g.fillOval(x+15,y+5,2,2);
- g.fillOval(x+15,y+15,2,2);
- }else if(faceValue==6){
- g.fillOval(x+5,y+5,2,2);
- g.fillOval(x+5,y+10,2,2);
- g.fillOval(x+5,y+15,2,2);
- g.fillOval(x+15,y+5,2,2);
- g.fillOval(x+15,y+10,2,2);
- g.fillOval(x+15,y+15,2,2);
- }else if(faceValue==7){
- g.fillOval(x+5,y+5,2,2);
- g.fillOval(x+5,y+10,2,2);
- g.fillOval(x+5,y+15,2,2);
- g.fillOval(x+15,y+5,2,2);
- g.fillOval(x+15,y+10,2,2);
- g.fillOval(x+15,y+15,2,2);
- g.fillOval(x+10,y+10,2,2);
- }else if(faceValue==8){
- g.fillOval(x+5,y+3,2,2);
- g.fillOval(x+5,y+6,2,2);
- g.fillOval(x+5,y+9,2,2);
- g.fillOval(x+5,y+12,2,2);
- g.fillOval(x+15,y+3,2,2);
- g.fillOval(x+15,y+6,2,2);
- g.fillOval(x+15,y+9,2,2);
- g.fillOval(x+15,y+12,2,2);
- }else if(faceValue==9) {
- g.fillOval(x+5,y+3,2,2);
- g.fillOval(x+5,y+6,2,2);
- g.fillOval(x+5,y+9,2,2);
- g.fillOval(x+5,y+12,2,2);
- g.fillOval(x+15,y+3,2,2);
- g.fillOval(x+15,y+6,2,2);
- g.fillOval(x+15,y+9,2,2);
- g.fillOval(x+15,y+12,2,2);
- g.fillOval(x+10,y+10,2,2);
- };
- };
- public boolean wait(int ms){
- try{
- Thread.sleep(ms);
- }catch(Exception e){
- return false;
- };
- return true;
- };
- public void run(){
- int[][][] arcs=arcPoints(3,90);
- int[][][] secondArcs=arcPoints(3,90);
- for(int i=0; i<arcs.length; i++){
- for(int pointSet=0; pointSet<arcs[i].length; pointSet++){
- g = window.getContentPane().getGraphics();
- g.clearRect(0,55,600,122); // x, y , w, h
- int[] points=arcs[i][pointSet];
- int[] secondPoints=secondArcs[i][pointSet];
- drawDice(points[0],points[1],(pointSet == arcs[i].length ? faceValues[0] : random(1, 6)));
- drawDice(260-secondPoints[0],secondPoints[1],(pointSet == arcs[i].length || faceValues[1] == 0 ? faceValues[1] : random(1, 6)));
- wait(20);
- };
- };
- wait(900);
- if (dispose)
- window.setVisible(false);
- };
- });
- currentThread.start();
- };
- };
- public class DiceAction {
- private int random(int min, int max) {
- return (min + (int)(Math.random() * ((max - min) + 1)));
- };
- public int rollDie() {
- return random(1, 6);
- };
- public int rollDie(int sides) {
- return random(1, sides);
- };
- public int rollDice() {
- return rollDie() + rollDie();
- };
- public int rollDice(int howMany) {
- return rollDice(howMany, 6);
- };
- public int rollDice(int howMany, int sides) {
- int total = 0;
- for (int i = 0; i < howMany; i++)
- total += rollDie(sides);
- return (total);
- };
- public double avgRoll() {
- return (avgRoll(10));
- };
- public double avgRoll(int times) {
- return (rollDice(times) / (times * 1.0));
- };
- public double avgRoll(int times, int sides) {
- return (rollDice(times, sides) / (times * 1.0));
- };
- public boolean isEven() {
- return (rollDie() % 2 == 0);
- };
- public boolean isEven(int sides) {
- return (rollDie(sides) % 2 == 0);
- };
- public char scattegories() {
- return (char)(random(1, 26) + 96);
- };
- public String scattegories(int len) {
- String scat = "";
- for (int i = 0; i < len; i++)
- scat += scattegories();
- return (scat);
- };
- public static void main(String[] args) {
- DiceAction d = new DiceAction();
- DiceAction.showDice(new int[] {d.rollDie(), 0}, "rollDie with no param");
- try {
- Thread.sleep(4000);
- } catch(Exception e) {};
- DiceAction.showDice(new int[] {d.rollDie(9), d.rollDie ()}, "rollDie with 9 as param");
- };
- public static void showDice(int[] values, String method){
- JFrame window = new JFrame(method);
- window.setVisible(true);
- window.setSize(300, 300);
- window.setLocation(300, 300);
- (new Animation()).animateFrame(window, values, true);
- };
- };
- public class Die {
- private int numOfSides;
- private int face;
- public Die() {
- this(6);
- };
- public Die(int numOfSides) {
- this.numOfSides = numOfSides;
- };
- private int random(int min, int max) {
- return (min + (int)(Math.random() * ((max - min) + 1)));
- };
- public int roll() {
- int face = random(1, this.numOfSides);
- this.face = face;
- return (face);
- };
- public String toString() {
- return ("face = " + face + ", numOfSides = " + numOfSides);
- };
- public int getFace() {
- return this.face;
- };
- };
- public class TwoDice {
- private Die die1;
- private Die die2;
- public TwoDice() {
- this.die1 = new Die();
- this.die2 = new Die();
- };
- public TwoDice(int numOfSides) {
- this(numOfSides, numOfSides);
- };
- public TwoDice(int numOfSides1, int numOfSides2) {
- this.die1 = new Die(numOfSides1);
- this.die2 = new Die(numOfSides2);
- };
- public int roll() {
- return die1.roll() + die2.roll();
- };
- public int getTotal() {
- return (die1.getFace() + die2.getFace());
- };
- public boolean isDoubles() {
- return (die1.getFace() == die2.getFace());
- };
- public String toString() {
- return (die1 + ";" + die2);
- };
- };
- public class DiceGame {
- private Scanner scanner = new Scanner(System.in);
- private String read() {
- return (scanner.nextLine());
- };
- private void print(String output) {
- System.out.print(output + "\n");
- };
- private void clearScreen() {
- for (int i = 0; i < 20; i++)
- print("");
- };
- public DiceGame(){};
- public int getBet(int money) {
- print("Please enter a bet");
- int bet;
- try {
- bet = Integer.parseInt(read());
- } catch(Exception e) {
- bet = getBet(money);
- };
- if (bet > money || bet < 0)
- bet = getBet(money);
- return (bet);
- };
- public void init() {
- int bet = 100, money = 500;
- print("Game is loading...");
- while (true) {
- read();
- clearScreen();
- print("Current bet = " + bet + "$, money = " + money + "$");
- if (money <= 0) {
- print ("Game over... you lose!");
- break;
- };
- print("Would you like to quit?");
- if (read().equals("quit"))
- break;
- print("Would you like to set the bet? (y/n)");
- if (read().equals("y") || (bet > money))
- bet = getBet(money);
- TwoDice computerDice = new TwoDice();
- computerDice.roll();
- print("The computer rolled " + computerDice);
- read();
- TwoDice yourDice = new TwoDice();
- yourDice.roll();
- print("You have rolled " + yourDice);
- if (computerDice.isDoubles() && yourDice.isDoubles()) {
- if (computerDice.getTotal() > yourDice.getTotal()) {
- money -= bet;
- print("You lose!");
- } else {
- money += bet;
- print("You win!");
- };
- } else if(computerDice.isDoubles() || yourDice.isDoubles()) {
- if (computerDice.isDoubles()) {
- money -= bet;
- print("You lose!");
- } else {
- money += bet;
- print("You win!");
- };
- }else {
- if (computerDice.getTotal() > yourDice.getTotal()) {
- money -= bet;
- print("You lose!");
- } else {
- money += bet;
- print ("You win!");
- };
- };
- };
- };
- public static void main(String[] args) {
- DiceGame game = new DiceGame();
- game.init();
- };
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement