2019-07-02

package main

import (

"fmt"

"math/rand"

"time"

)

type QiZhistruct {

Namestring

  Typesint

  Showstring

}

func (this *QiZhi)Set(namestring,typesint,showstring){

this.Name = name

this.Types = types

this.Show = show

}

type QiPanstruct {

Q [6][7]*QiZhi

}

func(this *QiPan)Init(){

n :=new(QiZhi)

n.Set("空",0,"*")

this.Q = [6][7]*QiZhi{}

for k,v :=range this.Q {

q := [7]*QiZhi{}

for kk,_ :=range v {

q[kk] = n

}

this.Q[k] = q

}

}

func (this *QiPan)Print() {

for _,v :=range this.Q{

for _,vv :=range v {

fmt.Print(vv.Show)

}

fmt.Print("\n")

}

}

func (this *QiPan)Play(x,yint,q *QiZhi) {

if this.Q[x][y].Types ==0 {

this.Q[x][y] = q

}

}

func (this *QiPan)PlayLast(yint,q *QiZhi)bool{

//棋子掉入该列最后空白处

  for i:=5;i >=0 ;i-- {

if this.Q[i][y].Types ==0 {

this.Q[i][y] = q

return true

      }

}

return false

}

func (this *QiPan)IsFinish()bool{

for _,v :=range this.Q {

for _,vv :=range v {

if vv.Types ==0{

return false

        }

}

}

return true

}

func Select(nint) {

switch n {

case 1:

q :=new(QiPan)

q.Init()

q.Print()

case 2:

q :=new(QiPan)

q.Init()

black := &QiZhi{

Name:"黑子",

        Types:1,

        Show:"X",

      }

white  := &QiZhi{

Name:"白子",

        Types:2,

        Show:"O",

      }

q.Play(0,3,black)

q.Play(3,2,black)

q.Play(2,3,white)

q.Play(3,5,white)

q.Print()

default:

fmt.Println("error type num is ",n)

}

}

func RandomPlay(){

q :=new(QiPan)

q.Init()

black := &QiZhi{

Name:"黑子",

      Types:1,

      Show:"X",

  }

white  := &QiZhi{

Name:"白子",

      Types:2,

      Show:"O",

  }

r := rand.New(rand.NewSource(time.Now().UnixNano()))

y := r.Intn(7)

n :=0

  for {

if q.IsFinish() {

q.Print()

return

      }else{

if n%2 ==0 {

//fmt.Println("下白子",y)

            if !q.PlayLast(y,white){

r = rand.New(rand.NewSource(time.Now().UnixNano()))

y = r.Intn(7)

}

}else{

//fmt.Println("下黑子",y)

            if !q.PlayLast(y,black){

r = rand.New(rand.NewSource(time.Now().UnixNano()))

y = r.Intn(7)

}

}

n ++

}

}

}

func main(){

//第一题

  Select(1)

fmt.Println("_________________________")

Select(2)

fmt.Println("-------------------------")

//第二题

  RandomPlay()

}

你可能感兴趣的:(2019-07-02)