【问题标题】:Android 4 * 4 tic tac toe game [closed]Android 4 * 4 tic tac toe 游戏[关闭]
【发布时间】:2013-11-03 11:32:20
【问题描述】:

我是 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");
}

}

【问题讨论】:

  • 所以您从 Internet 上获取了一些代码并希望我们为您扩展它?请返回一个具体的问题以及您个人尝试过的问题。如果你只是被灌输了答案,你将什么也学不到。
  • 非常抱歉,没有。我们可以帮助您解决在途中遇到的具体问题,但我们无法为您写出全部内容。
  • 不是一个真正的问题。海报要求编写或更正整个应用程序代码。他没有尝试过,也没有任何我们可以帮助他解决的问题。
  • 此外,您应该首先将循环增加 1,然后查看 4*4 网格的位置,正如其他人所说,您需要了解的不仅仅是复制粘贴来制作游戏您将需要了解 android 框架和基本游戏机制。调试至关重要。

标签: java android android-layout game-engine


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多