项目作者: dolotech

项目描述 :
Go实现斗地主核心算法
高级语言: Go
项目地址: git://github.com/dolotech/doudizhu.git
创建时间: 2018-03-19T09:37:13Z
项目社区:https://github.com/dolotech/doudizhu

开源协议:Apache License 2.0

下载


Go版本实现斗地主核心算法

紧凑的数据结构高效核心算法

  1. type AnalyseCards uint64
  2. func (this *AnalyseCards) Set(cards []byte) {
  3. *this = 0
  4. l := uint8(len(cards))
  5. for i := uint8(0); i < l; i++ {
  6. card := (GetValue(cards[i]) - 3) * 3
  7. count := ((*this >> card) & 0x07) + 1
  8. *this &= (^(0x07 << card))
  9. *this |= (count << card)
  10. }
  11. this.SetStartEnd()
  12. }
  13. func (this *AnalyseCards) SetSortCards(cards []byte) {
  14. *this = 0
  15. l := uint8(len(cards))
  16. for i := uint8(0); i < l; i++ {
  17. card := (cards[i] - 3) * 3
  18. count := ((*this >> card) & 0x07) + 1
  19. *this &= (^(0x07 << card))
  20. *this |= (count << card)
  21. }
  22. this.SetStartEnd()
  23. }
  24. func (this *AnalyseCards) Get(card byte) uint8 {
  25. return uint8((*this >> ((card - 3) * 3 )) & 0x07)
  26. }
  27. func (this *AnalyseCards) SetStartEnd() {
  28. var start, end uint8
  29. for i := uint8(THREE); i <= DA_WANG; i++ {
  30. if start == 0 && this.Get(i) > 0 {
  31. start = i
  32. }
  33. if end == 0 && this.Get(DA_WANG+THREE-i) > 0 {
  34. end = DA_WANG + THREE - i
  35. }
  36. if start > 0 && end > 0 {
  37. break
  38. }
  39. }
  40. card := uint8(46)
  41. *this &= (^(0xF << card))
  42. *this |= (AnalyseCards(start-3) << card)
  43. card = uint8(50)
  44. *this &= (^(0xF << card))
  45. *this |= (AnalyseCards(end-3) << card)
  46. }
  47. func (this *AnalyseCards) Reset() {
  48. this.SetValue(0)
  49. this.SetKind(0)
  50. this.SetStartEnd()
  51. }
  52. func (this *AnalyseCards) End() uint8 {
  53. return byte((*this>>50 )&0xF) + 3
  54. }
  55. func (this *AnalyseCards) Start() uint8 {
  56. return byte((*this>>46 )&0xF) + 3
  57. }
  58. func (this *AnalyseCards) GetValue() byte {
  59. return byte((*this >> 59 ) & 0x1F)
  60. }
  61. func (this *AnalyseCards) SetValue(value byte) {
  62. card := uint8(59)
  63. *this &= (^(0x1F << card))
  64. *this |= (AnalyseCards(value) << card)
  65. }
  66. func (this *AnalyseCards) GetKind() uint8 {
  67. return byte((*this >> 54 ) & 0x1F)
  68. }
  69. func (this *AnalyseCards) SetKind(kind uint8) {
  70. card := uint8(54)
  71. *this &= (^(0x1F << card))
  72. *this |= (AnalyseCards(kind) << card)
  73. }
  74. func (this *AnalyseCards) GetWeight() uint8 {
  75. return Weights[this.GetKind()]
  76. }
  77. func (this *AnalyseCards) ColorRecover(handcards []byte) []byte {
  78. analyseCards := *this
  79. cards := make([]byte, 0, 10)
  80. for _, v := range handcards {
  81. if analyseCards > 0 {
  82. card := GetValue(v)
  83. if analyseCards.Get(card) > 0 {
  84. cards = append(cards, v)
  85. analyseCards.Incr(card, -1)
  86. }
  87. }
  88. }
  89. return cards
  90. }
  91. func (this *AnalyseCards) Flat() []byte {
  92. cards := make([]byte, 0, this.Len())
  93. start := this.Start()
  94. end := this.End()
  95. for i := start; i <= end; i++ {
  96. count := this.Get(i)
  97. if count > 0 {
  98. for j := uint8(0); j < count; j++ {
  99. cards = append(cards, i)
  100. }
  101. }
  102. }
  103. return cards
  104. }
  105. func (this *AnalyseCards) Len() uint8 {
  106. var count uint8
  107. start := this.Start()
  108. end := this.End()
  109. for i := start; i <= end; i++ {
  110. count += this.Get(i)
  111. }
  112. return count
  113. }
  114. func (this *AnalyseCards) Sub(cards AnalyseCards) AnalyseCards {
  115. t := *this
  116. start := cards.Start()
  117. end := cards.End()
  118. for i := start; i <= end; i++ {
  119. count := cards.Get(i)
  120. if count > 0 {
  121. t.Incr(i, -int8(count))
  122. }
  123. }
  124. t.SetValue(0)
  125. t.SetKind(0)
  126. return t
  127. }
  128. func (this *AnalyseCards) Add(cards AnalyseCards) AnalyseCards {
  129. t := *this
  130. start := cards.Start()
  131. end := cards.End()
  132. for i := start; i <= end; i++ {
  133. count := cards.Get(i)
  134. if count > 0 {
  135. t.Incr(i, int8(count))
  136. }
  137. }
  138. t.SetValue(0)
  139. t.SetKind(0)
  140. return t
  141. }
  142. func (this *AnalyseCards) Incr(card byte, num int8) {
  143. card = (card - 3) * 3
  144. count1 := ((*this >> card) & 0x07)
  145. count := int64(count1) + int64(num)
  146. *this &= (^(0x07 << card))
  147. if count > 0 {
  148. *this |= ((AnalyseCards(count & 0x07) ) << card)
  149. }
  150. this.SetStartEnd()
  151. }
  152. func (this *AnalyseCards) Padding(count uint8, length uint8, card byte) {
  153. for m := uint8(0); m < count; m++ {
  154. c := card - m
  155. this.Incr(c, int8(length))
  156. }
  157. this.SetValue(0)
  158. this.SetKind(0)
  159. }