Java 安卓4*4井字游戏
我是Android的初学者。我已经做了一个基本的3乘3井字游戏使用的代码在互联网上可用 现在我想做一个4*4板的游戏。如果你知道4*4 tic tac toe的逻辑、算法或代码,请帮助我。请帮帮我。下面我已经发布了完整的源代码 布局:Java 安卓4*4井字游戏,java,android,android-layout,game-engine,Java,Android,Android Layout,Game Engine,我是Android的初学者。我已经做了一个基本的3乘3井字游戏使用的代码在互联网上可用 现在我想做一个4*4板的游戏。如果你知道4*4 tic tac toe的逻辑、算法或代码,请帮助我。请帮帮我。下面我已经发布了完整的源代码 布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_width="fill_parent" android:lay
<?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");
}
public类TicTacToe扩展活动实现OnClickListener{
private int[]GridID=new int[4][4];//即[0到3][0到3]
私有枚举网格代码{
空白,X,O
};
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
按钮;
int-id;
例如(int r=1;r你想为android开发一个游戏,你说你是一个完全的初学者。要制作一个android游戏(而不仅仅是从网上复制代码),你至少需要对3件事情有点熟悉:1)java的一般性,2)游戏开发,3)android
您的问题对于这里的任何人来说都是非常广泛和一般的。我建议您针对您想要更好地了解的特定领域寻找一些初学者教程。因此,您从互联网上获取了一些代码,希望我们为您扩展这些代码?请返回一个特定的问题以及您个人尝试的内容。您将如果你只是一匙一匙的回答,就什么也学不到。非常抱歉,但是没有。我们可以帮助你解决途中遇到的具体问题,但我们不能为你写全部内容。这不是一个真正的问题。Poster要求你写或更正整个应用程序代码。他没有尝试过,也没有任何问题我们可以帮助他解决e、 另外,你应该先把循环数增加1,然后看看你的4*4网格的效果如何,正如其他人所说的那样,你需要知道的不仅仅是复制n粘贴,要制作一个游戏,你需要了解android框架和基本的游戏机制。调试将是至关重要的。