package main
import (
"fmt";
"os";
"bufio";
"rand";
"strconv";
"strings";
)
func read_guess(in chan bool, out chan int) {
bufreader := bufio.NewReader(os.Stdin);
stop := false;
for ! stop {
strguess, err := bufreader.ReadString('\n');
if err != nil {
fmt.Printf("error: %s\n", err.String());
os.Exit(-1);
}
// ... but it works!
strguess = strings.Join(
strings.Split(strguess, "\n", 0), "" );
guess, err := strconv.Atoi(strguess);
if err != nil {
fmt.Printf("what???\n");
}
else {
out <- guess;
stop = <-in;
}
}
}
func main() {
guess := 0;
_, seed, _ := os.Time();
rand.Seed(seed);
sklet := rand.Int() % 60 + 45;
fmt.Printf("How old is grandpa?\n");
stopch := make( chan bool );
readch := make( chan int );
go read_guess( stopch, readch );
for guess != sklet {
guess = <-readch;
switch {
case guess < sklet:
fmt.Printf("more\n");
stopch <- false
case guess > sklet:
fmt.Printf("less\n");
stopch <- false
case guess == sklet:
fmt.Printf("exactly!\n");
stopch <- true
}
}
}