github.com/jefffederman/gophe@v0.0.0-20221203163656-b38beff92772/hand.go (about)

     1  package gophe
     2  
     3  type Hand struct {
     4  	size       uint8
     5  	suitHash   int
     6  	suitBinary [4]int
     7  	quinary    [13]byte
     8  }
     9  
    10  var binariesByID = [52]uint16{
    11  	0x1, 0x1, 0x1, 0x1,
    12  	0x2, 0x2, 0x2, 0x2,
    13  	0x4, 0x4, 0x4, 0x4,
    14  	0x8, 0x8, 0x8, 0x8,
    15  	0x10, 0x10, 0x10, 0x10,
    16  	0x20, 0x20, 0x20, 0x20,
    17  	0x40, 0x40, 0x40, 0x40,
    18  	0x80, 0x80, 0x80, 0x80,
    19  	0x100, 0x100, 0x100, 0x100,
    20  	0x200, 0x200, 0x200, 0x200,
    21  	0x400, 0x400, 0x400, 0x400,
    22  	0x800, 0x800, 0x800, 0x800,
    23  	0x1000, 0x1000, 0x1000, 0x1000,
    24  }
    25  
    26  var suitbitById = [52]uint16{
    27  	0x1, 0x8, 0x40, 0x200,
    28  	0x1, 0x8, 0x40, 0x200,
    29  	0x1, 0x8, 0x40, 0x200,
    30  	0x1, 0x8, 0x40, 0x200,
    31  	0x1, 0x8, 0x40, 0x200,
    32  	0x1, 0x8, 0x40, 0x200,
    33  	0x1, 0x8, 0x40, 0x200,
    34  	0x1, 0x8, 0x40, 0x200,
    35  	0x1, 0x8, 0x40, 0x200,
    36  	0x1, 0x8, 0x40, 0x200,
    37  	0x1, 0x8, 0x40, 0x200,
    38  	0x1, 0x8, 0x40, 0x200,
    39  	0x1, 0x8, 0x40, 0x200,
    40  }
    41  
    42  func (h Hand) Size() uint8 { return h.size }
    43  
    44  func (h Hand) getSuitHash() int { return h.suitHash }
    45  
    46  func (h Hand) getSuitBinary() [4]int { return h.suitBinary }
    47  
    48  func (h Hand) getQuinary() [13]byte { return h.quinary }
    49  
    50  func (h Hand) AddCard(c Card) Hand {
    51  	h.suitHash += int(suitbitById[c])
    52  	h.suitBinary[c&0x3] |= int(binariesByID[c])
    53  	h.quinary[c>>2]++
    54  	h.size++
    55  	return h
    56  }
    57  
    58  func (h *Hand) ModifyHand(cards ...Card) {
    59  	for _, c := range cards {
    60  		*h = (*h).AddCard(c)
    61  	}
    62  }
    63  
    64  func (h Hand) AddCards(cards ...Card) Hand {
    65  	for _, c := range cards {
    66  		h = h.AddCard(c)
    67  	}
    68  	return h
    69  }
    70  
    71  func NewHand(cards ...Card) *Hand {
    72  	h := &Hand{}
    73  	h.ModifyHand(cards...)
    74  	return h
    75  }