phase-10-tracker/main.odin

64 lines
1.8 KiB
Odin
Raw Normal View History

2026-02-15 20:58:06 +00:00
/*
----------------
Phase 10 Tracker
----------------
This program helps the user keep track of a game of Phase 10 as it is being
played. It will keep track of players and their scores over the course of the
game, inferring which phase they are on based on their score.
To use the program, first you will input the first names of all of the
players, seperated by spaces. Then, after each round is finished, you will
enter the score of each player's hand, again seperated by spaces and in the
same order that the names were supplied.
*/
2026-02-15 23:17:10 +00:00
package phase10
2026-02-15 20:58:06 +00:00
import "core:fmt"
import "core:os"
import "core:strings"
// Return a slice of space delimited strings from stdin
getSpaceDelimetedItems :: proc(backingBuffer: []byte) -> []string {
count, _ := os.read(os.stdin, backingBuffer)
response := string(backingBuffer[:count - 1]) // leave off the newline
strings.trim_space(response)
items, _ := strings.fields(response)
2026-02-15 20:58:06 +00:00
return items
}
// Prompt user for names of all players
getNames :: proc(backingBuffer: []byte) -> []string {
fmt.print("Enter Names: ")
return getSpaceDelimetedItems(backingBuffer)
}
main :: proc() {
// Prompt for names
buf: [2048]byte
names := getNames(buf[:])
defer delete(names)
2026-02-15 23:17:10 +00:00
// Create game
game: Game
2026-02-15 20:58:06 +00:00
for name in names {
2026-02-16 01:30:11 +00:00
addPlayer(&game, name)
2026-02-15 20:58:06 +00:00
}
2026-02-15 23:17:10 +00:00
defer deleteGameData(&game) // Clean up game data
2026-02-15 20:58:06 +00:00
2026-02-16 01:30:11 +00:00
// Main game loop
// fmt.println(game)
printGame(&game)
winner: int = -1
for winner == -1 {
2026-02-15 23:17:10 +00:00
addScores(&game)
2026-02-16 01:30:11 +00:00
updatePhasesByScores(&game)
// fmt.println(game)
printGame(&game)
2026-02-15 23:17:10 +00:00
winner = checkWinner(&game)
}
2026-02-16 01:30:11 +00:00
fmt.printfln("%v wins! They had %v points!", game.names[winner], getScore(&game, winner))
2026-02-15 20:58:06 +00:00
}