This was asked as an interview question.
Design a tic tac toe using object oriented principles. The interviewer said he is not interested in the logic and he wants only the design. I gave the design as below but I was not completely satisfied. Please let me know if there are any suggestions/improvements.
The interviewer was very particular about two things
My approach :
Rough sketch of classes.
interface Player {
Player takeTurn();
void markNextBox();
}
.
public class TicTacToeGameEngine implements GameRule{
@Override
public Boolean isWinner(Game game) {
// Check winner logic
return false;
}
@Override
public Square locateSquareToMark(Game game) {
List<Square> squares= game.getBoard().getFilledSquares();
//go through the list of squares and figure out a square to mark
return square;
}
}
.
public class Computer implements Player {
GameRule g = new TicTacToeGameEngine();
@Override
public void markNextBox() {
g.locateSquareToMark(game);
}
@Override
public Player takeTurn() {
// TODO Auto-generated method stub
return null;
}
}
.
public interface GameRule {
Boolean isWinner(Game game);
Square locateSquareToMark(Game game);
}
//Similar implementation for Human
Now the difficulties I found in this design is
Any flaws, improvements to design will be appreciated.