12.6 Exercises

  1. Give a detailed explanation of the relationship between the board and the tic-tac-toe objects.

  2. Design 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 log 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.

  3. 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: 2 to 10 are the value 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 two cards. If you are dissatisfied 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 she or he chooses to do so. If so, the game will restart like a brand-new game. Implement this in an OO fashion. If you split the work among your objects properly, you will be able to reuse a significant chunk of code on the next problem.

  4. Add scoreboard functionality to Exercise 3. To do this, you need to make a few game modifications. First, you need to ask for the user’s name at the beginning of every game. Next, you need the ability to view the scoreboard during a game; this should be done directly after prompting for a name. If a user’s name already exists on the scoreboard, you 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, the lowest score last. The scoring criteria are defined in Table 12-1.

    Table 12-1. Blackjack scoring criteria

    Total card value

    Points given

    Bust

    –15

    21

    40

    20

    30

    19

    20

    18

    10

    17

    5

    16

    1

    < 16

    0

  5. Being able to read and modify other people’s code is essential to being a successful computer scientist. Modify tictactoe.rb and board.rb to play tic-tac-toe on boards of size 3  × 3, 6  × 6, and 9  × 9 based on the user’s choice. Adjust everything so that the game works as well on 6  × 6 and 9  × 9 boards as it does on 3  × 3 boards.

  6. The current AI for the tic-tac-toe game is not perfect. Implement the behavior discussed in this chapter’s summary to guarantee at least a draw, and ensure its correctness.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.247.81