Software AG Interview Question
Java DevelopersCountry: India
Interview Type: In-Person
//Winner will be declared in constant time, no need to run loops to check winner on each move.
//This is main class to run a sample case
public class TicTacToeSample {
public static void main(String[] args) {
try {
final Board board = new Board(3);
Player X = new Player("X", board);
Player Y = new Player("Y", board);
X.takePosition(0, 0);
Y.takePosition(1, 1);
X.takePosition(2, 0);
Y.takePosition(2, 0);
Y.takePosition(1, 0);
X.takePosition(2, 2);
Y.takePosition(1, 2);
X.takePosition(2, 1);
} catch (WinnerException e) {
System.out.println(e.getMessage());
}
}
}
class Board{
private final Player[][] board;
private final int size;
public Board(int size){
this.size = size;
this.board = new Player[size][size];
}
public int getRows() {
return size;
}
public int getCols() {
return size;
}
public int getDiagonals() {
return 2;
}
public void setOnwer(Player player, int row, int col) throws IllegalStateException {
if(board[row][col] == null){
board[row][col] = player;
}else{
throw new IllegalStateException("Position already taken by player " + board[row][col]);
}
}
public Player getOnwer(int row, int col) {
return board[row][col];
}
public int totalPositions() {
return size;
}
}
@SuppressWarnings("serial")
class WinnerException extends Exception{
public WinnerException(String msg) {
super(msg);
}
}
class Player{
private final String name;
private final Board board;
private final int[] rows;
private final int[] cols;
private final int[] diagonal;
public Player(String name, Board board){
this.name = name;
this.board = board;
this.rows = new int[board.getRows()];
this.cols = new int[board.getCols()];
this.diagonal = new int[2];
}
@Override
public String toString() {
return name;
}
public void takePosition(int row, int col) throws WinnerException {
board.setOnwer(this, row, col);
if(++rows[row] == board.totalPositions()){
throw new WinnerException("Game winner is player " + this + " taken complete row " + row );
}
if(++cols[col] == board.totalPositions()){
throw new WinnerException("Game winner is player " + this + " taken complete column " + row );
}
if(row == col){
if(++diagonal[0] == board.totalPositions()){
throw new WinnerException("Game winner is player " + this + " taken complete diagonal left");
}
}
if(row + col + 1 == board.totalPositions()){
if(++diagonal[1] == board.totalPositions()){
throw new WinnerException("Game winner is player " + this + " taken complete diagonal right");
}
}
}
}
Here is the fully working Java code for 1 player vs computer Tic Tac Toe game.
Compile it. Run it!
- neerajlakhotia08 July 09, 2014