在这个游戏里面,程序首先会生成一个介于1~100之间的随机整数,然后提示玩家进行猜测。玩家每次输入一个数字,程序就会告诉玩家这个猜测的值是高于还是低于那个秘密的随机数,并且让玩家再次猜测,如果猜对了就告诉玩家胜利并且退出程序。
package main
import (
"fmt"
"math/rand"
)
func main() {
maxNum := 100
secretNumber := rand.Intn(maxNum)
fmt.Println("The secret number is ", secretNumber)
}
为了生成随机数,我们需要用到math/rand包,然后用rand.Intn生成一个随机数,再打印出这个随机数。
这时候会发现我们每次都会打印相同的数字到屏幕上,不符合要求。
打开文档我们会发现需要使用前设置随机数种子,我们可以在程序启动时用启动的时间戳来初始化随机数种子。
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
maxNum := 100
rand.Seed(time.Now().UnixNano())
secretNumber := rand.Intn(maxNum)
fmt.Println("The secret number is ", secretNumber)
}
我们可以用time.now.unix来初始化随机数种子
package main
import (
"bufio"
"fmt"
"math/rand"
"os"
"strconv"
"strings"
"time"
)
func main() {
maxNum := 100
rand.Seed(time.Now().UnixNano())
secretNumber := rand.Intn(maxNum)
fmt.Println("The secret number is ", secretNumber)
fmt.Println("Please input your guess")
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
fmt.Println("An error occured while reading input. Please try again", err)
return
}
input = strings.Trim(input, "\r\n")
guess, err := strconv.Atoi(input)
if err != nil {
fmt.Println("Invalid input. Please enter an integer value")
return
}
fmt.Println("You guess is", guess)
}
接下来我们需要实现用户的输入输出,并解析成数字。
每个程序执行的时候都会打开几个文件,如stdin,stdout,stderr等。我们可以用bufio.NewReader把一个文件转换成一个reader变量,然后用ReadString方法来读取一行,如果失败的话则打印错误并退出。ReadString返回的结果包含结尾的换行符,我们把它去掉,再转换成数字。如果转换失败,我们同样打印错误退出。
package main
import (
"bufio"
"fmt"
"math/rand"
"os"
"strconv"
"strings"
"time"
)
func main() {
maxNum := 100
rand.Seed(time.Now().UnixNano())
secretNumber := rand.Intn(maxNum)
fmt.Println("The secret number is ", secretNumber)
fmt.Println("Please input your guess")
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
fmt.Println("An error occured while reading input. Please try again", err)
return
}
input = strings.Trim(input, "\r\n")
guess, err := strconv.Atoi(input)
if err != nil {
fmt.Println("Invalid input. Please enter an integer value")
return
}
fmt.Println("You guess is", guess)
if guess > secretNumber {
fmt.Println("Your guess is bigger than the secret number. Please try again")
} else if guess < secretNumber {
fmt.Println("Your guess is smaller than the secret number. Please try again")
} else {
fmt.Println("Correct, you Legend!")
}
}
然后我们加入比较操作。
package main
import (
"bufio"
"fmt"
"math/rand"
"os"
"strconv"
"strings"
"time"
)
func main() {
maxNum := 100
rand.Seed(time.Now().UnixNano())
secretNumber := rand.Intn(maxNum)
// fmt.Println("The secret number is ", secretNumber)
fmt.Println("Please input your guess")
reader := bufio.NewReader(os.Stdin)
for {
input, err := reader.ReadString('\n')
if err != nil {
fmt.Println("An error occured while reading input. Please try again", err)
continue
}
input = strings.Trim(input, "\r\n")
guess, err := strconv.Atoi(input)
if err != nil {
fmt.Println("Invalid input. Please enter an integer value")
continue
}
fmt.Println("You guess is", guess)
if guess > secretNumber {
fmt.Println("Your guess is bigger than the secret number. Please try again")
} else if guess < secretNumber {
fmt.Println("Your guess is smaller than the secret number. Please try again")
} else {
fmt.Println("Correct, you Legend!")
break
}
}
}
此时我们的程序大致可以正常工作,但是玩家只能输入一次猜测,无论猜测是否正确,程序都会退出。为了改变这种行为,让游戏可以正常玩下去,我们需要加一个循环。我们把刚刚的代码挪到一个for循环里,然后再把return改成continue,以便于在出错的时候能够继续循环,在用户输入正确的时候break,这样才能在用户胜利的时候退出游戏。