-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.java
174 lines (139 loc) · 5.06 KB
/
MainActivity.java
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
package com.v.vrjco.scarnesdice;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
/**
* Created by VRUSHABH on 06-01-2018.
*/
public class MainActivity extends AppCompatActivity {
private TextView tvUserScore, tvComputerScore,
tvStatus;
private ImageView imDiceFace;
private Button bRoll, bHold, bReset;
private int[] diceFaceImages = {
R.drawable.dice1,
R.drawable.dice2,
R.drawable.dice3,
R.drawable.dice4,
R.drawable.dice5,
R.drawable.dice6
};
private int userOverallScore = 0, userTurnScore = 0;
private int computerOverallScore = 0, computerTurnScore = 0;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linkAllViews();
bRoll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
rollButtonClick();
}
});
bHold.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holdButtonClick();
}
});
bReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resetButtonClick();
}
});
}
private void linkAllViews() {
tvUserScore = findViewById(R.id.user_score);
tvComputerScore = findViewById(R.id.computer_score);
tvStatus = findViewById(R.id.play_status);
imDiceFace = findViewById(R.id.dice_face);
bRoll = findViewById(R.id.roll_button);
bHold = findViewById(R.id.hold_button);
bReset = findViewById(R.id.reset_button);
}
public void rollButtonClick() {
int rolledNumber = rollDice();
imDiceFace.setImageResource(diceFaceImages[rolledNumber]);
rolledNumber++;
if (rolledNumber == 1) {
userTurnScore = 0;
tvStatus.setText("You rolled a '1'");
computerTurn();
} else {
userTurnScore += rolledNumber;
tvStatus.setText(String.format("Your turn score is %d", userTurnScore));
}
}
public void holdButtonClick() {
userOverallScore += userTurnScore;
userTurnScore = 0;
tvUserScore.setText(String.format("Your Score: %d", userOverallScore));
if (!checkWinner())
computerTurn();
}
private void computerTurn() {
enableButtons(false);
while (true) {
int computerRolledNumber = rollDice();
imDiceFace.setImageResource(diceFaceImages[computerRolledNumber]);
computerRolledNumber++;
if (computerRolledNumber == 1) {
computerTurnScore = 0;
tvStatus.setText("Computer rolled a '1'");
tvStatus.setText("Now Player's turn");
enableButtons(true);
break;
} else {
computerTurnScore += computerRolledNumber;
tvStatus.setText(String.format("Computer turn score is %d", computerTurnScore));
}
if (computerTurnScore > 20) {
computerOverallScore += computerTurnScore;
computerTurnScore = 0;
tvComputerScore.setText(String.format("Computer Score: %d", computerOverallScore));
tvStatus.setText("Now Player's turn");
enableButtons(true);
break;
}
}
checkWinner();
}
public boolean checkWinner() {
if (userOverallScore >= 100) {
Toast.makeText(getApplicationContext(), "You Win!", Toast.LENGTH_SHORT).show();
resetButtonClick();
return true;
} else if (computerOverallScore >= 100) {
Toast.makeText(getApplicationContext(), "Computer Wins!", Toast.LENGTH_SHORT).show();
resetButtonClick();
return true;
}
return false;
}
public void resetButtonClick() {
userOverallScore = 0;
userTurnScore = 0;
computerOverallScore = 0;
computerTurnScore = 0;
tvUserScore.setText("Your Score: 0");
tvComputerScore.setText("Computer Score: 0");
tvStatus.setText("Status: Game Start");
enableButtons(true);
}
private int rollDice() {
Random random = new Random();
return random.nextInt(6);
}
private void enableButtons(boolean isEnabled) {
bRoll.setEnabled(isEnabled);
bHold.setEnabled(isEnabled);
}
}