main functionality done, needs polish

This commit is contained in:
Hayden Johnson 2026-02-15 17:30:11 -08:00
parent 03ddd79483
commit 6d2293685f
2 changed files with 71 additions and 38 deletions

View file

@ -17,7 +17,6 @@ package phase10
import "core:fmt" import "core:fmt"
import "core:os" import "core:os"
import "core:strings" import "core:strings"
import "core:strconv"
// Return a slice of space delimited strings from stdin // Return a slice of space delimited strings from stdin
getSpaceDelimetedItems :: proc(backingBuffer: []byte) -> []string { getSpaceDelimetedItems :: proc(backingBuffer: []byte) -> []string {
@ -33,22 +32,6 @@ getNames :: proc(backingBuffer: []byte) -> []string {
return getSpaceDelimetedItems(backingBuffer) return getSpaceDelimetedItems(backingBuffer)
} }
// Prompt user to enter scores for each player
addScores :: proc(game: ^Game) {
buf: [2048]byte
fmt.print(" ")
for name in game.names {
fmt.print(name, "")
}
fmt.println()
fmt.print("Scores: ")
newScores := getSpaceDelimetedItems(buf[:])
defer delete(newScores)
for score, index in newScores {
intScore, _ := strconv.parse_int(score)
append(&(game.scores[index]), intScore)
}
}
main :: proc() { main :: proc() {
// Prompt for names // Prompt for names
@ -56,31 +39,24 @@ main :: proc() {
names := getNames(buf[:]) names := getNames(buf[:])
defer delete(names) defer delete(names)
// Show names to user
for name in names {
fmt.println("Name:", name)
}
// Create game // Create game
game: Game game: Game
for name in names { for name in names {
if name == "hayden" { addPlayer(&game, name)
addPlayer(&game, name, phase = 10, score = 40)
} else {
addPlayer(&game, name)
}
} }
defer deleteGameData(&game) // Clean up game data defer deleteGameData(&game) // Clean up game data
// Main loop of adding scores // Main game loop
fmt.println(game) // fmt.println(game)
winner: string printGame(&game)
for winner == "" { winner: int = -1
for winner == -1 {
addScores(&game) addScores(&game)
updatePhases(&game) updatePhasesByScores(&game)
fmt.println(game) // fmt.println(game)
printGame(&game)
winner = checkWinner(&game) winner = checkWinner(&game)
} }
fmt.println("The winner is:", winner) fmt.printfln("%v wins! They had %v points!", game.names[winner], getScore(&game, winner))
} }

View file

@ -1,11 +1,17 @@
package phase10 package phase10
import "core:strconv"
import "core:fmt"
import "core:slice"
// Struct to hold all information about a game
Game :: struct { Game :: struct {
names: [dynamic]string, names: [dynamic]string,
scores: [dynamic][dynamic]int, scores: [dynamic][dynamic]int,
phases: [dynamic]int phases: [dynamic]int
} }
// Clean up memory allocated for a Game struct
deleteGameData :: proc(game: ^Game) { deleteGameData :: proc(game: ^Game) {
for scorelist in game.scores { for scorelist in game.scores {
delete(scorelist) delete(scorelist)
@ -15,7 +21,25 @@ deleteGameData :: proc(game: ^Game) {
delete(game.phases) delete(game.phases)
} }
updatePhases :: proc(game: ^Game) { // Prompt user to enter scores for each player
addScores :: proc(game: ^Game) {
buf: [2048]byte
fmt.print(" ")
for name in game.names {
fmt.print(name, "")
}
fmt.println()
fmt.print("Scores: ")
newScores := getSpaceDelimetedItems(buf[:])
defer delete(newScores)
for score, index in newScores {
intScore, _ := strconv.parse_int(score)
append(&(game.scores[index]), intScore)
}
}
// Update the status of each player's phase in a game based on scores each round
updatePhasesByScores :: proc(game: ^Game) {
for name, index in game.names { for name, index in game.names {
phase: int = 1 phase: int = 1
for score in game.scores[index] { for score in game.scores[index] {
@ -27,6 +51,7 @@ updatePhases :: proc(game: ^Game) {
} }
} }
// Add a player to the game
addPlayer :: proc(game: ^Game, name: string, score: int = 0, phase: int = 1) { addPlayer :: proc(game: ^Game, name: string, score: int = 0, phase: int = 1) {
append(&game.names, name) append(&game.names, name)
append(&game.phases, phase) append(&game.phases, phase)
@ -44,11 +69,43 @@ addPlayer :: proc(game: ^Game, name: string, score: int = 0, phase: int = 1) {
append(&game.scores, scores) append(&game.scores, scores)
} }
checkWinner :: proc(game: ^Game) -> string { // Get aggregate score of player by index
getScore :: proc(game: ^Game, playerIndex: int) -> int {
sum: int = 0
for score in game.scores[playerIndex] {
sum += score
}
return sum
}
// Returns the index of the player who wins, or -1 if there is no winner
checkWinner :: proc(game: ^Game) -> int {
winners: [dynamic]int
defer delete(winners)
for player, index in game.names { for player, index in game.names {
if game.phases[index] > 10 { if game.phases[index] > 10 {
return player append(&winners, index)
} }
} }
return ""
if len(winners) > 0 {
// sort by lowest points
context.user_ptr = game
point_order :: proc(lhs, rhs: int) -> bool {
game: ^Game = (^Game)(context.user_ptr)
return getScore(game, lhs) < getScore(game, rhs)
}
slice.sort_by(winners[:], point_order)
return winners[0]
}
return -1
}
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])
}
} }