Tuesday, November 30, 2010

The 2nd Little Schedule of Programming

I did it! I completed the first 8 programming assignments! Here are some more I need to do, definitely tougher:

#1 Create the logic for an application that contains an array of 10 multiple-choice questions related to your favorite hobby. Each question contains three answer choices. Also create a parallel array that holds the correct answer to each question - A, B, or C. Display each question and verify that the user enters only A, B, or C as the answer - if not, keep prompting the user until a valid response is entered. If the user responds to a question correctly, display "Correct!" Otherwise, display "The correct answer is" and the letter of the correct answer. After the user answers all the questions, display the number of correct and incorrect answers.

#2 Create the logic for a dice game. The application randomly "throws" five dice for the computer and five for the player. As each random "throw" is made, store it in an array. The application displays all the values, which can be from 1 to 6 inclusive for each die. Decide the winner based on the following hierarchy of die values. Any higher combination beats a lower; for example, five of a kind beats four of a kind.
-Five of a kind
-Four of a kind
-Three of a kind
-A pair
Numeric dice values do not count, if both players have a three of a kind, it's a tie no matter what the values of the dice are. The game does not recognize a full house.

#3 Improve #2 so that dice value DO make a difference. A pair of 5s beats a pair of 4s.

#4 Hangman - User guesses letters in a hidden word. Store letters of the word in array. Display a dash for each missing letter. Allow user to continuously guess until all letters in the word are guessed. As the user guesses correct letters, display the word with the filled in letter. For example if the word is computer --------, and the user guesses 'p', show the word again as ---p----. Also, make sure that when a user guesses a letter, ALL the matching letters are filled in. For example, if the word is banana and the user guesses 'a', make sure all As are filled in.

#5 Create two parallel arrays that represent a standard deck of 52 playing cards. One array is numeric and holds the values 1 through 13 (representing ace, 1, 2, etc.) The other array is a string and holds suits. Create the arrays so that all 52 card/suit combos are represented. Create a War card game that randomly selects two card (one for player, one for computer) and declares a winner (or tie) based on the numeric value of the two cards. The game should play for 26 rounds, dealing a whole deck with no repeated cards. For this game, assume the cards' values are based on their numbers. Display the value of the player's and computer's cards, compare their values, and determine the winner. When all cards in the deck are exhausted, display a count of the number of times the player wins, the number of times the computer wins, and the number of ties.
  Hints:
    -Start by creating an array of all 52 playing cards.
    -Select a random number for the deck position of the player's first card and assign the card at
      that array position to the player.
    -Move every higher-positioned card in the deck "down" one to fill in the gap. In other words, if
      the player's first random number is 49, select the card at position 49 (both the numeric value
      and the string), move the card that was in position 50 to position 49, and move the card that
      was in position 51 to position 50. Only 51 cards remain in the deck after the player's first
      card is dealt, so the available-card array is smaller by one.

#6 Create a guessing game called "Does It Have Legs?" in which you think of an animal and the computer tries to guess what it is. The game continually becomes "smarter" as you play because you teach it how to become more specific with its questions. For example, a typical game session might proceed as follows:
Computer: Does the animal you are thinking of have legs?
Player: Yes.
Computer: Yay! I win. Do you want to play again?
Player: Yes
Computer: Does the animal you are thinking of have legs?
Player: Yes
Computer: Is it a dog?
Player: No.
Computer: I give up. What was your animal?
Player: horse.
Computer: Type a question for which the answer is Yes for dog and No for horse.
Player: Does it live in the house?
Computer: Do you want to play again?
Player: Yes
Computer: Does the animal you are thinking of have legs?
Player: Yes
Computer: Does it live in the house?
Player: No
Computer: Is it a horse?
Player: No.
Computer: I give up. What was your animal?
Player: cow.
Computer: Type a question for which the answer is Yes for horse but No for cow.
Player: Do people ride it?
Computer: Do you want to play again?
Player: Yes
Computer: Does the animal you are thinking of have legs?
Player: Yes
Computer: Does it live in the house?
Player: No
Computer: Do people ride it?
Player: No
Computer: Is it a cow?
Player: Yes
Computer: Yay! I win. Do you want to play again?
Player: No
  Hint: Look up binary trees to organize this.

#7 Improve #4 to hold an array of 10 words. One at a time, pass each to a method that allows the user to continuously guess a letter until all the letters in the word are correctly guessed. The method returns the number of guesses it took to complete the word. Store the number in an array before returning to the method for the next word. After all 10 words have been guesses, display a summary of the number of guesses it took for each word as well as the average number of guesses per word.

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

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

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:
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

Celebrity Crushes

I'm not one for a huge celebrity crush, but I have thought that particular celebrities are more attractive than others. People have said that I have strange celebrity crushes, but eh, I don't mind I guess. Here are celebrities that I have had crushes on throughout my life:

Roy Scheider - (Only in Jaws) It has something to do with him driving a boat with a cigarette hanging out of his mouth in Jaws. I hate smoking, but for some reason, yeah, that's pretty hot to me.


















Gabriel Byrne - I have no idea.













Alan Cumming (But only when he was in Goldeneye, haha)














Michael Cera
Copyright © Shy Girl Speaks