Oracle Interview Question
Site Reliability EngineersTeam: GGN
Country: India
Interview Type: In-Person
Board.java
package com.vf.coding.problems.tictactoegame;
public class Board {
Cell[][] cell = null;
public static final int ROW=3;
public static final int COLUMN=3;
int currentRow,currentColumn;
public Board() {
cell = new Cell[ROW][COLUMN];
for (int row = 0; row < ROW; ++row) {
for (int col = 0; col < COLUMN; ++col) {
cell[row][col] = new Cell(row, col); // allocate element of the array
}
}
init();
}
private void init(){
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COLUMN; j++) {
cell[i][j].clear();
}
}
}
public void printBoard(){
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COLUMN; j++) {
cell[i][j].printCell();
}
System.out.println();
}
}
public boolean hasWon(Content player){
return (cell[currentRow][0].content == player // 3-in-the-row
&& cell[currentRow][1].content == player
&& cell[currentRow][2].content == player
|| cell[0][currentColumn].content == player // 3-in-the-column
&& cell[1][currentColumn].content == player
&& cell[2][currentColumn].content == player
|| currentRow == currentColumn // 3-in-the-diagonal
&& cell[0][0].content == player
&& cell[1][1].content == player
&& cell[2][2].content == player
|| currentRow + currentColumn == 2 // 3-in-the-opposite-diagonal
&& cell[0][2].content == player
&& cell[1][1].content == player
&& cell[2][0].content == player);
}
public boolean isDraw(Content player){
for (int row = 0; row < ROW; ++row) {
for (int col = 0; col < COLUMN; ++col) {
if (cell[row][col].content == Content.EMPTY) {
return false; // an empty seed found, not a draw, exit
}
}
}
return true; // no empty cell, it's a draw
}
}
Cell.java
package com.vf.coding.problems.tictactoegame;
public class Cell {
Content content;
int row,column;
public Cell(int row,int column) {
this.row = row;
this.column = column;
clear();
}
public void clear(){
content = Content.EMPTY;
}
public void printCell(){
switch (content) {
case CROSS:
System.out.print("X");
break;
case EMPTY:
System.out.println(" ");
break;
case NOUGHT:
System.out.println("O");
break;
}
}
}
Seed.java
package com.vf.coding.problems.tictactoegame;
public enum Seed {
CROSS,
NOUGHT,
EMPTY
}
GameState.java
package com.vf.coding.problems.tictactoegame;
public enum GameState {
CROSS_WON,
NOUGHT_WON,
PLAYING,
DRAW
}
TicTacToeGame.java
package com.vf.coding.problems.tictactoegame;
import java.util.Scanner;
public class TicTacToeGame {
private Seed currentPlayer;
private GameState currentState;
private Board board = null;
private Scanner scanner = new Scanner(System.in);
public TicTacToeGame() {
board = new Board();
init();
}
private void init(){
currentPlayer = Seed.CROSS;
currentState = GameState.PLAYING;
}
public void play(){
while(currentState.equals(GameState.PLAYING)){
makeMove();
board.printBoard();
updateGameState(currentPlayer);
switch (currentState) {
case CROSS_WON:
System.out.println("X Won!");
break;
case NOUGHT_WON:
System.out.println("Y Won!");
break;
case DRAW:
System.out.println("Its Draw. Bye!");
break;
default:
currentPlayer = currentPlayer.equals(Seed.CROSS)?Seed.NOUGHT:Seed.CROSS;
}
}
}
private void makeMove(){
boolean validInput=false;
do {
if(currentPlayer.equals(Seed.CROSS)){
System.out.println("Cross Make Your Move");
}else{
System.out.println("Nought Make Your Move");
}
int row = scanner.nextInt()-1;
int column = scanner.nextInt()-1;
if(row>=0 && row<Board.ROW && column>=0 && column<Board.COLUMN){
board.cell[row][column].content = currentPlayer;
board.currentColumn=column;
board.currentRow=row;
validInput=true;
}else{
System.out.println("This move at ("+(row+1)+","+(column+1)+") is not valid, Try again...");
}
} while (!validInput);
}
private void updateGameState(Seed currentPlayer){
if(board.hasWon(currentPlayer)){
currentState = currentPlayer == Seed.CROSS?GameState.CROSS_WON:GameState.NOUGHT_WON;
}else if(board.isDraw(currentPlayer)){
currentState = GameState.DRAW;
}
}
}
TicTacToeGameMain.java
package com.vf.coding.problems.tictactoegame;
public class TicTacToeGameMain {
public static void main(String[] args) {
TicTacToeGame game = new TicTacToeGame();
game.play();
}
}
Comments about the above implementation?
The print functions doesn't print properly, but that's not important.
Here is the solution form a blog. Its in Flex but same logic translates to Java as well.
- Kunal July 06, 2012dumbandidiot.blogspot.in/2012/01/cdata-import-mx.html