Files
se-poker/main.go
T
2025-12-22 16:45:48 +01:00

120 lines
2.3 KiB
Go

package main
import (
"bufio"
"fmt"
"os"
"strings"
)
// --- Testing ---
func parseFile(path string) {
file, _ := os.Open(path)
if file == nil {
fmt.Println("Could not open file:", path)
return
}
defer file.Close()
fscanner := bufio.NewScanner(file)
for fscanner.Scan() {
var input = fscanner.Text()
input = strings.TrimSpace(input)
if len(input) == 0 {
continue
}
if strings.HasPrefix(input, "//") {
input = strings.Trim(input, "/ ")
fmt.Printf("\n=== %s ===\n", input)
continue
}
input = strings.Trim(input, "\",")
hand, err := NewHand(input)
if err != nil {
fmt.Println("Error:", err)
continue
}
result := hand.Evaluate()
fmt.Printf("Input: [%s]\nResult: %s\n", input, handNames[result.Category])
}
}
// Reads all hands and compares them against each other
func compareFileAllVsAll(path string) {
file, err := os.Open(path)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
type SavedHand struct {
Raw string
Hand *Hand
}
var allHands []SavedHand
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if len(line) == 0 || strings.HasPrefix(line, "//") {
continue
}
cleanLine := strings.Trim(line, "\",")
hand, err := NewHand(cleanLine)
if err != nil {
continue
}
allHands = append(allHands, SavedHand{Raw: cleanLine, Hand: hand})
}
count := 0
for i := 0; i < len(allHands); i++ {
for j := i; j < len(allHands); j++ {
h1 := allHands[i]
h2 := allHands[j]
winner := h1.Hand.Compare(h2.Hand)
fmt.Printf("Comparison: [%s] vs [%s]\n", h1.Raw, h2.Raw)
h1Result := h1.Hand.Evaluate()
h2Result := h2.Hand.Evaluate()
switch winner {
case 1:
fmt.Printf("Winner: Hand 1 (%s beats %s)\n", handNames[h1Result.Category], handNames[h2Result.Category])
case 2:
fmt.Printf("Winner: Hand 2 (%s beats %s)\n", handNames[h2Result.Category], handNames[h1Result.Category])
default:
fmt.Printf("Tie: %s\n", handNames[h1Result.Category])
}
fmt.Println()
count++
}
}
fmt.Printf("Total comparisons: %d\n", count)
}
// --- Main ---
func main() {
filename := "Hands to be tested.txt"
fmt.Println("--- Task 1a: Single Evaluation ---")
parseFile(filename)
fmt.Println("\n--- Task 1b: Comparison (All vs All) ---")
compareFileAllVsAll(filename)
}