Camelia

Number guessing game in Perl 6

This is the Perl 6 solution to the exercise which is the beginning of a game called the Number Guessing game.

Note! This site is about Perl 6.
If you are looking for a solution for the current production version of Perl 5, please check out the Perl 5 tutorial.
Specifially there is a related article: Number guessing game in Perl 5.

examples/number_guessing.pl

use v6;

my $hidden = (1..200).pick;
# say $hidden;

my $guess = prompt "Type in your guess: ";
# say $guess;

if $guess == $hidden {
    say "Hit!";
} elsif $guess < $hidden {
    say "Your guess is too small";
} else {
    say "Your guess is too big";
}

Perl 6 has a function called rand to generate random numbers, but as I was told, the idiomatic way to generate random integers is to pick them from a range or to roll a dice.

Once we have the $hidden value we use the prompt function to ask for the user input.

Unlike in Ruby or in Python Perl does not require special conversion of the input to a number. We can go directly to the look at the 3 cases and provide the appropriate feedback to the user.


The Perl 6 Tricks and Treats newsletter has been around for a while. If you are interested to get special notification when there is new content on this site, it is the best way to keep track:
Email:
Full name:
This is a newsletter temporarily running on my personal site (szabgab.com) using Mailman, till I implement an alternative system in Perl 6.
Gabor Szabo
Written by Gabor Szabo

Published on 2015-10-18



Comments

In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus
Suggest a change
Elapsed time: 3.809905

Perl 6 Tricks and Treats newsletter

Register to the free newsletter now, and get updates and news.
Email:
Name: