Home
Preface
Chapter Selection
Programming Examples
Homework Problems
Homework Solutions MEA's
Search Useful Links

Homework Problems

Chapter 8 Homework Problems

8.1Give a detailed explanation of the relationship between the Board and the TicTacToe objects.
 
8.2Write a function that will split a string into multiple strings with the divisor being a single character. For example, if the divisor was “a” and the string was “fad” it should return [“f”, “d”]. Make sure to investigate the edge cases (unique conditions that can cause the program to function incorrectly) and account for them in your function.
 
8.3Write a program that reads a file and gives a line count, word count, and character count.
 
8.4Make a simple word frequency program. It should read in a text file and print out the number of times each word in the file occurred. A word is any group of characters separated by a space.
 
8.5Design and implement a game of “Pick a Number.” This is a simple guessing game where you think of a number between 1 and n in your head (don't let the computer know this number), tell the computer what n is, and then let the computer guess the number. If the guess is too low, you should let the computer know that the guess was too low; likewise, if the guess was too high you should also let the computer know. The computer can take multiple turns (HINT: it should take lg n turns), but simply guessing every number between 1 and n is far from an acceptable answer. Implement this program two different ways: procedurally and object oriented.
 
8.6Blackjack

Design and implement a simplified version of the card game Blackjack. The rules are as follows:

1) A standard 52 card deck is used and shuffled well.

2) The cards have the following values: Two – Ten are the values of the card, Jack to King have a value of 10, and Ace      has a value of 1 or 11.

3) Only you are playing, trying to get the cards to add up to 21 without going over 21. If you go over 21 you lose.

4) Initially you are dealt 2 cards. If you are unsatisfied with these cards you can ask for more. It is not a trade, additional      cards are added. If you feel like you have enough cards and do not want to bust, you can “stay” and end this round of      Blackjack.

After the game has ended the user should be able to play again if they choose to do so. If they do the game will restart like a brand new game. Implement this in a OO fashion. If you split up work between your objects properly, you will be able to reuse a significant chunk of code on the next problem.

 
8.7War!

Design and implement the card game War. The rules are as follows:

1) A standard 52 card deck is used and shuffled well.

2) Player One gets the top half of the deck, Player Two the bottom half.

3) Both players cannot see their cards; they can only draw the top card off the deck.

4) Each player draws the top card from their deck. If Player One's card is of a higher rank (two lowest to ace highest, face      doesn't matter) then he gets Player Two's card and puts both his card and Player Two's card at the bottom of his deck.      If Player Two's card is higher, he takes Player One's card and his card and puts them at the bottom of his deck. If the      cards are equal, there is a war. During a war the entire front line dies. What this means is that the first time a war      takes place all the two's are eliminated from the game, then all the three's, then the four's, and so on. Both of the      equal cards that began the war are also eliminated from the game.

5) First person to run out of cards loses.

Sometimes this card game ends in a stalemate. That is, there is a certain sequence of cards that will keep the game going forever. Be able to end games that result in a stalemate.

This game is completely systematic, so there is not really much for the user to do. Simply let the computer play against itself, output every turn, the winner, and how many turns it took for the winner to win. If there is a stalemate report that as well. Implement this in a OO fashion.

 
8.8 Question 8.6 says, “If you split up work between your objects properly, you will be able to reuse a significant chunk of code on the next problem.” What is this significant chunk of code?
 
8.9Add scoreboard functionality to 4.3. To do this we need to make a few game modifications. First, we need to ask for the user's name at the beginning of every game. Next we need the ability to view the scoreboard in game; this should be done directly after prompting for a name. If a user's name already exists on the scoreboard, we will modify that user's score. Otherwise the new user will be added to the scoreboard in the proper position. The scoreboard needs to remember scores even after the program is exited (HINT: Write scores out to a file). If a score surpasses another score, the ordering of scores on the scoreboard needs to change. The highest score should be listed first, lowest score last. The scoring criterion is as follows:
	Total Card Value	  Points Given
	Bust (Value > 21)	     -15
	     21	               40
	     20	               30
	     19	               20
	     18	               10
	     17	                5
	     16	                1
	    Else	                0
8.10

Note: The file “StudentRankings.txt” is required for this problem and is available on the Book Website and CD.

The file “StudentRankings.txt” contains the grades of fifty students, each of which take the same five classes. Calculate the Grade Point Average (GPA) of these 50 students. An 'A' is worth 4 points, 'B' is worth 3 points, 'C' is worth 2 points, 'D' is worth 1 point, and a failing grade is not worth anything. Calculate each students GPA and add a column to each students entry in “StudentRankings.txt.” Finally, order these entries so that the highest GPA is at the top of “StudentRankings.txt” and the lowest at the bottom. This must be done in an object oriented manner.

 
8.11The classic version of Tic-Tac-Toe implemented in this chapter uses a 3x3 grid. Alternate versions of Tic-Tac-Toe use different size boards. One of the most popular alternate versions uses a 9x9 grid. What changes would be required to make this code work for a 9x9 grid?
 
8.12Being able to read and modify other peoples code is essential to being a successful computer scientist. Modify TicTacToe.rb and Board.rb to play TicTacToe on boards of size 3x3, 6x6, and 9x9 – based upon the users choice. Adjust everything so that the game works as well on 6x6 and 9x9 boards as it does on 3x3 boards.