add libpike and some main testing

This commit is contained in:
2025-11-08 09:19:14 -05:00
parent e6ed3ed5d8
commit 10edb85219
4 changed files with 260 additions and 1 deletions

40
libpike/log/log.odin Normal file
View File

@@ -0,0 +1,40 @@
// SPDX-License-Identifer: MIT
/* log.odin
*
* Routines for logging
*
* Written by vx-clutch
*/
package log
import "core:fmt"
import "core:time"
import "../util"
RESET :: "\x1B[0m"
TIME :: "\x1B[0;32m"
LOCATION :: "\x1B[0;33m"
current_time :: proc() -> string {
t := time.now()
year, month, day := time.date(t)
hour, minute, second := time.clock(t)
return fmt.tprintf("%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second)
}
printl :: proc(msg: string, args: ..any, here := #caller_location) {
buf := current_time()
fmt.printf(
"%s[%12s]%s: %s:%d (%s)%s: ",
TIME,
buf,
LOCATION,
util.basename(here.file_path),
here.line,
here.procedure,
RESET,
)
fmt.printfln(msg, ..args)
}

199
libpike/poker/poker.odin Normal file
View File

@@ -0,0 +1,199 @@
// SPDX-License-Identifer: MIT
/* poker.odin
*
* Routines for handling poker related features
*
* Written by vx-clutch
*/
package poker
import "core:fmt"
import "core:math/rand"
import "../log"
/*
Type represeting a suit
*/
Suit :: enum {
CRUBS,
DIAMONDS,
HEARTS,
SPADES,
}
/*
Type represeting a hand's rank
*/
HandRank :: enum {
HIGH,
PAIR,
TWOPAIR,
THREE,
STRAIGHT,
FLUSH,
HOUSE,
FOUR,
SFLUSH,
ROYAL,
}
/*
Type represeting a card
*/
Card :: struct {
rank: int,
suit: Suit,
}
/*
Type represeting a poker hand
*/
Hand :: struct {
_: [5]Card,
}
/*
Type represeting a deck of cards
*/
Deck :: struct {
cards: [52]Card,
top_index: int,
}
/*
Creates a well-ordered deck
*/
create_deck :: proc() -> Deck {
deck: Deck
deck.top_index = 0
for suit in Suit {
for rank in 1 ..= 13 {
deck.cards[deck.top_index] = Card{rank, suit}
deck.top_index += 1
}
}
deck.top_index = 0
return deck
}
CRUB :: "♣"
DIAMOND :: "♦"
HEART :: "♥️"
SPADE :: "♠"
/*
Pretty print a card
*/
print_card :: proc(card: Card) {
switch card.suit {
case .CRUBS:
switch card.rank {
case 1:
fmt.printf("A%s", CRUB)
case 11:
fmt.printf("J%s", CRUB)
case 12:
fmt.printf("Q%s", CRUB)
case 13:
fmt.printf("K%s", CRUB)
case:
fmt.printf("%d%s", card.rank, CRUB)
}
case .DIAMONDS:
switch card.rank {
case 1:
fmt.printf("A%s", DIAMOND)
case 11:
fmt.printf("J%s", DIAMOND)
case 12:
fmt.printf("Q%s", DIAMOND)
case 13:
fmt.printf("K%s", DIAMOND)
case:
fmt.printf("%d%s", card.rank, DIAMOND)
}
case .HEARTS:
switch card.rank {
case 1:
fmt.printf("A%s", HEART)
case 11:
fmt.printf("J%s", HEART)
case 12:
fmt.printf("Q%s", HEART)
case 13:
fmt.printf("K%s", HEART)
case:
fmt.printf("%d%s", card.rank, HEART)
}
case .SPADES:
switch card.rank {
case 1:
fmt.printf("A%s", SPADE)
case 11:
fmt.printf("J%s", SPADE)
case 12:
fmt.printf("Q%s", SPADE)
case 13:
fmt.printf("K%s", SPADE)
case:
fmt.printf("%d%s", card.rank, SPADE)
}
}
}
/*
Pretty print out the deck
*/
print_deck :: proc(deck: Deck) {
i := 0
for card in deck.cards {
if i >= 13 {
fmt.println("")
i = 0
}
print_card(card)
fmt.print(" ")
i += 1
}
fmt.println("")
}
/*
Shuffles the deck
*/
shuffle :: proc(deck: ^Deck) {
rand.shuffle(deck^.cards[:])
}
/*
Deal one card from the top of the deck
*/
deal_card :: proc(deck: ^Deck) -> Card {
return deck^.cards[deck^.top_index]
}
/*
Deal a hand of size n
*/
deal_hand :: proc(d: Deck, n: int) -> Card {
log.printl("%s is NOT implemented", #procedure)
return Card{}
}
/*
Determine the rank of the hand
*/
evaluate_hand :: proc(hand: Hand) -> HandRank {
log.printl("%s is NOT implemented", #procedure)
return HandRank{}
}
/*
Compare two hands and return true if the
first one is better than the second one
*/
compare_hands :: proc(this: Hand, other: Hand) -> bool {
log.printl("%s is NOT implemented", #procedure)
return true
}

11
libpike/util/util.odin Normal file
View File

@@ -0,0 +1,11 @@
package util
import "core:strings"
basename :: proc(path: string) -> string {
idx := strings.last_index_byte(path, '/')
if idx < 0 {
return path
}
return path[idx+1:]
}

View File

@@ -15,6 +15,15 @@ package main
import "core:fmt" import "core:fmt"
import "libpike/log"
import "libpike/poker"
main :: proc() { main :: proc() {
fmt.println("pike init") log.printl("pike init")
deck := poker.create_deck()
// poker.shuffle(&deck)
poker.print_deck(deck)
} }