关于斗地主算法讨论(二)
斗地主发牌的算法:package main
import (
"container/list"
"fmt"
"math/rand"
"time"
)
type Person struct {
flagint
cards []string
}
func generateRandomNumber(start int, end int, count int) []int {
//范围检查
if end < start || (end-start) < count {
return nil
}
//存放结果的slice
nums := make([]int, 0)
//随机数生成器,加入时间戳保证每次生成的随机数不一样
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for len(nums) < count {
//生成随机数
num := r.Intn((end - start)) + start
//查重
exist := false
for _, v := range nums {
if v == num {
exist = true
break
}
}
if !exist {
nums = append(nums, num)
}
}
return nums
}
func DealCard() (string, string, string, string) {
card := []string{"A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10", "A11", "A12", "A13",
"B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "B10", "B11", "B12", "B13",
"C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10", "C11", "C12", "C13",
"D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10", "D11", "D12", "D13", "K99", "Q66"}
var list []int
var cardlist []string
var r1 string
var r2 string
var r3 string
var r4 string
list = generateRandomNumber(0, 54, 54)
fmt.Println(list)
for j := 0; j < 54; j++ {
//fmt.Println(card])
cardlist = append(cardlist, card])
}
for i1 := range cardlist {
r1 = cardlist
}
for i2 := range cardlist {
r2 = cardlist
}
for i3 := range cardlist {
r3 = cardlist
}
for i4 := range cardlist {
r4 = cardlist
}
return r1, r2, r3, r4
}
func (p *Person) Landlord(s []string) {
p.flag = 1
p.cards = append(p.cards, s...)
}
func (p *Person) PlayHand(r1 []string) {
for _, j := range r1 {
for x, y := range p.cards {
if j == y {
p.cards = append(p.cards[:x], p.cards...)
}
}
}
}
func main() {
r1, r2, r3, r4 := DealCard()
p1 := &Person{0, r1[:]}
p2 := &Person{0, r2[:]}
p3 := &Person{0, r3[:]}
fmt.Println(p1, p2, p3)
//升级为地主
p1.Landlord(r4[:])
fmt.Println(p1, p2, p3, r4)
plist := []Person{*p1, *p2, *p3}
l := list.New()
for _, v := range plist {
l.PushBack(v)
}
card_slice := p1.cards
for e := l.Front(); e != nil; e = e.Next() {
p := e.Value.(Person)
p.PlayHand(card_slice)
break
//fmt.Print(e.Value) //输出list的值,01234
}
fmt.Println(p1, p2, p3, r4)
}
************************************************
[ `go run 9.go` | done: 489ms ]
&{0 } &{0 } &{0 }
&{1 } &{0 } &{0 }
&{1 } &{0 } &{0 } 感觉好复杂 {:10_257:} 这怎么看着像买码啊?
页:
[1]