http://algojava.blog...oe-ai-java.html

However, the file itself did not include implementation of the algorithm merely the algorithm itself so I thought it would be rather easy to implement it however I have had a few issues namely when setting a position in the board to the player value that is changed. The compiler says that my newMove method is throwing out NullPointerException error , I am unsure as to how to amend this, the code is here:

NOTE: I know I copied and pasted to a large extent () , I just wish to see the method in action.

import java.io.*; public class MainFrame { /** * @param args the command line arguments */ /* the board */ public static int board[][]; public static char sBoard[][]; public static boolean endGame = false; /* empty */ public static final int EMPTY = 0; /* player one */ public static final int ONE = 1; /* player two */ public static final int TWO = 2; public void TicTacToeAI() { board = new int[3][3]; } /* get the board value for position (i,j) */ public int getBoardValue(int i,int j) { if(i < 0 || i >= 3) return EMPTY; if(j < 0 || j >= 3) return EMPTY; return board[i][j]; } /* calculate the winning move for current token */ public int []nextWinningMove(int token) { for(int i=0;i<3;i++) for(int j=0;j<3;j++) if(getBoardValue(i, j)==EMPTY) { board[i][j] = token; boolean win = isWin(token); board[i][j] = EMPTY; if(win) return new int[]{i,j}; } return null; } /* calculate the best move for current token */ public int []nextMove(int token) { /* if we can move on the next turn */ int winMove[] = nextWinningMove(token); if(winMove!=null) return winMove; /* choose the move that prevent enemy to win */ for(int i=0;i<3;i++) for(int j=0;j<3;j++) if(getBoardValue(i, j)==EMPTY) { board[i][j] = token; boolean ok = nextWinningMove(token==ONE ? TWO : ONE ) == null; board[i][j] = EMPTY; if(ok) return new int[]{i,j}; } /* lucky position in the center of board*/ if(getBoardValue(1, 1)==EMPTY) return new int[]{1,1}; /* choose available move */ for(int i=0;i<3;i++) for(int j=0;j<3;j++) if(getBoardValue(i, j)==EMPTY) return new int[]{i,j}; /* no move is available */ return null; } /* determine if current token is win or not win */ public boolean isWin(int token) { final int DI[]={-1,0,1,1}; final int DJ[]={1,1,1,0}; for(int i=0;i<3;i++) for(int j=0;j<3;j++) { /* we skip if the token in position(i,j) not equal current token */ if(getBoardValue(i, j)!=token) continue; for(int k=0;k<4;k++) { int ctr = 0; while(getBoardValue(i+DI[k]*ctr, j+DJ[k]*ctr)==token) {ctr++; if(ctr==3) return true; endGame = true; } } } return false; } public void paintBoard(char[][] displayBoard) { for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[i].length; j++ ) { System.out.println(displayBoard[i][j]); } } } public void newMove(int xpos, int ypos, int token) { if (token == ONE) { board[xpos][ypos] = token; } if (token == TWO) { board[xpos][ypos] = token; } updateBoard(board); paintBoard(sBoard); } public void updateBoard(int [][] board) { for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[i].length; j++) { if (board[i][j] == ONE) { sBoard[i][j] = 'x'; } else if (board[i][j] == TWO) { sBoard[i][j] = 'o'; } else { sBoard[i][j] = '_'; } } } } public static void main(String[] args) throws Exception { MainFrame mn = new MainFrame(); BufferedReader inKb = new BufferedReader (new InputStreamReader (System.in)); int token = 1; while (endGame == false) { System.out.println ("player " + token + " Enter your xpos value(x,y): "); int xpos = Integer.parseInt(inKb.readLine()); System.out.println ("player " + token + " Enter your ypos value(x,y): "); int ypos = Integer.parseInt(inKb.readLine()); mn.newMove( xpos, ypos, token); token = 2; for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[i].length; j++) { mn.getBoardValue(i,j); mn.nextMove(token); mn.nextWinningMove(token); } mn.updateBoard(board); } //Note: token = current player } } }

**Any help or a link to a different Algorithm/method to use would be much appreciated.**

thanks,

v0rtex

This post has been edited by **v0rtex**: 28 April 2011 - 11:43 AM