-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleCardGame.java
More file actions
55 lines (48 loc) · 1.75 KB
/
Copy pathSimpleCardGame.java
File metadata and controls
55 lines (48 loc) · 1.75 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
package algo.basics;
import java.util.Arrays;
import java.util.List;
public class SimpleCardGame {
public static int getValue(String cardValue){
return switch(cardValue){
case "T" -> 10;
case "J" -> 11;
case "Q" -> 12;
case "K" -> 13;
case "A" -> 14;
default -> Integer.parseInt(cardValue);
};
}
public String winner_byRicka(String[] deckSteve, String[] deckJosh) {
int a = 0, b = 0;
for (int i = 0; i < deckJosh.length; i++) {
if(SimpleCardGame.getValue(deckSteve[i]) > SimpleCardGame.getValue(deckJosh[i]))
a++;
else if(SimpleCardGame.getValue(deckSteve[i]) < SimpleCardGame.getValue(deckJosh[i]))
b++;
}
if(a == b)
return "Tie";
return a > b ? "Steve win " + a + " to " + b : " Josh win " + b + " to " + a;
}
public String winner(String[] deckSteve, String[] deckJosh) {
List<String> allCards = Arrays.asList(
"2","3","4","5","6","7","8","9","T","J","Q","K","A"
);
int pointsJosh = 0, pointsSteve = 0;
for (int i = 0; i < deckJosh.length; i++) {
String cardJosh = deckJosh[i];
String cardSteve = deckSteve[i];
if(allCards.indexOf(cardSteve) > allCards.indexOf(cardJosh)){
pointsSteve++;
}else if (allCards.indexOf(cardSteve) < allCards.indexOf(cardJosh)){
pointsJosh++;
}
}
if (pointsJosh > pointsSteve){
return "Josh win "+pointsJosh+" to "+pointsSteve;
}else if (pointsJosh < pointsSteve){
return "Steve win "+pointsSteve+" to "+pointsJosh;
}
return "Tie";
}
}