private void CalculateAllCardMoves() { MoveInfo move = null; DateTime t0 = DateTime.Now; c = 0; List <MoveInfo> cardMoves = GetAvailableCardMoves(currentBoard); c++; double bestWeight = AIHelper.GetBoardWeight(currentBoard, true); GameboardImpl bestBoard = currentBoard; foreach (MoveInfo cardMove in cardMoves) { GameboardImpl newBoard = currentBoard.MakeCopy(); cardMove.move.MakeMove(newBoard); c++; double newWeight = AIHelper.GetBoardWeight(newBoard, true); if (newWeight > bestWeight) { bestWeight = newWeight; bestBoard = newBoard; move = cardMove; } } currentBoard = bestBoard; if (move == null) { return; } if (onCalculateMove != null) { onCalculateMove(move.move); } move.c = c; move.dt = (DateTime.Now - t0).TotalMilliseconds; move.makeMoveCount = makeMoveCount; move.makeMoves = makeMoves; move.getMovesCount = getMovesCount; move.getMoves = getMoves; move.calcRouteToMultyCount = calcRouteToMultyCount; move.calcRouteToMulty = calcRouteToMulty; AIHelper.ShowLogForMove(move); CalculateAllCardMoves(); }
private GameboardImpl MakeEndOfTurn(GameboardImpl board)//ToDo: remove it { GameboardImpl newBoard = board.MakeCopy(); if (newBoard.cur_command_idx == 0) { newBoard.cur_command_idx = 1; } else { newBoard.cur_command_idx = 0; } CommandInfo command = newBoard.commands[newBoard.cur_command_idx]; foreach (UnitImpl unit in command.staff) { unit.made_move = false; } return(newBoard); }
public AI(GameboardImpl board) { this.currentBoard = board.MakeCopy(); }
private List <MoveInfo>[] GetMovesByDistForWalkerMode(GameboardImpl board, UnitImpl unit, bool isMax) //ToDo: move it to AIHelper { //1: int oldSpeed = unit.speed; unit.speed = 100; DateTime t0 = DateTime.Now; List <Move> simpleMoves = unit.GetMoves(board); getMovesCount++; getMoves += (DateTime.Now - t0).TotalMilliseconds; unit.speed = oldSpeed; List <MoveInfo> moves = new List <MoveInfo>(); //2: int maxDist = 0; foreach (Move move in simpleMoves) { MoveInfo moveInfo = new MoveInfo(move); moves.Add(moveInfo); t0 = DateTime.Now; //2.1: moveInfo.board = board.MakeCopy(); move.MakeMove(moveInfo.board); makeMoveCount++; makeMoves += (DateTime.Now - t0).TotalMilliseconds; //2.1: c++; moveInfo.weight = AIHelper.GetBoardWeight(moveInfo.board, isMax); //2.2: moveInfo.distance = (int)Math.Ceiling(move.Steps / (double)oldSpeed); if (moveInfo.distance == 0) { moveInfo.distance = 1; } if (moveInfo.distance > maxDist) { maxDist = moveInfo.distance; } } maxDist++; //it because array size will be count + 1 //3: List <MoveInfo>[] movesByDist = new List <MoveInfo> [maxDist]; for (int i = 0; i < maxDist; i++) { movesByDist[i] = new List <MoveInfo>(); } foreach (MoveInfo move in moves) { movesByDist[move.distance].Add(move); } return(movesByDist); }
private MoveInfo CalculateBoardWeight(GameboardImpl board, out double bestWeight, int depth, bool isMax, double alpha, double beta) { DateTime time0 = DateTime.Now; MoveInfo bestMoveInfo = null; bestWeight = isMax ? alpha : beta; CommandInfo command = board.commands[board.cur_command_idx]; foreach (UnitImpl unit in command.staff) { if (unit.made_move) { continue; } double candidateWeight; List <MoveInfo> moves = GetBestMoves(board, unit, isMax); bool allUnitsMoved = NoActiveUnitsCount(command.staff) == 1; foreach (MoveInfo move in moves) { DateTime t0 = DateTime.Now; GameboardImpl newBoard = board.MakeCopy(); move.move.MakeMove(newBoard); makeMoveCount++; makeMoves += (DateTime.Now - t0).TotalMilliseconds; MoveInfo nextMove = null; if (newBoard.is_game_finished) { c++; candidateWeight = AIHelper.GetBoardWeight(newBoard, isMax); } else { if (allUnitsMoved) { if (depth > 1) // && !newBoard.is_game_finished { newBoard = MakeEndOfTurn(newBoard); if (isMax) { CalculateBoardWeight(newBoard, out candidateWeight, depth - 1, !isMax, bestWeight, beta); } else { CalculateBoardWeight(newBoard, out candidateWeight, depth - 1, !isMax, alpha, bestWeight); } } else { c++; candidateWeight = AIHelper.GetBoardWeight(newBoard, isMax); } } else { if (isMax) { nextMove = CalculateBoardWeight(newBoard, out candidateWeight, depth, isMax, bestWeight, beta); } else { nextMove = CalculateBoardWeight(newBoard, out candidateWeight, depth, isMax, alpha, bestWeight); } } } //if (UpdateWeight(move, ref bestWeight, ref bestMoveInfo, isMax, candidateWeight) && depth == maxDepth) //bestMoveInfo.nextMove = nextMove; if (isMax && candidateWeight > bestWeight || !isMax && candidateWeight < bestWeight) { bestWeight = candidateWeight; bestMoveInfo = move; if (depth == maxDepth) { bestMoveInfo.nextMove = nextMove; } } if ((!isMax && candidateWeight <= alpha) || (isMax && candidateWeight >= beta)) { break; } } break; //for remove unnecessary moves } if (bestMoveInfo != null) { bestMoveInfo.dt = (DateTime.Now - time0).TotalMilliseconds; } return(bestMoveInfo); }