Saturday, July 25, 2015

[CC150v5] 8.8 Design Othello Game - Shuatiblog.com



[CC150v5] 8.8 Design Othello Game - Shuatiblog.com
Othello is played as follows: Each Othello piece is white on one side and black on the other. When a piece is surrounded by its opponents on both the left and right sides, or both the top and bottom, it is said to be captured and its color is flipped.
The win is assigned to the person with the most pieces. Implement the object-oriented design for Othello.

Class

  1. Game
    1. Two Player objects
    2. a board object
    3. singleton class (unless otherwise specified, this needs to be discussed)
  2. Board
    1. Keep the score (black/white count). Of course we can put score in Game Class as well, it seems logically related to the board a bit more.
    2. Array of Piece objects
    3. getScore() method
  3. Piece
    1. stores color info
    2. flip() function
  4. Player
    1. stores color info
    2. playPiece() method
  5. Color Enum
  6. Direction Enum

Functions

  1. placePiece() by the Player (which triggers the following 2 methods)
  2. private flipSection(Position fromWhere, Color c, Direction up/down/left/right) by the board
  3. private updateScore() by the board
  4. getScore() by the board

Follow up: Do we need separate Board and Game classes?

Strictly speaking, no. The drawback is adding an extra layers. A function that calls Game class is immediately calling Board class.
But keeping the objects separate allows us to have a logical separation between the board (which contains just logic involving placing pieces) and the game (which involves times, game flow, etc.).

Conclusion

This is an easy, and very standard OOD question. Keep the logic clear, design the layers and object hierarchy, and the rest of things will come naturally.
https://github.com/niannianli/Object-orientedDesign/blob/master/src/oodesign/Game8.java
public class Game8 {
private Player[] players;
private Board board;
private final int ROWS = 10;
private final int COLUMNS = 10;

private Game8() {
board = new Board(ROWS, COLUMNS);
players = new Player[2];
players[0] = new Player(Color.Black);
players[1] = new Player(Color.White);

}

public static Game8 getInstance() {
Game8 instance = null;
if (instance == null)
instance = new Game8();
return instance;

}

public Board getBoard() {
return board;
}
}

enum Direction {
left, right, up, down
}

enum Color {
White, Black
}

/*
 * The Board class managers the actual pieces themselves. It does not handle
 * much of the game play, leaving that up to the Game class.
 */

class Board {
private int blackCount = 0;
private int whiteCount = 0;
@SuppressWarnings("unused")
private Piece[][] board;

public Board(int rows, int columns) {
board = new Piece[rows][columns];
}

public void initialize() {
// initialize center black and white pieces
}

// attempt to place a piece of color color at(row, column)
// return true if we were successful
public boolean placeColor(int row, int column, Color color) {
return false;

}

// flips pieces starting at(row, column)
// and proceeding in direction d
@SuppressWarnings("unused")
private int flipSection(int row, int column, Color color, Direction d) {
return column;
}

public int getScoreForColor(Color c) {
if (c == Color.Black)
return blackCount;
else
return whiteCount;
}

// update board with additional newPieces pieces of color
// newColor, decrease score of opposite color
public void updateScore(Color newColor, int newPieces) {
}

}

/*
 * As described earlier, we implement the black and white pieces with the Piece
 * class, which has a simple Color variable representing whetehr it is a black
 * or white piece
 */
class Piece {
private Color color;

public Piece(Color c) {
color = c;
}

public void flip() {
if (color == Color.Black)
color = Color.White;
else
color = Color.Black;
}

public Color getColor() {
return color;
}
}

/*
 * The Player holds only a very limited amount of information. It does not even
 * hold its own score, but it does have a method one can call to get the score.
 * Player.getScore() will call out ot the GameManager to retrieve this value.
 */
class Player {
private Color color;

public Player(Color c) {
color = c;
}

public int getScore() {
return 0;
}

public boolean playPiece(int r, int c) {
return Game8.getInstance().getBoard().placeColor(r, c, color);
}

public Color getColor() {
return color;
}
}
http://www.math.grin.edu/~rebelsky/Courses/152/98S/Assignments/design.04.html
Read full article from [CC150v5] 8.8 Design Othello Game - Shuatiblog.com

Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts