Our project is to make a functional Rock Paper Scissors game using Go. I figured this would be a great place to ask for some pointers on some obvious mistakes I could be making.
I am having several problems.
- No matter user input the program says I am always entering in "rock.
- No matter what I input the program also always tells me it is a "tie"
So it's quite apparent to me I am having issues with my if/else statements but I am unsure where and what exactly it is. Also I know my PlayerPlay
func is ugly but for some reason when I originally had my display menu in there it would keep looping back to my menu without proceeding through the rest of the program.
package main
import (
"fmt"
"math/rand"
"time"
)
func ComputerPlay() int {
return rand.Intn(2) + 1
}
func PlayerPlay(play int) int {
fmt.Scanln(&play)
return play
}
func PrintPlay(playerName string, play int) {
fmt.Printf("%s picked ", playerName)
if play == 0 {
fmt.Printf("rock\n")
} else if play == 1 {
fmt.Printf("paper\n")
} else if play == 2 {
fmt.Printf("scissors\n")
}
fmt.Printf("Computer has chose ")
switch ComputerPlay() {
case 0:
fmt.Println("rock\n")
case 1:
fmt.Println("paper\n")
case 2:
fmt.Println("scissors\n")
}
}
func ShowResult(computerPlay int, humanPlay int){
var play int
computerPlay = ComputerPlay()
humanPlay = PlayerPlay(play)
if humanPlay == 0 && humanPlay == 0 {
fmt.Printf("It's a tie\n")
} else if humanPlay == 0 && computerPlay == 1 {
fmt.Printf(" Rock loses to paper\n")
} else if humanPlay == 0 && computerPlay == 2 {
fmt.Printf("Rock beats scissors\n")
} else if humanPlay == 1 && computerPlay == 0 {
fmt.Printf(" Paper beats rock\n")
} else if humanPlay == 1 && computerPlay == 1 {
fmt.Printf("It's a tie!\n")
} else if humanPlay == 1 && computerPlay == 2 {
fmt.Printf("Paper loses to scissors\n")
} else if humanPlay == 2 && computerPlay == 0 {
fmt.Printf("Scissors loses to rock\n")
} else if humanPlay == 2 && computerPlay == 1 {
fmt.Printf(" Scissors beats paper\n")
} else if humanPlay == 2 && computerPlay == 2 {
fmt.Printf(" It's a tie!\n")
}
}
func main() {
rand.Seed(time.Now().UnixNano())
fmt.Printf("Welcome to Rock, Paper, Scissors\n\n")
fmt.Printf("What is your name?\n")
var playerName string
fmt.Scanln(&playerName)
fmt.Printf("Choose\n")
fmt.Printf("0. Rock\n")
fmt.Printf("1. paper\n")
fmt.Printf("2. scissors\n")
fmt.Printf("Your choice -> ")
var play int
PlayerPlay(play)
PrintPlay(playerName, play)
var computerPlay int
ComputerPlay()
ShowResult(computerPlay, play)
}