“Write a GoLang program that can play the game of Math 24 and if the players gets the wrong answer, provide the answer.”
ChatGPT Answer (limitation, not able to validate anything more than simple addition)
package mainimport ("bufio""fmt""math/rand""os""strconv""strings""time")func main() {// Generate 4 random numbers between 1 and 10rand.Seed(time.Now().UnixNano())n1 := rand.Intn(10) + 1n2 := rand.Intn(10) + 1n3 := rand.Intn(10) + 1n4 := rand.Intn(10) + 1// Make sure the numbers can be used to solve the equation 24 using only addition and multiplicationfor n1+n2+n3+n4 != 24 && (n1*n2 != 24 && n1*n3 != 24 && n1*n4 != 24 && n2*n3 != 24 && n2*n4 != 24 && n3*n4 != 24) {n1 = rand.Intn(10) + 1n2 = rand.Intn(10) + 1n3 = rand.Intn(10) + 1n4 = rand.Intn(10) + 1}// Ask the player to solve the equation 24 using the 4 numbersfmt.Printf("Solve the equation 24 using the numbers %d, %d, %d, and %d\n", n1, n2, n3, n4)fmt.Print("Enter your answer: ")// Read the player's answerreader := bufio.NewReader(os.Stdin)input, _ := reader.ReadString('\n')input = strings.TrimSpace(input)// Check if the player's answer is correctanswer := strconv.Itoa(n1) + strconv.Itoa(n2) + strconv.Itoa(n3) + strconv.Itoa(n4)if input == answer {fmt.Println("Correct!")} else {fmt.Printf("Incorrect. The correct answer is %s\n", answer)}}
chatgpt-2022.12.11-math-24-interation-2Download