Skip to content

Instantly share code, notes, and snippets.

@hellojinjie
Created October 8, 2019 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hellojinjie/2282305911942095e7a08a994da5b0b7 to your computer and use it in GitHub Desktop.
Save hellojinjie/2282305911942095e7a08a994da5b0b7 to your computer and use it in GitHub Desktop.
package com.example.five_chess;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private int step = 0;
// 1 黑胜, 2 白胜
private int[][] board = new int[9][9];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout boardView = (LinearLayout) findViewById(R.id.board);
boardView.setBackground(this.getDrawable(R.drawable.board));
Log.i(TAG, "onCreate: " + boardView.getWidth());
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(metrics.widthPixels, metrics.widthPixels);
boardView.setLayoutParams(layoutParams);
for (int j = 0; j < 9; j++) {
LinearLayout linearLayout = new LinearLayout(this);
for (int i = 0; i < 9; i++) {
final Button button = new Button(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(metrics.widthPixels / 9, metrics.widthPixels / 9);
button.setBackgroundColor(Color.TRANSPARENT);
button.setTag(R.string.key_x, i);
button.setTag(R.string.key_y, j);
final Context context = this;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int x = (Integer) button.getTag(R.string.key_x);
int y = (Integer) button.getTag(R.string.key_y);
boolean result = false;
if (step % 2 ==0) {
button.setBackground(context.getDrawable(R.drawable.black));
board[y][x] = 1;
result = judge(x, y, 1);
} else {
button.setBackground(context.getDrawable(R.drawable.white));
board[y][x] = 2;
result = judge(x, y, 2);
}
if (result) {
if (step % 2 == 0) {
showResult("黑棋胜");
} else {
showResult("白棋胜");
}
}
step++;
}
});
linearLayout.addView(button, params);
}
boardView.addView(linearLayout);
}
}
private void showResult(String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("游戏结束")
.setMessage(message)
.setPositiveButton("重新开始", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
resetBoard();
dialogInterface.dismiss();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
builder.create().show();
}
private void resetBoard() {
this.board = new int[9][9];
this.step = 0;
LinearLayout boardView = (LinearLayout) findViewById(R.id.board);
for (int i = 0; i < 9; i++) {
LinearLayout line = (LinearLayout) boardView.getChildAt(i);
for (int j = 0; j < 9; j++) {
((Button) line.getChildAt(j)).setBackgroundColor(Color.TRANSPARENT);
}
}
}
private boolean judge(int x, int y, int myStep) {
int matched = 1;
// 横的方向
for (int m = x - 1; m >= 0; m--) {
if (board[y][m] == myStep) {
matched++;
} else {
break;
}
}
for (int m = x + 1; m < 9; m++) {
if (board[y][m] == myStep) {
matched++;
} else {
break;
}
}
if (matched >= 5) {
return true;
}
// 竖的方向
matched = 1;
for (int m = y - 1; m >= 0; m--) {
if (board[m][x] == myStep) {
matched++;
} else {
break;
}
}
for (int m = y + 1; m < 9; m++) {
if (board[m][x] == myStep) {
matched++;
} else {
break;
}
}
if (matched >= 5) {
return true;
}
// 左斜
matched = 1;
for (int m = x - 1, n = y - 1; m >= 0 && n >= 0; m--, n--) {
if (board[n][m] == myStep) {
matched++;
} else {
break;
}
}
for (int m = x + 1, n = y + 1; m < 9 && n < 9; m++, n++) {
if (board[n][m] == myStep) {
matched++;
} else {
break;
}
}
if (matched >= 5) {
return true;
}
// 右斜
matched = 1;
for (int m = x + 1, n = y - 1; m < 9 && n >= 0; m++, n--) {
if (board[n][m] == myStep) {
matched++;
} else {
break;
}
}
for (int m = x - 1, n = y + 1; m >= 0 && n< 9; m--, n++) {
if (board[n][m] == myStep) {
matched++;
} else {
break;
}
}
if (matched >= 5) {
return true;
}
return false;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:text="重新开始"
android:layout_gravity="center"
android:layout_margin="20dp"
android:textSize="20sp"
android:layout_height="wrap_content">
</Button>
<TextView
android:layout_width="match_parent"
android:textAlignment="center"
android:text="黑棋走子"
android:textSize="20sp"
android:layout_height="wrap_content"></TextView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_weight="1"
android:gravity="bottom"
android:orientation="horizontal"
android:layout_height="wrap_content">
<LinearLayout android:id="@+id/board"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment