-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiceGame-2.java
More file actions
136 lines (127 loc) · 2.99 KB
/
DiceGame-2.java
File metadata and controls
136 lines (127 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*Sudipti Dantuluri
* 10.5.2021
* DiceGame.java
* The program uses scanner and constructors to finish playing the game*/
import java.util.Scanner; //Import library
public class DiceGame
{
double cash;
int dice;
int sides;
Scanner inputdata;
public DiceGame()
{
cash = 0;
dice = 0;
sides = 0;
}
public DiceGame(double cash2, int sides2, int dice2)
{
cash = cash2;
dice = dice2;
sides = sides2;
}
public static void main (String [] args)
{
DiceGame dg = new DiceGame();
dg.runMethods();
}
public void runMethods()
{
inputdata = new Scanner(System.in);
System.out.print("\n\n\n");
DiceGame dg2 = new DiceGame( askMoney(), askSides(), askDice() );
dg2.runGame();
}
public double askMoney()
{
System.out.print("How much money would you like to start with? ");
cash = inputdata.nextDouble();
return cash;
}
public int askDice()
{
System.out.print("How many dice would you like(1-4)? ");
dice = inputdata.nextInt();
return dice;
}
public int askSides()
{
System.out.print("How many sides on each die? ");
sides = inputdata.nextInt();
return sides;
}
public void runGame()
{
Scanner inputdata = new Scanner(System.in);
//System.out.println("cash " + cash + " sides " + sides + " dice " + dice );
rolled("You");
System.out.println("");
System.out.printf("Would you like to re-roll your lowest for $1 yes(1), no(2)");
int choice = inputdata.nextInt();
if (choice == 1 || choice == 2)
{
int your_total = rolled("You");
int you_win = 10;
System.out.println(" = " + your_total);
int dealer_total = rolled("Dealer");
System.out.println(" = " + dealer_total);
if (choice == 1)
{
you_win = 9;
}
if (your_total >= dealer_total)
{
System.out.printf("You win $%d!\n", you_win);
cash += you_win;
}
else
{
System.out.println("Dealder win!");
cash -= 10;
}
System.out.printf("Your have a total of %.2f\n", cash);
}
else
{
System.out.printf("wrong choice\n");
}
}
public int rolled(String s)
{
int total = 0;
System.out.printf(s + " rolled ");
for (int i=0; i<dice; i++)
{
int t = 1 + (int)(Math.random()*sides);
System.out.printf(" %d", t);
total += t;
}
return total;
}
/*public int findMin(int x1, int x2, int x3, int x4)
{
if (dice==1)
{
//System.out.printf("You rolled %d",x1);
return x1;
}
else if (dice==2)
{
//System.out.printf("You rolled %d %d",x1,x2);
return Math.min(x1,x2);
}
else if (dice==3)
{
//System.out.printf("You rolled %d %d %d",x1,x2,x3);
return Math.min(x1,Math.min(x2,x3));
}
else if (dice==4)
{
//System.out.printf("You rolled %d %d %d %d",x1,x2,x3,x4);
return Math.min(Math.min(x1,x2),Math.min(x3,x4));
}
return 0;
}
*/
}