-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCardsGame.java
More file actions
102 lines (79 loc) · 3.05 KB
/
CardsGame.java
File metadata and controls
102 lines (79 loc) · 3.05 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
import java.util.Scanner;
public class CardsGame {
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
System.out.println("Please enter the number between 1 to 13");
int a = scn.nextInt();
System.out.println("Enter S = Spades, C = Club, D = Diamond, H = Hearts");
String color = scn.next();
Character ch = color.charAt(0);
Cards cdrs = new Cards(ch, a);
Games gms = new Games();
System.out.println(gms.setGame(cdrs).toString());
}
}
class Cards{
private Character color;
private int val;
public Cards(Character color, int val){
this.color = color;
this.val = val;
}
public Character getColor() {
return color;
}
public void setColor(Character color) {
this.color = color;
}
public int getVal() {
return val;
}
public void setVal(int val) {
this.val = val;
}
@Override
public String toString() {
return "Cards [color=" + color + ", val=" + val + "]";
}
}
class Games {
private Cards cards;
public Cards getCards() {
return cards;
}
public void setCards(Cards cards) {
this.cards = cards;
}
public Cards setGame(Cards crds){
int sandoz = (int)(Math.random() * 13) + 1;
Character[] arr = new Character[]{'S','C','D','H'};
int x = (int)(Math.random() * 4) + 1;
Character cardcomputer = arr[x-1];
Cards ccrrdds = new Cards(cardcomputer, sandoz);
Cards crdss = null;
if(crds.getVal() > ccrrdds.getVal()){
return crds;
}else if(ccrrdds.getVal() > crds.getVal()){
return ccrrdds;
}else if(ccrrdds.getVal() == crds.getVal()){
if(crds.getColor().equals(ccrrdds.getColor())){
throw new RuntimeException("It is a draw");
}else if(crds.getColor().equals('S') && (ccrrdds.getColor().equals('C') || ccrrdds.getColor().equals('D') || ccrrdds.getColor().equals('H'))){
crdss = crds;
}else if(crds.getColor().equals('D') && !ccrrdds.getColor().equals('S') && (ccrrdds.getColor().equals('C') || ccrrdds.getColor().equals('H'))){
crdss = crds;
}else if(crds.getColor().equals('H') && !(ccrrdds.getColor().equals('S') && ccrrdds.getColor().equals('D')) && ccrrdds.getColor().equals('C')){
crdss = crds;
}else if(ccrrdds.getColor().equals('S') && (crds.getColor().equals('C') || crds.getColor().equals('D') || crds.getColor().equals('H'))){
crdss = ccrrdds;
}else if(ccrrdds.getColor().equals('D') && !crds.getColor().equals('S') && (crds.getColor().equals('C') || crds.getColor().equals('H'))){
crdss = ccrrdds;
}else if(ccrrdds.getColor().equals('H') && !(crds.getColor().equals('S') && crds.getColor().equals('D')) && crds.getColor().equals('C')){
crdss = ccrrdds;
}
return crdss;
}else{
return crdss;
}
}
}