x x 1 endwhile return findLargest find the largest accumulated value this will

# X x 1 endwhile return findlargest find the largest

• Homework Help
• 77
• 98% (246) 240 out of 246 people found this document helpful

This preview shows page 70 - 74 out of 77 pages.

x = x + 1 endwhile return findLargest() // find the largest accumulated value – this will tell the // program the largest “of a kind” x = 0 computerLargest = 0 playerLargest = 0 while x < LIMIT-1 if computerValues[x+1] > computerValues[computerLargest] computerLargest = x + 1 endif if playerValues[x+1] > playerValues[playerLargest] playerLargest = x + 1 endif x = x + 1 endwhile computerMatch = computerValues[computerLargest] playerMatch = playerValues[playerLargest] return finishUp() output “Computer has ”, computerMatch, “ of a kind” output “You have ”, playerMatch, “ of a kind” if computerMatch > playerMatch then output “Computer wins” 70
Programming Logic and Design, 8e Solutions 6- else if playerMatch > computerMatch then output “You win” else output “Tie” endif endif return b. Improve the dice game so that when both players have the same combination of dice, the higher value wins. For example, two 6s beats two 5s. Answer: A sample solution follows Pseudocode: start Declarations num x num y num playerMatch num computerMatch num playerLargest num computerLargest num LIMIT = 6 num SIZE = 5 num playerDice[SIZE] = 0 num computerDice[SIZE] = 0 num playerValues[LIMIT] = 0 num computerValues[LIMIT] = 0 getReady() accumulateNums() findLargest() finishUp() stop getReady() x = 0 while x < SIZE playerDice[x] = random(LIMIT) computerDice[x] = random(LIMIT) x = x + 1 endwhile x = 0 output “Computer rolled: ” while x < SIZE output computerDice[x] x = x + 1 endwhile x = 0 71
Programming Logic and Design, 8e Solutions 6- output “You rolled: ” while x < SIZE output playerDice[x] x = x + 1 endwhile return accumulateNums() x = 0 while x < LIMIT y = 0 while y < SIZE if playerDice[y] = x + 1 then playerValues[x] = playerValues[x] + 1 endif if computerDice[y] = x + 1 then computerValues[x] = computerValues[x] + 1 endif y = y + 1 endwhile x = x + 1 endwhile return findLargest() x = 0 computerLargest = 0 playerLargest = 0 while x < LIMIT-1 if computerValues[x+1] > computerValues[computerLargest] computerLargest = x + 1 endif if playerValues[x+1] > playerValues[playerLargest] playerLargest = x + 1 endif x = x + 1 endwhile computerMatch = computerValues[computerLargest] playerMatch = playerValues[playerLargest] return finishUp() output “Computer has ”, computerMatch, “ of a kind” output “You have ”, playerMatch, “ of a kind” if computerMatch > playerMatch then output “Computer wins” else if playerMatch > computerMatch then output “You win” else if computerLargest > playerLargest then output “Computer wins” else if playerLargest > computerLargest then output “You win” 72
Programming Logic and Design, 8e 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
• • • 