Pseudocode Problems

1. Study the method below. The code functions correctly. Make any changes you think would make the code easier to read, understand, or maintain. Then write the documentation comments for the method, following the pseudocode standard. Also write a brief method header comment.

	/**
	 *  
	 */
	public void updateScore()
	{
		String str = "Score\tName\n";

		for (int i=0; i<players.size(); i++)
		{

			str += ((Player) players.get(i)).toString() + "\n";
		}

		gui.setPlayersList(str);
	}


2. Write the documentation comments for the method below, following the pseudocode standard.
    /**
     * Return an array of winners.  It looks at all the scores, and gets
     * the players
     * with the highest score.
     *
     * @return  an array of players representing the winners
     */
    public Player[] getWinners()
    {
        Player player;

        Vector winners = new Vector();

        winners.add(players.get(0));

        for (int i=1; i<players.size(); i++)
        {
            player = (Player) players.get(i);

            if (player.getScore() > ((Player) winners.get(0)).getScore())
            {
                winners = new Vector();
                winners.add(player);
            }

            else if (player.getScore() == ((Player) winners.get(0)).getScore())
            {
                winners.add(player);
            }
        }

        Player wins[] = new Player[winners.size()];
        winners.toArray(wins);

        return wins;
    }

3. The example below shows some poorly written pseudocode.  Rewrite the algorithm so it is clear and correct.  Feel free to make whatever assumptions seem appropriate about the requirements or high level design.


/**
* Deal all the cards in the deck to the n players in the game.
* Each player in turn is dealt a card until the deck is empty.
*/
public void distributeCards()

WHILE deck is not empty

    Assign card at start of the deck to each player

    Decrement number of cards on the deck

END WHILE

Set each player's hand with distributed cards


4. Write the pseudocode for the body of the addLetter() method below from the Hangman application. (Don't use break in your solution. Don't place return inside a loop.)
    /**    
     *  Add a correctly guessed letter to user's word.
     *  That is, place the user's correct guess in its
     *  proper position in the user's word.  If it occurs more
     *  than once, do ALL occurrences.
     *  @param hiddenWord the word the player is trying to guess.
     *  @param letter the player's guess.
     *  @param userWord the buffer containing correctly guessed letters.
     *  @pre letter is in the hidden word.
     *  @post userWord is updated with letter in each correct position
     */
    private void addLetter(String hiddenWord, char letter, StringBuffer userWord)

5. Write the pseudocode for the body of the method with the definition given below. (Don't use break in your solution. Don't place return inside a loop.) You should assume that the Card class has a rank() method for accessing the card's rank.
/** Evaluate a poker hand for a pair - 2 cards of equal rank.
    If the hand contains more than one pair, find the first (lowest) one.
    @param cards a List of the five cards in the hand.
    @pre cards is ordered by rank from low to high.
    @return a list containing the two cards of the pair if they exist, 
            otherwise return an empty list.
*/
    public Hand getPair(List cards)

Solution (for instructor only)