27.10.2014 Views

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Solutions</strong> to Chapter 7 | Object Oriented Design<br />

7.8 O<strong>the</strong>llo is played as follows: Each O<strong>the</strong>llo piece is white on one side <strong>and</strong> black on <strong>the</strong><br />

o<strong>the</strong>r. When a piece is surrounded by its opponents on both <strong>the</strong> left <strong>and</strong> right sides,<br />

or both <strong>the</strong> top <strong>and</strong> bottom, it is said to be captured <strong>and</strong> its color is flipped. On your<br />

turn, you must capture at least one of your opponent’s pieces. The game ends when<br />

ei<strong>the</strong>r user has no more valid moves, <strong>and</strong> <strong>the</strong> win is assigned to <strong>the</strong> person with <strong>the</strong><br />

most pieces. Implement <strong>the</strong> object oriented design for O<strong>the</strong>llo.<br />

SOLUTION<br />

pg 62<br />

O<strong>the</strong>llo has <strong>the</strong>se major steps:<br />

2. Game () which would be <strong>the</strong> main function to manage all <strong>the</strong> activity in <strong>the</strong> game:<br />

3. Initialize <strong>the</strong> game which will be done by constructor<br />

4. Get first user input<br />

5. Validate <strong>the</strong> input<br />

6. Change board configuration<br />

7. Check if someone has won <strong>the</strong> game<br />

8. Get second user input<br />

9. Validate <strong>the</strong> input<br />

10. Change <strong>the</strong> board configuration<br />

11. Check if someone has won <strong>the</strong> game...<br />

NOTE: The full code for O<strong>the</strong>llo is contained in <strong>the</strong> code attachment.<br />

1 public class Question {<br />

2 private final int white = 1;<br />

3 private final int black = 2;<br />

4 private int[][] board;<br />

5<br />

6 /* Sets up <strong>the</strong> board in <strong>the</strong> st<strong>and</strong>ard o<strong>the</strong>llo starting positions,<br />

7 * <strong>and</strong> starts <strong>the</strong> game */<br />

8 public void start () { ... }<br />

9<br />

10 /* Returns <strong>the</strong> winner, if any. If <strong>the</strong>re are no winners, returns<br />

11 * 0 */<br />

12 private int won() {<br />

13 if (!canGo (white) && !canGo (black)) {<br />

14 int count = 0;<br />

1 6 3<br />

<strong>Cracking</strong> <strong>the</strong> <strong>Coding</strong> <strong>Interview</strong> | Concepts <strong>and</strong> Algorithms

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!