Created
November 30, 2011 02:09
-
-
Save cdzombak/1407664 to your computer and use it in GitHub Desktop.
StopOn500
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:orientation="vertical" | |
android:gravity="center" | |
android:clickable="true" | |
android:id="@+id/wholeScreen"> | |
<TextView | |
android:id="@+id/countView" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:gravity="center" | |
android:textSize="100dip" | |
/> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cguild.stopon500; | |
import java.util.concurrent.atomic.AtomicInteger; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.util.Log; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
public class StopOn500Activity extends Activity { | |
private static final String TAG = StopOn500Activity.class.getSimpleName(); | |
private static final int TARGET = 500; | |
private static final int INITIAL_DIFFICULTY = 25; | |
private static final int DIFFICULTY_CHANGE = 5; | |
private static final int HARDEST_DIFFICULTY = 10; | |
private static final int GOOD_DELTA = 10; | |
private AtomicInteger mCount = new AtomicInteger(); | |
private int mDifficulty = INITIAL_DIFFICULTY; | |
private Thread mTimerThread = null; | |
private TextView mCountView = null; | |
private boolean mWon = false; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
mCountView = (TextView) findViewById(R.id.countView); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
mDifficulty = INITIAL_DIFFICULTY; | |
startGame(); | |
findViewById(R.id.wholeScreen).setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
mTimerThread.interrupt(); | |
try { | |
mTimerThread.join(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
updateCountUi(); | |
final int delta = Math.abs(TARGET-mCount.get()); | |
gameFinished(delta); | |
} | |
}); | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
mTimerThread.interrupt(); | |
mTimerThread = null; | |
} | |
private void updateCountUi() { | |
mCountView.setText(mCount.get()+""); | |
} | |
private void startGame() { | |
if (mTimerThread != null && !mTimerThread.isInterrupted()) { | |
Log.w(TAG, "Timer thread is already running!"); | |
} | |
mCount.set(0); | |
mTimerThread = new TimerThread(new Handler()); | |
mTimerThread.start(); | |
} | |
private void gameFinished(final int delta) { | |
if (delta <= GOOD_DELTA) { | |
mDifficulty -= DIFFICULTY_CHANGE; | |
if (mDifficulty < HARDEST_DIFFICULTY) { | |
mWon = true; | |
mCountView.setText("You win!"); | |
} else { | |
Toast.makeText(this, "Good!", Toast.LENGTH_SHORT).show(); | |
startGame(); | |
} | |
} else { | |
Toast.makeText(this, "Bad :(", Toast.LENGTH_SHORT).show(); | |
startGame(); | |
} | |
} | |
private class CountUpdater implements Runnable { | |
@Override | |
public void run() { | |
if (!mWon) { | |
updateCountUi(); | |
} | |
} | |
} | |
private class TimerThread extends Thread { | |
private final Handler mHandler; | |
public TimerThread(final Handler handler) { | |
mHandler = handler; | |
} | |
@Override | |
public void run() { | |
while (!isInterrupted()) { | |
mCount.incrementAndGet(); | |
mHandler.post(new CountUpdater()); | |
try { | |
Thread.sleep(mDifficulty); | |
} catch (InterruptedException e) { | |
Thread.currentThread().interrupt(); | |
return; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment