package main
import (
"container/list"
"fmt"
"math/rand"
"time"
)
type Person struct {
flag int
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() ([17]string, [17]string, [17]string, [4]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 [17]string
var r2 [17]string
var r3 [17]string
var r4 [4]string
list = generateRandomNumber(0, 54, 54)
fmt.Println(list)
for j := 0; j < 54; j++ {
//fmt.Println(card[list[j]])
cardlist = append(cardlist, card[list[j]])
}
for i1 := range cardlist[0:16] {
r1[i1] = cardlist[0:16][i1]
}
for i2 := range cardlist[17:33] {
r2[i2] = cardlist[17:33][i2]
}
for i3 := range cardlist[34:50] {
r3[i3] = cardlist[34:50][i3]
}
for i4 := range cardlist[51:54] {
r4[i4] = cardlist[51:54][i4]
}
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[x+1:]...)
}
}
}
}
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[0:3]
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)
}
************************************************