add proper game importing

This commit is contained in:
Hayden Johnson 2026-02-19 21:10:23 -08:00
parent 0adeba4daa
commit cdbe614e22
2 changed files with 39 additions and 21 deletions

View file

@ -40,26 +40,26 @@ main :: proc() {
}
}
// TODO don't just test this, actually read the fields into a game struct
if len(os.args) > 1 {
importGame(nil, os.args[1])
}
// Prompt for names
buf: [2048]byte
names := getNames(buf[:])
defer delete(names)
// Create game
game: Game
for name in names {
addPlayer(&game, name)
if len(os.args) > 1 {
importGame(&game, os.args[1])
} else {
// Prompt for names
buf: [2048]byte
names := getNames(buf[:])
defer delete(names)
for name in names {
addPlayer(&game, name)
}
}
defer deleteGameData(&game) // Clean up game data
// Main game loop
printGame(&game)
winner: int = -1
winner: int = checkWinner(&game)
for winner == -1 {
addScores(&game)
updatePhasesByScores(&game)

View file

@ -17,11 +17,14 @@ Game :: struct {
// Clean up memory allocated for a Game struct
deleteGameData :: proc(game: ^Game) {
for name in game.names {
delete(name)
}
for scorelist in game.scores {
delete(scorelist)
}
delete(game.scores)
delete(game.names)
delete(game.scores)
delete(game.phases)
}
@ -61,9 +64,10 @@ updatePhasesByScores :: proc(game: ^Game) {
// Add a player to the game
addPlayer :: proc(game: ^Game, name: string, score: int = 0, phase: int = 1) {
append(&game.names, name)
stringCopy: string = strings.clone(name)
append(&game.names, stringCopy)
append(&game.phases, phase)
// Calculate necesasry score sheet to result in score and phase
// Calculate necessary score sheet to result in score and phase
scores: [dynamic]int
for i := 1; i < phase - 1; i += 1 {
append(&scores, 0)
@ -114,8 +118,12 @@ printGame :: proc(game: ^Game) {
fmt.printf("Name\tScore\tPhase\n")
fmt.printf("----\t-----\t-----\n")
for name, index in game.names {
fmt.printf("%v\t%v\t%v\n", name, getScore(game, index),
game.phases[index])
fmt.printf(
"%v\t%v\t%v\n",
name,
getScore(game, index),
game.phases[index]
)
}
}
@ -213,9 +221,19 @@ importGame :: proc(game: ^Game, filename: string) {
}
for r, i, err in csv.iterator_next(&r) {
if err != nil { /* Do something with error */ }
for f, j in r {
fmt.printfln("Record %v, field %v: %q", i, j, f)
}
}
if err != nil { /* Do something with error */ }
for f, j in r {
if i == 0 {
nameClone := strings.clone(f)
scores: [dynamic]int
append(&game.names, nameClone)
append(&game.scores, scores)
append(&game.phases, 1)
} else {
score, _ := strconv.parse_int(f)
append(&game.scores[j], score)
}
}
}
updatePhasesByScores(game)
}