Last active
April 16, 2019 16:39
-
-
Save Manikkumar1988/49f902c2a3aacd02e9564c03c6b1ea9e to your computer and use it in GitHub Desktop.
Skeleton of Tictactoe Game Guided by TDD Using object oriented design
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Player { | |
private final String type; | |
Player(String type) { | |
this.type = type; | |
} | |
public void play(Square square) { | |
square.setPlayer(this); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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