Skip to content

Instantly share code, notes, and snippets.

@3t14
Created February 8, 2013 08:06
Show Gist options
  • Save 3t14/4737378 to your computer and use it in GitHub Desktop.
Save 3t14/4737378 to your computer and use it in GitHub Desktop.
Simple Maru-Batsu Game (noughts-and-crosses) Code for an Android app
package com.dev_training.android.maru_batsu;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private static final int STATE_BATSU = 1;
private static final int STATE_MARU = 2;
ImageButton imageButton[][] = new ImageButton[3][];
int cellState[][] = new int[3][];
boolean mode = false; // false = maru, true = batsu
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i=0; i<3; i++){
imageButton[i] = new ImageButton[3];
cellState[i] = new int[3];
}
imageButton[0][0] = (ImageButton) findViewById(R.id.ib00);
imageButton[0][1] = (ImageButton) findViewById(R.id.ib01);
imageButton[0][2] = (ImageButton) findViewById(R.id.ib02);
imageButton[1][0] = (ImageButton) findViewById(R.id.ib10);
imageButton[1][1] = (ImageButton) findViewById(R.id.ib11);
imageButton[1][2] = (ImageButton) findViewById(R.id.ib12);
imageButton[2][0] = (ImageButton) findViewById(R.id.ib20);
imageButton[2][1] = (ImageButton) findViewById(R.id.ib21);
imageButton[2][2] = (ImageButton) findViewById(R.id.ib22);
findViewById(R.id.resetButton).setOnClickListener(this);
}
void reset(){
for (int y = 0; y < 3; y++){
for (int x = 0; x < 3; x++){
imageButton[y][x].setImageResource(R.drawable.cell_back);
imageButton[y][x].setOnClickListener(this);
cellState[y][x] = 0;
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onClick(View v) {
// クリックしたときの処理
for (int y = 0; y < 3; y++){
for (int x = 0; x < 3; x++){
if (imageButton[y][x].getId() == v.getId()){
// 設置されていない場合
if (cellState[y][x] == 0) {
// ヒットしたら、○もしくは×を描画
imageButton[y][x].setImageResource(
(mode)? R.drawable.batsu: R.drawable.maru);
cellState[y][x] = (mode)? STATE_BATSU : STATE_MARU;
// 判定
if(judge()) {
// 結果表示
Toast.makeText(this, "勝負あり!", Toast.LENGTH_LONG).show();
return;
}
mode = !mode;
break;
}
}
}
}
if (v.getId() == R.id.resetButton){
reset();
}
}
// 判定
boolean judge(){
// 横、斜め、縦方向に3連続のコマがないか探索
int state = (mode)? STATE_BATSU: STATE_MARU;
for (int row=0; row<3; row++) {
if (judgeRow(row)) return true;
}
for (int column=0; column<3; column++) {
if (judgeColumn(column)) return true;
}
// 左上から右下の判定
if (cellState[0][0] == state
&& cellState[1][1] == state
&& cellState[2][2] == state ) return true;
// 右上から左下の判定
if (cellState[0][2] == state
&& cellState[1][1] == state
&& cellState[2][0] == state ) return true;
return false;
}
boolean judgeRow(int row){
int state = (mode)? STATE_BATSU: STATE_MARU;
for (int i=0; i<3; i++){
if (cellState[row][i] != state) return false;
}
return true;
}
boolean judgeColumn(int column){
int state = (mode)? STATE_BATSU: STATE_MARU;
for (int i=0; i<3; i++){
if (cellState[i][column] != state) return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment