package main//必须有个main包
import (
"fmt"
"math/rand"
"time"
)
type Qipanstruct {
}
func main() {
var a [6][7]string
var player1byte =97
var player2byte =98
var qpQipan
//初始化数组
(&qp).initQipan(&a)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
//轮训数组是否被填满
label:
for {
var kint =0
//玩家轮流放入棋子
(&qp).putQiZi(r.Intn(7), &a, player1)
(&qp).putQiZi(r.Intn(7), &a, player2)
//每次遍历都查找数组中的 " 0" 如果没有 " 0" 就退出for 循环 打印数组
for i :=0; i <6; i++ {
for j :=0; j <7; j++ {
if a[i][j] ==" 0" {
k++
}
}
}
//检查 k的值也就是 " 0"的个数
if k ==0 {
break label
}
}
// 打印数组
(&qp).printQipan(&a)
}
//初始化数组
func (p *Qipan)initQipan(a *[6][7]string) {
for i :=0; i <6; i++ {
for j :=0; j <7; j++ {
a[i][j] =" 0"
}
}
}
//打印数组
func (p *Qipan)printQipan(a *[6][7]string) {
for i :=0; i <6; i++ {
for j :=0; j <7; j++ {
fmt.Printf(a[i][j])
}
fmt.Println("")
}
}
//玩家根据空白位置坐标摆放其子
func (p *Qipan)putQiZi(columnint, a *[6][7]string, playerbyte) {
for i :=5; i >=0; i-- {//外层遍历行
//内层固定列x
//在列上寻找空白的位置 从最低层开始
if a[i][column] ==" 0" {
if player ==97 {
a[i][column] =" o"
break
}
if player ==98 {
a[i][column] =" x"
break
}
}
}
}
网友评论