This is a simple Minimax Algorithm based Tic Tac Toe in Java Console. I developed this as a hobby project and there is also some bugs.So,this is not a 100% completed project,but you can learn easily from the code.There are many tic tac toe codes available and this is a different approach.
Introduction
There are 3 java files:
- tikitaka.java
- bordmaker.java
- AIPlayer.java
tikitaka is the main java file and this contains checking of status of gameplay ie Winning /Losing/Draw.
bordmaker is used for making User Interface for board in console.
AIPlayer is the java class which performs Minimax Algorithm[wikipedia]. Minimax is a recursive. You can also use Alpha Beta Prunning Method[], which is used mostly.Minimax is a beginner level.
Let’s Start…
1) Create a New Project,Name it as tikitaka (I am using NetBeans for Java projects)
2) Create class tikitaka.java, You can just read the code and it is easy to understand.This function ai.minimax(temp,player ) is the minimax function which gives the next calculated movement.
package tikitaka; import java.util.Random; import java.util.Scanner; public class tikitata { public static int player; static int flag =0; //temp var for wining the game true public static String[][] s = new String[11][11]; //bordmaker q = new bordmaker(); void check_A(){ bordmaker q = new bordmaker(); if(s[0][0]==s[1][0]){ if(s[1][0]==s[2][0]){ if(s[1][0]=="X"){ System.out.print("Player A win the Game :)\n"); q.dispbrd(); System.exit(0); } } } if(s[0][0]==s[0][1]){ if(s[0][0]==s[0][2]){ if(s[0][0]=="X"){ System.out.print("Player A win the Game :)\n"); q.dispbrd(); System.exit(0); } } } if(s[1][0]==s[1][1]){ if(s[1][0]==s[1][2]){ if(s[1][0]=="X"){ System.out.print("Player A win the Game :)\n"); q.dispbrd(); System.exit(0); } } } if(s[2][0]==s[2][1]){ if(s[2][0]==s[2][2]){ if(s[2][0]=="X"){ System.out.print("Player A win the Game :)\n"); q.dispbrd(); System.exit(0); } } } if(s[0][1]==s[1][1]){ if(s[1][1]==s[2][1]){ if(s[0][1]=="X"){ System.out.print("Player A win the Game :)\n");q.dispbrd(); System.exit(0); } } } if(s[0][2]==s[1][2]){ if(s[1][2]==s[2][2]){ if(s[0][2]=="X"){ System.out.print("Player A win the Game :)\n");q.dispbrd(); System.exit(0); } } } if(s[0][0]==s[1][1]){ if(s[1][1]==s[2][2]){ if(s[1][1]=="X"){ System.out.print("Player A win the Game :)\n");q.dispbrd(); System.exit(0); } } } if(s[0][2]==s[1][1]){ if(s[1][1]==s[2][0]){ if(s[1][1]=="X"){ System.out.print("Player A win the Game :)\n");q.dispbrd(); System.exit(0); } } } if(s[0][0]==s[1][0]){ if(s[1][0]==s[2][0]){ if(s[1][0]=="X"){ System.out.print("Player A win the Game :)\n");q.dispbrd(); System.exit(0); } } } } void check_B(){ int chb = 1;bordmaker q = new bordmaker(); if(s[0][0]==s[0][1]){ if(s[0][0]==s[0][2]){ if(s[0][2]=="O"){ System.out.print("Computer win the Game :(\n");q.dispbrd(); System.exit(0); } } } if(s[1][0]==s[1][1]){ if(s[1][0]==s[1][2]){ if(s[1][0]=="O"){ System.out.print("Computer win the Game :(\n");q.dispbrd(); System.exit(0); } } } if(s[2][0]==s[2][1]){ if(s[2][0]==s[2][2]){ if(s[2][0]=="O"){ System.out.print("Computer win the Game :(\n");q.dispbrd(); System.exit(0); } } } if(s[0][1]==s[1][1]){ if(s[1][1]==s[2][1]){ if(s[0][1]=="O"){ System.out.print("Computer win the Game :(\n");q.dispbrd(); System.exit(0); } } } if(s[0][2]==s[1][2]){ if(s[1][2]==s[2][2]){ if(s[0][2]=="O"){ System.out.print("Computer win the Game :(\n");q.dispbrd(); System.exit(0); } } } if(s[0][0]==s[1][1]){ if(s[1][1]==s[2][2]){ if(s[1][1]=="O"){ System.out.print("Computer win the Game :(\n");q.dispbrd(); System.exit(0); } } } if(s[0][2]==s[1][1]){ if(s[1][1]==s[2][0]){ if(s[0][2]=="O"){ System.out.print("Computer win the Game :(\n");q.dispbrd(); System.exit(0); } } } if(s[0][0]==s[1][0]){ if(s[1][0]==s[2][0]){ if(s[1][0]=="X"){ System.out.print("Computer win the Game :(\n");q.dispbrd(); System.exit(0); } } } } public static void main (String[] args) throws InterruptedException{ Scanner e = new Scanner (System.in); tikitata t =new tikitata(); Random r = new Random(); int low =0; int high =3; AIPlayer ai = new AIPlayer(); int a; int row,col; //entered row and column for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ s[i][j] =" "; } } System.out.print("Welcome to AI Tic-Tac_toe in Console \n"); bordmaker q = new bordmaker(); int temp =16; for(a=1;a<10;a++){ q.dispbrd(); if(a%2 !=0){ System.out.println("Enter the row and column for player A (X) "); player = 21; row= e.nextInt(); col =e.nextInt(); do{ if((row<0&&row>3)&& (col<0&&col>3)){ System.out.println("Wrong Enter,Please enter row and col again"); row= e.nextInt(); col =e.nextInt(); } if(s[row][col]!=" "){ System.out.println("Wrong Enter,Please enter row and col again"); row= e.nextInt(); col =e.nextInt(); } }while(s[row][col]!=" "); System.out.println("\n"); t.s[row][col] = "X"; t.check_A(); } else{ player = 29; int [] result = ai.minimax(temp,player ); //temp--; row = result[1]; col=result[2]; //System.out.println("BestScore"+result[0]); //System.out.println("Col is "+col); System.out.println("\n"); t.s[row][col]="O"; t.check_B(); } }//if for a%2!=0 closed System.out.println("Draw match"); } }
2) bordmaker.java
package tikitaka;
/** * * Student */ public class bordmaker extends tikitata { public void dispbrd(){ //System.out.println(" | | "); System.out.println(s[0][0]+" | "+s[0][1]+" | "+s[0][2]); System.out.println("-----------"); System.out.println(s[1][0]+" | "+s[1][1]+" | "+s[1][2]); System.out.println("-----------"); System.out.println(s[2][0]+" | "+s[2][1]+" | "+s[2][2]); //System.out.println(" | | "); } }
3) AIPlayer.java
package tikitaka; import java.util.ArrayList; import java.util.List; import com.sun.xml.internal.ws.api.pipe.NextAction; public class AIPlayer extends tikitata { //player may be computer(29) or opponent(21) public int[] minimax(int depth,int player){ ListnextMoves = generateMoves(); //System.out.println(player); int bestScore =(player==29)?Integer.MIN_VALUE:Integer.MAX_VALUE; //System.out.println(bestScore); int currentScore; int bestRow =-1; int bestCol =-1; if(nextMoves.isEmpty() || depth ==0 ) { //System.out.print("nextMoveEmpty()"); bestScore = evaluate(); } else { for (int[] moves:nextMoves){ if(player ==21){ s[moves[0]][moves[1]] = "X"; }else if(player ==29){ s[moves[0]][moves[1]] = "O"; } if(player == 29){ currentScore =minimax(depth-1, 21)[0]; //Dont know about this [0] if(currentScore >bestScore){ bestScore = currentScore; bestRow = moves[0]; bestCol = moves[1]; } } else { currentScore =minimax(depth-1, 29)[0]; //Dont know about this [0] if(currentScore 0){ Score=Score*10; } else if(Score <0){ return 0; } else { Score =1; } } else if(s[row3][col3]=="X"){ if(Score < 0){ Score =Score*10; } else if(Score >1){ return 0; } else { Score =1; } } return Score; } private List generateMoves() { List nextMoves = new ArrayList (); if(eval_A() || eval_B()){ return nextMoves; } for(int row=0;row<3;row++){ for(int col=0;col<3;col++){ if(s[row][col]==" "){ nextMoves.add(new int[] {row,col}); } } } return nextMoves; } boolean eval_A(){ if(s[0][0]==s[1][0]){ if(s[1][0]==s[2][0]){ if(s[1][0]=="X"){ return true; } } } if(s[0][0]==s[0][1]){ if(s[0][0]==s[0][2]){ if(s[0][0]=="X"){ return true; } } } if(s[1][0]==s[1][1]){ if(s[1][0]==s[1][2]){ if(s[1][0]=="X"){ return true; } } } if(s[2][0]==s[2][1]){ if(s[2][0]==s[2][2]){ if(s[2][0]=="X"){ return true;} } } if(s[0][1]==s[1][1]){ if(s[1][1]==s[2][1]){ if(s[0][1]=="X"){ return true; } } } if(s[0][2]==s[1][2]){ if(s[1][2]==s[2][2]){ if(s[0][2]=="X"){ return true; } } } if(s[0][0]==s[1][1]){ if(s[1][1]==s[2][2]){ if(s[1][1]=="X"){ return true; } } } if(s[0][2]==s[1][1]){ if(s[1][1]==s[2][0]){ if(s[1][1]=="X"){ return true; } } } if(s[0][0]==s[1][0]){ if(s[1][0]==s[2][0]){ if(s[1][0]=="X"){ return true; } } } return false; } boolean eval_B(){ if(s[0][0]==s[0][1]){ if(s[0][0]==s[0][2]){ if(s[0][2]=="O"){ return true; } } } if(s[1][0]==s[1][1]){ if(s[1][0]==s[1][2]){ if(s[1][0]=="O"){ return true; } } } if(s[2][0]==s[2][1]){ if(s[2][0]==s[2][2]){ if(s[2][0]=="O"){ return true; } } } if(s[0][1]==s[1][1]){ if(s[1][1]==s[2][1]){ if(s[0][1]=="O"){ return true; } } } if(s[0][2]==s[1][2]){ if(s[1][2]==s[2][2]){ if(s[0][2]=="O"){ return true; } } } if(s[0][0]==s[1][1]){ if(s[1][1]==s[2][2]){ if(s[1][1]=="O"){ return true; } } } if(s[0][2]==s[1][1]){ if(s[1][1]==s[2][0]){ if(s[0][2]=="O"){ return true; } } } if(s[0][0]==s[1][0]){ if(s[1][0]==s[2][0]){ if(s[1][0]=="X"){ return true; } } } return false; } }
If you need AI player.then you need to study about how the minimax algorithm works before starts doing.I tried many from internet and try to keep simple as possible.
Sources:
Pingback: Sudoku Generator in Java()