在Android Studio中实现五子棋胜负判断

五子棋是一种经典的棋类游戏,虽然规则简单,但实现起来涉及到较多逻辑。本文将逐步指导你如何在Android Studio中实现五子棋的胜负判断,帮助你掌握这个过程。

整体实现流程

为了让你更清晰地理解实现步骤,下面是整体流程的表格:

步骤 描述
1 创建一个棋盘的界面
2 编写棋盘点击事件,记录棋子
3 实现胜负判断逻辑
4 显示胜负结果

每一步的具体实现

1. 创建棋盘的界面

首先,你需要在Android Studio中创建一个简单的用户界面。你可以使用GridLayout来构建棋盘。

<!-- res/layout/activity_main.xml -->
<GridLayout
    xmlns:android="
    android:id="@+id/gridLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:rowCount="15"
    android:columnCount="15" />

2. 编写棋盘点击事件,记录棋子

接着,创建一个MainActivity类来处理事件。每个棋盘上的格子都可以用一个按钮来表示,点击按钮时记录棋子放置的位置。

// MainActivity.kt
class MainActivity : AppCompatActivity() {
    private val board = Array(15) { Array(15) { 0 } } // 0: 空,1: 黑子,2: 白子
    private var currentPlayer = 1 // 1: 黑子,2: 白子

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val gridLayout = findViewById<GridLayout>(R.id.gridLayout)
        for (i in 0 until 15) {
            for (j in 0 until 15) {
                val button = Button(this)
                button.layoutParams = GridLayout.LayoutParams().apply {
                    width = 0
                    height = 0
                    columnSpec = GridLayout.spec(j, 1f)
                    rowSpec = GridLayout.spec(i, 1f)
                }
                button.setOnClickListener {
                    if (board[i][j] == 0) {
                        board[i][j] = currentPlayer
                        button.text = if (currentPlayer == 1) "●" else "○"
                        currentPlayer = if (currentPlayer == 1) 2 else 1
                        checkWin(i, j) // 检查胜负
                    }
                }
                gridLayout.addView(button)
            }
        }
    }
}

3. 实现胜负判断逻辑

现在,我们需要实现胜负判断的逻辑。以下是判断胜负的代码。

// MainActivity.kt
private fun checkWin(row: Int, col: Int) {
    val directions = listOf(
        Pair(1, 0),  // 纵向
        Pair(0, 1),  // 横向
        Pair(1, 1),  // 斜向右
        Pair(1, -1)  // 斜向左
    )
    
    for (direction in directions) {
        var count = 1
        for (step in 1..4) {
            val newRow = row + direction.first * step
            val newCol = col + direction.second * step
            if (isValid(newRow, newCol) && board[newRow][newCol] == board[row][col]) {
                count++
            } else {
                break
            }
        }
        
        for (step in 1..4) {
            val newRow = row - direction.first * step
            val newCol = col - direction.second * step
            if (isValid(newRow, newCol) && board[newRow][newCol] == board[row][col]) {
                count++
            } else {
                break
            }
        }
        
        if (count >= 5) {
            Toast.makeText(this, "玩家 ${board[row][col]} 胜利!", Toast.LENGTH_SHORT).show()
            return
        }
    }
}

private fun isValid(row: Int, col: Int): Boolean {
    return row in 0..14 && col in 0..14
}

在这里,我们使用了一个checkWin函数来检查四个方向的连子情况。

4. 显示胜负结果

如果有玩家获胜,你可以通过Toast显示消息。通过上面的逻辑,我们就已实现了基本的胜负判断。

类图

下面是我们实现的MainActivity类的类图:

classDiagram
    class MainActivity {
        +board: Array<Array<Int>>
        +currentPlayer: Int
        +onCreate(savedInstanceState: Bundle): Unit
        +checkWin(row: Int, col: Int): Unit
        +isValid(row: Int, col: Int): Boolean
    }

甘特图

以下是整个实现过程的甘特图:

gantt
    title 实现五子棋胜负判断的过程
    dateFormat  YYYY-MM-DD
    section 过程
    创建棋盘界面           :a1, 2023-01-01, 1d
    编写棋盘点击事件       :after a1  , 2d
    实现胜负判断逻辑       :after a1  , 2d
    显示胜负结果           :after a1  , 1d

结尾

通过以上步骤,你应该能够在Android Studio中实现一个简单的五子棋,并实现胜负判断功能。继续探索更多功能,如悔棋、保存游戏状态等,会为你未来的开发之路增添色彩。祝你编码愉快!