DEV Community

Cover image for Simple Rock Paper Scissors game build using JuliaLang
Atharva Shirdhankar
Atharva Shirdhankar

Posted on

Simple Rock Paper Scissors game build using JuliaLang

Hello everyone 👋, in this article📃 I'll be discussing about how I m learning JuliaLang by building mini projects.

Lets start building Rock Paper Scissors game using JuliaLang.
But first we definitely need an IDE or TextEditor for running JuliaLang code.
We can use VSCode with required extension for running JuliaLang code but since I have just started with JuliaLang so I used repl.it here for coding and exploring JuliaLang(its an awesome online IDE try it).

Motivation behind getting started with Learning JuliaLang:

  1. Best part about JuliaLang is that it is easy to learn just like Python.
  2. The execution speed of JuliaLang is similar to C/C++. That means it's fast.
  3. And it is widely used for Data Science , Machine Learning , Deep Learning and even Web Development(And more lot stuff).
  4. And JuliaLang Community is wide & inclusive,if you stuck somewhere while creating any project using JuliaLang. There are many amazing folks to help you. And if are mathematics, bioinformatics or something related to Statistics and Maths or even you are just a Tech Enthusiast join JuliaLang slack channel. You will find many awesome peers who are enthusiast from of the above fields.
  5. Around 200+ JuliaLang packages are present to make your development work more easy.

Complete Code :

# learning julialang basics by creating classical stone paper scissors game


function play_game()
    moves = ["rock","paper","scissor"]
    computer_move = moves[rand(1:3)]

    human_move = Base.prompt("Please enter stone, paper, or scissor")

    print("Rock...")
    sleep(1.0)

    print("Paper...")
    sleep(1.0)

    print("Scissors...")
    sleep(1.0)

    print("Shoot!\n")

    if computer_move == human_move
        print("You tied, please try again")
    elseif computer_move == "rock" && human_move == "scissor"
        print("You lose, the computer won with rock, please try again")
    elseif computer_move == "paper" && human_move == "rock"
        print("You lose, the computer won with paper, please try again")
    elseif computer_move == "scissor" && human_move == "paper"
        print("You lose, the computer won with scissor, please try again")
    else
        print("You won, the computer lost with $computer_move, nice work!")
    end

end

Enter fullscreen mode Exit fullscreen mode

My Learnings from this mini project build:

1) Learned how to write comment in JuliaLang.

# This is how we write JuliaLang comments
# hashtag followed with text you wanted to commented
Enter fullscreen mode Exit fullscreen mode

2) Learned how to print text.

print("Your text here inside double quotes")
Enter fullscreen mode Exit fullscreen mode

3) Learned how to create function(without parameters)

function fun_name()
.
your code here
.
end
Enter fullscreen mode Exit fullscreen mode

Whenever you create function its necessary to tell compiler where to stop by using end keyword in JuliaLang.

4) Learned how to use conditional loop (if-elseif-else loop).

function test(x, y)
           if x < y
               println("x is less than y")
           elseif x > y
               println("x is greater than y")
           else
               println("x is equal to y")
           end
       end
Enter fullscreen mode Exit fullscreen mode

Whenever we use if-else or if-elseif-else conditional loop after the else condition statement we should use end keyword to tell compiler where does the conditional loops ends.

5) Learned about few inbuilt function.

  • sleep() : Its an inbuilt method of JuliaLang which comes under Base class and we don't need to import package for it. It basically stops execution of code for specified time. The minimum sleep time is 1 milliseconds.
sleep(seconds)
Enter fullscreen mode Exit fullscreen mode
  • rand() : It picks random element from array or list of elements. And similar to sleep method its an inbuilt method which comes under Base class.
rand(1:3)
Enter fullscreen mode Exit fullscreen mode

Here we randomly selected 1 element from 3 elements inside the collection.

Source :

Top comments (3)

Collapse
 
logankilpatrick profile image
Logan Kilpatrick

I love this! Glad it is helping you :)

Collapse
 
star_trooper profile image
Atharva Shirdhankar

I'm currently learning JuliaLang I might have missed something.
Feedback is always welcomed :)

Collapse
 
shinjithdev profile image
shinjith

Nice work!