67 lines
1.8 KiB
Odin
67 lines
1.8 KiB
Odin
/*
|
|
----------------
|
|
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.
|
|
*/
|
|
package phase10
|
|
|
|
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)
|
|
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)
|
|
|
|
// Create game
|
|
game: Game
|
|
for name in names {
|
|
addPlayer(&game, name)
|
|
}
|
|
defer deleteGameData(&game) // Clean up game data
|
|
|
|
// Main game loop
|
|
printGame(&game)
|
|
winner: int = -1
|
|
for winner == -1 {
|
|
addScores(&game)
|
|
updatePhasesByScores(&game)
|
|
// fmt.println(game)
|
|
printGame(&game)
|
|
winner = checkWinner(&game)
|
|
}
|
|
|
|
fmt.printfln(
|
|
"%v wins! They had %v points!",
|
|
game.names[winner],
|
|
getScore(&game, winner)
|
|
)
|
|
}
|