Skip to content

Instantly share code, notes, and snippets.

@ssaurel
Created June 15, 2018 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssaurel/dbb004375f60d6444c14a39c483867fd to your computer and use it in GitHub Desktop.
Save ssaurel/dbb004375f60d6444c14a39c483867fd to your computer and use it in GitHub Desktop.
Main Activity for the Roulette Game on the SSaurel's Channel
package com.ssaurel.roulettegame;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Random;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
// sectors of our wheel (look at the image to see the sectors)
private static final String[] sectors = { "32 red", "15 black",
"19 red", "4 black", "21 red", "2 black", "25 red", "17 black", "34 red",
"6 black", "27 red","13 black", "36 red", "11 black", "30 red", "8 black",
"23 red", "10 black", "5 red", "24 black", "16 red", "33 black",
"1 red", "20 black", "14 red", "31 black", "9 red", "22 black",
"18 red", "29 black", "7 red", "28 black", "12 red", "35 black",
"3 red", "26 black", "zero"
};
@BindView(R.id.spinBtn)
Button spinBtn;
@BindView(R.id.resultTv)
TextView resultTv;
@BindView(R.id.wheel)
ImageView wheel;
// We create a Random instance to make our wheel spin randomly
private static final Random RANDOM = new Random();
private int degree = 0, degreeOld = 0;
// We have 37 sectors on the wheel, we divide 360 by this value to have angle for each sector
// we divide by 2 to have a half sector
private static final float HALF_SECTOR = 360f / 37f / 2f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick(R.id.spinBtn)
public void spin(View v) {
degreeOld = degree % 360;
// we calculate random angle for rotation of our wheel
degree = RANDOM.nextInt(360) + 720;
// rotation effect on the center of the wheel
RotateAnimation rotateAnim = new RotateAnimation(degreeOld, degree,
RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
rotateAnim.setDuration(3600);
rotateAnim.setFillAfter(true);
rotateAnim.setInterpolator(new DecelerateInterpolator());
rotateAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// we empty the result text view when the animation start
resultTv.setText("");
}
@Override
public void onAnimationEnd(Animation animation) {
// we display the correct sector pointed by the triangle at the end of the rotate animation
resultTv.setText(getSector(360 - (degree % 360)));
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
// we start the animation
wheel.startAnimation(rotateAnim);
}
private String getSector(int degrees) {
int i = 0;
String text = null;
do {
// start and end of each sector on the wheel
float start = HALF_SECTOR * (i * 2 + 1);
float end = HALF_SECTOR * (i * 2 + 3);
if (degrees >= start && degrees < end) {
// degrees is in [start;end[
// so text is equals to sectors[i];
text = sectors[i];
}
i++;
// now we can test our Android Roulette Game :)
// That's all !
// In the second part, you will learn how to add some bets on the table to play to the Roulette Game :)
// Subscribe and stay tuned !
} while (text == null && i < sectors.length);
return text;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment