day 21a complete
transferred the game structs to the shared area, linked list can be manipulated to have a different board type and so placing in shared reduces code size
This commit is contained in:
parent
73de067afc
commit
f406939197
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"AdventOfCode2021/shared"
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
@ -13,19 +14,19 @@ func main() {
|
|||||||
// content := returnContent("../testInput")
|
// content := returnContent("../testInput")
|
||||||
|
|
||||||
//Construct Game board... in reverse :) (Just makes it easier for stringing them together)
|
//Construct Game board... in reverse :) (Just makes it easier for stringing them together)
|
||||||
positions := make(map[int]*GamePosition)
|
positions := make(map[int]*shared.GamePosition)
|
||||||
gameBoard := GameBoard{
|
gameBoard := shared.GameBoard{
|
||||||
Start: &GamePosition{
|
Start: &shared.GamePosition{
|
||||||
Position: 1,
|
Position: 1,
|
||||||
Next: nil,
|
Next: nil,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
positions[1] = gameBoard.Start
|
positions[1] = gameBoard.Start
|
||||||
next := gameBoard.Start
|
next := gameBoard.Start
|
||||||
var currentPosition *GamePosition
|
var currentPosition *shared.GamePosition
|
||||||
|
|
||||||
for i := 10; i > 1; i-- {
|
for i := 10; i > 1; i-- {
|
||||||
currentPosition = new(GamePosition)
|
currentPosition = new(shared.GamePosition)
|
||||||
currentPosition.Position = i
|
currentPosition.Position = i
|
||||||
currentPosition.Next = next
|
currentPosition.Next = next
|
||||||
next = currentPosition
|
next = currentPosition
|
||||||
@ -35,11 +36,11 @@ func main() {
|
|||||||
gameBoard.Start.Next = currentPosition
|
gameBoard.Start.Next = currentPosition
|
||||||
|
|
||||||
//Add Players to the game
|
//Add Players to the game
|
||||||
players := make(map[int]*Player)
|
players := make(map[int]*shared.Player)
|
||||||
numPlayers := 0
|
numPlayers := 0
|
||||||
|
|
||||||
for _, value := range *content {
|
for _, value := range *content {
|
||||||
newPlayer := Player{
|
newPlayer := shared.Player{
|
||||||
PlayerNum: value[0],
|
PlayerNum: value[0],
|
||||||
Score: 0,
|
Score: 0,
|
||||||
Position: positions[value[1]],
|
Position: positions[value[1]],
|
||||||
@ -56,7 +57,7 @@ func main() {
|
|||||||
for {
|
for {
|
||||||
for i := 1; i <= numPlayers; i++ {
|
for i := 1; i <= numPlayers; i++ {
|
||||||
p := players[i]
|
p := players[i]
|
||||||
if p.Roll(&diceVal, &numRolls) {
|
if Roll(p, &diceVal, &numRolls) {
|
||||||
winner = p.PlayerNum
|
winner = p.PlayerNum
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -76,22 +77,7 @@ func main() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type GameBoard struct {
|
func Roll(p *shared.Player, diceVal *int, numRolls *int) bool {
|
||||||
Start *GamePosition
|
|
||||||
}
|
|
||||||
|
|
||||||
type GamePosition struct {
|
|
||||||
Position int
|
|
||||||
Next *GamePosition
|
|
||||||
}
|
|
||||||
|
|
||||||
type Player struct {
|
|
||||||
PlayerNum int
|
|
||||||
Score int
|
|
||||||
Position *GamePosition
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Player) Roll(diceVal *int, numRolls *int) bool {
|
|
||||||
//simulate 3 consecutive dice rolls
|
//simulate 3 consecutive dice rolls
|
||||||
dice := (*diceVal + 1) * 3
|
dice := (*diceVal + 1) * 3
|
||||||
*diceVal += 3
|
*diceVal += 3
|
||||||
|
16
shared/LinkedListGameBoard.go
Normal file
16
shared/LinkedListGameBoard.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package shared
|
||||||
|
|
||||||
|
type GameBoard struct {
|
||||||
|
Start *GamePosition
|
||||||
|
}
|
||||||
|
|
||||||
|
type GamePosition struct {
|
||||||
|
Position int
|
||||||
|
Next *GamePosition
|
||||||
|
}
|
||||||
|
|
||||||
|
type Player struct {
|
||||||
|
PlayerNum int
|
||||||
|
Score int
|
||||||
|
Position *GamePosition
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user