Created
February 10, 2016 10:14
-
-
Save ewhitebloom/c527ddd81f43d7f6feca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"strings" | |
"time" | |
) | |
type Player struct { | |
Location int | |
Speed int | |
} | |
func main() { | |
success := play("****~~*~~**~*~~*~*~*~*") | |
fmt.Println(success) | |
success2 := play("*****~~~*") | |
fmt.Println(success2) | |
} | |
func play(River string) bool { | |
player := Player{0, 0} | |
waterLength := 0 | |
elements := strings.Split(River, "") | |
for i, element := range elements { | |
player.Location = i | |
// change momentum if on a stepping stone. | |
if element == "*" { | |
player.Speed = player.Speed + speedDelta() | |
} | |
waterLength = trackJumpLength(waterLength, element) | |
// track progress | |
fmt.Println(player.Speed) | |
fmt.Println(element) | |
fmt.Println() | |
if player.Speed < waterLength && element == "~" { | |
return false | |
} | |
} | |
return true | |
} | |
// returns random speed change from -1..1 | |
func speedDelta() int { | |
s := rand.NewSource(time.Now().UnixNano()) | |
r := rand.New(s) | |
speedDelta := r.Intn(2) | |
if r.Intn(2) == 1 { | |
speedDelta = speedDelta * -1 | |
} | |
return speedDelta | |
} | |
// calculates the accrued length of the river jump | |
func trackJumpLength(waterLength int, element string) int { | |
if element == "~" { | |
return waterLength + 1 | |
} else { | |
return 0 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment