Skip to content

Instantly share code, notes, and snippets.

@Manikkumar1988
Last active April 16, 2019 16:39
Show Gist options
  • Save Manikkumar1988/49f902c2a3aacd02e9564c03c6b1ea9e to your computer and use it in GitHub Desktop.
Save Manikkumar1988/49f902c2a3aacd02e9564c03c6b1ea9e to your computer and use it in GitHub Desktop.
Skeleton of Tictactoe Game Guided by TDD Using object oriented design
public class Board {
private final int BOUNDARY;
private final Square boardLayout[][];
public Board(int boundary) {
this.BOUNDARY = boundary;
boardLayout = new Square[BOUNDARY][BOUNDARY];
}
public boolean hasEmptyPosition() {
}
public boolean hasWon(Player player) {
return isVerticallyFilled(player) || isHorizontallyFilled(player);
}
private boolean isHorizontallyFilled(Player player) {
}
private boolean isVerticallyFilled(Player player) {
}
public void placeMarker(Player player, Position position) {
if(isLegalMove(getSquare(position))) {
player.play(getSquare(position));
}
}
private Square getSquare(Position position) {
}
private boolean isLegalMove(Square square) {
}
}
public class Game {
private static final int BOUNDARY = 3;
private Board board;
private Queue<Player> players;
public Game() {
board = new Board();
players = new LinkedList<>();
addPlayers();
}
private void addPlayers() {
players.add(new Player("X"));
players.add(new Player("O"));
}
private void startGame() throws IOException {
do {
Player currentPlayer = players.remove();
System.out.println("Enter position for Player "+currentPlayer.toString());
java.io.BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
board.placeMarker(currentPlayer,new Position(Integer.parseInt(in.readLine()),Integer.parseInt(in.readLine())));
players.add(currentPlayer);
System.out.println(board.toString());
if(board.hasWon(currentPlayer)){
break;
}
}while (board.hasEmptyPosition());
}
public static void main(String[] args) {
Game game= new Game();
game.startGame();
}
}
public class Player {
private final String type;
Player(String type) {
this.type = type;
}
public void play(Square square) {
square.setPlayer(this);
}
}
public class Square {
private final Position position;
private Player player;
public Square(Position position) {
this.position = position;
}
public void setPlayer(Player player) {
this.player = player;
}
public boolean isEmpty() {
return player == null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment