1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| package main
import ( "fmt" "math/rand" "os" "time" )
func generate_random_num(length int) []int { initial := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} random := make([]int, length) if length > 10 || length < 0 { fmt.Println("invalid length:", length) os.Exit(-1) } r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < length; i++ { tmp := r.Intn(len(initial) - i)
random[i] = initial[tmp] initial[tmp], initial[len(initial)-1-i] = initial[len(initial)-1-i], initial[tmp] } return random }
func mapping_num(input, random []int) bool { var A, B int
if len(input) != len(random) { fmt.Println("Number you entered didn't match the number you set it!") return false } for i := 0; i < len(input); i++ { if input[i] == random[i] { A++ } for j := 0; j < len(input); j++ { if input[i] == random[j] { B++ } } } B -= A fmt.Printf("%dA%dB\n", A, B)
if A == len(input) { fmt.Println("You Win!") return true } return false }
func main() { var input_int, length int var input []int status := false
fmt.Println("Welcome to the Final Code Game!") fmt.Println("Please entering the number you want to guess!") fmt.Scanf("%d", &length)
random_num := generate_random_num(length)
for !status { fmt.Printf("Please input numbers\n") fmt.Scanf("%d", &input_int)
for input_int%10 != 0 { input = append([]int{input_int % 10}, input...) input_int = input_int / 10 }
status = mapping_num(input, random_num)
if !status { input = nil } } }
|