-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathandroidFarkle.java
More file actions
266 lines (234 loc) · 10.4 KB
/
androidFarkle.java
File metadata and controls
266 lines (234 loc) · 10.4 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
The following code, is the code of an android farkle game. Where you roll 6 dice which can be scored
in various ways, and you can stop the round and add to your total score.
By:Deyvik Bhan
Date:2/20/19
*/
package com.example.dbhan.androidfarkle;
import android.content.DialogInterface;
import android.graphics.Color;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
// Initializes variables
ImageButton[] buttons = new ImageButton[6];
int[] buttonState = new int[6];
int[] dieImages = new int[6];
int[] dieValue = new int[6];
// Arrays that hold different attributes of images
final int HOT_DIE = 0;
final int SCORE_DIE = 1;
final int LOCKED_DIE = 2;
// Final integers that hold value of what kind of die each image is
Button roll;
Button score;
Button stop;
TextView currentScoreTV;
TextView totalScoreTV;
TextView currentRoundTV;
// Initializes different parts of layout
int currentScore;
int totalScore;
int currentRound;
// INfo which will be displayed on app
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttons[0] = (ImageButton)this.findViewById(R.id.imageButton1);
buttons[1] = (ImageButton)this.findViewById(R.id.imageButton2);
buttons[2] = (ImageButton)this.findViewById(R.id.imageButton3);
buttons[3] = (ImageButton)this.findViewById(R.id.imageButton4);
buttons[4] = (ImageButton)this.findViewById(R.id.imageButton5);
buttons[5] = (ImageButton)this.findViewById(R.id.imageButton6);
// Sets a image button to a corresponding spot in an array
for (int a = 0; a < buttons.length; a ++) {
buttons[a].setOnClickListener(this);
buttons[a].setEnabled(false);
buttons[a].setBackgroundColor(Color.LTGRAY);
}
// Set certain properties for all imagebuttons
roll =(Button)this.findViewById(R.id.button1);
roll.setOnClickListener(this);
score =(Button)this.findViewById(R.id.button2);
score.setOnClickListener(this);
score.setEnabled(false);
stop =(Button)this.findViewById(R.id.button3);
stop.setOnClickListener(this);
stop.setEnabled(false);
currentScoreTV = (TextView) this.findViewById(R.id.textView1);
totalScoreTV = (TextView) this.findViewById(R.id.textView2);
currentRoundTV = (TextView) this.findViewById(R.id.textView3);
dieImages[0] = R.drawable.one;
dieImages[1] = R.drawable.two;
dieImages[2] = R.drawable.three;
dieImages[3] = R.drawable.four;
dieImages[4] = R.drawable.five;
dieImages[5] = R.drawable.six;
// Sets each die image to a drawable
}
@Override
public void onClick(View v) {
if (v.equals(roll)) {
// If roll is clicked
for (int a = 0; a < buttons.length; a++) {
if (buttonState[a] == HOT_DIE) {
int choice = (int) (Math.random() * 6);
dieValue[a] = choice;
buttons[a].setImageResource(dieImages[choice]);
buttons[a].setEnabled(true);
roll.setEnabled(false);
score.setEnabled(true);
stop.setEnabled(false);
}
}
} else if (v.equals(score)) {
// If score is clicked
int[] valueCounts = new int[7];
for (int a = 0; a < buttonState.length; a++) {
if (buttonState[a] == SCORE_DIE) {
valueCounts[dieValue[a] + 1]++;
}
}
if (valueCounts[2] > 0 && valueCounts[2] < 3 ||
valueCounts[3] > 0 && valueCounts[3] < 3 ||
valueCounts[4] > 0 && valueCounts[4] < 3 ||
valueCounts[6] > 0 && valueCounts[6] < 3) {
// If dice can not be scored
// INVALID DIE DETECTED
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Invalid die selected");
alertDialogBuilder
.setMessage("You can only select scoring dice")
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
} else if (valueCounts[1] == 0 && valueCounts[2] == 0 && valueCounts[3] == 0 &&
valueCounts[4] == 0 && valueCounts[5] == 0 && valueCounts[6] == 0) {
// IF dice can't be scored as well
AlertDialog.Builder AlertDialog;
android.support.v7.app.AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("No score!");
alertDialogBuilder
.setMessage("Forfeit score and go to next round?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
currentScore = 0;
currentRound++;
currentScoreTV.setText("Current Score:" + currentScore);
currentRoundTV.setText("Current Round:" + currentRound);
resetDice();
// Goes to next round and resets dice
dialog.dismiss();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
} else {
// Below scores the dice based on the rules of the game
if (valueCounts[1] < 3) {
currentScore += (valueCounts[1] * 100);
}
if (valueCounts[5] < 3) {
currentScore += (valueCounts[5] * 50);
}
if (valueCounts[1] >= 3) {
currentScore += (1000 * (valueCounts[1] - 2));
}
if (valueCounts[2] >= 3) {
currentScore += (200 * (valueCounts[2] - 2));
}
if (valueCounts[3] >= 3) {
currentScore += (300 * (valueCounts[3] - 2));
}
if (valueCounts[4] >= 3) {
currentScore += (400 * (valueCounts[4] - 2));
}
if (valueCounts[5] >= 3) {
currentScore += (500 * (valueCounts[5] - 2));
}
if (valueCounts[6] >= 3) {
currentScore += (600 * (valueCounts[6] - 2));
}
currentScoreTV.setText("Current Score:" + currentScore);
for (int a = 0; a < buttons.length; a++) {
if (buttonState[a] == SCORE_DIE) {
buttonState[a] = LOCKED_DIE;
buttons[a].setBackgroundColor(Color.BLUE);
buttons[a].setEnabled(false);
}
}
roll.setEnabled(true);
stop.setEnabled(true);
score.setEnabled(false);
int lockedCount = 0;
for (int a = 0; a < buttons.length; a++) {
if (buttonState[a] == LOCKED_DIE) {
lockedCount++;
}
}
// What to do if all dice become locked(can not roll anymore)
if (lockedCount == 6) {
for (int a = 0; a < buttons.length; a++) {
buttonState[a] = HOT_DIE;
buttons[a].setBackgroundColor(Color.LTGRAY);
}
roll.setEnabled(true);
stop.setEnabled(true);
score.setEnabled(false);
}
}
} else if (v.equals(stop)) {
// If stop is clicked
totalScore += currentScore;
currentScore = 0;
currentScoreTV.setText("Current Score:" + currentScore);
totalScoreTV.setText("Total Score:" + totalScore);
currentRound++;
currentRoundTV.setText("Current Round:" + currentRound);
resetDice();
// resets dice as well as changes round, resets current score, and changes total score
} else {
for (int a = 0; a < buttons.length; a++) {
if (v.equals(buttons[a])) {
if (buttonState[a] == HOT_DIE) {
buttons[a].setBackgroundColor(Color.RED);
buttonState[a] = SCORE_DIE;
// If button is clicked
} else {
buttons[a].setBackgroundColor(Color.LTGRAY);
buttonState[a] = HOT_DIE;
// If not clicked
}
}
}
}
}
// Below resets the dice to original state
private void resetDice() {
for(int a = 0; a < buttons.length; a++) {
buttons[a].setEnabled(false);
buttonState[a] = HOT_DIE;
buttons[a].setBackgroundColor(Color.LTGRAY);
}
roll.setEnabled(true);
stop.setEnabled(false);
score.setEnabled(false);
}
}