|
import java.util.Arrays; |
|
import java.util.Scanner; |
|
|
|
/** |
|
* Created by molayodecker on 28/11/2016. |
|
* Welcome |
|
* Before your interview, write a program that lets two humans play a game of Tic Tac Toe in a terminal. |
|
* The program should let the players take turns to input their moves. |
|
* The program should report the outcome of the game. |
|
* During your interview, you will pair on adding support for a computer player to your game. |
|
* You can start with random moves and make the AI smarter if you have time. |
|
* |
|
* Game : Tic Tac Toe game |
|
* | | |
|
* -----|-----|----- |
|
* | | |
|
* -----|-----|----- |
|
* | | |
|
* |
|
* |
|
*/ |
|
|
|
public class playGround { |
|
private char[][] playground; |
|
private boolean gameOngoing = true; |
|
|
|
public playGround() { |
|
this.playground = new char[3][3]; |
|
|
|
for (int row = 0; row < playground.length; row++){ |
|
Arrays.fill(playground[row], ' '); |
|
} |
|
} |
|
|
|
/* |
|
* This method just displays the tictactoe playground |
|
* */ |
|
public void displayPlayGround(){ |
|
for (int row = 0; row < playground.length; row++){ |
|
for(int col = 0; col < playground[0].length; col++){ |
|
System.out.print("\t" +playground[row][col]); |
|
if(col == 0 || col == 1){ |
|
System.out.print("\t|"); |
|
} |
|
} |
|
if(row == 0 || row == 1) { |
|
System.out.printf("%n-------------------------%n"); |
|
} |
|
} |
|
} |
|
|
|
/* |
|
This method checks if the row or col the player is trying to play is empty or filled. |
|
if it's not empty return false else add the player in that position |
|
* */ |
|
public boolean play(char player, int row, int col){ |
|
if(row >= 0 && row <= 2 && col >= 0 && col <= 2){ |
|
if(playground[row][col] != ' '){ |
|
return false; |
|
}else { |
|
playground[row][col] = player; |
|
return true; |
|
} |
|
|
|
} |
|
return false; |
|
}// end of play |
|
|
|
/*This method checks if the game is ongoing or still active |
|
The default value for gameOngoing is true |
|
So this method sets the gameActive method to true |
|
* */ |
|
public boolean gameActive(){ |
|
return gameOngoing; |
|
} |
|
|
|
/* |
|
* This method ask the player to enter the rows and columns he want to play. |
|
* If the move is valid (Meaning if it has not been played) it plays that position |
|
* */ |
|
public void askPlayer(char player){ |
|
Scanner stdin = new Scanner(System.in); |
|
int row,col; |
|
do{ |
|
System.out.printf("Player %s, please enter a row (1 - 3): ", player); |
|
row = stdin.nextInt(); |
|
System.out.printf("Player %s, Please enter a column (1 - 3): ", player); |
|
col = stdin.nextInt(); |
|
}while (inValid(row,col)); |
|
|
|
play(player,row-1,col-1); |
|
} |
|
|
|
/* Invalid method checks to see if the move is between 1 - 3 and not empty. |
|
If the move is valid return true if not return false |
|
* */ |
|
public boolean inValid(int row, int col){ |
|
if(row > 3 || row < 1 || col > 3 || col < 1 || !isEmpty(row,col)){ |
|
return true; |
|
}else{ |
|
return false; |
|
} |
|
|
|
} |
|
|
|
public boolean isEmpty(int row, int col){ |
|
if(playground[row-1][col-1] == ' '){ |
|
return true; |
|
}else { |
|
System.out.println("Wrong move"); |
|
return false; |
|
} |
|
} |
|
|
|
/* |
|
The checkwinner() method checks to see if 'X' or 'O' has been able to form a straight horizontal, Vertical |
|
or diagonal pattern. |
|
* */ |
|
public boolean checkWinner(){ |
|
for(int row = 0; row < playground.length; row++){ |
|
if(playground[row][0] == playground[row][1] && playground[row][1] == playground[row][2] && playground[row][0] != ' '){ |
|
System.out.println("The winner is " + playground[row][0]); |
|
gameOngoing = false; |
|
} |
|
} |
|
|
|
for(int col = 0; col < playground.length; col++){ |
|
if(playground[0][col] == playground[1][col] && playground[1][col] == playground[2][col] && playground[0][col] != ' '){ |
|
System.out.println("The winner is " + playground[0][col]); |
|
gameOngoing = false; |
|
} |
|
} |
|
|
|
if(playground[0][0] == playground[1][1] && playground[1][1] == playground[2][2] && playground[0][0] != ' '){ |
|
System.out.println("The winner is " + playground[0][0]); |
|
gameOngoing = false; |
|
} |
|
|
|
if(playground[0][2] == playground[1][1] && playground[1][1] == playground[2][0] && playground[0][2] != ' '){ |
|
System.out.println("The winner is " + playground[0][2]); |
|
gameOngoing = false; |
|
} |
|
|
|
return false; |
|
} |
|
|
|
} |