refactor
This commit is contained in:
parent
e08a78010c
commit
248270eae9
56
main.odin
56
main.odin
|
|
@ -12,11 +12,10 @@
|
||||||
enter the score of each player's hand, again seperated by spaces and in the
|
enter the score of each player's hand, again seperated by spaces and in the
|
||||||
same order that the names were supplied.
|
same order that the names were supplied.
|
||||||
*/
|
*/
|
||||||
package main
|
package phase10
|
||||||
|
|
||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
import "core:os"
|
import "core:os"
|
||||||
import "core:mem"
|
|
||||||
import "core:strings"
|
import "core:strings"
|
||||||
import "core:strconv"
|
import "core:strconv"
|
||||||
|
|
||||||
|
|
@ -35,10 +34,10 @@ getNames :: proc(backingBuffer: []byte) -> []string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prompt user to enter scores for each player
|
// Prompt user to enter scores for each player
|
||||||
addScores :: proc(scores: [][dynamic]int, names: []string) {
|
addScores :: proc(game: ^Game) {
|
||||||
buf: [2048]byte
|
buf: [2048]byte
|
||||||
fmt.print(" ")
|
fmt.print(" ")
|
||||||
for name in names {
|
for name in game.names {
|
||||||
fmt.print(name, "")
|
fmt.print(name, "")
|
||||||
}
|
}
|
||||||
fmt.println()
|
fmt.println()
|
||||||
|
|
@ -47,26 +46,11 @@ addScores :: proc(scores: [][dynamic]int, names: []string) {
|
||||||
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)
|
||||||
append(&(scores[index]), intScore)
|
append(&(game.scores[index]), intScore)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
main :: proc() {
|
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
|
// Prompt for names
|
||||||
buf: [2048]byte
|
buf: [2048]byte
|
||||||
names := getNames(buf[:])
|
names := getNames(buf[:])
|
||||||
|
|
@ -77,24 +61,26 @@ main :: proc() {
|
||||||
fmt.println("Name:", name)
|
fmt.println("Name:", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create lists of scores for each player. Each element will by a dynamic
|
// Create game
|
||||||
// array of scores per player.
|
game: Game
|
||||||
scores : [dynamic][dynamic]int
|
|
||||||
for name in names {
|
for name in names {
|
||||||
scorelist: [dynamic]int
|
if name == "hayden" {
|
||||||
append(&scores, scorelist)
|
addPlayer(&game, name, phase = 10, score = 40)
|
||||||
}
|
} else {
|
||||||
defer {
|
addPlayer(&game, name)
|
||||||
for scorelist in scores {
|
|
||||||
delete(scorelist)
|
|
||||||
}
|
}
|
||||||
delete(scores)
|
|
||||||
}
|
}
|
||||||
|
defer deleteGameData(&game) // Clean up game data
|
||||||
|
|
||||||
// Main loop of adding scores
|
// Main loop of adding scores
|
||||||
fmt.println(scores)
|
fmt.println(game)
|
||||||
addScores(scores[:], names)
|
winner: string
|
||||||
addScores(scores[:], names)
|
for winner == "" {
|
||||||
addScores(scores[:], names)
|
addScores(&game)
|
||||||
fmt.println(scores)
|
updatePhases(&game)
|
||||||
|
fmt.println(game)
|
||||||
|
winner = checkWinner(&game)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.println("The winner is:", winner)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
BIN
phase-10-tracker
BIN
phase-10-tracker
Binary file not shown.
55
phase10.odin
55
phase10.odin
|
|
@ -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 ""
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue