-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.java
More file actions
127 lines (107 loc) · 3.31 KB
/
Model.java
File metadata and controls
127 lines (107 loc) · 3.31 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
import java.util.Random;
/**
* Model class
* Stores and manipulates data
*
* @author Anton Samoilov, MSc SD <2459087S@student.gla.ac.uk> 2459087S
*/
public class Model {
private final String[] cardLabels = { "Joker", "Ace", "Jack", "Queen", "King" };
private int balance;
private int cardOneID, cardTwoID, cardThreeID;
private String endGameStatus;
private boolean gameEnded;
private String message;
private final int jokerWeight = 25;
private final int fullSequenceWeight = 50;
private final int partSequenceWeight = 20;
private final int minBalance = 0;
private final int winBalance = 150;
public Model() {
reset();
}
public String getCardOne() {
return getCardLabel(cardOneID);
}
public String getCardTwo() {
return getCardLabel(cardTwoID);
}
public String getCardThree() {
return getCardLabel(cardThreeID);
}
public int getBalance() {
return balance;
}
public String getBalanceMessage() {
return "balance is " + balance;
}
public String getEndGameStatus() {
return endGameStatus;
}
public boolean getGameEnded() {
return gameEnded;
}
public String getMessage() {
return message;
}
public void spinCards() {
Random rand = new Random();
int rand1 = rand.nextInt(4);
int rand2 = rand.nextInt(4);
int rand3 = rand.nextInt(4);
cardOneID = rand1;
cardTwoID = rand2;
cardThreeID = rand3;
updateBalance(cardOneID, cardTwoID, cardThreeID);
checkWinOrLose();
}
private void checkWinOrLose() {
if (balance < minBalance) {
endGameStatus = "You lose";
gameEnded = true;
} else if (balance >= winBalance) {
endGameStatus = "You win";
gameEnded = true;
}
}
private int countJoker(int cardOneID, int cardTwoID, int cardThreeID) {
int count = 0;
if (cardOneID == 0)
count++;
if (cardTwoID == 0)
count++;
if (cardThreeID == 0)
count++;
return count;
}
private void updateBalance(int cardOne, int cardTwo, int cardThree) {
int countJokers = countJoker(cardOne, cardTwo, cardThree);
if (countJokers > 0) {
int lostBalance = jokerWeight * countJokers;
balance -= lostBalance;
message = countJokers + " Joker(s): you lose " + lostBalance + " points";
} else {
if (cardOne == cardTwo && cardTwo == cardThree) {
balance += fullSequenceWeight;
message = "Three of a kind - you win " + fullSequenceWeight + " points";
} else if (cardOne == cardTwo || cardTwo == cardThree || cardOne == cardThree) {
balance += partSequenceWeight;
message = "Two of a kind - you win " + partSequenceWeight + " points";
} else {
message = "Balance unchanged";
}
}
}
public String getCardLabel(int cardID) {
return cardLabels[cardID];
}
public void reset() {
balance = 100;
cardOneID = 4;
cardTwoID = 3;
cardThreeID = 2;
endGameStatus = "";
message = "Welcome!";
gameEnded = false;
}
}