-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (86 loc) · 1.79 KB
/
main.go
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"fmt"
"math/rand"
"os"
"strconv"
"sync"
)
func main() {
var choice int
fmt.Println("----- Rock, Paper, Scissors -----")
for {
displayMenu()
fmt.Print("Your choice: ")
fmt.Scan(&choice)
switch choice {
case 1:
play()
case 2:
os.Exit(0)
default:
panic("Invalid input entered.")
}
choice = 0
}
}
func play() {
var playerChoice int
var playerAction string
var computerChoice int
var computerAction string
var winner string = ""
var wg sync.WaitGroup
wg.Add(1)
go computerPlay(&computerChoice, &computerAction, &wg)
fmt.Println("Computer Moving...")
wg.Add(1)
go playerPlay(&playerChoice, &playerAction, &wg)
wg.Wait()
fmt.Printf("Player - %v vs %v - Computer\n", playerAction, computerAction)
if playerChoice == 1 && computerChoice == 3 {
winner = "Winner!"
} else if playerChoice == 2 && computerChoice == 1 {
winner = "Winner!"
} else if playerChoice == 3 && computerChoice == 2 {
winner = "Winner!"
} else if playerChoice == computerChoice {
winner = "Draw!"
} else {
winner = "Loser!"
}
fmt.Printf("\n\n----- %v -----\n\n", winner)
}
func playerPlay(pc *int, pa *string, wg *sync.WaitGroup) {
defer wg.Done()
var input string
fmt.Println("\n\n----- Your Move -----")
fmt.Println("1. Rock")
fmt.Println("2. Paper")
fmt.Println("3. Scissors")
fmt.Print("Move: ")
fmt.Scan(&input)
*pc, _ = strconv.Atoi(input)
*pa = getAction(*pc)
}
func computerPlay(cc *int, ca *string, wg *sync.WaitGroup) {
defer wg.Done()
*cc = rand.Intn(3) + 1
*ca = getAction(*cc)
}
func getAction(chose int) string {
switch chose {
case 1:
return "Rock"
case 2:
return "Paper"
case 3:
return "Scissors"
default:
panic("Invalid input entered")
}
}
func displayMenu() {
fmt.Println("1. Play")
fmt.Println("2. Quit")
}