import java.util.Random;
import java.util.Scanner;
public class tictactoe {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
char[][] board = new char[3][3];
char defaultPlayer = 'X';
int currentMove = -1;
boolean errorCheck;
fillEmptySpaces(board, ' ');
System.out.println("****************" );
System.out.println(" Tic-Tac-Toe!" );
System.out.println("****************");
printBoard(board);
while (!finalGame(board)) {
do {
errorCheck = true;
System.out.print("Please enter column[Letter] and row[#]: " +currentMove);
String userMove = in.nextLine().toUpperCase();
{
if (userMove.equals("A1")){
currentMove = 1;
}
else if (userMove.equalsIgnoreCase("A2")){
currentMove = 4;
}
else if (userMove.equalsIgnoreCase("A3")){
currentMove = 7;
}
else if (userMove.equalsIgnoreCase("B1")){
currentMove = 2;
}
else if (userMove.equalsIgnoreCase("B2")){
currentMove = 5;
}
else if (userMove.equalsIgnoreCase( "B3")){
currentMove = 8;
}
else if (userMove.equalsIgnoreCase( "C1")){
currentMove = 3;
}
else if (userMove.equalsIgnoreCase("C2")){
currentMove = 6;
}
else if (userMove.equalsIgnoreCase("C3")){
currentMove = 9;
}
else{
errorCheck = false;
System.out.println("That move is invalid! Please enter move again.");
}
}
} while(errorCheck == false);
userMove(defaultPlayer, currentMove, board);
computerMove(defaultPlayer, currentMove, board);
printBoard(board);
}
Results(board);
}
/*********************************************************************
* Method Name: MOVE_INFO
* Description: Char map that shows where to put the X's and O's
*
* Each char represents the the index of the element of 2D array.
*
* move = 1; [0][1]
* move = 2; [0][2]
* move = 3; [0][3]
* move = 4; [1][1]
* move = 5; [1][2]
* move = 6; [1][3]
* move = 7; [2][1]
* move = 8; [2][2]
* move = 9; [2][3]
*
*********************************************************************/
public static final char[][] MOVE_INFO ={{'1', '2', '3'},
{ '4', '5', '6'},
{ '7', '8', '9'}};
/*********************************************************************
* Method Name: fillEmptySpaces
* Parameters: a, k
* Returns: NONE
* Description: It fills all the elements in the array with spaces so
* the program can check if it's anymore moves left.
*********************************************************************/
public static void fillEmptySpaces(char[][] a, char k) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] = k;
}
}
}
/*********************************************************************
* Method Name: userMove
* Parameters: player(user, 'X')
* move
* board
* Returns: none
* Description: Uses the currMove and converts it to move. Each move
* corresponds to the index of an array. See above.
*********************************************************************/
public static void userMove(char player, int move, char[][] board) {
int row, col;
if (!finalGame(board)){
row = (move - 1)/ board.length;
col = (move - 1) % board[row].length;
board[row][col] = 'X';
}
}
/*********************************************************************
* Method Name: computerMove
* Parameters: player(computer, 'O')
* move
* board
* Returns: none
* Description: Uses random class to generate a double to pick a random
* place for the O to go.
*********************************************************************/
public static void computerMove(char player, int move, char[][] board){
int row, col;
if (!finalGame(board)){
do {
Random randomNumbers = new Random();
move = (int)(randomNumbers.nextDouble() *9) +1;
row = (move - 1)/ board.length;
col = (move - 1) % board[row].length;
} while ((board[row][col] !=' '));
board[row][col] = 'X';
}
}
/*********************************************************************
* Method Name: printBoard
* Parameters: board
* Returns: NONE
* Description: Prints out the display of the board using nested for
* loops.
*********************************************************************/
public static void printBoard(char[][] board) {
String VerticalDividers = " | | ";
String HorizaontalDividers = "------------";
System.out.println();
System.out.println(" A" +" B" +" C");
for (int i = 0; i < board.length; i++) {
int sideLegend= i+1;
System.out.println( VerticalDividers + " " + sideLegend );
for (int j = 0; j < board[i].length; j++) {
System.out.print(" " + board[i][j] + " ");
if (j < board[i].length - 1) {
}
else {
System.out.println();
}
}
if (i < board.length - 1) {
System.out.println(HorizaontalDividers);
}
}
System.out.println();
}
/*********************************************************************
* Method Name: MovesLeft
* Parameters: board
* Returns: true/false
* Description: Searches the array to find any blank spaces. If there
* then it returns false so the program goes again.
* if it there are spaces, it'll return true so it'll stop
* the game and searches for winning conditions.
*********************************************************************/
public static boolean MovesLeft(char[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == ' ') {
return false;
}
}
}
return true;
}
/*********************************************************************
* Method Name: finalGame
* Parameters: board
* Returns: true or false
* Description: Uses isWinner to find whether or not the game is truly
* over for X/O.
*********************************************************************/
public static boolean finalGame(char[][] board) {
return MovesLeft(board)
|| checkingWinningCondition('X', board)
|| checkingWinningCondition('O', board);
}
/*********************************************************************
* Method Name: checkingWinningCondition
* Parameters: player - player to find condition for
* board
* Returns: boolean value
* Description: Goes through all the possible winning conditions
* Returns value true if condition is found.
*********************************************************************/
public static boolean checkingWinningCondition(char player, char[][] board) {
// Checks Horizontal condition
for (int row = 0; row < 3; row++) {
if (board[row][0] == player && board[row][1] == player
&& board[row][2] == player) {
return true;
}
}
// Checks Vertical condition
for (int col = 0; col < 3; col++) {
if (board[0][col] == player && board[1][col] == player
&& board[2][col] == player) {
return true;
}
}
// Checks Diagonals Condition
if (board[0][0] == player && board[1][1] == player
&& board[2][2] == player) {
return true;
} else if (board[0][2] == player && board[1][1] == player
&& board[2][0] == player) {
return true;
} else {
return false;
}
}
/*********************************************************************
* Method Name: Results
* Parameters: player(computer, 'O')
* move
* board
* Returns: none
* Description: Uses checkWinningCondition to check for winning condition
* If condition is found, it outputs it into text and ends the
* input of user move at main.
*********************************************************************/
public static void Results(char[][] board) {
if (checkingWinningCondition('O', board)) {
System.out.println("O is the winner!");
}
else if (checkingWinningCondition('X', board)) {
System.out.println("X is the winner!");
}
else {
System.out.println("The game is CAT!.");
}
}
}