public int GetHighestValueMove(Board b, int searchRange = 1) { int[] moves = b.GetInterestingMoves(searchRange); if (moves.Length == 0) { return((b.width * b.width) / 2); } int maxVal = int.MinValue; int bestI = 0; for (int i = 0; i < moves.Length; i++) { Board copy = new Board(b); copy.ChangeSquare(moves[i]); int val; if (b.blackTurn) { val = EvaluateBoard(copy); } else { val = -EvaluateBoard(copy); } if (val > maxVal) { maxVal = val; bestI = i; } } return(moves[bestI]); }
public int RootAlphaBeta(int alpha, int beta, Board board, int depth) { Stopwatch s = new Stopwatch(); s.Start(); Board bCopy = board; int[] moves = bCopy.GetInterestingMoves(); if (moves.Length == 0) { return((board.width * board.width) / 2); } int bestId = 0; int color = board.blackTurn ? 1 : -1; string deb = ""; for (int i = 0; i < moves.Length; i++) { bCopy = new Board(board); bCopy.ChangeSquare(moves[i]); bCopy.EndTurn(); int score = -AlphaBeta(-beta, -alpha, bCopy, depth - 1, color); if (score > alpha) { alpha = score; bestId = i; deb += "Move: " + moves[i].ToString() + " has value " + score.ToString() + ".\n "; } } s.Stop(); deb += "\n\n Execution took " + s.ElapsedMilliseconds.ToString() + " miliseconds."; board.DebugWrite(deb); return(moves[bestId]); }
public int AlphaBetaOptimized(int alpha, int beta, Board b, int depth, int color, List <int> movesLast) { Board bCopy = b; if (depth == 0) { return(EvaluateBoard(bCopy) * color); } List <int> moves; if (movesLast != null) { moves = movesLast; } else { moves = new List <int>(bCopy.GetInterestingMoves()); } for (int i = 0; i < moves.Count; i++) { bCopy = new Board(b); if (bCopy.board[moves[i]] != 0) { continue; } bCopy.ChangeSquare(moves[i]); color = bCopy.blackTurn ? -1 : 1; int value = EvaluateBoard(bCopy) * color; if (value > 9000) { return(-value * depth); } else if (value < -9000) { return(-value * depth); } bCopy.EndTurn(); List <int> movesCopy = new List <int>(moves); int[] movesToAdd = FindNewMoves(bCopy, bCopy.lastMove); for (int j = 0; j < movesToAdd.Length; j++) { if (!movesCopy.Contains(movesToAdd[j])) { movesCopy.Add(movesToAdd[j]); } } int score = -AlphaBetaOptimized(-beta, -alpha, bCopy, depth - 1, color, movesCopy); if (beta != -2147483648 && score >= beta) { return(beta); } if (score > alpha) { alpha = score; } } return(alpha); }
public int GetRandomMove(Board b, int searchRange = 1) { int[] moves = b.GetInterestingMoves(searchRange); if (moves.Length == 0 && b.board[0] == 0) { return((b.width * b.width) / 2); } else if (moves.Length == 0) { return(-1); } return(moves[rand.Next(moves.Length)]); }
public int[] FindMoves(Board state, int additionalDepth) { List <int> moves = new List <int>(state.GetInterestingMoves()); //Go through the moves and remove the real bad ones for (int i = moves.Count - 1; i >= 0; i--) { Board copy = new Board(state); int alpha = int.MinValue; int beta = int.MaxValue; copy.ChangeSquare(moves[i]); copy.EndTurn(); int win = copy.CheckWin(copy.lastMove); if ((win == 1 && state.blackTurn) || (win == 2 && !state.blackTurn)) { return(new int[] { moves[i] }); } int color = copy.blackTurn ? 1 : -1; int value = AlphaBeta(alpha, beta, copy, additionalDepth, color); //Console.WriteLine("move " + moves[i] + " val " + value); //Console.Read(); if (value > 10000) { moves.RemoveAt(i); } else if (value < -10000) { return(new int[] { moves[i] }); } } if (moves.Count == 0) { return(state.GetInterestingMoves()); } return(moves.ToArray()); }
public int AlphaBeta(int alpha, int beta, Board b, int depth, int color) { Board bCopy = b; if (depth == 0) { return(EvaluateBoard(bCopy) * color); } int[] moves = bCopy.GetInterestingMoves(); for (int i = 0; i < moves.Length; i++) { bCopy = new Board(b); bCopy.ChangeSquare(moves[i]); color = bCopy.blackTurn ? -1 : 1; int value = EvaluateBoard(bCopy) * color; if (value > 9000) { return(-value * depth); } else if (value < -9000) { return(-value * depth); } bCopy.EndTurn(); int score = -AlphaBeta(-beta, -alpha, bCopy, depth - 1, color); if (beta != -2147483648 && score >= beta) { return(beta); } if (score > alpha) { alpha = score; } } return(alpha); }
public int MultiThreadedRAlphaBeta(Board board, int depth, int maxThreadNum) { Stopwatch s = new Stopwatch(); s.Start(); Queue <int> moves; if (depth > 4) { moves = new Queue <int>(FindMoves(board, 3)); } else { moves = new Queue <int>(board.GetInterestingMoves()); } //Queue<int> moves = new Queue<int>(board.GetInterestingMoves()); int moveNum = moves.Count; if (moves.Count == 0) { return((board.width * board.width) / 2); } else if (moves.Count == 1) { board.DebugWrite("Execution took " + s.ElapsedMilliseconds.ToString() + " miliseconds for " + moveNum + " move. (defmoves) = " + board.GetInterestingMoves().Length); return(moves.Dequeue()); } int bestMove = 0; int alpha = int.MinValue; int color = board.blackTurn ? 1 : -1; string deb = ""; int threadNum = 4; if (moves.Count <= 4) { threadNum = moves.Count; } else if (moves.Count >= 32) { threadNum = maxThreadNum - 2 > 4 ? maxThreadNum - 2 : 4; } else { for (int i = maxThreadNum; i >= 4; i--) { if (moves.Count % i == 0) { threadNum = i; break; } } } Thread[] threads = new Thread[threadNum]; results.Clear(); for (int i = 0; i < threadNum; i++) { int[] threadMoves; if (moves.Count > moveNum / threadNum) { threadMoves = new int[moveNum / threadNum]; } else { threadMoves = new int[moves.Count]; } for (int j = 0; j < threadMoves.Length; j++) { threadMoves[j] = moves.Dequeue(); } threads[i] = new Thread(() => { AlphaBetaFromMoves(board, depth, threadMoves); }); threads[i].Start(); } for (int i = 0; i < threadNum; i++) { threads[i].Join(); } for (int i = 0; i < threadNum; i++) { deb += "Move: " + results[i].Key.ToString() + " has value " + results[i].Value.ToString() + ".\n "; if (results[i].Value > alpha && results[i].Key != -1) { bestMove = results[i].Key; alpha = results[i].Value; } } s.Stop(); deb += "\n\n Execution took " + s.ElapsedMilliseconds.ToString() + " miliseconds for " + moveNum + " moves. (defmoves) = " + board.GetInterestingMoves().Length; //deb += "\n\n After last move FindNewMoves found " + FindNewMoves(board, board.lastMove).Length + " new moves."; board.DebugWrite(deb); return(bestMove); }
public Node(int move, Node parent, Board state, int searchRange = 1) { this.move = move; this.parent = parent; this.untriedMoves = new List <int>(state.GetInterestingMoves(searchRange)); }