Thanks. I am posting entire code here. check out.
/*//Interface for all constants. * To change this template, choose Tools | Templates * and open the template in the editor. */package consoletictactoe;/** * * @author Administrator */public interface gameProperties { int size = 3; int maxDepth=3; char board[][] = new char[size][size]; int check_Win(); void play(); void Display(); void Player1(); void Player2();}
//Main body
package consoletictactoe;import java.util.*;public class Game implements gameProperties { int maxdepth = 4; int i; int j; int status = 0; Scanner read = new Scanner(System.in); public Game() { for (i = 0; i < size; i++) { for (j = 0; j < size; j++) { board[j] = ' '; } } status = 0; } public int check_Win()//Winning Conditions { int stat = 0; //Testing for Horizontal Win for (int row = 0; row < size; ++row) { for (int col = 0; col < size - 2; ++col) { if (board[row][col] == board[row][col + 1] && board[row][col] == board[row][col + 2] && board[row][col] != ' ') { // System.out.println("Horizontal Win"); stat = -1; } } } //Testing for Vertical Win for (int row = 0; row < size - 2; ++row) { for (int col = 0; col < size; ++col) { if (board[row][col] == board[row + 1][col] && board[row][col] == board[row + 2][col] && board[row][col] != ' ') { //System.out.println("Vertical Win"); stat = -1; } } }//Testing for positive diagonal win (0,4,8) for (int row = 0; row < size - 2; ++row) { for (int col = 0; col < size - 2; ++col) { if (board[row][col] == board[row + 1][col + 1] && board[row][col] == board[row + 2][col + 2] && board[row][col] != ' ') { // System.out.println("+ve Diagonal Win"); stat = -1; } } }//Testing for negative diagonal win (2,4,6) for (int row = 2; row < size; ++row) { for (int col = 0; col < size - 2; ++col) { if (board[row][col] == board[row - 1][col + 1] && board[row - 2][col + 2] == board[row][col] && board[row][col] != ' ') { // System.out.println("-ve Diagonal Win"); stat = -1; } } } return stat; } public void play()//Main method for handlng of the game. { int player = 1; int no = 1; int moves = 1; while (status != -1 && moves < 10) { no = player % 2; switch (no) { case 1: { Computer(); } break; case 0: Player2(); break; } status = check_Win(); if (status == -1) { System.out.println("Game Over - Player " + player + " Wins "); } moves++; if (moves > 9 && status != -1) { System.out.println("Draw"); } player = 3 - player; // System.out.println("moves= " + moves); } } public void Display()//Displays the board. { System.out.print(board[0][0] + " | " + board[0][1] + " | " + board[0][2] + "\n"); System.out.print("---+----+---\n"); System.out.print(board[1][0] + " | " + board[1][1] + " | " + board[1][2] + "\n"); System.out.print("---+----+---\n"); System.out.print(board[2][0] + " | " + board[2][1] + " | " + board[2][2] + "\n"); } public void Player1()// Moves of Player 1. { int row, column, square; System.out.println("Make your move PLAYER1, enter the square number,the starting square number is 0");//This is done purposely to show how to conver a 1d array in 2-d array. square = read.nextInt(); row = square / 3; column = square % 3; boolean valid = Condition(row, column); if (valid == true) { if (board[row][column] == ' ') { board[row][column] = 'X'; } System.out.println(); System.out.println(); System.out.println(); System.out.println(); Display(); } else { Player1(); } } public void Player2()//Moves of Player 2 { int row, column, square; System.out.println("Make your move PLAYER2, enter the square number,the starting square number is 0");//This is done purposely to show how to conver a 1d array in 2-d array. square = read.nextInt(); row = square / 3; column = square % 3; boolean valid = Condition(row, column); if (valid == true) { if (board[row][column] == ' ') { board[row][column] = '0'; } System.out.println(); System.out.println(); System.out.println(); System.out.println(); Display(); } else { Player2(); } } public void Computer() { int index; index = get_Move(true); board[index / 3][index % 3] = 'X'; Display(); } public int get_Move(boolean turn) { int alpha = -2; int beta = 2; int index = 0; int sq; int eval = 0; char board1[] = new char[size * size]; for (sq = 0; sq < 9; sq++) { board1[sq] = board[sq / 3][sq % 3]; } for (sq = 0; sq < 9; sq++) { if (board1[sq] == ' ') { board1[sq] = (turn ? 'X' : 'O'); eval = -negamax(!turn, maxdepth - 1, -beta, -alpha, board1); if (eval > alpha) { alpha = eval; index = sq; } } } return index; } int negamax(boolean xTurn, int depth, int alpha, int beta, char[] board1) { int eval; eval = check_Win(board1); //System.out.println("eval " + eval); if ((eval == -1) || (depth == 0)) { return eval; } if (board1[0] != ' ' && board1[1] != ' ' && board1[2] != ' ' && board1[3] != ' ' && board1[4] != ' ' && board1[5] != ' ' && board1[6] != ' ' && board1[7] != ' ' && board1[8] != ' ') { return check_Win(board1); } for (int sq = 0; sq < 9; sq++) { if (board1[sq] == ' ') { board1[sq] = (xTurn ? 'X' : 'O'); eval = -negamax(!xTurn, depth - 1, -beta, -alpha, board1); board1[sq] = ' '; if (eval >= beta) { return beta; } if (eval > alpha) { alpha = eval; } } } System.out.println("stat= " + eval); return alpha; } boolean Condition(int row, int column) { try { if (row > 2 || column > 2) { System.out.println("Out of Bounds"); } if (board[row][column] != ' ') { System.out.println("Already Occupied"); } else { return true; } } catch (Exception e) { } return false; } public int check_Win(char[] board1) { int stat = 0; if (board1[0] == board1[1] && board1[0] == board1[2] && board1[0] != ' ' && board1[1] != ' ' && board1[2] != ' ') { stat = -1; } if (board1[3] == board1[4] && board1[3] == board1[5] && board1[3] != ' ' && board1[4] != ' ' && board1[5] != ' ') { stat = -1; } if (board1[6] == board1[7] && board1[6] == board1[8] && board1[6] != ' ' && board1[7] != ' ' && board1[8] != ' ') { stat = -1; } if (board1[0] == board1[3] && board1[0] == board1[6] && board1[0] != ' ' && board1[3] != ' ' && board1[6] != ' ') { stat = -1; } if (board1[1] == board1[4] && board1[1] == board1[7] && board1[1] != ' ' && board1[4] != ' ' && board1[7] != ' ') { stat = -1; } if (board1[2] == board1[5] && board1[2] == board1[8] && board1[2] != ' ' && board1[5] != ' ' && board1[8] != ' ') { stat = -1; } if (board1[0] == board1[4] && board1[0] == board1[8] && board1[0] != ' ' && board1[4] != ' ' && board1[8] != ' ') { stat = -1; } if (board1[2] == board1[4] && board1[2] == board1[6] && board1[2] != ' ' && board1[4] != ' ' && board1[6] != ' ') { stat = -1; } return stat; } }
//Main method()
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package consoletictactoe;public class TicTacToe { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here for(int x=0;x<25;x++) System.out.println(); Game g=new Game(); g.Display(); g.play(); }}
No it plays moves but does not care for wining or blocking.
[Edited by - mandar9589 on March 31, 2010 12:19:10 PM]