Search Apps Documentation Source Content File Folder Download Copy Actions Download

types.gno

3.87 Kb · 188 lines
  1package poker
  2
  3// Address is a string alias for wallet addresses
  4type Address = string
  5
  6// Card represents a playing card
  7type Card struct {
  8	Suit  int // 0=Spades, 1=Hearts, 2=Diamonds, 3=Clubs
  9	Value int // 2-14 (14=Ace)
 10}
 11
 12// Player represents a player at the table
 13type Player struct {
 14	Address    Address
 15	Hand       [2]Card
 16	Chips      int64
 17	CurrentBet int64
 18	HasFolded  bool
 19	IsAllIn    bool
 20	IsActive   bool
 21	SeatIndex  int
 22}
 23
 24// GameState represents the current phase of a hand
 25type GameState int
 26
 27const (
 28	StateWaiting  GameState = 0 // Waiting for players
 29	StatePreFlop  GameState = 1 // Hole cards dealt, pre-flop betting
 30	StateFlop     GameState = 2 // Flop dealt (3 community cards)
 31	StateTurn     GameState = 3 // Turn dealt (4th community card)
 32	StateRiver    GameState = 4 // River dealt (5th community card)
 33	StateShowdown GameState = 5 // All betting complete, determine winner
 34)
 35
 36func (gs GameState) String() string {
 37	switch gs {
 38	case StateWaiting:
 39		return "Waiting"
 40	case StatePreFlop:
 41		return "Pre-Flop"
 42	case StateFlop:
 43		return "Flop"
 44	case StateTurn:
 45		return "Turn"
 46	case StateRiver:
 47		return "River"
 48	case StateShowdown:
 49		return "Showdown"
 50	default:
 51		return "Unknown"
 52	}
 53}
 54
 55// Table represents a poker table
 56type Table struct {
 57	ID            string
 58	Name          string
 59	Creator       Address
 60	Players       []*Player
 61	MaxPlayers    int   // 4 or 6
 62	SmallBlind    int64 // in ugnot (e.g., 2500000 = 2.5 GNOT)
 63	BigBlind      int64 // in ugnot (e.g., 5000000 = 5 GNOT)
 64	MinBet        int64 // minimum raise amount
 65	MaxBet        int64 // maximum single bet
 66	MinBuyIn      int64 // minimum chips to join
 67	MaxBuyIn      int64 // maximum chips to join
 68	Pot           int64
 69	Community     []Card
 70	State         GameState
 71	DealerIdx     int    // Dealer button position
 72	CurrentTurn   int    // Index of player whose turn it is
 73	DeckSeed      int64  // Seed for deck shuffle
 74	HandCount     int    // Number of hands played
 75	RakeCollected int64  // Total rake collected at this table
 76}
 77
 78// HandRank represents the rank of a poker hand
 79type HandRank int
 80
 81const (
 82	HighCard      HandRank = 0
 83	OnePair       HandRank = 1
 84	TwoPair       HandRank = 2
 85	ThreeOfAKind  HandRank = 3
 86	Straight      HandRank = 4
 87	Flush         HandRank = 5
 88	FullHouse     HandRank = 6
 89	FourOfAKind   HandRank = 7
 90	StraightFlush HandRank = 8
 91	RoyalFlush    HandRank = 9
 92)
 93
 94func (hr HandRank) String() string {
 95	switch hr {
 96	case HighCard:
 97		return "High Card"
 98	case OnePair:
 99		return "One Pair"
100	case TwoPair:
101		return "Two Pair"
102	case ThreeOfAKind:
103		return "Three of a Kind"
104	case Straight:
105		return "Straight"
106	case Flush:
107		return "Flush"
108	case FullHouse:
109		return "Full House"
110	case FourOfAKind:
111		return "Four of a Kind"
112	case StraightFlush:
113		return "Straight Flush"
114	case RoyalFlush:
115		return "Royal Flush"
116	default:
117		return "Unknown"
118	}
119}
120
121// HandResult stores the evaluated hand result for comparison
122type HandResult struct {
123	Rank      HandRank
124	TieBreak  [5]int // Values for breaking ties, highest first
125	BestCards [5]Card
126}
127
128// SuitName returns the name of a suit
129func SuitName(suit int) string {
130	switch suit {
131	case 0:
132		return "♠"
133	case 1:
134		return "♥"
135	case 2:
136		return "♦"
137	case 3:
138		return "♣"
139	default:
140		return "?"
141	}
142}
143
144// ValueName returns the display name of a card value
145func ValueName(value int) string {
146	switch value {
147	case 11:
148		return "J"
149	case 12:
150		return "Q"
151	case 13:
152		return "K"
153	case 14:
154		return "A"
155	default:
156		if value >= 2 && value <= 10 {
157			return IntToStr(value)
158		}
159		return "?"
160	}
161}
162
163// CardString returns a human-readable string for a card
164func CardString(c Card) string {
165	return ValueName(c.Value) + SuitName(c.Suit)
166}
167
168// IntToStr converts an integer to a string
169func IntToStr(n int) string {
170	if n == 0 {
171		return "0"
172	}
173	s := ""
174	neg := false
175	if n < 0 {
176		neg = true
177		n = -n
178	}
179	for n > 0 {
180		digit := n % 10
181		s = string(rune('0'+digit)) + s
182		n /= 10
183	}
184	if neg {
185		s = "-" + s
186	}
187	return s
188}