用go写的五子棋预测算法的实现
程序员文章站
2023-02-17 17:27:10
详细请看 github:https://github.com/shanhuijie/gowatch/tree/master/fiveinarow
five in a row (五...
详细请看 github:https://github.com/shanhuijie/gowatch/tree/master/fiveinarow
five in a row (五子棋成功预测)
从横、纵、 左斜升、 左斜降 四个角度判断
const( matrix = 50*50 point = 3 ) type coordinat struct{ x int y int } type allinat struct{ key []coordinat } func inarray(need coordinat, needarr []coordinat) bool { for _,v := range needarr{ if need == v{ return true } } return false } func inverted(tmp []int) bool { //倒序检查 var i int for k := len(tmp)-1; k>=0;k--{ if k == 0{ //最后一个下标说明无法对比 return false } if tmp[k]-1 == tmp[k]{ //说明值是连续数字 i++ if i == point{ //如果达到连续数就返回 return true } }else{ return false } } return false } func postive(tmp []int) bool { //正序检查 var i int for ck, cv := range tmp { if ck == len(tmp)-1{ //最后一个下标说明无法对比 return false } if cv+1 == tmp[ck+1] { //说明值是连续数字 i++ if i == point{ //如果达到连续数就返回 return true } }else{ return false } } return false } func slope(inat *allinat,coor coordinat) bool { var ( xmax,xmin int = coor.x+4,coor.x-4 ymax,ymin int = coor.y+4,coor.y-4 j,p int lrise,lfall coordinat //tmp []int ) if xmin < 0 { xmin = 0 } if ymin < 0 { ymin = 0 } for i:=xmin; i<=xmax; i++{ xmin = xmin+1 ymin = ymin+1 lrise.x = xmin lrise.y = ymin if inarray(lrise,inat.key) { j++ //fmt.println(lrise,j) if j == point{ return true } } if ymin == ymax { break } } for ii := xmax; ii>=xmin; ii--{ xmax = xmax-1 ymin = ymin+1 lfall.x = xmax lfall.y = ymin if inarray(lfall,inat.key) { p++ //fmt.println(lfall,p) if p == point{ return true } } if ymin == ymax { return false } } return false } func lengthways(inat *allinat,coor coordinat) bool { var ( max,min int = coor.x+4,coor.x-4 tmp []int ) if min < 0 { min = 0 } for _,c := range inat.key{ if (max >= c.x && c.y == coor.y) || (min >= c.x && c.y == coor.y){ tmp = append(tmp,c.x) } } sort.ints(tmp) if (inverted(tmp) == true) || (postive(tmp) == true) { return true } return false } func crosswise(inat *allinat,coor coordinat) bool { var ( max,min int = coor.y+4,coor.y-4 tmp []int ) for _,c := range inat.key{ if (max >= c.y && c.x == coor.x) || (min >= c.y && c.x == coor.x){ tmp = append(tmp,c.y) } } sort.ints(tmp) if (inverted(tmp) == true) || (postive(tmp) == true) { return true } return false } func isfive(inat *allinat,coor coordinat) bool { ok := crosswise(inat,coor) ok2 := lengthways(inat,coor) ok3 := slope(inat,coor) //slope(inat) if ok == true || ok2 == true || ok3 == true{ return true } return false } func (inat *allinat)addcoordinat(coor coordinat){ for _,coslice := range inat.key{ if coslice == coor { return } } c := isfive(inat,coor) fmt.println(c,"*****",coor) if c == false{ //not finish five inat.key = append(inat.key,coor) fmt.println("没有连成") return } fmt.println("连成point颗") return }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Python修改MP3文件的方法