-4

我是Android的初学者。我使用互联网上提供的代码制作了一个基本的 3 x 3 井字游戏。

现在我想为 4*4 棋盘制作这款游戏​​。如果您知道 4*4 tic tac toe 的逻辑或算法或代码,请帮助我。请帮我。下面我发布了完整的源代码。

布局:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#242424"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:gravity="center_horizontal"
        android:lines="1"
        android:padding="5sp"
        android:text="TicTacToe"
        android:textColor="#8eab27"
        android:textStyle="bold"
        android:textSize="25dip" />

    <TableLayout
        android:id="@+id/tlGrid"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stretchColumns="*" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_weight="1"
            android:gravity="center" >

            <Button
                android:id="@+id/btnGrid11"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="7dip"
                android:text=""
                android:textColor="#242424"
                android:textSize="35dip"
                android:textStyle="bold" />

            <Button
                android:id="@+id/btnGrid12"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="7dip"
                android:text=""
                android:textColor="#242424"
                android:textSize="35dip"
                android:textStyle="bold" />

            <Button
                android:id="@+id/btnGrid13"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="7dip"
                android:text=""
                android:textColor="#242424"
                android:textSize="35dip"
                android:textStyle="bold" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_weight="1"
            android:gravity="center" >

            <Button
                android:id="@+id/btnGrid21"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="7dip"
                android:text=""
                android:textColor="#242424"
                android:textSize="35dip"
                android:textStyle="bold" />

            <Button
                android:id="@+id/btnGrid22"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="7dip"
                android:text=""
                android:textColor="#242424"
                android:textSize="35dip"
                android:textStyle="bold" />

            <Button
                android:id="@+id/btnGrid23"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="7dip"
                android:text=""
                android:textColor="#242424"
                android:textSize="35dip"
                android:textStyle="bold" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_weight="1"
            android:gravity="center" >

            <Button
                android:id="@+id/btnGrid31"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="7dip"
                android:text=""
                android:textColor="#242424"
                android:textSize="35dip"
                android:textStyle="bold" />

            <Button
                android:id="@+id/btnGrid32"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="7dip"
                android:text=""
                android:textColor="#242424"
                android:textSize="35dip"
                android:textStyle="bold" />

            <Button
                android:id="@+id/btnGrid33"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="7dip"
                android:text=""
                android:textColor="#242424"
                android:textSize="35dip"
                android:textStyle="bold" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_weight="1"
            android:gravity="center" >

            <Button
                android:id="@+id/btnReset"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_span="3"
                android:padding="7dip"
                android:text=""
                android:textColor="#242424"
                android:textSize="20dip"
                android:textStyle="bold" />
        </TableRow>
    </TableLayout>

</LinearLayout>

JAVA源代码:

public class TicTacToe extends Activity implements OnClickListener {

private int[][] GridID = new int[4][4]; // i.e. [0 to 3][0 to 3]

private enum GridCode {
    BLANK, X, O
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button button;
    int id;
    for (int r = 1; r <= 3; r++) {
        for (int c = 1; c <= 3; c++) {
            id = getResources().getIdentifier("btnGrid" + r + c, "id",
                    getPackageName());
            GridID[r][c] = id;
            button = (Button) findViewById(id);
            button.setText(""); // seems to be needed
            button.setOnClickListener(this);
        }
    }

    button = (Button) findViewById(R.id.btnReset);
    button.setText("");
    button.setOnClickListener(this);
}

private boolean gameHasEnded() {
    Button button = (Button) findViewById(R.id.btnReset);
    return button.getText() != "";
}

private void resetGrid() {
    Button button = (Button) findViewById(R.id.btnReset);
    button.setText("");
    for (int r = 1; r <= 3; r++) {
        for (int c = 1; c <= 3; c++) {
            button = (Button) findViewById(GridID[r][c]);
            button.setText("");
        }
    }

}

public void onClick(View v) {
    Button button = (Button) v;

    if (button == (Button) findViewById(R.id.btnReset)) {
        if (gameHasEnded())
            resetGrid();
        return;
    }

    if (gameHasEnded() || button.getText() != "")
        return;

    button.setText("X");

    int xCountByRow[] = new int[4];
    int oCountByRow[] = new int[4];
    int xCountByColumn[] = new int[4];
    int oCountByColumn[] = new int[4];
    int xCountByDiagonal[] = new int[3];
    int oCountByDiagonal[] = new int[3];

    GridCode gc[][] = new GridCode[4][4];

    for (int r = 1; r <= 3; r++) {
        for (int c = 1; c <= 3; c++) {
            button = (Button) findViewById(GridID[r][c]);
            if (button.getText() == "X") {
                gc[r][c] = GridCode.X;
                xCountByRow[r]++;
                xCountByColumn[c]++;
                if (r == c)
                    xCountByDiagonal[1]++;
                if (r + c == 4)
                    xCountByDiagonal[2]++;
            } else if (button.getText() == "O") {
                gc[r][c] = GridCode.O;
                oCountByRow[r]++;
                oCountByColumn[c]++;
                if (r == c)
                    oCountByDiagonal[1]++;
                if (r + c == 4)
                    oCountByDiagonal[2]++;
            } else {
                gc[r][c] = GridCode.BLANK;
            }
        }
    }

    // Have we lost?
    for (int r = 1; r <= 3; r++) {
        if (xCountByRow[r] == 3) {
            declareLoss();
            return;
        }
    }
    for (int c = 1; c <= 3; c++) {
        if (xCountByColumn[c] == 3) {
            declareLoss();
            return;
        }
    }
    for (int d = 1; d <= 2; d++) {
        if (xCountByDiagonal[d] == 3) {
            declareLoss();
            return;
        }
    }

    // Can we win?
    for (int r = 1; r <= 3; r++) {
        if (oCountByRow[r] == 2 && xCountByRow[r] == 0) {
            for (int c = 1; c <= 3; c++) {
                if (gc[r][c] == GridCode.BLANK) {
                    button = (Button) findViewById(GridID[r][c]);
                    button.setText("O");
                    declareWin();
                    return;
                }
            }
        }
    }
    for (int c = 1; c <= 3; c++) {
        if (oCountByColumn[c] == 2 && xCountByColumn[c] == 0) {
            for (int r = 1; r <= 3; r++) {
                if (gc[r][c] == GridCode.BLANK) {
                    button = (Button) findViewById(GridID[r][c]);
                    button.setText("O");
                    declareWin();
                    return;
                }
            }
        }
    }
    for (int d = 1; d <= 2; d++) {
        if (oCountByDiagonal[d] == 2 && xCountByDiagonal[d] == 0) {
            for (int r = 1; r <= 3; r++) {
                int c = (d == 1) ? r : 4 - r;
                if (gc[r][c] == GridCode.BLANK) {
                    button = (Button) findViewById(GridID[r][c]);
                    button.setText("O");
                    declareWin();
                    return;
                }
            }
        }
    }

    // Do we need to block a win?
    for (int r = 1; r <= 3; r++) {
        if (xCountByRow[r] == 2 && oCountByRow[r] == 0) {
            for (int c = 1; c <= 3; c++) {
                if (gc[r][c] == GridCode.BLANK) {
                    button = (Button) findViewById(GridID[r][c]);
                    button.setText("O");
                    return;
                }
            }
        }
    }
    for (int c = 1; c <= 3; c++) {
        if (xCountByColumn[c] == 2 && oCountByColumn[c] == 0) {
            for (int r = 1; r <= 3; r++) {
                if (gc[r][c] == GridCode.BLANK) {
                    button = (Button) findViewById(GridID[r][c]);
                    button.setText("O");
                    return;
                }
            }
        }
    }
    for (int d = 1; d <= 2; d++) {
        if (xCountByDiagonal[d] == 2 && oCountByDiagonal[d] == 0) {
            for (int r = 1; r <= 3; r++) {
                int c = (d == 1) ? r : 4 - r;
                if (gc[r][c] == GridCode.BLANK) {
                    button = (Button) findViewById(GridID[r][c]);
                    button.setText("O");
                    return;
                }
            }
        }
    }

    // TODO:
    // Can we create a double threat?
    // Do we need to prevent a double threat?

    // Move randomly
    Button buttons[] = new Button[9];
    int buttonCount = 0;
    for (int r = 1; r <= 3; r++) {
        for (int c = 1; c <= 3; c++) {
            if (gc[r][c] == GridCode.BLANK) {
                buttonCount++;
                buttons[buttonCount] = (Button) findViewById(GridID[r][c]);
            }
        }
    }
    if (buttonCount == 0) {
        declareDraw();
        return;
    }
    Random random = new Random();
    Button randomButton = buttons[random.nextInt(buttonCount) + 1];
    randomButton.setText("O");
}

private void declareSomething(String something) {
    Button button = (Button) findViewById(R.id.btnReset);
    button.setText(something + "! \n(click to reset)");
}

private void declareLoss() {
    declareSomething("Congratulations! You win");
}

private void declareWin() {
    declareSomething("You Lose!Computer Win");
}

private void declareDraw() {
    declareSomething("Draw");
}

}

4

1 回答 1

0

你想为安卓开发一个游戏,你说你是一个完全的初学者。要制作一个安卓游戏(而不仅仅是从网上复制代码),您至少需要熟悉 3 件事:1)一般的 java,2)游戏开发,3)android。

您的问题对于这里的任何人来说都是广泛而笼统的回答,我建议您针对您想要变得更好的特定领域寻找一些初学者教程。

于 2013-11-03T12:12:20.013 回答