Add bounds checking for addScores

This commit is contained in:
Hayden Johnson 2026-02-18 13:50:34 -08:00
parent b1fc251574
commit 0e079d13bb
2 changed files with 10 additions and 2 deletions

View file

@ -48,7 +48,6 @@ main :: proc() {
defer deleteGameData(&game) // Clean up game data defer deleteGameData(&game) // Clean up game data
// Main game loop // Main game loop
// fmt.println(game)
printGame(&game) printGame(&game)
winner: int = -1 winner: int = -1
for winner == -1 { for winner == -1 {
@ -59,5 +58,9 @@ main :: proc() {
winner = checkWinner(&game) winner = checkWinner(&game)
} }
fmt.printfln("%v wins! They had %v points!", game.names[winner], getScore(&game, winner)) fmt.printfln(
"%v wins! They had %v points!",
game.names[winner],
getScore(&game, winner)
)
} }

View file

@ -31,6 +31,10 @@ addScores :: proc(game: ^Game) {
fmt.println() fmt.println()
fmt.print("Scores: ") fmt.print("Scores: ")
newScores := getSpaceDelimetedItems(buf[:]) newScores := getSpaceDelimetedItems(buf[:])
if len(newScores) != len(game.names) {
fmt.println("[ERROR] == Invalid number of scores")
return
}
defer delete(newScores) defer delete(newScores)
for score, index in newScores { for score, index in newScores {
intScore, _ := strconv.parse_int(score) intScore, _ := strconv.parse_int(score)
@ -101,6 +105,7 @@ checkWinner :: proc(game: ^Game) -> int {
return -1 return -1
} }
// Print state of the game as a nice table
printGame :: proc(game: ^Game) { printGame :: proc(game: ^Game) {
fmt.printf("Name\tScore\tPhase\n") fmt.printf("Name\tScore\tPhase\n")
fmt.printf("----\t-----\t-----\n") fmt.printf("----\t-----\t-----\n")