This commit is contained in:
Hayden Johnson 2026-02-15 15:17:10 -08:00
parent e08a78010c
commit 248270eae9
3 changed files with 75 additions and 36 deletions

View file

@ -12,11 +12,10 @@
enter the score of each player's hand, again seperated by spaces and in the
same order that the names were supplied.
*/
package main
package phase10
import "core:fmt"
import "core:os"
import "core:mem"
import "core:strings"
import "core:strconv"
@ -35,10 +34,10 @@ getNames :: proc(backingBuffer: []byte) -> []string {
}
// Prompt user to enter scores for each player
addScores :: proc(scores: [][dynamic]int, names: []string) {
addScores :: proc(game: ^Game) {
buf: [2048]byte
fmt.print(" ")
for name in names {
for name in game.names {
fmt.print(name, "")
}
fmt.println()
@ -47,26 +46,11 @@ addScores :: proc(scores: [][dynamic]int, names: []string) {
defer delete(newScores)
for score, index in newScores {
intScore, _ := strconv.parse_int(score)
append(&(scores[index]), intScore)
append(&(game.scores[index]), intScore)
}
}
main :: proc() {
// Set up memory tracker
// track: mem.Tracking_Allocator
// mem.tracking_allocator_init(&track, context.allocator)
// context.allocator = mem.tracking_allocator(&track)
//
// defer {
// if len(track.allocation_map) > 0 {
// fmt.eprintf("=== %v allocations not freed: ===\n", len(track.allocation_map))
// for _, entry in track.allocation_map {
// fmt.eprintf(" - %v bytes @ %v\n", entry.size, entry.location)
// }
// }
// mem.tracking_allocator_destroy(&track)
// }
// Prompt for names
buf: [2048]byte
names := getNames(buf[:])
@ -77,24 +61,26 @@ main :: proc() {
fmt.println("Name:", name)
}
// Create lists of scores for each player. Each element will by a dynamic
// array of scores per player.
scores : [dynamic][dynamic]int
// Create game
game: Game
for name in names {
scorelist: [dynamic]int
append(&scores, scorelist)
}
defer {
for scorelist in scores {
delete(scorelist)
if name == "hayden" {
addPlayer(&game, name, phase = 10, score = 40)
} else {
addPlayer(&game, name)
}
delete(scores)
}
defer deleteGameData(&game) // Clean up game data
// Main loop of adding scores
fmt.println(scores)
addScores(scores[:], names)
addScores(scores[:], names)
addScores(scores[:], names)
fmt.println(scores)
fmt.println(game)
winner: string
for winner == "" {
addScores(&game)
updatePhases(&game)
fmt.println(game)
winner = checkWinner(&game)
}
fmt.println("The winner is:", winner)
}

Binary file not shown.

View file

@ -1 +1,54 @@
package main
package phase10
Game :: struct {
names: [dynamic]string,
scores: [dynamic][dynamic]int,
phases: [dynamic]int
}
deleteGameData :: proc(game: ^Game) {
for scorelist in game.scores {
delete(scorelist)
}
delete(game.scores)
delete(game.names)
delete(game.phases)
}
updatePhases :: proc(game: ^Game) {
for name, index in game.names {
phase: int = 1
for score in game.scores[index] {
if score < 50 {
phase += 1
}
}
game.phases[index] = phase
}
}
addPlayer :: proc(game: ^Game, name: string, score: int = 0, phase: int = 1) {
append(&game.names, name)
append(&game.phases, phase)
// Calculate necesasry score sheet to result in score and phase
scores: [dynamic]int
for i := 1; i < phase - 1; i += 1 {
append(&scores, 0)
}
if score >= 50 {
append(&scores, 0)
}
if (phase != 1) {
append(&scores, score)
}
append(&game.scores, scores)
}
checkWinner :: proc(game: ^Game) -> string {
for player, index in game.names {
if game.phases[index] > 10 {
return player
}
}
return ""
}