private static void UndoMove(MainGame b, Mutare myMove, string tempToName, int tempToValue, string tempFromName, int tempFromValue) { b.pieceIdBoard[myMove.From].PieceName = tempFromName; b.pieceIdBoard[myMove.From].PieceValue = tempFromValue; b.pieceIdBoard[myMove.To].PieceName = tempToName; b.pieceIdBoard[myMove.To].PieceValue = tempToValue; }
private void ExecutaMutarea(MainGame m, Mutare myMove) { m.pieceIdBoard[myMove.To].PieceName = m.pieceIdBoard[myMove.From].PieceName; m.pieceIdBoard[myMove.To].PieceValue = m.pieceIdBoard[myMove.From].PieceValue; m.pieceIdBoard[myMove.From].PieceName = "-"; m.pieceIdBoard[myMove.From].PieceValue = 0; }
internal List <Mutare> EvaluareMutare(int adancime, MainGame m) { List <Mutare> ret = new List <Mutare>(); List <Mutare> generator = new List <Mutare>(); if (m.DatePartidaCastig.Culoare == CuloarePiesa.Negru) { generator = m.GetToateMutarilePosibileWhite(m); } else { generator = m.GetToateMutarilePosibileBlack(m); } List <Mutare> newGameMoves = generator.OrderByDescending(o => o.Value).ToList(); int alpha = Int32.MinValue; int beta = Int32.MaxValue; int bestMove = alpha; Mutare bestMoveFound = new Mutare(); bool immediateAttack = false; for (int j = 0; j < newGameMoves.Count; j++) { Mutare myMove = newGameMoves[j]; string tempToName = m.pieceIdBoard[myMove.To].PieceName; int tempToValue = m.pieceIdBoard[myMove.To].PieceValue; string tempFromName = m.pieceIdBoard[myMove.From].PieceName; int tempFromValue = m.pieceIdBoard[myMove.From].PieceValue; AttackValue = 0; //Make the move ExecutaMutarea(m, myMove); //check for immediate attack immediateAttack = CheckForImmediateAttack(m, immediateAttack, myMove, tempToValue); // if none, get the best move if (!immediateAttack) { int value = MinMax(adancime - 1, false, alpha, beta, m); if (value >= bestMove) { if (bestMoveFound.To == 0 && bestMoveFound.From == 0) { bestMoveFound = myMove; } bestMoveFound = myMove; ret.Add(bestMoveFound); bestMove = value; } } UndoMove(m, myMove, tempToName, tempToValue, tempFromName, tempFromValue); immediateAttack = false; } List <Mutare> retturnList = ret.OrderBy(o => o.Value).ToList(); return(retturnList); }
private static bool CheckForImmediateAttack(MainGame m, bool skip, Mutare myMove, int tempToValue) { List <Mutare> pnewGameMoves; if (m.DatePartidaCastig.Culoare == CuloarePiesa.Alb) { pnewGameMoves = m.GetToateMutarilePosibileWhite(m); } else { pnewGameMoves = m.GetToateMutarilePosibileBlack(m); } foreach (Mutare mov in pnewGameMoves) { if (mov.To == myMove.To) { if (tempToValue <= Math.Abs(m.pieceIdBoard[myMove.To].PieceValue)) { skip = true; } } } return(skip); }
public int MinMax(int depth, bool isMaximiser, int alpha, int beta, MainGame m) { //end of specified traversal length, evaluate board if (depth == 0) { int k = -EvaluateBoard(m); return(k); } List <Mutare> newGameMoves = new List <Mutare>(); List <Mutare> pnewGameMoves = new List <Mutare>(); if (isMaximiser) { if (m.DatePartidaCastig.Culoare == CuloarePiesa.Negru) { pnewGameMoves = m.GetToateMutarilePosibileWhite(m); } else { pnewGameMoves = m.GetToateMutarilePosibileBlack(m); } newGameMoves = pnewGameMoves.OrderByDescending(o => o.Value).ToList(); int bestMove = Int32.MinValue; for (int i = 0; i < newGameMoves.Count; i++) { Mutare myMove = newGameMoves[i]; string tempToName = m.pieceIdBoard[myMove.To].PieceName; int tempToValue = m.pieceIdBoard[myMove.To].PieceValue; string tempFromName = m.pieceIdBoard[myMove.From].PieceName; int tempFromValue = m.pieceIdBoard[myMove.From].PieceValue; int minValue = m.pieceIdBoard[myMove.To].PieceValue; ExecutaMutarea(m, myMove); bestMove = Math.Max(bestMove, MinMax(depth - 1, false, alpha, beta, m)); UndoMove(m, myMove, tempToName, tempToValue, tempFromName, tempFromValue); alpha = Math.Max(alpha, bestMove); if (alpha >= beta) { break; } } return(bestMove); } else { if (m.DatePartidaCastig.Culoare != CuloarePiesa.Negru) { pnewGameMoves = m.GetToateMutarilePosibileWhite(m); } else { pnewGameMoves = m.GetToateMutarilePosibileBlack(m); } newGameMoves = pnewGameMoves.OrderByDescending(o => o.Value).ToList(); int bestMove = Int32.MaxValue; for (int i = 0; i < newGameMoves.Count; i++) { Mutare myMove = newGameMoves[i]; int maxValue = m.pieceIdBoard[myMove.To].PieceValue; string tempToName = m.pieceIdBoard[myMove.To].PieceName; int tempToValue = m.pieceIdBoard[myMove.To].PieceValue; string tempFromName = m.pieceIdBoard[myMove.From].PieceName; int tempFromValue = m.pieceIdBoard[myMove.From].PieceValue; int minValue = m.pieceIdBoard[myMove.To].PieceValue; ExecutaMutarea(m, myMove); bestMove = Math.Min(bestMove, MinMax(depth - 1, true, alpha, beta, m)); UndoMove(m, myMove, tempToName, tempToValue, tempFromName, tempFromValue); beta = Math.Min(beta, bestMove); if (alpha >= beta) { break; } } return(bestMove); } }