Skip to content

Instantly share code, notes, and snippets.

@Binary-Finery
Created October 1, 2018 11:36
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 Binary-Finery/a82ee43801e471d2a01789746879e751 to your computer and use it in GitHub Desktop.
Save Binary-Finery/a82ee43801e471d2a01789746879e751 to your computer and use it in GitHub Desktop.
latency (Wafi Hasan)
package com.wafihasan.latency;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Locale;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
final int WIN_SCORE = 2;
String[] names = {"RED", "BROWN", "GREEN", "PURPLE", "YELLOW", "BLUE", "PINK"};
String[] colours = {"#F44336", "#795548", "#4CAF50", "#9C27B0", "#FFEB3B", "#3F51B5", "#E91E63"};
Random r = new Random();
TextView p1Area, p2Area;
Button button1, button2;
Handler handler;
Runnable runnable;
int score1 = 0, score2 = 0, name_index, colour_index, i = 0;
boolean winner = false, isCorrect = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(getSupportActionBar()!=null){
getSupportActionBar().hide();
}
handler = new Handler();
p1Area = findViewById(R.id.p1Area);
p2Area = findViewById(R.id.p2Area);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
displayLetsPlayDialog();
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isCorrect) {
p1Plus();
} else {
p1Minus();
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isCorrect) {
p2Plus();
} else {
p2Minus();
}
}
});
}
public void setColours() {
runnable = new Runnable() {
@Override
public void run() {
if (!winner) {
handler.postDelayed(runnable, 1000);
name_index = r.nextInt(names.length);
colour_index = r.nextInt(colours.length);
isCorrect = name_index == colour_index;
setTextColors();
}
}
};
handler.post(runnable);
}
private void checkForWin() {
if (score1==WIN_SCORE || score2==WIN_SCORE){
winner = true;
Toast.makeText(getApplicationContext(), "we have a winner", Toast.LENGTH_LONG).show();
}
}
private void setTextColors() {
p1Area.setText(names[name_index]);
p2Area.setText(names[name_index]);
p1Area.setTextColor(Color.parseColor(colours[colour_index]));
p2Area.setTextColor(Color.parseColor(colours[colour_index]));
}
public void p1Plus() {
score1 += 1;
button1.setText(String.format(Locale.getDefault(), "Score: %d", score1));
checkForWin();
}
public void p2Plus() {
score2 += 1;
button2.setText(String.format(Locale.getDefault(), "Score: %d", score2));
checkForWin();
}
public void p1Minus() {
score1 -= 1;
button1.setText(String.format(Locale.getDefault(), "Score: %d", score1));
checkForWin();
}
public void p2Minus() {
score2 -= 1;
button2.setText(String.format(Locale.getDefault(), "Score: %d", score2));
checkForWin();
}
private void displayLetsPlayDialog() {
final AlertDialog builder = new AlertDialog.Builder(MainActivity.this).create();
@SuppressLint("InflateParams")
View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.start_dialog, null);
TextView tvPlay = v.findViewById(R.id.tv_start);
builder.setView(v);
builder.setCanceledOnTouchOutside(false);
tvPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setColours();
builder.dismiss();
}
});
builder.show();
}
@Override
protected void onStop() {
super.onStop();
if (handler != null) {
handler.removeCallbacks(runnable);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment