// Rock-Paper-Scissors -- finished version. // // Run with: go run projects/rock-paper-scissors/finished.go package main import ( "bufio" "fmt" "math/rand" "os" "strings" ) func main() { reader := bufio.NewReader(os.Stdin) fmt.Print("Choose rock, paper, or scissors: ") text, _ := reader.ReadString('\n') player := strings.TrimSpace(text) // The computer picks one of three at random, without a list. roll := rand.Intn(3) + 1 var computer string switch roll { case 1: computer = "rock" case 2: computer = "paper" default: computer = "scissors" } fmt.Printf("The computer chose %s.\n", computer) if player != "rock" && player != "paper" && player != "scissors" { fmt.Println("That is not a valid choice.") } else if player == computer { fmt.Println("It is a tie!") } else if (player == "rock" && computer == "scissors") || (player == "paper" && computer == "rock") || (player == "scissors" && computer == "paper") { fmt.Println("You win!") } else { fmt.Println("You lose!") } }