View difference between Paste ID: AYrNztS0 and czyZP1s6
SHOW: | | - or go back to the newest paste.
1
import java.util.*;
2
import java.lang.*;
3
import javax.swing.*;
4
import java.awt.*;
5
import java.awt.event.*;
6
7
class Animation{
8
    private Thread currentThread;
9
    public void animateFrame(JFrame window, int[] faceValues, boolean dispose){
10
        try{
11
            currentThread.stop();
12
        }catch(Exception e){};
13
        currentThread=new Thread(new Runnable(){ 
14
            Graphics g = window.getContentPane().getGraphics();
15
            private int random(int min, int max) {
16
                return (min + (int)(Math.random() * ((max - min) + 1)));
17
            };
18
            public int[][][] arcPoints(int numberOfArcs, int maxHeight){
19
                int[][][] arcs=new int[numberOfArcs][10][];
20
                double min=maxHeight*.8;//loses .2 of total maxHeight
21
                double split=min/numberOfArcs;//number of change in each arc 
22
                for(int arc=0; arc<numberOfArcs; arc++){
23
                    int[][] points=new int[32][2];//array of coords (x,y)
24
                    for(int index=0; index<32; index++){
25
                        int heightForArc=(int)(maxHeight-(split*arc));
26
                        points[index][0]=(int)((arc*32+index)*1.25);// x-coordinate
27
                        points[index][1]=150-(int)(Math.abs(Math.sin(index*.1)*heightForArc));// y-coordinate
28
                    };
29
                    arcs[arc]=points;
30
                };
31
                return(arcs);
32
            };
33
            public void drawDice(int x,int y,int faceValue){
34
                if (faceValue != 0)
35
                    g.drawRect(x,y,20,20);
36
                if(faceValue==1){
37
                    g.fillOval(x+10,y+10,2,2);
38
                }else if(faceValue==2){
39
                    g.fillOval(x+5,y+10,2,2);
40
                    g.fillOval(x+15,y+10,2,2);
41
                }else if(faceValue==3){
42
                    g.fillOval(x+10,y+5,2,2);
43
                    g.fillOval(x+10,y+10,2,2);
44
                    g.fillOval(x+10,y+15,2,2);
45
                }else if(faceValue==4){
46
                    g.fillOval(x+5,y+5,2,2);
47
                    g.fillOval(x+5,y+15,2,2);
48
                    g.fillOval(x+15,y+5,2,2);
49
                    g.fillOval(x+15,y+15,2,2);
50
                }else if(faceValue==5){
51
                    g.fillOval(x+5,y+5,2,2);
52
                    g.fillOval(x+5,y+15,2,2);
53
                    g.fillOval(x+10,y+10,2,2);
54
                    g.fillOval(x+15,y+5,2,2);
55
                    g.fillOval(x+15,y+15,2,2);
56
                }else if(faceValue==6){
57
                    g.fillOval(x+5,y+5,2,2);
58
                    g.fillOval(x+5,y+10,2,2);
59
                    g.fillOval(x+5,y+15,2,2);
60
                    g.fillOval(x+15,y+5,2,2);
61
                    g.fillOval(x+15,y+10,2,2);
62
                    g.fillOval(x+15,y+15,2,2);
63
                }else if(faceValue==7){
64
                    g.fillOval(x+5,y+5,2,2);
65
                    g.fillOval(x+5,y+10,2,2);
66
                    g.fillOval(x+5,y+15,2,2);
67
                    g.fillOval(x+15,y+5,2,2);
68
                    g.fillOval(x+15,y+10,2,2);
69
                    g.fillOval(x+15,y+15,2,2);
70
                    g.fillOval(x+10,y+10,2,2);
71
                }else if(faceValue==8){
72
                    g.fillOval(x+5,y+3,2,2);
73
                    g.fillOval(x+5,y+6,2,2);
74
                    g.fillOval(x+5,y+9,2,2);
75
                    g.fillOval(x+5,y+12,2,2);
76
                    g.fillOval(x+15,y+3,2,2);
77
                    g.fillOval(x+15,y+6,2,2);
78
                    g.fillOval(x+15,y+9,2,2);
79
                    g.fillOval(x+15,y+12,2,2);
80
                }else if(faceValue==9) {
81
                    g.fillOval(x+5,y+3,2,2);
82
                    g.fillOval(x+5,y+6,2,2);
83
                    g.fillOval(x+5,y+9,2,2);
84
                    g.fillOval(x+5,y+12,2,2);
85
                    g.fillOval(x+15,y+3,2,2);
86
                    g.fillOval(x+15,y+6,2,2);
87
                    g.fillOval(x+15,y+9,2,2);
88
                    g.fillOval(x+15,y+12,2,2);
89
                    g.fillOval(x+10,y+10,2,2);
90
                };
91
            };
92
            public boolean wait(int ms){
93
                try{
94
                    Thread.sleep(ms);
95
                }catch(Exception e){
96
                    return false;
97
                };
98
                return true;
99
            };
100
            public void run(){
101
                int[][][] arcs=arcPoints(3,90);
102
                int[][][] secondArcs=arcPoints(3,90);
103
                for(int i=0; i<arcs.length; i++){
104
                    for(int pointSet=0; pointSet<arcs[i].length; pointSet++){
105
                        g = window.getContentPane().getGraphics();
106
                        g.clearRect(0,55,600,122); // x, y , w, h
107
                        int[] points=arcs[i][pointSet];
108
                        int[] secondPoints=secondArcs[i][pointSet];
109
                        drawDice(points[0],points[1],(pointSet == arcs[i].length ? faceValues[0] : random(1, 6)));
110
                        drawDice(260-secondPoints[0],secondPoints[1],(pointSet == arcs[i].length || faceValues[1] == 0 ? faceValues[1] : random(1, 6)));
111
                        wait(20);
112
                    };
113
                };
114
                wait(900);
115
                if (dispose)
116
                    window.setVisible(false);
117
            };
118
        });
119
        currentThread.start();
120
    };
121
};
122
public class DiceAction {
123
    private int random(int min, int max) {
124
        return (min + (int)(Math.random() * ((max - min) + 1)));
125
    };
126
    public int rollDie() {
127
        return random(1, 6);
128
    };
129
    public int rollDie(int sides) {
130
        return random(1, sides);
131
    };
132
    public int rollDice() {
133
        return rollDie() + rollDie();
134
    };
135
    public int rollDice(int howMany) {
136
        return rollDice(howMany, 6);
137
    };
138
    public int rollDice(int howMany, int sides) {
139
        int total = 0;
140
        for (int i = 0; i < howMany; i++)
141
            total += rollDie(sides);
142
        return (total);
143
    };
144
    public double avgRoll() {
145
        return (avgRoll(10));
146
    };
147
    public double avgRoll(int times) {
148
        return (rollDice(times) / (times * 1.0));
149
    };
150
    public double avgRoll(int times, int sides) {
151
        return (rollDice(times, sides) / (times * 1.0));
152
    };
153
    public boolean isEven() {
154
        return (rollDie() % 2 == 0);
155
    };
156
    public boolean isEven(int sides) {
157
        return (rollDie(sides) % 2 == 0);
158
    };
159
    public char scattegories() {
160
        return (char)(random(1, 26) + 96);
161
    };
162
    public String scattegories(int len) {
163
        String scat = "";
164
        for (int i = 0; i < len; i++)
165
            scat += scattegories();
166
        return (scat);
167
    };
168
    public static void main(String[] args) {
169
        DiceAction d = new DiceAction();
170
        DiceAction.showDice(new int[] {d.rollDie(), 0}, "rollDie with no param");
171
        try {
172
            Thread.sleep(4000);
173
        } catch(Exception e) {};
174
        DiceAction.showDice(new int[] {d.rollDie(9), d.rollDie ()}, "rollDie with 9 as param");
175
    };
176
    
177
    public static void showDice(int[] values, String method){ 
178
        JFrame window = new JFrame(method);
179
            window.setVisible(true);
180
            window.setSize(300, 300);
181
            window.setLocation(300, 300);
182
       (new Animation()).animateFrame(window, values, true);
183
    };
184
};
185
public class Die {
186
    private int numOfSides;
187
    private int face;
188
    public Die() {
189
        this(6);
190
    };
191
    public Die(int numOfSides) {
192
        this.numOfSides = numOfSides;
193
    };
194
    private int random(int min, int max) {
195
        return (min + (int)(Math.random() * ((max - min) + 1)));
196
    };
197
    
198
    
199
    public int roll() {
200
        int face = random(1, this.numOfSides);
201
        this.face = face;
202
        return (face);
203
    };
204
    public String toString() {
205
        return ("face = " + face + ", numOfSides = " + numOfSides);
206
    };
207
    public int getFace() {
208
        return this.face;
209
    };
210
};
211
public class TwoDice {
212
    private Die die1;
213
    private Die die2;
214
    public TwoDice() {
215
        this.die1 = new Die();
216
        this.die2 = new Die();
217
    };
218
    public TwoDice(int numOfSides) {
219
        this(numOfSides, numOfSides);
220
    };
221
    public TwoDice(int numOfSides1, int numOfSides2) {
222
        this.die1 = new Die(numOfSides1);
223
        this.die2 = new Die(numOfSides2);
224
    };
225
    
226
    public int roll() {
227
        return die1.roll() + die2.roll();
228
    };
229
    public int getTotal() {
230
        return (die1.getFace() + die2.getFace());
231
    };
232
    public boolean isDoubles() {
233
        return (die1.getFace() == die2.getFace());
234
    };
235
    public String toString() {
236
        return (die1 + ";" + die2);
237
    };
238
};
239
public class DiceGame {
240
    private Scanner scanner = new Scanner(System.in);
241
    private String read() {
242
       return (scanner.nextLine());
243
    };
244
    private void print(String output) {
245
        System.out.print(output + "\n");
246
    };
247
    private void clearScreen() {
248
        for (int i = 0; i < 20; i++)
249
            print("");
250
    };
251
    
252
    public DiceGame(){};
253
    
254
    
255
    public int getBet(int money) {
256
        print("Please enter a bet");
257
        int bet;
258
        try {
259
            bet = Integer.parseInt(read());
260
        } catch(Exception e) {
261
            bet = getBet(money);
262
        };
263
        if (bet > money || bet < 0)
264
            bet = getBet(money);
265
        return (bet);
266
    };
267
    public void init() {
268
        int bet = 100, money = 500;
269
        print("Game is loading...");
270
        while (true)  {
271
            read();
272
            clearScreen();
273
            print("Current bet = " + bet + "$, money = " + money + "$");
274
            if (money <= 0) {
275
                print ("Game over... you lose!");
276
                break;
277
            };
278
            print("Would you like to quit?");
279
            if (read().equals("quit"))
280
                break;
281
            print("Would you like to set the bet? (y/n)");
282
            if (read().equals("y") || (bet > money))
283
                bet = getBet(money);
284
            TwoDice computerDice = new TwoDice();
285
                computerDice.roll();
286
            print("The computer rolled " + computerDice);
287
            read();
288
            TwoDice yourDice = new TwoDice();
289
                yourDice.roll();
290
            print("You have rolled " + yourDice);
291
            if (computerDice.isDoubles() && yourDice.isDoubles()) {
292
                if (computerDice.getTotal() > yourDice.getTotal()) {
293
                    money -= bet;
294
                    print("You lose!");
295
                } else {
296
                    money += bet;
297
                    print("You win!");
298
                };
299
            } else if(computerDice.isDoubles() || yourDice.isDoubles()) {
300
                if (computerDice.isDoubles()) {
301
                    money -= bet;
302
                    print("You lose!");
303
                } else {
304
                    money += bet;
305
                    print("You win!");
306
                };
307
            }else {
308
                if (computerDice.getTotal() > yourDice.getTotal()) {
309
                    money -= bet;
310
                    print("You lose!");
311
                } else {
312
                    money += bet;
313
                    print ("You win!");
314
                };
315
            };
316
        };
317
    };
318
    
319
    public static void main(String[] args) {
320
        DiceGame game = new DiceGame();
321
            game.init();
322
    };
323
};