rock paper scissors go code example

Example 1: rock paper scissors in golang

package main

import (
	"fmt"
	"math/rand"
)

func rpsGame() int {
	won := false
	turns := 0
	for !won {
		fmt.Println("Rock Paper Scissors")
		fmt.Println("1=Rock 2=Paper 3=Scissors")
		fmt.Println("VVV Enter VVV")
		choice := 10
		for !(choice < 4 && choice > 0) {
			fmt.Println("VVV")
			fmt.Scanln(&choice)
		}
		pchoice := rand.Intn(4)
		for pchoice == 0 {
			pchoice = rand.Intn(4)
		}
		fmt.Println(pchoice)
		if pchoice-choice == 1 || choice-pchoice == 2 {
			fmt.Println("You lose")
		} else if choice-pchoice == 1 || pchoice-choice == 2 {
			fmt.Println("you win!!")
			won = true
		} else if choice-pchoice == 0 || pchoice-choice == 0 {
			fmt.Println("TIE!!")
		}
		turns++
	}
	return turns
}

func main() {
	terminate := false
	for !terminate {
		fmt.Print(rpsGame())
		fmt.Println(" turns to win")
		fmt.Println("play again? 1 = yes, anything else is no")
		g := 0
		fmt.Scanln(&g)
		if g != 1 {
			terminate = true
		}
	}
}

Example 2: rock paper scissors

My Rock Paper Scissor game:
https://codepen.io/Lorenzo-SC/pen/ExgpPEO
Please give me a like if you appreciate :)

Example 3: rock paper scissors

import random

print('Get ready for rock,paper,scissors')
user = input('Enter your element:')
element = ['I choose Rock', 'I choose Paper', 'I choose Scissors']
AI = random.choice(element)
print(AI)
# Ties
if AI == str('I choose Rock') and user == str('rock'):
    print('Its a tie')
if AI == str('I choose Paper') and user == str('paper'):
    print('Its a tie')
if AI == str('I choose Scissors') and user == str('scissors'):
    print('Its a tie')
# Rock AI
if AI == str('I choose Rock') and user == str('paper'):
    print('You won')
if AI == str('I choose Rock') and user == str('scissors'):
    print('You lost')
# Rock user
if AI == str('I choose Paper') and user == str('rock'):
    print('You lost')
if AI == str('I choose Scissors') and user == str('rock'):
    print('You won')
# Paper AI
if AI == str('I choose Paper') and user == str('rock'):
    print('You lost')
if AI == str('I choose Paper') and user == str('scissors'):
    print('You won')
# Paper user
if AI == str('I choose Rock') and user == str('paper'):
    print('You won')
if AI == str('I choose Scissors') and user == str('paper'):
    print('You lost')
# Scissors AI
if AI == str('I choose Scissors') and user == str('paper'):
    print('You lost')
if AI == str('I choose Scissors') and user == str('rock'):
    print('You won')
# Scissors user
if AI == str('I choose Paper') and user == str('scissors'):
    print('You won')
if AI == str('I choose Rock') and user == str('scissors'):
    print('You lost')


def restartgame():
    print('Get ready for rock,paper,scissors')
    user = input('Enter your element:')
    element = ['I choose Rock', 'I choose Paper', 'I choose Scissors']
    AI = random.choice(element)
    print(AI)
    # Ties
    if AI == str('I choose Rock') and user == str('rock'):
        print('Its a tie')
    if AI == str('I choose Paper') and user == str('paper'):
        print('Its a tie')
    if AI == str('I choose Scissors') and user == str('scissors'):
        print('Its a tie')
    # Rock AI
    if AI == str('I choose Rock') and user == str('paper'):
        print('You won')
    if AI == str('I choose Rock') and user == str('scissors'):
        print('You lost')
    # Rock user
    if AI == str('I choose Paper') and user == str('rock'):
        print('You lost')
    if AI == str('I choose Scissors') and user == str('rock'):
        print('You won')
    # Paper AI
    if AI == str('I choose Paper') and user == str('rock'):
        print('You lost')
    if AI == str('I choose Paper') and user == str('scissors'):
        print('You won')
    # Paper user
    if AI == str('I choose Rock') and user == str('paper'):
        print('You won')
    if AI == str('I choose Scissors') and user == str('paper'):
        print('You lost')
    # Scissors AI
    if AI == str('I choose Scissors') and user == str('paper'):
        print('You lost')
    if AI == str('I choose Scissors') and user == str('rock'):
        print('You won')
    # Scissors user
    if AI == str('I choose Paper') and user == str('scissors'):
        print('You won')
    if AI == str('I choose Rock') and user == str('scissors'):
        print('You lost')
    again = input('Would you like to play again')
    if again == str('yes'):
        restartgame()
    if again == str('no'):
        raise SystemExit(0)


again = input('Would you like to play again')
if again == str('yes'):
    restartgame()
if again == str('no'):
    raise SystemExit(0)

Tags:

Go Example