Programming logic and design 9e solutions 6 else

This preview shows page 58 - 60 out of 61 pages.

Programming Logic and Design, 9e Solutions 6- else output “Tie” endif endif endif endif return 4. Design the logic for the game Hangman, in which the user guesses letters in a hidden word. Store the letters of a word in an array of characters. Display a dash for each missing letter. Allow the user to continuously guess a letter until all the letters in the word are guessed correctly. As the user enters each guess, display the word again, filling in the guess if it was correct. For example, if the hidden word is computer , first display a series of eight dashes: --------. After the user guesses p, the display becomes --- p ---. Make sure that when a user makes a correct guess, all the matching letters are filled in. For example, if the word is banana and the user guesses a , all three a characters should be filled in. Answer: A sample solution follows Pseudocode: start Declarations num x num numCorrect num LENGTH = 8 string WORD[LENGTH] = “C”, “O”, “M”, “P”, “U”, “T”, “E”, “R” string hiddenWord[LENGTH] = “-”,“-”,“-”,“-”, “-”,“-”,“-”,“-” string guess getReady() while numCorrect not equal to LENGTH detailLoop() endwhile finishUp() stop getReady() numCorrect = 0 output hiddenWord output “Please guess a letter” input guess return detailLoop() x = 0 while x < LENGTH if WORD[x] = guess then numCorrect = numCorrect + 1 hiddenWord[x] = guess 58
Programming Logic and Design, 9e Solutions 6- endif x = x + 1 endwhile output hiddenWord if numCorrect not equal to LENGTH output “Please guess a letter” input guess endif return finishUp() output “You guessed the word!” output hiddenWord return 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, 2 through 10, Jack, Queen, and King). The other array is a string array that holds suits (Clubs, Diamonds, Hearts, and Spades). Create the arrays so that all 52 cards are represented. Then, create a War card game that randomly selects two cards (one for the player and one for the computer) and declares a winner or a tie based on the numeric value of the two cards. The game should last for 26 rounds and use a full deck with no repeated cards. For this game, assume that the lowest card is the Ace. Display the values of the player’s and computer’s cards, compare their values, and determine the winner. When all the 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. Here are some 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. In the same way, randomly select a card for the computer and “remove” the card from the deck.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture