package main
import (
"fmt"
"math/rand"
"strconv"
"strings"
"time"
)
type hand uint
const (
NilHand hand = iota
BlackHand
WhiteHand
)
func (h hand) Str() string {
switch h {
case NilHand:
return "."
case BlackHand:
return "X"
case WhiteHand:
return "O"
default:
return "."
}
}
type Grid struct {
value hand
left *Grid
right *Grid
top *Grid
bottom *Grid
}
func (g *Grid) LeftTop() *Grid {
if g.left != nil {
return g.left.top
}
return nil
}
func (g *Grid) LeftBottom() *Grid {
if g.left != nil {
return g.left.bottom
}
return nil
}
func (g *Grid) RightTop() *Grid {
if g.right != nil {
return g.right.top
}
return nil
}
func (g *Grid) RightBottom() *Grid {
if g.right != nil {
return g.right.bottom
}
return nil
}
func (g *Grid) Set(row, col int, value hand) {
offset := g
offset = g.Offset(row, col)
if offset == nil {
return
} else if offset.value == NilHand {
offset.value = value
}
return
}
func (g *Grid) RightOffset(col int) *Grid {
var tmp *Grid
tmp = g
if g == nil {
return nil
}
for i := 1; i <= col; i++ {
if i == col && tmp != nil {
return tmp
}
if tmp == nil {
return nil
}
if tmp.right != nil {
tmp = tmp.right
}
}
return nil
}
func (g *Grid) BottomOffset(row int) *Grid {
var tmp *Grid
tmp = g
if g == nil {
return nil
}
for i := 1; i <= row; i++ {
if i == row && tmp != nil {
return tmp
}
if tmp == nil {
return nil
}
if tmp.bottom != nil {
tmp = tmp.bottom
}
}
return nil
}
func (g *Grid) GetRowLen() int {
var row int
tmp := g
for tmp != nil {
row++
tmp = tmp.bottom
}
return row
}
func (g *Grid) GetColLen() int {
var col int
tmp := g
for tmp != nil {
col++
tmp = tmp.right
}
return col
}
func (g *Grid) Print() {
fmt.Println("当前棋盘布局为:")
var colNumStr = ""
col := g.GetColLen()
fillC := strconv.Itoa(g.GetColLen())
for i := 1; i <= col; i++ {
colNumStr += " " + StrLeftFill(len(fillC), i)
}
fmt.Println(StrLeftFill(len(strconv.Itoa(g.GetRowLen())), ""), strings.TrimLeft(colNumStr, " "))
for row := 1; row <= g.GetRowLen(); row++ {
var rowStr = ""
for col := 1; col <= g.GetColLen(); col++ {
rowStr += " " + StrLeftFill(len(strconv.Itoa(g.GetColLen())), g.Offset(row, col).value.Str())
}
fmt.Println(StrLeftFill(len(strconv.Itoa(g.GetRowLen())), row), strings.TrimLeft(rowStr, " "))
}
fmt.Println("")
}
func (g *Grid) Offset(row, col int) *Grid {
offset := g
if g == nil {
return nil
}
var i, j int
i, j = 1, 1
for i <= row {
if row == i {
for j <= col {
if col == j {
return offset
}
if offset == nil {
return nil
}
offset = offset.right
j++
}
}
if offset == nil {
return nil
}
offset = offset.bottom
i++
}
return nil
}
func (g *Grid) GetLastRow(col int) *Grid {
offset := g.Offset(1, col)
for row := 1; row <= g.GetRowLen(); row++ {
if row == g.GetRowLen() {
return offset
}
offset = offset.bottom
}
return nil
}
func (g *Grid) GetEmptyLastRow(col int) *Grid {
if col <= 0 || col > g.GetColLen() {
return nil
}
last := g.GetLastRow(col)
if last == nil {
return nil
}
for row := 1; row <= g.GetRowLen(); row++ {
if last.value == NilHand {
return last
}
last = last.top
if last == nil {
return nil
}
}
return nil
}
func (g *Grid) IsFull() bool {
for row := 1; row <= g.GetRowLen(); row++ {
for col := 1; col <= g.GetColLen(); col++ {
if g.Offset(row, col).value == NilHand {
return false
}
}
}
return true
}
func (g *Grid) Count() (black, white int) {
for row := 1; row <= g.GetRowLen(); row++ {
for col := 1; col <= g.GetColLen(); col++ {
if g.Offset(row, col).value == BlackHand {
black++
} else if g.Offset(row, col).value == WhiteHand {
white++
}
}
}
return
}
func (g *Grid) IsWin(row, col int) bool {
offset := g.Offset(row, col)
h := offset.value
if h == NilHand {
return false
}
count := 1
left := offset.left
right := offset.right
for {
if left == nil {
break
}
if left.value == h {
count++
} else {
break
}
left = left.left
}
for {
if right == nil {
break
}
if right.value == h {
count++
} else {
break
}
right = right.right
}
if count >= 4 {
switch h {
case WhiteHand:
fmt.Println("白手赢", fmt.Sprintf("最后一个落子点为(row:%d,col:%d)", row, col))
case BlackHand:
fmt.Println("黑手赢", fmt.Sprintf("最后一个落子点为(row:%d,col:%d)", row, col))
}
return true
}
count = 1
top := offset.top
bottom := offset.bottom
for {
if top == nil {
break
}
if top.value == h {
count++
} else {
break
}
top = top.top
}
for {
if bottom == nil {
break
}
if bottom.value == h {
count++
} else {
break
}
bottom = bottom.bottom
}
if count >= 4 {
switch h {
case WhiteHand:
fmt.Println("白手赢", fmt.Sprintf("最后一个落子点为(row:%d,col:%d)", row, col))
case BlackHand:
fmt.Println("黑手赢", fmt.Sprintf("最后一个落子点为(row:%d,col:%d)", row, col))
}
return true
}
count = 1
leftTop := offset.LeftTop()
rightBottom := offset.RightBottom()
for {
if leftTop == nil {
break
}
if leftTop.value == h {
count++
} else {
break
}
leftTop = leftTop.LeftTop()
}
for {
if rightBottom == nil {
break
}
if rightBottom.value == h {
count++
} else {
break
}
rightBottom = rightBottom.RightBottom()
}
if count >= 4 {
switch h {
case WhiteHand:
fmt.Println("白手赢", fmt.Sprintf("最后一个落子点为(row:%d,col:%d)", row, col))
case BlackHand:
fmt.Println("黑手赢", fmt.Sprintf("最后一个落子点为(row:%d,col:%d)", row, col))
}
return true
}
count = 1
rightTop := offset.RightTop()
leftBottom := offset.LeftBottom()
for {
if rightTop == nil {
break
}
if rightTop.value == h {
count++
} else {
break
}
rightTop = rightTop.RightTop()
}
for {
if leftBottom == nil {
break
}
if leftBottom.value == h {
count++
} else {
break
}
leftBottom = leftBottom.LeftBottom()
}
if count >= 4 {
switch h {
case WhiteHand:
fmt.Println("白手赢", fmt.Sprintf("最后一个落子点为(row:%d,col:%d)", row, col))
case BlackHand:
fmt.Println("黑手赢", fmt.Sprintf("最后一个落子点为(row:%d,col:%d)", row, col))
}
return true
}
if g.IsFull() {
fmt.Println("平手")
return true
} else {
return false
}
}
func InitGrid(row, col int, head *Grid) *Grid {
l := &Grid{}
l = head
c := col
for c > 1 {
var tmp Grid
tmp.left = l
l.right = &tmp
l = &tmp
c--
}
l = head
r := row
for r > 1 {
var tmp Grid
tmp.top = l
l.bottom = &tmp
l = &tmp
r--
}
for i := 2; i <= row; i++ {
for j := 2; j <= col; j++ {
top := head.Offset(i-1, j)
left := head.Offset(i, j-1)
var tmp Grid
tmp.top = top
tmp.left = left
top.bottom = &tmp
left.right = &tmp
}
}
return head
}
func TestGrid(row, col int) {
grid := InitGrid(row, col, &Grid{})
grid.Set(1, 4, BlackHand)
grid.Set(4, 3, BlackHand)
grid.Set(3, 4, WhiteHand)
grid.Set(4, 6, WhiteHand)
grid.Print()
}
func GameOne(row, col int) {
grid := InitGrid(row, col, &Grid{})
rand.Seed(time.Now().Unix())
i := 0
for {
if grid.IsFull() {
black, white := grid.Count()
if black > white {
fmt.Println("黑手胜", fmt.Sprintf("黑手:%d;白手:%d", black, white))
} else if black < white {
fmt.Println("白手胜", fmt.Sprintf("黑手:%d;白手:%d", black, white))
} else {
fmt.Println("平手", fmt.Sprintf("黑手:%d;白手:%d", black, white))
}
grid.Print()
break
}
h := grid.GetEmptyLastRow(rand.Intn(grid.GetColLen()) + 1)
if h == nil {
} else if i%2 == 0 {
h.value = BlackHand
} else {
h.value = WhiteHand
}
i++
}
}
type XY struct {
row int
col int
}
func GameTwo(row, col int) {
grid := InitGrid(row, col, &Grid{})
xy := map[int]XY{}
var loop int
for r := 1; r <= row; r++ {
for c := 1; c <= col; c++ {
xy[loop] = XY{row: r, col: c}
loop++
}
}
rand.Seed(time.Now().Unix())
p := XY{1, 1}
i := 0
for {
if grid.IsWin(p.row, p.col) {
grid.Print()
break
}
for {
if v, ok := xy[rand.Intn(loop+1)]; ok {
p = v
delete(xy, loop)
break
}
}
if i%2 == 0 {
grid.Set(p.row, p.col, BlackHand)
} else {
grid.Set(p.row, p.col, WhiteHand)
}
i++
}
}
func StrLeftFill(s int, value interface{}) string {
var format = ""
format = "%" + strconv.Itoa(s) + "v"
return fmt.Sprintf(format, value)
}
func main() {
grid := InitGrid(6, 7, &Grid{})
grid.Print()
TestGrid(6, 7)
GameOne(6, 7)
GameTwo(20, 20)
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现