private boolean haschess(int col, int row, Color c) { Boolean result = false; for (int i = 0; i < chessCount; i++) { Chess ch = chessList[i]; if (ch != null && ch.getCol() == col && ch.getRow() == row && ch.getColor() == c) { result = true; } } return result; }
时间: 2024-03-31 15:37:35 浏览: 17
这段代码看起来是一个判断指定位置是否有指定颜色棋子的函数。其中参数col和row表示要判断的位置所在的列和行,参数c表示要判断的棋子的颜色。函数的实现思路是通过遍历棋盘上所有棋子,判断是否有与指定位置和颜色相同的棋子。如果找到相同的棋子,将result设为true,最后返回result。如果遍历完所有棋子后仍未找到相同的棋子,result的值仍为false,表示该位置没有指定颜色的棋子。
相关问题
private boolean isWin(int col, int row) { boolean result = false; int CountCh = 1; Color c = null; if (isBlack) { c = Color.BLACK; } else { c = Color.WHITE; }
这段代码看起来像是一个判断棋局胜负的函数,其中参数col和row表示最近一次落子的列和行。函数的实现思路是通过向上、向下、向左、向右、左上、右下、左下、右上八个方向遍历棋盘,检查是否有连成五子的情况。变量CountCh表示当前方向上连续出现的同色棋子数量,初始值为1表示最近落子的棋子。变量c表示当前执棋方的颜色。变量result表示最终的胜负结果,初始值为false表示未分出胜负。
android扫雷代码
以下是一个简单的 Android 扫雷游戏的示例代码,供参考:
MainActivity.java:
```
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Random;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final int MINE_NUMBER = 10;
private static final int ROW_NUMBER = 10;
private static final int COL_NUMBER = 10;
private int[][] mGameBoard = new int[ROW_NUMBER][COL_NUMBER];
private Button[][] mButtons = new Button[ROW_NUMBER][COL_NUMBER];
private boolean[][] mIsMine = new boolean[ROW_NUMBER][COL_NUMBER];
private boolean[][] mIsOpened = new boolean[ROW_NUMBER][COL_NUMBER];
private boolean mIsGameOver = false;
private TextView mTvResult;
private GridLayout mGridLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTvResult = findViewById(R.id.tv_result);
mGridLayout = findViewById(R.id.grid_layout);
initGameBoard();
initButtons();
}
@Override
public void onClick(View v) {
if (mIsGameOver) {
return;
}
int row = (int) v.getTag(R.id.tag_row);
int col = (int) v.getTag(R.id.tag_col);
if (mIsOpened[row][col]) {
return;
}
if (mIsMine[row][col]) {
revealAllMines();
mTvResult.setText("You lose!");
mIsGameOver = true;
return;
}
openCell(row, col);
if (checkWin()) {
revealAllMines();
mTvResult.setText("You win!");
mIsGameOver = true;
}
}
private void initGameBoard() {
int cnt = 0;
Random random = new Random();
while (cnt < MINE_NUMBER) {
int row = random.nextInt(ROW_NUMBER);
int col = random.nextInt(COL_NUMBER);
if (!mIsMine[row][col]) {
mIsMine[row][col] = true;
cnt++;
}
}
for (int i = 0; i < ROW_NUMBER; i++) {
for (int j = 0; j < COL_NUMBER; j++) {
if (!mIsMine[i][j]) {
int cntMine = countNeighborMine(i, j);
mGameBoard[i][j] = cntMine;
}
}
}
}
private void initButtons() {
for (int i = 0; i < ROW_NUMBER; i++) {
for (int j = 0; j < COL_NUMBER; j++) {
Button button = new Button(this);
button.setTag(R.id.tag_row, i);
button.setTag(R.id.tag_col, j);
button.setOnClickListener(this);
mButtons[i][j] = button;
mGridLayout.addView(button);
}
}
}
private int countNeighborMine(int row, int col) {
int cnt = 0;
for (int i = row - 1; i <= row + 1; i++) {
for (int j = col - 1; j <= col + 1; j++) {
if (i < 0 || i >= ROW_NUMBER || j < 0 || j >= COL_NUMBER) {
continue;
}
if (mIsMine[i][j]) {
cnt++;
}
}
}
return cnt;
}
private void openCell(int row, int col) {
mIsOpened[row][col] = true;
mButtons[row][col].setEnabled(false);
if (mGameBoard[row][col] > 0) {
mButtons[row][col].setText(String.valueOf(mGameBoard[row][col]));
return;
}
for (int i = row - 1; i <= row + 1; i++) {
for (int j = col - 1; j <= col + 1; j++) {
if (i < 0 || i >= ROW_NUMBER || j < 0 || j >= COL_NUMBER) {
continue;
}
if (mIsOpened[i][j]) {
continue;
}
openCell(i, j);
}
}
}
private boolean checkWin() {
for (int i = 0; i < ROW_NUMBER; i++) {
for (int j = 0; j < COL_NUMBER; j++) {
if (!mIsMine[i][j] && !mIsOpened[i][j]) {
return false;
}
}
}
return true;
}
private void revealAllMines() {
for (int i = 0; i < ROW_NUMBER; i++) {
for (int j = 0; j < COL_NUMBER; j++) {
if (mIsMine[i][j]) {
mButtons[i][j].setText("X");
}
}
}
}
}
```
activity_main.xml:
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textStyle="bold"
android:textAlignment="center"
android:text=""/>
<GridLayout
android:id="@+id/grid_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:columnCount="10"
android:rowCount="10"/>
</LinearLayout>
```
注意:该示例代码仅供学习参考,可能存在不足之处,使用时请注意安全。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)