Skip to content

Instantly share code, notes, and snippets.

@mrp1q7z
Last active August 29, 2015 13:57
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 mrp1q7z/9705061 to your computer and use it in GitHub Desktop.
Save mrp1q7z/9705061 to your computer and use it in GitHub Desktop.
Whac Mole
package com.yojiokisoft.whacmole.app;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
private Button[][] mButton = new Button[5][4]; // [行][列]
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton[0][0] = (Button) findViewById(R.id.Button1A);
mButton[0][1] = (Button) findViewById(R.id.Button1B);
mButton[0][2] = (Button) findViewById(R.id.Button1C);
mButton[0][3] = (Button) findViewById(R.id.Button1D);
mButton[1][0] = (Button) findViewById(R.id.Button2A);
mButton[1][1] = (Button) findViewById(R.id.Button2B);
mButton[1][2] = (Button) findViewById(R.id.Button2C);
mButton[1][3] = (Button) findViewById(R.id.Button2D);
mButton[2][0] = (Button) findViewById(R.id.Button3A);
mButton[2][1] = (Button) findViewById(R.id.Button3B);
mButton[2][2] = (Button) findViewById(R.id.Button3C);
mButton[2][3] = (Button) findViewById(R.id.Button3D);
mButton[3][0] = (Button) findViewById(R.id.Button4A);
mButton[3][1] = (Button) findViewById(R.id.Button4B);
mButton[3][2] = (Button) findViewById(R.id.Button4C);
mButton[3][3] = (Button) findViewById(R.id.Button4D);
mButton[4][0] = (Button) findViewById(R.id.Button5A);
mButton[4][1] = (Button) findViewById(R.id.Button5B);
mButton[4][2] = (Button) findViewById(R.id.Button5C);
mButton[4][3] = (Button) findViewById(R.id.Button5D);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onResume() {
super.onResume();
printMole();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void printMole() {
int row = (int) (Math.random() * 5);
int col = (int) (Math.random() * 4);
mButton[row][col].setBackgroundResource(R.drawable.mole);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment