I completed the "war" game as outlined in this post (problem #5.)
It was actually fairly easy for me to write, but with the help of a friend who writes awesomely in Ruby on Rails, I was able to learn a better way to write this program. The program can be found here:
https://gist.github.com/724880
Showing newest posts with label Programming. Show older posts
Showing newest posts with label Programming. Show older posts
Saturday, December 4, 2010
Tuesday, November 30, 2010
First to 100 Game Program
This is activity #8 from this post.
This one was monumentally more comfortable to write than the lottery program although it use similar ideas. I have a few questions that I will ask about things I can do to make the program smaller, but I am still proud of this program despite its size.
The url: https://gist.github.com/721398
This one was monumentally more comfortable to write than the lottery program although it use similar ideas. I have a few questions that I will ask about things I can do to make the program smaller, but I am still proud of this program despite its size.
The url: https://gist.github.com/721398
Labels:
Programming
The Lottery Program
This program is from problem #4 at this post.
So you may have noticed I haven't posted for a while. Why? The lottery program. Yeah. It was a dreaded horrible beast. I saw there for weeks, really, staring at the lines of code I'd spewed forth, stressing, rewriting, throwing things (okay, not really.) And then I was told to take a break. I did... a long one. It would be just fine if I could just write every program in the book just not this one. It was impossible and a stupid task. But alas, I could not excuse myself from the project so I sat down to it. Ever have one of those moments when you feel like one synapsis fires, and then thus another one, and then it's just a cascade of just everything working... it puts you on quite a roll. That's what happened. I HATED this program for the longest time, but as I was finishing the program, I just felt so on top of everything. It felt really good, empowering or something.
I created a new GitHub... actually I just changed my sn over there. This is because I'm going now... this is what I've always wanted to do growing up and no matter how many walls I face how many tall, towering lottery programs I face, I will cower like a big baby, cry about it, and then face my problem head on. Here is the link to my completed lottery program. Yeah, there are better ways to write it, but as I learn those ways, I will come back and improve upon the program. I think showing these changes would be better viewed on github than here, but I will continue to document my journey here.
The program:
https://gist.github.com/707654
So you may have noticed I haven't posted for a while. Why? The lottery program. Yeah. It was a dreaded horrible beast. I saw there for weeks, really, staring at the lines of code I'd spewed forth, stressing, rewriting, throwing things (okay, not really.) And then I was told to take a break. I did... a long one. It would be just fine if I could just write every program in the book just not this one. It was impossible and a stupid task. But alas, I could not excuse myself from the project so I sat down to it. Ever have one of those moments when you feel like one synapsis fires, and then thus another one, and then it's just a cascade of just everything working... it puts you on quite a roll. That's what happened. I HATED this program for the longest time, but as I was finishing the program, I just felt so on top of everything. It felt really good, empowering or something.
I created a new GitHub... actually I just changed my sn over there. This is because I'm going now... this is what I've always wanted to do growing up and no matter how many walls I face how many tall, towering lottery programs I face, I will cower like a big baby, cry about it, and then face my problem head on. Here is the link to my completed lottery program. Yeah, there are better ways to write it, but as I learn those ways, I will come back and improve upon the program. I think showing these changes would be better viewed on github than here, but I will continue to document my journey here.
The program:
https://gist.github.com/707654
Labels:
Programming
Monday, November 1, 2010
Dice Game: Ifs & Loops
This program is from problem #7 at this post.
I had a little fun with some 'ifs' and 'while loops' with this program. When I first started the program I became somewhat frustrated. I stopped and slept on it. When I came back to the code, it was sooo easy to piece together. The break was a really good idea. The code:
I had a little fun with some 'ifs' and 'while loops' with this program. When I first started the program I became somewhat frustrated. I stopped and slept on it. When I came back to the code, it was sooo easy to piece together. The break was a really good idea. The code:
count = 0
count2 = 3
guess = 0
while guess <2 || guess >12
puts( 'Pick a number 2-12: ')
guess = gets.chomp.to_i
end
dieTotal = 0
while dieTotal != guess && count != 3
count = count + 1
count2 = count2 - 1
die1 = rand(6)
die1 = die1 + 1
die2 = rand(6)
die2 = die2 + 1
dieTotal = die1 + die2
input = ''
puts( 'Type roll to roll the dice: ')
input = gets.chomp
while input != 'roll'
puts( 'Type roll to roll the dice: ')
input = gets.chomp
puts( 'I don\'t understand. ')
end
puts
puts( "Your guess is #{guess}.")
puts( "You rolled #{die1} and #{die2}. For a total of #{dieTotal}.")
puts
if dieTotal != guess && count2 > 1
puts( "You have #{count2} rolls left.")
puts
elsif dieTotal != guess && count2 == 1
puts( "You have #{count2} roll left.")
puts
elsif dieTotal != guess && count2 == 0
puts( "You have no rolls left.")
puts( "Sorry, you lose.")
else
puts( "Congrats! You win!")
end
end
Labels:
Programming
Thursday, October 28, 2010
Quiz Program (Loops)
This is from problem #6 as outlined in this post.
This one was difficult for me, but not as much as the last time I encountered loops. I think after a few more small programs with loops I'll feel more comfortable with this. I created a quiz about me. Feel free to run it and take it. (It's in Ruby.)
This one was difficult for me, but not as much as the last time I encountered loops. I think after a few more small programs with loops I'll feel more comfortable with this. I created a quiz about me. Feel free to run it and take it. (It's in Ruby.)
puts( ' <(^.^)> LITTLE KNOWN FACTS O\' MELANIE <(^.^)>') puts puts( 'To play, select the letter that corresponds with your answer then press return') answer = '' while answer != 'c' puts( '#1 Gym class sux. How would Melanie get out of running laps in Elem school?') puts puts( 'a. She got a doctors note') puts( 'b. She refused') puts( 'c. She hid') puts( 'd. She got extra help in another subject instead of going to gym') answer = gets.chomp.downcase end puts( 'Correct!') answer = '' while answer != 'b' puts( '#2 Melanie has a scar on her knee from kindergarten. How did she get it?') puts puts( 'a. She fell off the jungle gym') puts( 'b. She was running in flip flops and tripped') puts( 'c. She got beat up') puts( 'd. Her brother threw something at her') answer = gets.chomp.downcase end puts( 'Correct!') answer = '' while answer != 'a' puts( '#3 Melanie failed ONE class in high school. Which was it?') puts puts( 'a. government') puts( 'b. business services technology') puts( 'c. geometry') puts( 'd. art') answer = gets.chomp.downcase end puts( 'Correct!') answer = '' while answer != 'c' puts( '#4 Melanie is l337 fail at sports. Which sport did she play in high school?') puts puts( 'a. basketball') puts( 'b. cheerleading') puts( 'c. track') puts( 'd. volleyball') answer = gets.chomp.downcase end puts( 'Correct!') answer = '' while answer != 'b' puts( '#5 Melanie went on a family vacation when she was 10. Where did she go?') puts puts( 'a. Virginia Beach, VA') puts( 'b. Chattanooga, TN') puts( 'c. Groton, CT') puts( 'd. Tell City, IN') answer = gets.chomp.downcase end puts( 'Correct!')
Labels:
Programming
Random Number Game
This post contains the program(s) for the random number game as described in this post. (Assignment #2, #3, and #5)
It was a little difficult to come up with how to assign the random number to the variable -- it was a durrr moment for sure. So without further ado here is the first part of the program (or problem #2 according to that post I linked to above:)
Here is problem #3:
I figured out how to fix problem #3 for that error message. Dumbest thing ever... how about a variable? I'm sure we all have those blonde moments, though. Also, got help from a friend on the chomp.to_i thing (that's what you do>> gets.chomp.CONVERSION NOT gets.CONVERSION.chomp.) New code:
Problem #5 was much easier to complete and more fun as I'm getting the hang of loops:
It was a little difficult to come up with how to assign the random number to the variable -- it was a durrr moment for sure. So without further ado here is the first part of the program (or problem #2 according to that post I linked to above:)
randomNumber = rand(10)
randomNumber = randomNumber + 1
print( 'Guess my number! Enter a number (1-10): ')
userNumber = gets.chomp()
puts( "Your number is #{userNumber}. My number is #{randomNumber}.
This program is not yet advanced enough to tell you whether you
are correct -- but you should be able to tell.")Here is problem #3:
# Try to figure out if there is a way to chomp & convert to an integer on
# same line (lines 7 and 8)
# Try to figure out how to print failure message (lines 14-16 & 20-22) for
# both cases in which the user is incorrect without having it in the program
# twice.
randNumber = rand(10)
randNumber = randNumber + 1
print( 'Guess my number! Enter a number (1-10): ')
userNumber = gets.chomp.to_i()
if userNumber == randNumber
puts( 'You guessed correctly! You are SOOO smart!')
elsif userNumber > randNumber
puts( "Your number (#{userNumber}) is higher than my number (#{randNumber}.")
print( "You are unable to guess again because you are such a failure. It
has nothing to do with my inability to program this such that you could
guess again.")
else
puts( "Your number (#{userNumber}) is lower than my number (#{randNumber}.)")
print( "You are unable to guess again because you are such a failure. It
has nothing to do with my inability to program this such that you could
guess again.")
end
Update:I figured out how to fix problem #3 for that error message. Dumbest thing ever... how about a variable? I'm sure we all have those blonde moments, though. Also, got help from a friend on the chomp.to_i thing (that's what you do>> gets.chomp.CONVERSION NOT gets.CONVERSION.chomp.) New code:
randNumber = rand(10)
randNumber = randNumber + 1
print( 'Guess my number! Enter a number (1-10): ')
userNumber = gets.chomp.to_i()
failureMessage = 'You are unable to guess again because you are such a failure. It
has nothing to do with my inability to program this in such a way that you
could guess again.'
if userNumber == randNumber
puts( 'You guessed correctly! You are SOOO smart!')
elsif userNumber > randNumber
puts( "Your number (#{userNumber}) is higher than my number (#{randNumber}.")
print failureMessage
else
puts( "Your number (#{userNumber}) is lower than my number (#{randNumber}.)")
print failureMessage
end
Problem #5 was much easier to complete and more fun as I'm getting the hang of loops:
randNumber = rand(10)
randNumber = randNumber + 1
count = 0
userNumber = ()
print( 'Guess my number! Enter a number (1-10): ')
while userNumber != randNumber
userNumber = gets.chomp.to_i()
count = count + 1
failureMessage = 'You did not guess correctly. Guess again:'
if userNumber == randNumber
puts( "You guessed correctly! It took you #{count} time(s) to guess.")
elsif userNumber > randNumber
puts( "Your number (#{userNumber}) is higher than my number.")
print failureMessage
else
puts( "Your number #{userNumber} is lower than my number.")
print failureMessage
end
end
Labels:
Programming
Little Schedule of Programming
So I'm getting some assignments out of this book I had to use for a class. There is a section at the end of each chapter that asks students to describe how the logic of a particular program would work. Instead of describing the logic, however, I am just going to write the program. So here's what I have on the docket in order from easy to hard:
#1 Create a madlib.
#2 Create a program that generates a random number (1-10) and then asks the user to
guess the number. Then display the random number so the user can see whether he/she
is correct.
#3 Modify game 2 so that a message is displayed telling the user whether or not their numberwas too high or too low.
#4 Generate 3 random numbers (0-9,) ask the user to guess three numbers. Compare each
guess to the three random numbers. Display a message that includes the user's guess, the
three random numbers, and the amount of money the user has won as follows:
No matches: $0
One match: $10
Two matches: $100
Three matching, not in order: $1000
Three matching, in order: $1,000,000
(See page 192 for repeating digit info.)
#5 Modify game 2,3 so the user can continue to guess the correct number, display the number
of times it took the user to guess the correct number.
#6 Create a five question quiz about something. Multiple choice questions with four options each.
When the user gets an answer correct, display a congratulatory message. If the user was
wrong display the respective message and ask the question again until the user correctly
answers it.
#7 Create a game simulating two dice rolls and generates two numbers (1-6.) The user chooses a
number between 2 (the lowest possible roll) and 12 (the highest possible roll.) The user then
"rolls" the dice three times. If the number the user has pre-chosen is rolled, the user wins. If
not, the computer wins.
#8 Create a dice game where the user and computer compete to first score 100 points. On a turn,
the player rolls two dice. Rules:
a.) If NONE of the dice on a roll have a number one on them, the dice values are added to a
running total. The player can choose to roll again or to end their turn.
b.) If there is a 1 on only ONE of the dice, nothing is added to the player's total and their
turn ends.
c.) If a 1 appears on BOTH the dice, the player's turn is over AND their score is reset to zero.
d.) To simulate whether the computer chooses to end their turn or roll again (a,) generate a
random value (1-2.) If the value is 1, the computer "decides" to end its turn. If the value
is 2, the computer rolls again.
These will keep me busy for a while and comprise about a third of the book. When I complete these activities, I will post more.
Labels:
Programming
Getting in the Groove (Embedded Variables)
Yeah, I'm still on the whole embedded variables thing, but this is SOOOOOO much easier. Today I worked on getting used to doing things this way. It'll take a bit of time to memorize, but in the meantime I created a mad lib to kind of get used to it. Enjoy!
# Is it customary to start variable counts at one? (ie. noun1) or
# at zero (ie. noun and the second being noun1) or how else is this
# handled?
print( 'Enter an adjective: ' )
adjective1 = gets.chomp()
print( 'Enter a verb ending in "ed": ')
verbEd = gets.chomp()
print( 'Enter a plural noun: ')
pluralNoun1 = gets.chomp()
print( 'Enter a liquid: ')
liquid = gets.chomp()
print( 'Enter another plural noun: ')
pluralNoun2 = gets.chomp()
print( 'Enter a famous person: ')
famousPerson = gets.chomp()
print( 'Enter a place: ')
place = gets.chomp()
print( 'Enter an occupation: ')
occupation = gets.chomp()
print( 'Enter a noun: ')
noun1 = gets.chomp()
print( 'Enter a nationality (ie. American or Irish): ')
nationality = gets.chomp()
print( 'Enter a female celebrity: ')
femaleCelebrity = gets.chomp()
print( 'Enter another noun: ')
noun2 = gets.chomp()
print( 'Enter the name of a female friend: ')
femaleFriend = gets.chomp()
print( 'You\'re almost done. Enter another plural noun: ')
pluralNoun3 = gets.chomp()
print( 'Enter a number: ')
number = gets.chomp()
print( 'Enter an adjective: ')
adjective2 = gets.chomp()
puts ( 'Personal Ad:')
puts
puts ( "I enjoy long, #{adjective1} walks on the beach, getting #{verbEd} in
the rain and serendipitous encounters with #{pluralNoun1}.
I really like piƱa coladas mixed with #{liquid}, and romantic, candle-lit
#{pluralNoun2}. I am well-read from Dr. Seuss to #{famousPerson}. I travel
frequently, especially to #{place}, when I am not busy with work. (I am a
#{occupation}.) I am looking for #{noun1} and beauty in the form of a
#{nationality} goddess. She should have the physique of #{femaleCelebrity}
and the #{noun2} of #{femaleFriend}. I would prefer if she knew how to
cook, clean, and wash my #{pluralNoun3}. I know I’m not very attractive in
my picture, but it was taken #{number} days ago, and I have since become
more #{adjective2}. ")
Labels:
Programming
Embedded Variables in Strings
So I'm learning Ruby again, but this time I've got a program in mind. It will take a lot of brushing up and general learning before I will be able to start the program. I already know how to write the program without an interface, but I want to build an interface too, so it'll take a bit of work. I think once I get this particular program coded that I'll know enough about programming to be able to work on other programs so that will be really nice.
Tonight I started reading "The Book of Ruby" which is a big step up from the last book I read, but I think I'm understanding it fairly well considering the difference in difficulty between the two texts.
So tonight's topic was Strings and Embedded Evaluation. The sample code in the lesson is as follows:
After reading the OTHER book on Ruby I would have programmed this like so:
However, I learned that by using "print" instead of puts, I can keep the cursor on the same line... which is cool, I don't know what real difference it would make other than aesthetics, but I guess I'll find out later. I did add a period so that I could make it a proper sentence and added the corresponding chomp to make this possible.
What I mostly learned was this whole embedded variables thing. I'm a little confused as to the purpose -- if it's just faster to code like this and less confusing than all the plusses and whatnot from something like 'Words here' + someVariable + 'another string.' I can really see that being the case, however, I'm not going to assume that it is simply for cleaner and faster programming -- until yeah, I am told so.
I also played a bit with "\n" and "\t" which are newline and tab. It took a bit getting used to the whole new way of doing the embedded thing and playing with newline and tab helped me get used to it a bit. Here's the code I created using this newfangled stuff I learned:
I used the sentence "I like rocks and giraffes." (Without the quotes.) And thus, the program spits out this:
I like rocks and giraffes.
I like rocks and giraffes.
I like rocks and giraffes.
Definitely some stuff to play around with for a bit. I perhaps I could rewrite this to capitalize the first letter if it is not capitalized and to add a period if there is not one. I think this would be a good project for tomorrow. :P
Tonight I started reading "The Book of Ruby" which is a big step up from the last book I read, but I think I'm understanding it fairly well considering the difference in difficulty between the two texts.
So tonight's topic was Strings and Embedded Evaluation. The sample code in the lesson is as follows:
print( 'Enter your name: ' )
name = gets()
puts( "Hello #{name}" )
After reading the OTHER book on Ruby I would have programmed this like so:
puts 'Enter your name:' name = gets.chomp puts 'Hello ' + name + '.'
However, I learned that by using "print" instead of puts, I can keep the cursor on the same line... which is cool, I don't know what real difference it would make other than aesthetics, but I guess I'll find out later. I did add a period so that I could make it a proper sentence and added the corresponding chomp to make this possible.
What I mostly learned was this whole embedded variables thing. I'm a little confused as to the purpose -- if it's just faster to code like this and less confusing than all the plusses and whatnot from something like 'Words here' + someVariable + 'another string.' I can really see that being the case, however, I'm not going to assume that it is simply for cleaner and faster programming -- until yeah, I am told so.
I also played a bit with "\n" and "\t" which are newline and tab. It took a bit getting used to the whole new way of doing the embedded thing and playing with newline and tab helped me get used to it a bit. Here's the code I created using this newfangled stuff I learned:
# Playing with tab \t and newline \n
puts ( 'How about a sentence of sort?')
sentence = gets()
puts ( "\t #{sentence}\t\t #{sentence}")
I used the sentence "I like rocks and giraffes." (Without the quotes.) And thus, the program spits out this:
I like rocks and giraffes.
I like rocks and giraffes.
I like rocks and giraffes.
Definitely some stuff to play around with for a bit. I perhaps I could rewrite this to capitalize the first letter if it is not capitalized and to add a period if there is not one. I think this would be a good project for tomorrow. :P
Labels:
Programming
Subscribe to:
Posts (Atom)
