private List <char[]> GenerateMove(char[] location) { BoardCommon b = new BoardCommon(); List <char[]> piecesToMove = new List <char[]>(); char[] copyOfBoard = new char[50]; int[] neighboursList; for (int ind = 0; ind < location.Length; ind++) { if (location[ind] == 'W') { neighboursList = b.Neighbours(ind); foreach (int neighbour in neighboursList) { if (location[neighbour] == 'x') { copyOfBoard = (char[])location.Clone(); copyOfBoard[ind] = 'x'; copyOfBoard[neighbour] = 'W'; if (b.CloseMill(neighbour, copyOfBoard)) { GenerateRemove(copyOfBoard, piecesToMove); } else { piecesToMove.Add(copyOfBoard); } } } } } return(piecesToMove); }
public List <char[]> GenerateAdd(char[] board) { BoardCommon b = new BoardCommon(); List <char[]> piecesListAdd = new List <char[]>(); char[] copyOfBoard; for (int ind = 0; ind < board.Length; ind++) { if (board[ind] == 'x') { copyOfBoard = (char[])board.Clone(); copyOfBoard[ind] = 'W'; if (b.CloseMill(ind, copyOfBoard)) { piecesListAdd = b.GenerateRemove(copyOfBoard, piecesListAdd); } else { piecesListAdd.Add(copyOfBoard); } } } return(piecesListAdd); }
private List <char[]> GenerateHopping(char[] location) { BoardCommon b = new BoardCommon(); List <char[]> hoppingList = new List <char[]>(); char[] copyOfBoard = new char[50]; for (int ind = 0; ind < location.Length; ind++) { if (location[ind] == 'W') { for (int j = 0; j < location.Length; j++) { if (location[j] == 'x') { copyOfBoard = (char[])location.Clone(); copyOfBoard[ind] = 'x'; copyOfBoard[j] = 'W'; if (b.CloseMill(j, copyOfBoard)) { GenerateRemove(copyOfBoard, hoppingList); } else { hoppingList.Add(copyOfBoard); } } } } } return(hoppingList); }
private List <char[]> GenerateRemove(char[] board, List <char[]> l) { BoardCommon b = new BoardCommon(); List <char[]> piecesListRemove = l.Select(item => (char[])item.Clone()).ToList(); for (int ind = 0; ind < board.Length; ind++) { if (board[ind] == 'B') { if (!(b.CloseMill(ind, board))) { char[] copyOfBoard = (char[])board.Clone(); //Console.WriteLine("Remove black piece at postion " + ind); copyOfBoard[ind] = 'x'; //Removed Black piece from board piecesListRemove.Add(copyOfBoard); } else { char[] copyOfBoard = (char[])board.Clone(); piecesListRemove.Add(copyOfBoard); } } } return(piecesListRemove); }
public char[] MinMax(char[] val, int depth, bool isOpening, double alpha, double beta) { BoardCommon b = new BoardCommon(); ABGame mid = new ABGame(); if (depth > 0) { depth--; List <char[]> blackChild = new List <char[]>(); char[] minValue = new char[50]; char[] maxValue; blackChild = GenerateBlackMove(val, isOpening); double v = Double.PositiveInfinity; for (int ind = 0; ind < blackChild.Count; ind++) { maxValue = MaxMin(blackChild[ind], depth, isOpening, alpha, beta); double staticEstimate = isOpening ? StaticEstimationOpening(minValue) : mid.StaticEstimationMidEndGame(minValue, isOpening); if (v > staticEstimate) { v = staticEstimate; _ABMiniMaxEstimate = v; minValue = blackChild[ind]; } if (v <= alpha) { return(minValue); } else { beta = Math.Min(v, beta); } } return(minValue); } else if (depth == 0) { _ABPositions_Evaluated++; } return(val); }
public char[] MaxMin(char[] val, int depth, bool isOpening, double alpha, double beta) { BoardCommon b = new BoardCommon(); ABGame mid = new ABGame(); if (depth > 0) { depth--; List <char[]> child = new List <char[]>(); char[] minValue; char[] maxValue = new char[50]; child = isOpening ? GenerateAdd(val) : mid.GenerateMovesMidgameEndgame(val); //so that midgame move and generate add are called by passing parameter from calling function double v = Double.NegativeInfinity; for (int ind = 0; ind < child.Count; ind++) { minValue = MinMax(child[ind], depth, isOpening, alpha, beta); double staticEstimate = isOpening ? StaticEstimationOpening(minValue) : mid.StaticEstimationMidEndGame(minValue, isOpening); if (v < staticEstimate) { v = staticEstimate; _ABMiniMaxEstimate = v; maxValue = child[ind]; } if (v >= beta) { return(maxValue); } else { alpha = Math.Max(v, alpha); } } return(maxValue); } else if (depth == 0) { _ABPositions_Evaluated++; } return(val); }
public int StaticEstimationMidEndGame(char[] board, bool isOpening) { BoardCommon b = new BoardCommon(); int numWhitePieces = 0; int numBlackPieces = 0; List <char[]> blackMoves = new List <char[]>(); blackMoves = b.GenerateBlackMove(board, isOpening); //send midgame parameter so that generatemovemidgame is called and not generate add int blackMovesCount = blackMoves.Count(); for (int ind = 0; ind < board.Length; ind++) { numWhitePieces = board[ind] == 'W' ? numWhitePieces + 1 : numWhitePieces; numBlackPieces = board[ind] == 'B' ? numBlackPieces + 1 : numBlackPieces; } if (numBlackPieces <= 2) { return(10000); } else if (numWhitePieces <= 2) { return(-10000); } else if (blackMovesCount == 0) { return(10000); } else { return((1000 * (numWhitePieces - numBlackPieces)) - blackMovesCount); } }
static void Main(string[] args) { while (true) { Console.WriteLine("Do you prefer Improved - I / Unimproved - U static estimation : (I/U)"); string isImproved = Console.ReadLine(); if (isImproved.ToUpper() == "U") { Console.WriteLine("Enter MiniMaxGame/MiniMaxGameBlack: (W/B)"); string gameType = Console.ReadLine(); if (gameType == "W") { Console.WriteLine("Enter the input file location : "); string inputFile = Console.ReadLine(); Console.WriteLine("Enter the output file location : "); string outputFile = Console.ReadLine(); Console.WriteLine("Enter depth : "); int depth = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Is your game Opening or MidGame : (O/M) "); string isOpeningInput = Console.ReadLine(); Console.WriteLine("MiniMax / AlphaBeta : (M/A) "); string miniMaxOrAlphaBeta = Console.ReadLine(); try { using (StreamReader sr = new StreamReader(inputFile)) { char[] inputPiecePosition = new char[50]; string wholeInput; // read lines from the file while ((wholeInput = sr.ReadLine()) != null) { inputPiecePosition = wholeInput.ToCharArray(); } BoardCommon b = new BoardCommon(); MiniMaxOpening mOpen = new MiniMaxOpening(); ABOpening aBOpening = new ABOpening(); Console.WriteLine("Input Board Position " + new String(inputPiecePosition)); bool isOpening = isOpeningInput.ToUpper() == "O" ? true : false; bool isMiniMax = miniMaxOrAlphaBeta.ToUpper() == "M" ? true : false; char[] afterMiniMax = isMiniMax ? b.MaxMin(inputPiecePosition, depth, isOpening) : aBOpening.MaxMin(inputPiecePosition, depth, isOpening, Double.NegativeInfinity, Double.PositiveInfinity); Console.WriteLine("New Board Position " + new String(afterMiniMax)); if (isMiniMax) { Console.WriteLine("Position evaluated " + b._Positions_Evaluated); Console.WriteLine("Minimax Estimate " + b._MiniMaxEstimate); using (StreamWriter file = new StreamWriter(outputFile)) { file.WriteLine("New Board Position " + new String(afterMiniMax)); file.WriteLine("Position evaluated " + b._Positions_Evaluated); file.WriteLine("Minimax Estimate " + b._MiniMaxEstimate); } } else { Console.WriteLine("Position evaluated " + aBOpening._ABPositions_Evaluated); Console.WriteLine("Minimax Estimate " + aBOpening._ABMiniMaxEstimate); using (StreamWriter file = new StreamWriter(outputFile)) { file.WriteLine("New Board Position " + new String(afterMiniMax)); file.WriteLine("Position evaluated " + aBOpening._ABPositions_Evaluated); file.WriteLine("Minimax Estimate " + aBOpening._ABMiniMaxEstimate); } } } } catch (Exception e) { Console.WriteLine("Exception Occured in Part 1"); } Console.ReadLine(); } else { try { Console.WriteLine("Is your game Opening or MidGame : (O/M) "); string isOpeningInput = Console.ReadLine(); bool isOpening = isOpeningInput.ToUpper() == "O" ? true : false; MiniMaxGameBlack miniMaxGameBlack = new MiniMaxGameBlack(); MiniMaxOpeningBlack miniMaxOpeningBlack = new MiniMaxOpeningBlack(); if (isOpening) { miniMaxOpeningBlack.MiniMaxOpeningForBlack(); } else { miniMaxGameBlack.MiniMaxGameForBlack(); } } catch (Exception ee) { Console.WriteLine("Exception Occured in MiniMax Black"); } } } else { try { Console.WriteLine("Is your game Opening or MidGame : (O/M)"); string isOpeningInput = Console.ReadLine(); if (isOpeningInput.ToUpper() == "O") { MiniMaxOpeningImproved miniMaxOpeningImproved = new MiniMaxOpeningImproved(); miniMaxOpeningImproved.MiniMaxOpeningForImproved(); } else { MiniMaxGameImproved miniMaxGameImproved = new MiniMaxGameImproved(); miniMaxGameImproved.MiniMaxGameForImproved(); } } catch (Exception ee) { Console.WriteLine("Exception Occured in MiniMax Improveds"); } } } }