008.井字遊戲

  • 布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Button" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Button" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Button" />

        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Button" />

        <Button
            android:id="@+id/button6"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Button" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <Button
            android:id="@+id/button7"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Button" />

        <Button
            android:id="@+id/button8"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Button" />

        <Button
            android:id="@+id/button9"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Button" />
    </LinearLayout>

</LinearLayout>

  • MainActivity.java
package com.example.app8;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    Button[] buttons = new Button[9];
    Resources res;
    int n = 0;// 按第幾次
    int ocolor;
    int xcolor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        res = this.getResources();
        ocolor = res.getColor(R.color.ocolor);
        xcolor = res.getColor(R.color.xcolor);

        for (int i = 0; i < buttons.length; i++) {
            int id = res.getIdentifier("button" + (i + 1), "id", this.getPackageName());
            buttons[i] = (Button) this.findViewById(id);
            buttons[i].setText("");
            buttons[i].setTextSize(50);
            buttons[i].setOnClickListener(clickListener);
        }
    }

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // Toast.makeText(MainActivity.this, "Hello",
            // Toast.LENGTH_SHORT).show();

            Button b = (Button) v;
            // b.setEnabled(false);//點了之後不能再點
            b.setClickable(false);
            if (n % 2 == 0) {
                b.setText("O");
                b.setTextColor(ocolor);
            } else {
                b.setText("X");
                b.setTextColor(xcolor);
            }

            if (checkGame()) {// true遊戲結束,false遊戲尚未結束
                Toast.makeText(MainActivity.this, "遊戲結束", Toast.LENGTH_SHORT).show();
                finish();
            }
            n++;
        }

        private boolean checkGame() {

            int[][] rules = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 },
                    { 0, 4, 8 }, { 2, 4, 6 }, { 0, 3, 6 }, { 1, 4, 7 },
                    { 2, 5, 8 }
            };

            for (int i = 1; i < rules.length; i++) {

                String s1, s2, s3;
                s1 = buttons[rules[i][0]].getText().toString();
                s2 = buttons[rules[i][1]].getText().toString();
                s3 = buttons[rules[i][2]].getText().toString();
                if (s1.equals("O") && s2.equals("O") && s3.equals("O")) {
                    Toast.makeText(MainActivity.this, "O贏了", Toast.LENGTH_SHORT).show();
                    return true;
                } else if (s1.equals("X") && s2.equals("X") && s3.equals("X")) {
                    Toast.makeText(MainActivity.this, "X贏了", Toast.LENGTH_SHORT).show();
                    return true;
                }
            }
            return false;
        }
    };
}