You are on page 1of 4

D:\Documents\workspace\Maxit\src\com\fredwilby\cp103\project1\MinimaxPlayer.

java

Tuesday, January 21, 2014 4:39 PM

package com.fredwilby.cp103.project1; import java.awt.Point; import java.util.ArrayList; import java.util.Arrays; public class MinimaxPlayer extends Player { private int playerNum; private GameNode gameTree, state; long gn; /** *CreatesanewMinimaxPlayerandbuildsitsinternalrepresentationofthegame. */ public MinimaxPlayer(int[][] board, Point active, int playerNum) { super(board, active, playerNum); gn = 0; gameTree = new GameNode(new int[]{ 0, 0 }, 0, new Point(active), boardCopy(board)); addNodes(gameTree); //Buildthegametree System.out.println(gn); evaluateTree(gameTree); //addvaluestothetree this.playerNum = playerNum; state = gameTree; } /** *Copiesa2darrayofintstoanew2darray.Usedforcopyinggameboards. */ private int[][] boardCopy(int[][] board) { int[][] result = new int[board.length][board[0].length]; /*iteratethroughcolumnsofthearray*/ for(int col = 0; col < result.length; col++) { /*iteratethroughrowsofthearray*/ for(int row = 0; row < result.length; row++) { result[col][row] = board[col][row]; } } return result; } /** *PickstheGameNodewiththelowestvalue(thebest)fromthechildrenofthecurrentstate. */ private GameNode bestChild(GameNode parent) { int best = Integer.MAX_VALUE, bestIndex = 1; for(int x = 0; x < parent.children.size(); x++) { if(parent.children.get(x).value < best)
-1-

D:\Documents\workspace\Maxit\src\com\fredwilby\cp103\project1\MinimaxPlayer.java

Tuesday, January 21, 2014 4:39 PM

{ best = parent.children.get(x).value; bestIndex = x; } } return parent.children.get(bestIndex); } /** * */ @Override public Point makeMove(Point playerMove) { /*Updategamestate*/ for(GameNode ch : state.children) { if(ch.active.equals(playerMove)) { state = ch; break; } } GameNode move = bestChild(state); state = move; return move.active; } private void addNodes(GameNode parent) { ArrayList<Point> moves = PossibleMoves(parent.board, parent.active); if(moves.size() == 0) { return; } else { for(Point move : moves) { int[][] temp = boardCopy(parent.board); int[] newscores = new int [] { parent.scores[0], parent.scores[1] }; newscores[parent.playerTurn] += temp[move.x][move.y]; temp[move.x][move.y] = 1; GameNode child = new GameNode(newscores, 1parent.playerTurn, move, temp); parent.children.add(child); addNodes(child); } }

} private void evaluateTree(GameNode tree) { if(tree.children.size() == 0) { tree.value = tree.scores[playerNum] tree.scores[1playerNum];


-2-

D:\Documents\workspace\Maxit\src\com\fredwilby\cp103\project1\MinimaxPlayer.java

Tuesday, January 21, 2014 4:39 PM

} else { int sum = 0; for(GameNode child : tree.children) { evaluateTree(child); sum += child.value; } tree.value = sum; } } public static ArrayList<Point> PossibleMoves(int[][] board, Point active) { ArrayList<Point> moves = new ArrayList<Point>(); for(int x = 0; x < board.length; x++) { if(board[x][active.y] > 0) { moves.add(new Point(x, active.y)); } } for(int y = 0; y < board[active.x].length; y++) { if(board[active.x][y] > 0) { moves.add(new Point(active.x, y)); } } return moves; } class GameNode { int value; int [] scores; int playerTurn; Point active; int[][] board; public GameNode(int[] scores, int playerTurn, Point active, int[][] board) { gn++; if(gn % 1000 == 0) System.out.println(gn); this.scores = scores; this.playerTurn = playerTurn; this.active = active; this.board = board; children = new ArrayList<GameNode>(); } ArrayList<GameNode> children; }

-3-

D:\Documents\workspace\Maxit\src\com\fredwilby\cp103\project1\MinimaxPlayer.java

Tuesday, January 21, 2014 4:39 PM

-4-

You might also like