Layout
<?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" >
<TextView
android:id="@+id/titleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/title" />
<Button
android:id="@+id/newGameBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="newGame"
android:text="New Game" />
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/topLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:height="100dp"
android:text="O"
android:width="100dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:height="100dp"
android:text="O"
android:width="100dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:height="100dp"
android:text="O"
android:width="100dp" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:height="100dp"
android:text="O"
android:width="100dp" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:height="100dp"
android:text="O"
android:width="100dp" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:height="100dp"
android:text="O"
android:width="100dp" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:height="100dp"
android:text="O"
android:width="100dp" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:height="100dp"
android:text="O"
android:width="100dp" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:height="100dp"
android:text="O"
android:width="100dp" />
</TableRow>
</TableLayout>
</LinearLayout>
Java
package com.example.NaughtsAndCrosses;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class MainActivity extends Activity {
/**
* 轮到了正方还是反方,默认为X
*/
private boolean noughtsTurn = false;
/**
* 用于记录表格中的数据
*/
private char board[][] = new char [ 3 ] [ 3 ];
@ Override
public void onCreate ( Bundle savedInstanceState ) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.main );
setupOnClickListeners ( );
resetButtons ( );
}
/**
* 当点击new Game按钮时候的点击事件 --> 初始化数据
*/
public void newGame ( View view ) {
noughtsTurn = false;
board = new char [ 3 ] [ 3 ];
resetButtons ( );
}
/**
* 重置每个按钮的颜色以及文字
*/
private void resetButtons ( ) {
// 找到TableLayout组件
TableLayout T = ( TableLayout ) findViewById ( R.id.tableLayout );
/**
* TabLayout --> TabRow --> Button
*/
for ( int y = 0 ; y < T.getChildCount ( ) ; y ++ ) {
if (T.getChildAt ( y ) instanceof TableRow) {
TableRow R = ( TableRow ) T.getChildAt ( y );
for ( int x = 0 ; x < R.getChildCount ( ) ; x ++ ) {
if (R.getChildAt ( x ) instanceof Button) {
Button B = ( Button ) R.getChildAt ( x );
B.setText ( "" );
B.setEnabled ( true );
}
}
}
}
// 找到文字组件并 设置提示的文字信息
TextView t = ( TextView ) findViewById ( R.id.titleText );
t.setText ( R.string.title );
}
/**
* 检查是否有人获胜
*/
private boolean checkWin ( ) {
// 获胜方为X
char winner = ‘\0‘;
if (checkWinner ( board , 3 , ‘X‘ )) {
winner = ‘X‘;
}
// 获胜方为Y
else if (checkWinner ( board , 3 , ‘O‘ )) {
winner = ‘O‘;
}
// 没有人获胜
if (winner == ‘\0‘) {
return false;
}
// 有人获胜,并提示是哪一个获胜
else {
// display winner
TextView T = ( TextView ) findViewById ( R.id.titleText );
T.setText ( winner + " wins" );
return true;
}
}
/**
* 检查是哪一方获胜
*/
private boolean checkWinner ( char [ ][ ] board , int size ,
char player ) {
// 检查每一行
for ( int x = 0 ; x < size ; x ++ ) {
int total = 0;
for ( int y = 0 ; y < size ; y ++ ) {
if (board [ x ] [ y ] == player) {
total ++ ;
}
}
if (total >= size) {
return true;
}
}
// 检查每一列
for ( int y = 0 ; y < size ; y ++ ) {
int total = 0;
for ( int x = 0 ; x < size ; x ++ ) {
if (board [ x ] [ y ] == player) {
total ++ ;
}
}
if (total >= size) {
return true;
}
}
// 检查X == Y的那条对角线
int total = 0;
for ( int x = 0 ; x < size ; x ++ ) {
for ( int y = 0 ; y < size ; y ++ ) {
if (x == y
&& board [ x ] [ y ] == player) {
total ++ ;
}
}
}
if (total >= size) {
return true;
}
// 检查X != Y的对角线
total = 0;
for ( int x = 0 ; x < size ; x ++ ) {
for ( int y = 0 ; y < size ; y ++ ) {
if (x + y == size - 1
&& board [ x ] [ y ] == player) {
total ++ ;
}
}
}
// 有人获胜
if (total >= size) {
return true;
}
// 没有人获胜
return false;
}
/**
* 有人获胜的情况下,禁用网格上的所有按钮
*/
private void disableButtons ( ) {
TableLayout T = ( TableLayout ) findViewById ( R.id.tableLayout );
for ( int y = 0 ; y < T.getChildCount ( ) ; y ++ ) {
if (T.getChildAt ( y ) instanceof TableRow) {
TableRow R = ( TableRow ) T.getChildAt ( y );
for ( int x = 0 ; x < R.getChildCount ( ) ; x ++ ) {
if (R.getChildAt ( x ) instanceof Button) {
Button B = ( Button ) R.getChildAt ( x );
B.setEnabled ( false );
}
}
}
}
}
/**
* 每次点击按钮时候的处理事件,得到点击的x和y坐标,
*/
private void setupOnClickListeners ( ) {
TableLayout T = ( TableLayout ) findViewById ( R.id.tableLayout );
for ( int y = 0 ; y < T.getChildCount ( ) ; y ++ ) {
if (T.getChildAt ( y ) instanceof TableRow) {
TableRow R = ( TableRow ) T.getChildAt ( y );
for ( int x = 0 ; x < R.getChildCount ( ) ; x ++ ) {
View V = R.getChildAt ( x );
V.setOnClickListener ( new PlayOnClick (
x ,
y ) );
}
}
}
}
/**
* Button 按钮的自定义点击事件,然后根据noughtsTurn是true还是false
* 来决定存储的是X还是O,并设置点击过的按钮为不可编辑
*
* 并检查是否有人获胜 ,然后把noughtsTurn取反 表示另外一个人在操作
*/
private class PlayOnClick implements View.OnClickListener {
// 点击按钮的x坐标
private int x = 0;
// 点击按钮的Y坐标
private int y = 0;
public PlayOnClick (
int x , int y ) {
this.x = x;
this.y = y;
}
@ Override
public void onClick ( View view ) {
if (view instanceof Button) {
Button B = ( Button ) view;
board [ x ] [ y ] = noughtsTurn ? ‘O‘
: ‘X‘;
B.setText ( noughtsTurn ? "O"
: "X" );
B.setEnabled ( false );
noughtsTurn = ! noughtsTurn;
// 检查是否有人获胜
if (checkWin ( )) {
disableButtons ( );
}
}
}
}
}
String.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Noughts & Crosses</string>
<string name="title">Naughts & Crosses - By Lyndon Armitage</string>
</resources>

原文:http://www.cnblogs.com/SM-t/p/4301483.html