import-bot (20211) [Avatar] Offline
#1
[Originally posted by cdplayeron]

OK, OK I didn't do my exercises as I should have and it all caught up with me
in Chapter 5. I had the “Ya I got it fever”. I am much better now. SO I
swallowed my pride and WENT BACK and complete all of the exercises up to 3.3.
Boy did I learn a lot!!!

This exercise was pretty interesting until I got to part three where I have to
come up with a way to display the dice. Whew! Anywho the following is my
solution I don't think that it is pretty but it appears to work. I would love
for you to tell me how I could have completed this assignment better. I am
sure you know of a better way than I.

Thanks!
Elton

#!/usr/bin/perl
use strict;
print "Welcome to the ROLL THE DICE program
";
my $quit = 0;
my $numroll;
my $response;
until ($quit) {
my $first_number = int (rand (6)) + 1; # range: 1 to 6 inclusive
my $second_number = int (rand (6)) + 1; # range: 1 to 6 inclusive

$numroll = $numroll +1;

print "Please ROLL THE DICE by hitting the return key (Enter 'q' to Quit)
";
$response = <STDIN> ;
chomp ($response);
#vaild input is the letter 'q' or the <RET> key

if ($response eq 'q' )
{ $quit = 1;
print "Exiting the ROLL THE DICE program Goodbye!
";

} elsif ($response =~ m/^d+$/ or $response =~ m/[A-Za-z]/) #checking
for invalid characters.
{
print "Invalid number(s) or character(s) entered!

";
} else {
if ($response = ' ')
{ my @boxes;
$boxes[1] = "#######
# #
# # #
# #
#######
";
$boxes[2] = "#######
# # #
# #
# # #
#######
";
$boxes[3] = "#######
# # #
# # #
# # #
#######
";
$boxes[4] = "#######
# # # #
# #
# # # #
#######
";
$boxes[5] = "#######
# # # #
# # #
# # # #
#######
";
$boxes[6] = "#######
# # # #
# # # #
# # # #
#######
";
{
print "$boxes[$first_number]";
print "$boxes[$second_number]";
}

print "Roll number: $numroll
Dice one: $first_number Dice two:
$second_number
";
}
}
}
import-bot (20211) [Avatar] Offline
#2
Re: Exercise 3.3
[Originally posted by jandrew]

Elton,

Remember, exercise 3.3.2 just says to design and *pseudo-code* a program
to print the total of each roll --- and to *consider* how one might
display the dice. However, you are not the first reader to try to
actually try their hand at writing the program smilie

If you go back to page 1 of this forum you'll see a thread entitled
"Throw dice", and on page 3 of this forum, another entitled "Problem
with Exercise 3.3(number 2) --- the first post in this latter thread
has serious formatting problems in the code, but the remainder of the
thread is readable. But you already cleared the main hurdle by storing
your dice pictures in an array!

About your code, your last if() conditional:

if ($response = ' ')

is actually assigning ' ' to $response, and then it will be a true
value. Had you used -w, you'd have been warned. You probably meant:

if ($response == ' ')

But that isn't correct either because == is numerical equality, and
you really wanted string equality:

if ($response eq ' ')

About ruling out invalid response characters: you test only if the
response entered is all digits, or contains any alphanumeric
character, and disallow it if it does. What if the user types '***',
or "#"? I'd not bother to check for invalid entry --- just check for
'q' or roll the dice (if they hit other keys before return it won't
bother us). You could be a bit more forgiving with the 'q' test as
well, some people might try typing 'quit' or 'Q' etc.

# quit if response starts with 'q' or 'Q'

if($response =~ m/^[qQ]/) {
#... do quit stuff
} else {
# roll the dice
}

If you did want validity checking --- think about only accepting good
stuff rather than disallowing bad stuff. For example, in your case you
only want to allow responses starting with 'q' or 'Q' (to quit),
otherwise the response should be empty (after chomp() anyway):

chomp($response);
if($response =~ m/^[qQ]/) {
# starting with 'q' or 'Q' is allowed
#... do quit stuff
} elsif ($respons ne '') {
# else if it wasn't the empty string
# ... do invalid input stuff
} else {
# roll the dice
}



You certainly have the right idea for doing the dice pictures ---
storing them in an array from element 1 to 6, then you don't a big series
of if/elsif/... tests to get the right picture, just look up the roll
in the array. My only suggestion would be to define this array before
the main while-loop so it only gets built once instead of every time
through the loop (will make your loop much smaller and easier to
follow the logic as well).

Lastly, I know this forum messes up code postings (in particular, it
actually interprets
into real newlines --- so try to replace all
your
's with \n's before posting (what a pain, oh well)). But
aside from that, some of your indentation seems rather inconsistent
--- ie, the first two lines in your loop are indented 8-spaces (1
tab?), the next is not indented, the next few are indented 16-spaces
(2 tabs?), etc. You will find it much easier to both to develop your
programs and debug or maintain them afterwards if you use a
consistent indentation as discussed in chapter 2.

However, all that said, the only real bug is using '=' rather than
'eq' in one conditional (and not using -w). And your validity check
wasn't as strict as you might have wanted. A very good start indeed!

So, now that I've given you all these pointers for your current
attempt --- try thinking of a way for your program to display the pair
of dice side-by-side instead of one above the other (you might want
to wait until after you read through chapter 6's regular expression
stuff to try this).

regards,
andrew