Computer Science Canada

Rock Paper Scissors Lizard Spock

Author:  slender0123 [ Mon Sep 16, 2013 6:29 pm ]
Post subject:  Rock Paper Scissors Lizard Spock

This is my second game i ever made.. I made it to prove I could make a rock paper scissors game thats 1 player after only have 2 weeks to work with turing.

It was fun to make. I will probibly add a score button and such to it, maybe a longest streak button too.

Turing:

import GUI
var rock : boolean := false
var paper : boolean := false
var scissors : boolean := false
var computer1 : int
var spock : boolean := false
var lizard : boolean := false

proc ROCK
 rock := true
  cls
  GUI.Refresh
  computer1 := Rand.Int(1,5)
   if computer1 = 1 then
    put "Tie!"
   elsif computer1 = 2 then
    put "You lose! Paper covers rock!"
   elsif computer1 = 3 then
    put "You win! Rock beats Scissors!"
   elsif computer1 = 4 then
    put "You win! Rock crushes lizard!"
   elsif computer1 = 5 then
    put "you lose! Spock vaporizes rock!"
    end if
  end ROCK

proc PAPER
 paper := true
  cls
  GUI.Refresh
  computer1 := Rand.Int(1,5)
   if computer1 = 1 then
    put "You win!! Paper covers rock!" 
   elsif computer1 = 2 then
    put "Tie!"
   elsif computer1 = 3 then
    put "You lose! Scissor cuts paper!"
   elsif computer1 = 4 then
    put "You lose! Lizard eats paper!"   
   elsif computer1 = 5 then
    put "You win! Paper disproves spock!"
   end if
 end PAPER

proc SCISSOR
 scissors := true
  cls
  GUI.Refresh
  computer1 := Rand.Int(1,5)
   if computer1 = 1 then
    put "You lose! Rock beats Scissors!"
   elsif computer1 = 2 then
    put "You win! scissors cuts paper!" 
   elsif computer1 = 3 then
    put "Tie!"
   elsif computer1 = 4 then
    put "You win! Scissors dicapitate lizard!"
   elsif computer1 = 5 then
    put "You lose! Spock smashes scissors!"
  end if
 end SCISSOR
 
proc SPOCK
 spock := true
  cls
  GUI.Refresh
  computer1 := Rand.Int(1,5)
   if computer1 = 1 then
    put "You win! Spock vapourises rock!"
   elsif computer1 = 2 then
    put "You lose! Paper disproves spock!" 
   elsif computer1 = 3 then
    put "You win! Spock smashes scissors"
   elsif computer1 = 4 then
    put "You lose! Lizard poisons spock!!"
   elsif computer1 = 5 then
    put "Tie!!"
  end if
end SPOCK

proc LIZARD
 lizard := true
  cls
  GUI.Refresh
  computer1 := Rand.Int(1,5)
   if computer1 = 1 then
    put "You lose! Rock crushes lizard!"
   elsif computer1 = 2 then
    put "You win! Lizard eats paper!" 
   elsif computer1 = 3 then
    put "You lose! Scissors decapitate lizard!"
   elsif computer1 = 4 then
    put "You win! Lizard poisons spock!"
   elsif computer1 = 5 then
    put "Tie!!"
  end if
end LIZARD

View.Set ("graphics:375;250,nobuttonbar")
var Rock : int := GUI.CreateButtonFull (55, 100, 0, "Rock",
    ROCK, 0, '^D', true)
   
var Paper : int := GUI.CreateButtonFull (150, 50, 0, "Paper",
    PAPER, 0, '^D', true)
   
var Scissor : int := GUI.CreateButtonFull (250, 50, 0, "Scissors",
    SCISSOR, 0, '^D', true)
   
var Lizard : int := GUI.CreateButtonFull (50, 50, 0, "Lizard",
    LIZARD, 0, '^D', true)
   
var Spock : int := GUI.CreateButtonFull (255, 100, 0, "Spock",
    SPOCK, 0, '^D', true)
   
var quitBtn : int := GUI.CreateButton (152, 10, 0, "Quit", GUI.Quit)

loop
exit when GUI.ProcessEvent
end loop

Author:  Nathan4102 [ Mon Sep 16, 2013 7:22 pm ]
Post subject:  RE:Rock Paper Scissors Lizard Spock

Embarassed In my last post, I for some reason thought we were talking about a Tic-Tac-Toe AI. :p

Nice job though, it works perfectly. The next step is to:

A) Pretty the game up. Make the GUI look nice, add a title, some colors, maybe some animations, its up to you!

B) Shorten your code, and try to make it more efficient. This isn't THAT important when working with small games like this, but when you get to larger programs, its going to be one of your biggest concerns. Try to always make an effort to get your code both as short and efficient as possible.

Author:  Raknarg [ Mon Sep 16, 2013 7:37 pm ]
Post subject:  RE:Rock Paper Scissors Lizard Spock

FOr this game, there is an algorithm you can use to shorten it Razz I can't remember how I did it though, it has to do with if you order all the options in a circle, it should be able to beat any item two elements awayfrom it. Can't remember how I implemented it

Author:  slender0123 [ Mon Sep 16, 2013 7:47 pm ]
Post subject:  RE:Rock Paper Scissors Lizard Spock

@Nathan OOh! Tic tac toe sounds like a good idea... hmm... That would be a bit difficult for me lol. I think i might try it though. And tic tac toe is pretty different then rock paper scissors lol.

@Raknarg For now, I think i should get used to certain statements, which is why i've had a tendency of repeating almost the same thing over and over again. I will practice on shortening up my code once I get more familier with different statements and such.

Author:  Raknarg [ Mon Sep 16, 2013 8:02 pm ]
Post subject:  RE:Rock Paper Scissors Lizard Spock

Here we go. This works if you order them in rock, paper, scissors, spock, lizard. Or any other list, where 2n positions away is a win for it, where 0 < n < number of elements / 2.

You could write a RPS game with 101 elements if you want. If you use this formula, it makes certain your code is nice and short.

Turing:

const hand : array 1 .. 5 of string := init ("rock", "paper", "scissors", "spock", "lizard")

fcn win (player, opponent : int) : string
    if player = opponent then
        result "t"
    end if
   
    for i : player + 2 .. player + upper (hand) by 2
        if (i - 1) mod 5 + 1  = opponent then
            result "w"
        end if
    end for
   
    result "l"
end win

var p1, p2 : int
var cont : char

loop
    p1 := Rand.Int (1, 5)
    p2 := Rand.Int (1, 5)
   
    put "Player 1: " + hand (p1)
    put "Player 2: " + hand (p2)
   
    if win (p1, p2) = "w" then
        put hand (p1) + " beats " + hand (p2)
    elsif win (p1, p2) = "l" then
        put hand (p2) + " beats " + hand (p1)
    else
        put "Tie"
    end if
   
    cont := getchar
   
    cls
end loop

Author:  slender0123 [ Tue Sep 17, 2013 8:47 am ]
Post subject:  RE:Rock Paper Scissors Lizard Spock

Lol, I gotta learn how to do that. its like magic. lol. I managed to fix my ball bounce game by the way. Its updated on the oen topic I think.

Yours is more or less watching someone else do it though.

Its not likely a 1 player game, more like a computer vs a computers XD lol

Author:  Nathan4102 [ Tue Sep 17, 2013 9:55 am ]
Post subject:  RE:Rock Paper Scissors Lizard Spock

That program was just whipped together to show his algorithm. It won't be hard to make it work 1P or 2P.


: