Golang makes a Doudizhu game server [6]: playing cards and cards

Finally arrived the most important (in fact, the least important) card game.

The logic of playing cards is actually very simple

After the landlord is selected, the landlord draws the cards

 

Determine whether the hand card is consumed ->

-> Draw -> Someone is following 

              -> No one is following the card -> The cycle will reach you -> The end of the round

-> Game over


// playcard 出牌阶段
func (self *TDDZ) playcard() {
	// 检查是否还有手牌
	if len(self.pHandCardList[self.nTurnPosition-1]) == 0 {
		log.Println("手牌打完, 游戏结束")
		return
	}
	if self.nCardType == 0 {
		// 1. 如果当前轮没有类型, 表示才刚开始出牌, 可以随机选一张牌型来出, 其他人只能跟着这个牌型继续打, 除非使用炸弹
		self.roundStart()
	} else if self.nLargePosition == self.nTurnPosition {
		// 2. 如果最大的牌的位置. 和当前要出牌的人的位置一样. 表示他出的牌过了一轮没有人要的起, 那么下一轮开始
		self.roundEnd()
	} else {
		// 3. 跟牌
		self.roundFollow()
	}

	// 继续打牌
	self.playcard()
}

The process is roughly the above process. Because there is no client, the logic is to automatically issue a ticket when timeout and automatically check when timeout

The logic is relatively simple. Because it involves client operations. So don’t write anything for now

 

Get temporary logic to deal with it

// 回合开始
func (self *TDDZ) roundStart() {
	log.Println("回合开始, 准备打牌")
	//1. 新人打牌, 打第一张
	ch := make(chan int, 1)
	self.chPlayDZ = ch

	select {
	case <-ch:
		log.Println("模拟打牌")
	case <-time.After(time.Second * 1): // 假设5秒超时
		log.Println("roundStart 超时")
	}

	// 默认当做单牌
	// 要找到一个能匹配上的牌组
	log.Println("打出前")
	self.pHandCardList[self.nTurnPosition-1].print()

	pOutCard, pLeftCard := self.pHandCardList[self.nTurnPosition-1].ai(cardTypeSINGLE, cardPointNIL)

	log.Println("位置", self.nTurnPosition, "打出牌:")
	pOutCard.print()

	// 选完以后重新弄上手牌
	self.pHandCardList[self.nTurnPosition-1] = make(TCards, len(pLeftCard))
	copy(self.pHandCardList[self.nTurnPosition-1], pLeftCard)
	pLeftCard.print()

	self.pHandCardList[self.nTurnPosition-1].print()

	//
	self.nLargePosition = self.nTurnPosition
	self.nTurnPosition++ // 当前轮到的人的
	if self.nTurnPosition > 3 {
		self.nTurnPosition -= 3
	}
	self.nHand++ // 手次

	self.nCardType = cardTypeSINGLE      // 当前出牌牌型
	self.nCardPoint = pOutCard[0].nPoint // 当前出牌点数

}

// 下家跟拍
func (self *TDDZ) roundFollow() {
	ch := make(chan int, 1)
	self.chPlayDZ = ch

	select {
	case <-ch:
		log.Println("模拟打牌")
	case <-time.After(time.Second * 1): // 假设5秒超时
		log.Println("roundFollow 超时")
	}

	// 这里超时是不出的
	self.nTurnPosition++ // 当前轮到的人的
	if self.nTurnPosition > 3 {
		self.nTurnPosition -= 3
	}
	self.nHand++ // 手次

	log.Println("当前位置变成了他", self.nTurnPosition)

	// 这两个保持不变
}

// 下一轮
func (self *TDDZ) roundEnd() {
	self.nHand = 0
	self.nCardType = cardTypeNIL
	self.nCardPoint = cardPointNIL
}

 

Written here. The game of Doudizhu has basically come to an end for the time being. It is difficult to continue writing without a client, and it is still necessary to simply build the server. The next section may need to talk about part of the logical construction work.

 

Guess you like

Origin blog.csdn.net/warrially/article/details/88645411