betway必威手机版 > betway必威手机版官网 > 11气的算法改进

原标题:11气的算法改进

浏览次数:153 时间:2020-03-26

tags: 应用, 开采小说

已成功章节索引

用斯威夫特写围棋App-00序用斯维夫特写围棋App-01预备工效能斯威夫特写围棋App-02创立工程用Swift写围棋App-03StoryBoard用Swift写围棋App-04棋盘用斯威夫特写围棋App-05初识棋谱用斯维夫特写围棋App-06深入剖判器初版用Swift写围棋App-07深入解析器改良用Swift写围棋App-08制图每一手棋用Swift写围棋App-09分片算法用Swift写围棋App-10气的算法用斯威夫特写围棋App-11算法改良

上次写完气的算法后,以为快临近尾声了。在利用中却发现存一部分难题:

  • 倘使在博艺进度中,要是已经有多手下在同叁个义务,则总计不得法;
  • 在回落时,被"提"的棋类呈现不健康;
  • 在“打劫”的时候程序管理不精确;对这些题材反复思索,笔者开掘根本原因是计量进程中不经意了棋子的前后相继顺序,分组和算气的时候只是思量了棋子的职分,而并未有杜撰落子前后相继。小编那才晓得: 围棋的长河是时间相关的,棋子的先后具备显要的成效。具体说来有以下准绳:
  • 每一步棋只是在现阶段的地势下起作用; 並且只会影响对方的某一片棋的生老病死; (棋谱中不设有一手棋让投机没气的情景卡塔尔
  • 正史上一度死了棋不会复活; 它们也不会对分组只怕别的一片棋起效果;想知道了那几个题目,改正算法就简单了。首先,大家需求在 Model相当于Move中著录落子的顺序:
class Move: NSObject { ... var handNumber = 0 //the order of move var handOfDead = -1 // in which step the move is caculated to be dead ...

下一场,当前这一步只须求思考对前一步结果的熏陶。因而,成了八个递归难点。其前期状态是第一手棋,围棋中央行政机关接鲜明是黑棋,它不管落在哪儿,这些棋自身就成了三个组,并且也是直接这一个状态时的并世无双二个组:

 // the very beginning if hand == 0 { let group0 = MoveGroup() group0.name = "B0" group0.addMove(allMoves[0]) return [group0] }

接下去,后边每一手正视于前一手的结果:var groups = playToHand当前手对分组的影响只会影响自身这一只已经存在的组:

  • 比如当前手和近日本身方的别样一组中的任何三个棋子相连,则当前手参与那一组;
  • 只要当前手和多个组相连,则这个组必要统一为贰个新的组;
  • 设若当前手不和别的自个儿方的组相连,则须求树立三个新的组;实现如下:
let groupsWithSameColor = groups.filter({$0.name.hasPrefix var handled = false lastMove.groupName = "" for g in groupsWithSameColor { for move in g.allMoves { if move.isConnectedTo { if lastMove.groupName == "" { handled = true g.addMove } else { // last move is already in a group, current group needs to be merged into that group let groupToConnect = groupsWithSameColor.filter({$0.name == lastMove.groupName}).first! g.mergeTo(groupToConnect) } break } }// end for move }//end for g

分完组再总计每二个对方组的气,判别其死活:

 // filter out empty groups let liveGroups = groups.filter({$0.allMoves.count > 0 }) let allOccupied = occupiedLocations(allMoves.filter({$0.handNumber <= hand && $0.handOfDead == -1})) // only need to check opposite party let oppositeGroups = liveGroups.filter({!$0.name.hasPrefix for grp in oppositeGroups{ let liberty = grp.calculateLiberty(allOccupied) if liberty == 0 { for move in grp.allMoves { move.isDead = true move.handOfDead = hand } grp.isDead = true } }

写起来文不加点。完整的算法完结:

 func playToHand-> [MoveGroup]{ // the very beginning if hand == 0 { let group0 = MoveGroup() group0.name = "B0" group0.addMove(allMoves[0]) return [group0] } //the current step depends on the last status var groups = playToHand let lastMove = allMoves[hand] let type = String(lastMove.type.rawValue) //"B" or "W" let groupsWithSameColor = groups.filter({$0.name.hasPrefix var handled = false lastMove.groupName = "" for g in groupsWithSameColor { for move in g.allMoves { if move.isConnectedTo { if lastMove.groupName == "" { handled = true g.addMove } else { // last move is already in a group, current group needs to be merged into that group let groupToConnect = groupsWithSameColor.filter({$0.name == lastMove.groupName}).first! g.mergeTo(groupToConnect) } break } }// end for move }//end for g if !handled { let groupNew = MoveGroup() groupNew.name = "\(lastMove.type.rawValue)\(lastMove.handNumber)" groupNew.addMove groups.append } // filter out empty groups let liveGroups = groups.filter({$0.allMoves.count > 0 }) let allOccupied = occupiedLocations(allMoves.filter({$0.handNumber <= hand && $0.handOfDead == -1})) // only need to check opposite party let oppositeGroups = liveGroups.filter({!$0.name.hasPrefix for grp in oppositeGroups{ let liberty = grp.calculateLiberty(allOccupied) if liberty == 0 { for move in grp.allMoves { move.isDead = true move.handOfDead = hand } grp.isDead = true } } return liveGroups.filter({!$0.isDead }) }

效果如下:

图片 1screen.jpg

最新的代码已经松开github: :)

本文由betway必威手机版发布于betway必威手机版官网,转载请注明出处:11气的算法改进

关键词: 算法 Swift 围棋

上一篇:必威体育网址:彩票项目,监听滚动导航条渐隐

下一篇:没有了