Skip to content

Instantly share code, notes, and snippets.

@giacomoferretti
Created March 24, 2019 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giacomoferretti/564b850908d4db488f97b397b1892f55 to your computer and use it in GitHub Desktop.
Save giacomoferretti/564b850908d4db488f97b397b1892f55 to your computer and use it in GitHub Desktop.
Carta Forbici Sasso scritto in Go
package main
import "fmt"
import "math/rand"
import "os"
import "os/exec"
import "time"
func main() {
clearScreen()
fmt.Println(" ┌─── Carta Forbici Sasso ───┐ ")
fmt.Println(" │ v1.0 │ ")
fmt.Println(" └───────────────────────────┘ ")
fmt.Println(" CARTA FORBICI SASSO ")
fmt.Println(" 1 2 3 ")
fmt.Println()
rand.Seed(time.Now().UnixNano())
p2 := randBetween(1, 4)
var p1 int
for p1 < 1 || p1 > 3 {
fmt.Print("Digita un numero [1-3]: ")
fmt.Scan(&p1)
if (p1 < 1 || p1 > 3) {
fmt.Println("Devi digitare un numero compreso tra 1 e 3. Riprova.")
fmt.Println()
}
}
fmt.Println()
fmt.Println("Hai scelto", printMove(p1))
fmt.Println()
fmt.Println("La CPU ha scelto", printMove(p2))
if p1 == p2 {
fmt.Println()
fmt.Println(printMove(p1), "==", printMove(p2))
fmt.Println("Hai pareggiato.")
} else if (p1 == 1 && p2 == 3) || (p1 == 2 && p2 == 1) || (p1 == 3 && p2 == 2) {
fmt.Println()
fmt.Println(printMove(p1), "=>", printMove(p2))
fmt.Println("HAI VINTO!")
} else {
fmt.Println()
fmt.Println(printMove(p1), "<=", printMove(p2))
fmt.Println("Hai perso. :(")
}
}
func randBetween(min, max int) int {
return min + rand.Intn(max - min)
}
func printMove(move int) string {
switch (move) {
case 1:
return "CARTA"
case 2:
return "FORBICI"
case 3:
return "SASSO"
default:
return "ERROR"
}
}
func clearScreen() {
c := exec.Command("clear")
c.Stdout = os.Stdout
c.Run()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment