Java gomoku minimax -
i'm trying create strategy play against human or strategy game gomoku. have kind of minimax function, although don't quite understand how work , have getscore function, should send best score minimax ? problem getscore function not recognize if there 4 or 5 circles in row/col/diagonally.
here code:
public class jarmostrategyv1 implements computerstrategy { public static int lrow = 0; public static int lcol = 0; public static int drow = 0; public static int dcol = 0; public static final int e = 0; public static final int x = 1; // black public static final int o = -1; // white public static final int winscore = 100; private static final int wincount = 5; public location getmove(simpleboard board, int player) { // let's operate on 2-d array int[][] b = board.getboard(); system.out.println(getscore(b, player)); (int row = 0; row < b.length; row++) { (int col = 0; col < b[0].length; col++) { if (b[row][col] == simpleboard.empty) { // first empty location return new location(row, col); } } } return null; } @override public string getname() { return "student name"; } public static int minimax(int[][] board, int player, int depth) { if (getscore(board, player) == winscore) { // return winscore; } if (depth == 2) { return getscore(board, player); } int max = integer.min_value; if (player == -1){ max = integer.max_value; } system.out.println(max); list<location> possiblemoves = getpossiblemoves(board); (location loc : possiblemoves) { board[loc.getrow()][loc.getcolumn()] = player; int newplayer = 0 - player; if(newplayer == 1){ int value = minimax(board, newplayer,depth + 1); if(value < max) { max = value; } } if (newplayer == -1){ int value = minimax(board, newplayer, depth + 1); if (value > max) { max = value; } } board[loc.getrow()][loc.getcolumn()] = e; } return max; } public static int getscore(int[][] board, int muutuja) { //int yks = 0; (int row = 0; row < board.length; row++) { (int col = 0; col < board[row].length; col++) { if (board[row][col] == muutuja) { if (row <= (board.length - 5)) { if (col <= board.length && getcount(board, row, col, 0, 1, muutuja, wincount) >= (wincount - 1) && getcount(board, row, (col + 4), 0, 1, e, 1 ) >= 1) return 1; // - 4 in row if (row >= 1 && getcount(board, row, col, 1, 0, muutuja, wincount) >= (wincount -1) && getcount(board, (row - 1), col, 1, 0, e, 1) == 1) return 1; if (getcount(board, row, col, 1, 0, muutuja, wincount) >= wincount) return 100; // | 5 in row if (col <= wincount && getcount(board, row, col, 1, 1, muutuja, wincount) >= wincount) return 100; // \ if (col >= wincount && getcount(board, row, col, 1, -1, muutuja, wincount) >= wincount) return 100; // / } if (col <= wincount && getcount(board, row, col, 0, 1, muutuja, wincount) >= wincount) return 100; // - } } } return 0; } public static int getcount(int[][] board, int row, int col, int rowd, int cold, int player, int test) { int count = 0; (int = 0; < test; i++) { if (board[row + * rowd][col + * cold] == player) count++; else break; } return count; } public static arraylist<location> getpossiblemoves(int[][] board) { arraylist<location> availablemoves = new arraylist<location>(); (int row = 0; row < board.length; row++) { (int col = 0; col < board[row].length; col++) { if (board[row][col] == e) { availablemoves.add(new location(row, col)); } } } return availablemoves; } }
there seems kind of problem getscore, when run code can play while until gomoku app crashes.
if want try out can open via eclipse. download project file: http://www68.zippyshare.com/v/fewl2qwc/file.html import eclipse projects/workspace. , have build path 2 jar files in lib folder. note: can edit files in gomoku.strategies package.
the stacktrace shows exception. add debug print or run debugger
java.lang.illegalargumentexception: it's computer's turn, pass player makemove() @ gomoku.game.makemove(game.java:476)
Comments
Post a Comment