Overloading
Projects
2.30 |
Write Vector and Matrix classes consistent with Figure 2-28. Figure 2-28. UML class diagram of the classes for Exercise 2.30. Since a particular Vector is not associated with a particular Matrix, nor vice versa, there is no arrow connecting the classes. (This item is displayed on page 63 in the print version) |
|||||||||
2.31 |
Implement the game of Reversi (Figure 2-29).
|
2.32 |
Implement the game of Go-Moku (Figure 2-30).
|
2.33 |
Implement the game of Mancala (Figure 2-31). (Hint: Use a one-dimensional array of ints to represent the board. Number the pits counterclockwise. When sowing pebbles, if you are currently at pit i, the next pit is at position (i + 1) % 14, although your program must remember to skip this pit if it is the opponent's mancala.) |
Mancala |
---|
Players: 2 |
Object: To have the most pebbles in your mancala (goal) at the end of the game. |
Board: The Mancala board consists of 14 pits, each holding a number of pebbles. The starting position is shown below. |
|
Play: On your turn, pick up all of the pebbles in any nonempty pit on your side of the board. Proceeding counterclockwise, sow one pebble in each pit until you run out. When sowing pebbles, include your own mancala, but skip your opponent's. An example of a first move is shown below. |
Free Move: If the last pebble you sow lands in your own Mancala, you get to move again. |
Capture: If the last pebble you sow lands in a previously empty pit on your side of the board, you move that pebble, as well as any pebbles in the pit directly across the board, into your mancala. |
Game End: The game ends when, after either player's move, one player has no pebbles left in any of the six pits on her side of the board. The other player moves all of the pebbles left on his side of the board to his mancala. |
2.34 |
The Mancala rules in Figure 2-31 are the Egyptian rules. The Ethiopian rules make two changes: players may choose to sow pebbles either clockwise or counterclockwise around the board, and a move may not start from a pit containing only one pebble.The game ends when one player has no legal move. Modify the program from Project 2.33 to use the Ethiopian rules. (Hint: Remember that the % operator does not behave as modulo when the first operand is negative. This will be a problem if you evaluate (pit - 1) % 14 when pit is 0. You can get around this by first adding 14 to ensure that the first operand to % is positive.) |