Created
April 3, 2013 00:22
-
-
Save download13/5297426 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 ( | |
"net/http" | |
"io/ioutil" | |
"strings" | |
"strconv" | |
"math" | |
"fmt" | |
) | |
type C struct { | |
Name string | |
Guess uint8 | |
} | |
var darksidersContestants = []*C{ | |
&C{"iSparkz", 64}, | |
&C{"tupto", 128}, | |
&C{"Nitrous_Ninja", 142}, | |
&C{"hotsweatyham", 199}, | |
&C{"phillmans", 21}, | |
&C{"dualstrike98", 138}, | |
&C{"Cheeseburgur26", 32}, | |
&C{"Roger_rabbit23", 62}, | |
} | |
var rfaContestants = []*C{ | |
&C{"rbryan06", 17}, | |
&C{"Abe_lincolin", 56}, | |
&C{"MrPanda1593", 149}, | |
} | |
func getNumber() uint8 { | |
res, _ := http.Get("http://www.random.org/integers/?num=1&min=0&max=255&col=1&base=10&format=plain&rnd=new") | |
defer res.Body.Close() | |
body, _ := ioutil.ReadAll(res.Body) | |
response := string(body) | |
response = strings.Trim(response, "\t \n") | |
no, _ := strconv.ParseUint(response, 10, 8) | |
no8 := uint8(no) | |
if no8 == 42 { | |
return getNumber() | |
} | |
return no8 | |
} | |
func pickClosest(n uint8, contestants []*C) *C { | |
f := float64(n) | |
closest := contestants[0] | |
for _, c := range(contestants) { | |
if math.Abs(float64(c.Guess) - f) < math.Abs(float64(closest.Guess) - f){ | |
closest = c | |
} | |
} | |
return closest | |
} | |
func Rollin(game string, contestants []*C) string { | |
winningNo := getNumber() | |
winner := pickClosest(winningNo, contestants) | |
return fmt.Sprintf("%s wins %s with a guess of %d which was closest to %d!", winner.Name, game, winner.Guess, winningNo) | |
} | |
func main() { | |
fmt.Println(Rollin("Darksiders", darksidersContestants)) | |
fmt.Println(Rollin("Red Faction: Armageddon", rfaContestants)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment