• macOS swift下使用贝塞尔曲线制作五子棋盘(2)



    上一篇我们实现了绘制棋盘格子以及棋子的功能,这一篇我们来实现黑白子交替落子和判断胜负。

    落子

    黑白子交替

    //我们定义一个bool值,因为五子棋里总是黑子落地切换白子,白子落地切换黑子,所以每当执行完一次落子,就应该取反bool值
    var isblackOrWhite: Bool = true
    
    • 1
    • 2

    计算落子的point

    //我们定义一个结构体,point代表当前子的横竖列坐标,isblackOrWhite代表当前子为黑子还是摆子
    struct Piece {
        let point: NSPoint
        let isblackOrWhite: Bool
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    //我们在鼠标落下来捕捉当前鼠标点击区域
    override func mouseDown(with event: NSEvent) {
        super.mouseDown(with: event)
        //获取鼠标在当前窗口的位置
        let mousePoint = event.locationInWindow
        print("x: \(mousePoint.x), y: \(mousePoint.y)")
        //如果鼠标超出了我们固定的棋盘大小,则丢弃此次点击时间
        if (mousePoint.x + 23) > 500 || (mousePoint.y + 23) > 500 {
            return
        }
        //判断触摸的位置在棋盘的交叉点
        var xPoint: Int
        var yPoint: Int
        //刚好获取列行点
        xPoint = Int((mousePoint.x)/30)
        yPoint = Int((mousePoint.y)/30)
    
        if containsDuplicate(NSPoint(x: xPoint, y: yPoint)) { return }
        //刷新棋盘,此时会出发draw方法,重绘棋盘
        needsDisplay = true
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    功能

    判断当前落点是否有相同棋子,并保存

        //判断是否有相同坐标棋子并保存,相同坐标棋子则不做处理
        private func containsDuplicate(_ point: NSPoint) -> Bool {
            //判断是否有相同棋子
            for p in points {
                let x = p.point.x - point.x
                let y = p.point.y - point.y
                //判断相同点
                if abs(x * x + y * y) < 0.00001 {
                    return true
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
        //构建棋子结构体
        struct Piece {
            let point: NSPoint
            let isblackOrWhite: Bool
        }
        //储存棋子数组
        var points: Array<Piece> = []
        //保存棋子
        let piece = Piece.init(point: point, isblackOrWhite: isblackOrWhite)
        points.append(piece)
        return false
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    判断输赢

    五子棋的输赢极好判断,上下左右及斜边满足五个相同类型棋子就可以获胜,如图所示:
    在这里插入图片描述

    分析

    左右分析

    右边来讲,棋子x+1,左边来讲棋子x-1。

    上下分析

    上边来讲,棋子y+1,下边来讲棋子y-1。

    斜向分析

    左上:棋子x-1,y+1
    左下:棋子x-1,y-1
    右上:棋子x+1,y+1
    右下:棋子x+1,y-1
    获得这些信息后,我们可以计算出

    override func mouseDown(with event: NSEvent) {
        super.mouseDown(with: event)
        
        let mousePoint = event.locationInWindow
        print("x: \(mousePoint.x), y: \(mousePoint.y)")
        
        if (mousePoint.x + 23) > 500 || (mousePoint.y + 23) > 500 {
            return
        }
        
        //判断触摸的位置在棋盘的交叉点
        var xPoint: Int
        var yPoint: Int
        
        xPoint = Int((mousePoint.x)/30)
        yPoint = Int((mousePoint.y)/30)
        
        if containsDuplicate(NSPoint(x: xPoint, y: yPoint)) { return }
        needsDisplay = true
        //判断输赢
        if chess_horizontal(xPoint, yPoint, isblackOrWhite) == 5 || chess_vertical(xPoint, yPoint, isblackOrWhite) == 5 || chess_oblique(xPoint, yPoint, isblackOrWhite) == 5 {
            let alert = NSAlert()
            alert.addButton(withTitle: "确定")
            alert.informativeText = "\(isblackOrWhite ? "黑棋" : "白棋")你赢了"
            alert.messageText = ""
            alert.alertStyle = .informational
            alert.beginSheetModal(for: self.view.window!) { handler in
                if handler == NSApplication.ModalResponse.alertFirstButtonReturn { }
            }
            
            //置空棋盘
            isblackOrWhite = true
            points.removeAll()
            needsDisplay = true
        }
        //轮到另一位选手
        isblackOrWhite = !isblackOrWhite
    }
    
    private func containsDuplicate(_ point: NSPoint) -> Bool {
        //判断是否有相同棋子
        for p in points {
            let x = p.point.x - point.x
            let y = p.point.y - point.y
            if abs(x * x + y * y) < 0.00001 {
                return true
            }
        }
        let piece = Piece.init(point: point, isblackOrWhite: isblackOrWhite)
        points.append(piece)
        return false
    }
    
    private func chess_horizontal(_ chessX: Int ,_ chessY: Int, _ isblackOrWhite: Bool) -> Int {
        var count = 0 //定义一个棋子的判断器
        var pieces: Array<Piece> = []
        for p in points {
            if p.isblackOrWhite == isblackOrWhite && Int(p.point.y) == chessY { pieces.append(p) }
        }
        
        for i in 0..<5 {
            //向右判断
            for piece in pieces {
                if chessX+i == Int(piece.point.x) {
                    count += 1
                    if count == 5 {
                        return count
                    }
                }
            }
        }
        
        for i in 1..<5 {
            //向左判断
            for piece in pieces {
                if chessX-i == Int(piece.point.x) {
                    count += 1
                    if count == 5 {
                        return count
                    }
                }
            }
        }
        
        return 0
    }
    
    private func chess_vertical(_ chessX: Int ,_ chessY: Int, _ isblackOrWhite: Bool) -> Int {
        var count = 0 //定义一个棋子的判断器
        var pieces: Array<Piece> = []
        for p in points {
            if p.isblackOrWhite == isblackOrWhite && Int(p.point.x) == chessX { pieces.append(p) }
        }
        
        for i in 0..<5 {
            //向上判断
            for piece in pieces {
                if chessY+i == Int(piece.point.y) {
                    count += 1
                    if count == 5 {
                        return count
                    }
                }
            }
        }
        
        for i in 1..<5 {
            //向左判断
            for piece in pieces {
                if chessY-i == Int(piece.point.y) {
                    count += 1
                    if count == 5 {
                        return count
                    }
                }
            }
        }
        
        return 0
    }
    
    private func chess_oblique(_ chessX: Int ,_ chessY: Int, _ isblackOrWhite: Bool) -> Int{
        var count = 0 //定义一个棋子的判断器
        var pieces: Array<Piece> = []
        for p in points {
            if p.isblackOrWhite == isblackOrWhite { pieces.append(p) }
        }
        
        for i in 0..<5 {
            //右上方向判断
            for piece in pieces {
                if chessX+i == Int(piece.point.x) && chessY+i == Int(piece.point.y) {
                    count += 1
                    if count == 5 {
                        return count
                    }
                }
            }
        }
        
        for i in 1..<5 {
            //右下判断
            for piece in pieces {
                if chessX+i == Int(piece.point.x) && chessY-i == Int(piece.point.y) {
                    count += 1
                    if count == 5 {
                        return count
                    }
                }
            }
        }
        
        for i in 1..<5 {
            //左上判断
            for piece in pieces {
                if chessX-i == Int(piece.point.x) && chessY+i == Int(piece.point.y) {
                    count += 1
                    if count == 5 {
                        return count
                    }
                }
            }
        }
        
        for i in 1..<5 {
            //左下判断
            for piece in pieces {
                if chessX-i == Int(piece.point.x) && chessY-i == Int(piece.point.y) {
                    count += 1
                    if count == 5 {
                        return count
                    }
                }
            }
        }
        
        return 0
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    //合并判断方法
    //判断输赢
    if chess_horizontal(xPoint, yPoint, isblackOrWhite) == 5 || chess_vertical(xPoint, yPoint, isblackOrWhite) == 5 || chess_oblique(xPoint, yPoint, isblackOrWhite) == 5 {
        let alert = NSAlert()
        alert.addButton(withTitle: "确定")
        alert.informativeText = "\(isblackOrWhite ? "黑棋" : "白棋")你赢了"
        alert.messageText = ""
        alert.alertStyle = .informational
        alert.beginSheetModal(for: self.view.window!) { handler in
            if handler == NSApplication.ModalResponse.alertFirstButtonReturn { }
        }
        
        //置空棋盘
        isblackOrWhite = true
        points.removeAll()
        needsDisplay = true
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    悔棋

        //撤销上一步动作
        @IBAction func undoAction(_ sender: Any) {
            if points.count != 0 {
                //如果棋子数组不为0,删除最后一个棋子并刷新棋盘
                points.removeLast()
                needsDisplay = true
                //记住撤销上一部动作后,白黑棋子必须交替。
                isblackOrWhite = !isblackOrWhite
            }else {
                let alert = NSAlert()
                alert.addButton(withTitle: "确定")
                alert.informativeText = "没有棋子,无法悔棋"
                alert.messageText = ""
                alert.alertStyle = .informational
                alert.beginSheetModal(for: self.view.window!) { handler in
                    if handler == NSApplication.ModalResponse.alertFirstButtonReturn { }
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    重开游戏

        @IBAction func reStartAction(_ sender: Any) {
            if points.count == 0 { return }
            let alert = NSAlert()
            alert.addButton(withTitle: "确定")
            alert.addButton(withTitle: "取消")
            alert.informativeText = "重开游戏"
            alert.messageText = ""
            alert.alertStyle = .informational
            alert.beginSheetModal(for: self.view.window!) { [self] handler in
                if handler == NSApplication.ModalResponse.alertFirstButtonReturn {
                    //清空所有棋子并刷新棋盘
                    points.removeAll()
                    isblackOrWhite = true
                    needsDisplay = true
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    demo链接

    链接: 五子棋

  • 相关阅读:
    基于人工大猩猩部队算法优化概率神经网络PNN的分类预测 - 附代码
    前端项目开发流程
    微服务框架 SpringCloud微服务架构 4 Ribbon 4.3 饥饿加载
    【Oracle】查看存储过程编译错误信息
    机器学习模型的评估方法
    C++ 虚析构函数的作用?
    【Java毕设项目合集】26款Java毕设项目合集
    Java 学习笔记
    Java swing实现应用程序对数据库的访问
    端口渗透篇:Java RMI 远程代码执行漏洞
  • 原文地址:https://blog.csdn.net/quanhaoH/article/details/126221433