private List <MoveSet> FindPossibleMoves(GridCell cell) { List <MoveSet> moveSets = new List <MoveSet>(); for (int y = 1; y <= 8; y++) { for (int x = 1; x <= 8; x++) { if (_moveChecker.ValidMove(_grid, cell.Piece.Id, y, x)) { _grid.MovePiece(cell.Piece.Id, x, y); var piece = _grid.GetByCoords(x, y).Piece; var king = _grid.GetKingByColor(piece.Color); if (!_checkChecker.CheckForCheck(_grid, king.Piece.Id, king.XCoord, king.YCoord)) { MoveSet move = new MoveSet() { XCoord = x, YCoord = y, Piece = piece, Score = 16 }; moveSets.Add(move); } _grid.RevertHistory(); } } } return(moveSets); }
private MoveSet HighestScoredMove(GridCell cell) { List <MoveSet> moveSets = FindPossibleMoves(cell); if (!moveSets.Any()) { return(new MoveSet() { Score = -1 }); } moveSets = ScoreMoves(moveSets); MoveSet highestScoredMove = moveSets[0]; foreach (var m in moveSets) { if (m.Score >= highestScoredMove.Score) { highestScoredMove = m; } } return(highestScoredMove); }
public MoveSet DecideMove(Grid grid, string color) { _billysColor = color; _grid = grid; List <GridCell> billyPieces = grid.GetAllByColor(color); List <MoveSet> AllBestMoves = new List <MoveSet>(); foreach (var p in billyPieces) { if (p.Piece == null) { continue; } var highestScoredMove = HighestScoredMove(p); if (highestScoredMove.Score == -1) { continue; } grid.MovePiece(highestScoredMove.Piece.Id, highestScoredMove.XCoord, highestScoredMove.YCoord); List <GridCell> op1 = grid.GetAllByOppositeColor(color); MoveSet obm1 = FindBestMoveForColor(op1); if (obm1.Score == -1) { grid.RevertHistory(); highestScoredMove.Score += 10000; AllBestMoves.Add(highestScoredMove); continue; } grid.MovePiece(obm1.Piece.Id, obm1.XCoord, obm1.YCoord); highestScoredMove.Score -= (obm1.Score / 2); List <GridCell> bp1 = grid.GetAllByColor(color); MoveSet bbm1 = FindBestMoveForColor(bp1); if (bbm1.Score == -1) { continue; } grid.MovePiece(bbm1.Piece.Id, bbm1.XCoord, bbm1.YCoord); highestScoredMove.Score += bbm1.Score; List <GridCell> op2 = grid.GetAllByOppositeColor(color); MoveSet obm2 = FindBestMoveForColor(op2); if (obm2.Score == -1) { grid.RevertHistory(); grid.RevertHistory(); grid.RevertHistory(); highestScoredMove.Score += 10000; AllBestMoves.Add(highestScoredMove); continue; } grid.MovePiece(obm2.Piece.Id, obm2.XCoord, obm2.YCoord); highestScoredMove.Score -= (obm2.Score / 2); List <GridCell> bp2 = grid.GetAllByColor(color); MoveSet bbm2 = FindBestMoveForColor(bp2); if (bbm2.Score == -1) { continue; } highestScoredMove.Score += bbm2.Score / 2; AllBestMoves.Add(highestScoredMove); grid.RevertHistory(); grid.RevertHistory(); grid.RevertHistory(); grid.RevertHistory(); } return(PickBestMove(AllBestMoves)); }