-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTargetCards.java
More file actions
171 lines (169 loc) · 4.57 KB
/
Copy pathTargetCards.java
File metadata and controls
171 lines (169 loc) · 4.57 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import java.util.Random;
import java.util.Scanner;
public class TargetCards {
public void shuffle(int[] list) {
int totalElements = list.length;
// initialize random number generator
Random random = new Random();
for (int i = 0; i < totalElements; i++) {
// get the list element at current index
int currentElement = list[i];
// generate a random index within the range of list size
int randomIndex = i + random.nextInt(totalElements - i);
// set the element at current index with the element at random
// generated index
// set the element at random index with the element at current loop
// index
int temp = list[i];
list[i] = list[randomIndex];
list[randomIndex] = temp;
}
}
public static void main(String[] args) {
//11 = ACE
//12 = JACK
//13 = QUEEN
//14 = KING
int[] deck = { 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14 };
TargetCards tc = new TargetCards();
int target = 51;
Scanner sc = new Scanner(System.in);
int[] P1Scores = {0,0,0,0};
int[] P2Scores = {0,0,0,0};
for(int i = 1; i <= 4; i++) {
tc.shuffle(deck);
System.out.println("GAME "+i+":");
Stack P1 = new LinkedStack();
Stack P2 = new LinkedStack();
int P1Cards, P2Cards;
if(i%2!=0) {
System.out.println("Player 1 has 1st guess");
System.out.print("Guess the number of cards: ");
P1Cards = sc.nextInt();
for(int j = 0; j<P1Cards;j++) {
P1.push(deck[j]);
}
System.out.println("Player 1 has guessed "+P1Cards+" cards.");
tc.shuffle(deck);
System.out.println("Player 2 has 2nd guess");
System.out.print("Guess the number of cards: ");
P2Cards = sc.nextInt();
for(int j = 0; j<P2Cards;j++) {
P2.push(deck[j]);
}
System.out.println("Player 2 has guessed "+P2Cards+" cards.");
}
else {
System.out.println("Player 2 has 1st guess");
System.out.print("Guess the number of cards: ");
P2Cards = sc.nextInt();
for(int j = 0; j<P2Cards;j++) {
P2.push(deck[j]);
}
System.out.println("Player 2 has guessed "+P2Cards+" cards.");
tc.shuffle(deck);
System.out.println("Player 1 has 2nd guess");
System.out.print("Guess the number of cards: ");
P1Cards = sc.nextInt();
for(int j = 0; j<P1Cards;j++) {
P1.push(deck[j]);
}
System.out.println("Player 1 has guessed "+P1Cards+" cards.");
}
int P1Sum = 0;
int P2Sum = 0;
int P1Score, P2Score;
boolean P1Ace = false;
boolean P2Ace = false;
System.out.print("Player 1: ");
while(!P1.isEmpty()) {
if(P1.peek()==11) {
System.out.print("A ");
P1Sum+=11;
P1Ace = true;
}
else if(P1.peek()==12) {
System.out.print("J ");
P1Sum+=10;
}
else if(P1.peek()==13) {
System.out.print("Q ");
P1Sum+=10;
}
else if(P1.peek()==14) {
System.out.print("K ");
P1Sum+=10;
}
else {
System.out.print(P1.peek()+" ");
P1Sum+=P1.peek();
}
P1.pop();
}
if(P1Sum > target) {
if(P1Ace) {
P1Sum -= 10;
}
}
P1Score = target - P1Sum;
if(P1Score<0) {
P1Score = target;
}
System.out.println("\tSum = "+P1Sum+" Score = "+P1Score);
System.out.print("Player 2: ");
while(!P2.isEmpty()) {
if(P2.peek()==11) {
System.out.print("A ");
P2Sum+=11;
P2Ace = true;
}
else if(P2.peek()==12) {
System.out.print("J ");
P2Sum+=10;
}
else if(P2.peek()==13) {
System.out.print("Q ");
P2Sum+=10;
}
else if(P2.peek()==14) {
System.out.print("K ");
P2Sum+=10;
}
else {
System.out.print(P2.peek()+" ");
P2Sum+=P2.peek();
}
P2.pop();
}
if(P2Sum > target) {
if(P2Ace) {
P2Sum -= 10;
}
}
P2Score = target - P2Sum;
if(P2Score<0) {
P2Score = target;
}
System.out.println("\tSum = "+P2Sum+" Score = "+P2Score);
P1Scores[i-1] = P1Score;
P2Scores[i-1] = P2Score;
}
int P1Total = 0;
int P2Total = 0;
for(int i = 0; i < 4; i++) {
P1Total+=P1Scores[i];
P2Total+=P2Scores[i];
}
System.out.println("Player 1 total is "+ P1Total);
System.out.println("Player 2 total is "+ P2Total);
if(P1Total<P2Total) {
System.out.println("Player 1 wins!");
}
else if(P1Total>P2Total) {
System.out.println("Player 2 wins!");
}
else {
System.out.println("Game Draw!");
}
}
}