Home >Backend Development >Golang >How to implement the Niu Niu game using Go language
Go language is an open source, simple and efficient programming language. Its emergence solves the performance problems of traditional programming languages. In recent years, with the continuous development and popularization of Internet technology, the Go language has received more and more attention and has become one of the popular programming languages. This article will introduce how to use Go language to implement the Niu Niu game.
Niu Niu game is a popular poker game, also known as the "Bullfighting" game. The rules of the game are to combine 5 playing cards to calculate the size of the cow. The size of the cow is determined by 5 cards. Calculated by the sum of the points of poker cards. If the sum of the points is a multiple of 10, it is called "Niu Niu", which is the largest card type.
First, we need to define the data structure of the poker deck. The code is as follows:
type Card struct { num int // 扑克牌点数 typ int // 扑克牌花色 } type Deck struct { cards []*Card // 每个牌组的扑克牌 }
Next, what we need to implement is the shuffling method, using the rand package to implement random shuffling. The code is as follows:
func (d *Deck) Shuffle() { for i := range d.cards { j := rand.Intn(i + 1) d.cards[i], d.cards[j] = d.cards[j], d.cards[i] } }
The next step is how to calculate the size of the cow. We first define an enumeration type to represent the card type:
type Cow int const ( Cow0 Cow = iota // 无牛 Cow1 Cow = iota // 牛1 Cow2 Cow = iota // 牛2 Cow3 Cow = iota // 牛3 Cow4 Cow = iota // 牛4 Cow5 Cow = iota // 牛5 Cow6 Cow = iota // 牛6 Cow7 Cow = iota // 牛7 Cow8 Cow = iota // 牛8 Cow9 Cow = iota // 牛9 CowCow Cow = iota // 牛牛 )
The method for calculating the size of the cow is as follows:
func calcCow(cards []*Card) Cow { // 先将所有牌从小到大排序 sort.Slice(cards, func(i, j int) bool { return cards[i].num < cards[j].num }) for i := 0; i < 3; i++ { for j := i + 1; j < 4; j++ { for k := j + 1; k < 5; k++ { sum := cards[i].num + cards[j].num + cards[k].num if sum%10 == 0 { // 如果剩下两张牌的点数之和也为10的倍数,则是牛牛 if (cards[0].num+cards[1].num+cards[2].num+cards[3].num+cards[4].num-sum)%10 == 0 { return CowCow } // 否则计算剩下的两张牌点数之和 s := (cards[0].num + cards[1].num + cards[2].num + cards[3].num + cards[4].num - sum) % 10 if s == 0 { return CowCow } else { return Cow(s) } } } } } // 如果没有牛,则返回无牛 return Cow0 }
Finally, we need to implement the game process, including dealing cards, calculating the size of the cow, comparing the size of the cards, etc. The complete code is as follows:
func main() { // 初始化一副牌 deck := &Deck{} for i := 1; i <= 13; i++ { for j := 1; j <= 4; j++ { deck.cards = append(deck.cards, &Card{num: i, typ: j}) } } // 洗牌 deck.Shuffle() // 发牌 player1 := deck.cards[0:5] player2 := deck.cards[5:10] // 计算牛的大小 cow1 := calcCow(player1) cow2 := calcCow(player2) // 比较牌的大小 if cow1 > cow2 { fmt.Println("player1 wins") } else if cow1 < cow2 { fmt.Println("player2 wins") } else { fmt.Println("draw") } }
Summary:
This article introduces How to use Go language to implement the Niu Niu game. By implementing the Cow Cow game, we learned how to define data structures, use random numbers to shuffle cards, calculate the size of the cow, compare the sizes of cards, etc. These are skills commonly used in actual development of Go language.
The above is the detailed content of How to implement the Niu Niu game using Go language. For more information, please follow other related articles on the PHP Chinese website!