private void MakeTurnNodes(TurnNode currentNode, Lib.PlayersSide whoseTurn, int recurciveLevel) { if (recurciveLevel <= NumberOfRecurciveLevels) { List <TurnNode> TurnNodes = new List <TurnNode>(); if (recurciveLevel == 0) { currentNode = new TurnNode(boardState, -1, 0); // корень дерева } List <IChecker> allies = GetAllies(whoseTurn, currentNode.state); List <IChecker> enemies = GetEnemies(whoseTurn, currentNode.state); foreach (var checker in allies) { if (checker.IsKing) { CheckKingKilling(currentNode, whoseTurn, checker, recurciveLevel); CheckKingMovement(currentNode, whoseTurn, checker, recurciveLevel); } else { CheckKilling(currentNode, whoseTurn, checker, recurciveLevel); CheckMovement(currentNode, whoseTurn, checker, recurciveLevel); } } } }
public FieldState(IChecker[][] grid, List <string> stepsHistory, Lib.PlayersSide turn) { Grid = new IChecker[9][]; WhoWasKing = new Dictionary <IChecker, bool>(); for (int i = 1; i <= 8; i++) { Grid[i] = new IChecker[9]; for (int j = 1; j <= 8; j++) { Grid[i][j] = grid[i][j]; if (grid[i][j] != null) { WhoWasKing.Add(grid[i][j], grid[i][j].IsKing); } } } StepsHistory = new List <string>(); foreach (string message in stepsHistory) { StepsHistory.Add(message); } Turn = turn; }
private void CheckKingKilling(TurnNode currentNode, Lib.PlayersSide whoseTurn, IChecker checker, int level) { int i = checker.CurrentCoord.Row; int j = checker.CurrentCoord.Column; while ((i <= 8) && (j <= 8)) { if (checker.CheckPossibilityToKill(new Coord(i, j), gameField)) { currentNode.AddChild(KillingRecurcion(currentNode, whoseTurn, checker, new Coord(i, j), level)); } i++; j++; } i = checker.CurrentCoord.Row; j = checker.CurrentCoord.Column; while ((i <= 8) && (j >= 1)) { if (checker.CheckPossibilityToKill(new Coord(i, j), gameField)) { currentNode.AddChild(KillingRecurcion(currentNode, whoseTurn, checker, new Coord(i, j), level)); } i++; j--; } i = checker.CurrentCoord.Row; j = checker.CurrentCoord.Column; while ((i >= 1) && (j <= 8)) { if (checker.CheckPossibilityToKill(new Coord(i, j), gameField)) { currentNode.AddChild(KillingRecurcion(currentNode, whoseTurn, checker, new Coord(i, j), level)); } i--; j++; } i = checker.CurrentCoord.Row; j = checker.CurrentCoord.Column; while ((i >= 1) && (j >= 1)) { if (checker.CheckPossibilityToKill(new Coord(i, j), gameField)) { currentNode.AddChild(KillingRecurcion(currentNode, whoseTurn, checker, new Coord(i, j), level)); } i--; j--; } }
private TurnNode MovingRecurcion(TurnNode currentNode, Lib.PlayersSide whoseTurn, IChecker checker, Coord destination, int level) { IChecker[][] state = CreateStateCopy(currentNode.state); state = ExecuteOperationToMove(state, state[checker.CurrentCoord.Row][checker.CurrentCoord.Column], destination); TurnNode newNode = new TurnNode(state, EstimatingFunction(state), level); bool a = (side == Lib.PlayersSide.BLACK && checker is BlackChecker) || (side == Lib.PlayersSide.WHITE && checker is WhiteChecker); if (level == 0 && a) { MovingCommand command = new MovingCommand(game, checker, destination); newNode.turnCommand = command; //присваеваем команду на первом уровне рекурсии TurnNodes.Add(newNode); } MakeTurnNodes(newNode, AnotherPlayer(whoseTurn), level + 1); return(newNode); }
public void Capitulate(Lib.PlayersSide side) { for (int i = 1; i <= 8; i++) { for (int j = 1; j <= 8; j++) { if (side == Lib.PlayersSide.BLACK && GameField.Grid[i][j] is BlackChecker) { GameField.Grid[i][j] = null; } if (side == Lib.PlayersSide.WHITE && GameField.Grid[i][j] is WhiteChecker) { GameField.Grid[i][j] = null; } } } CheckGameOver(); }
private List <IChecker> GetEnemies(Lib.PlayersSide side, IChecker[][] currentBoardState) { List <IChecker> enemies = new List <IChecker>(); int a = (side == Lib.PlayersSide.BLACK) ? 0 : 1; for (int i = 1; i < 9; i++) { for (int j = 1; j < 9; j++) { IChecker checker = currentBoardState[i][j]; int b = (checker is BlackChecker) ? 0 : 1; if (currentBoardState[i][j] != null && a != b) { enemies.Add(currentBoardState[i][j]); } } } return(enemies); }
private void CheckKilling(TurnNode currentNode, Lib.PlayersSide whoseTurn, IChecker checker, int level) { List <TurnNode> turnNodes = new List <TurnNode>(); if (checker.CheckPossibilityToKill(new Coord(checker.CurrentCoord.Row + 2, checker.CurrentCoord.Column + 2), gameField)) { turnNodes.Add(KillingRecurcion(currentNode, whoseTurn, checker, new Coord(checker.CurrentCoord.Row + 2, checker.CurrentCoord.Column + 2), level)); } if (checker.CheckPossibilityToKill(new Coord(checker.CurrentCoord.Row - 2, checker.CurrentCoord.Column + 2), gameField)) { turnNodes.Add(KillingRecurcion(currentNode, whoseTurn, checker, new Coord(checker.CurrentCoord.Row - 2, checker.CurrentCoord.Column + 2), level)); } if (checker.CheckPossibilityToKill(new Coord(checker.CurrentCoord.Row + 2, checker.CurrentCoord.Column - 2), gameField)) { turnNodes.Add(KillingRecurcion(currentNode, whoseTurn, checker, new Coord(checker.CurrentCoord.Row + 2, checker.CurrentCoord.Column - 2), level)); } if (checker.CheckPossibilityToKill(new Coord(checker.CurrentCoord.Row - 2, checker.CurrentCoord.Column - 2), gameField)) { turnNodes.Add(KillingRecurcion(currentNode, whoseTurn, checker, new Coord(checker.CurrentCoord.Row - 2, checker.CurrentCoord.Column - 2), level)); } currentNode.AddChild(turnNodes); }
private void CheckMovement(TurnNode currentNode, Lib.PlayersSide whoseTurn, IChecker checker, int level) { List <TurnNode> turnNodes = new List <TurnNode>(); if (checker.CheckPossibilityToMove(new Coord(checker.CurrentCoord.Row + 1, checker.CurrentCoord.Column + 1), gameField)) { turnNodes.Add(MovingRecurcion(currentNode, whoseTurn, checker, new Coord(checker.CurrentCoord.Row + 1, checker.CurrentCoord.Column + 1), level)); } if (checker.CheckPossibilityToMove(new Coord(checker.CurrentCoord.Row - 1, checker.CurrentCoord.Column + 1), gameField)) { turnNodes.Add(MovingRecurcion(currentNode, whoseTurn, checker, new Coord(checker.CurrentCoord.Row - 1, checker.CurrentCoord.Column + 1), level)); } if (checker.CheckPossibilityToMove(new Coord(checker.CurrentCoord.Row + 1, checker.CurrentCoord.Column - 1), gameField)) { turnNodes.Add(MovingRecurcion(currentNode, whoseTurn, checker, new Coord(checker.CurrentCoord.Row + 1, checker.CurrentCoord.Column - 1), level)); } if (checker.CheckPossibilityToMove(new Coord(checker.CurrentCoord.Row - 1, checker.CurrentCoord.Column - 1), gameField)) { turnNodes.Add(MovingRecurcion(currentNode, whoseTurn, checker, new Coord(checker.CurrentCoord.Row - 1, checker.CurrentCoord.Column - 1), level)); } currentNode.AddChild(turnNodes); }
private TurnNode KillingRecurcion(TurnNode currentNode, Lib.PlayersSide whoseTurn, IChecker checker, Coord destination, int level) { int killingValue = (whoseTurn == Lib.PlayersSide.BLACK) ? 100 : 0; IChecker[][] state = CreateStateCopy(currentNode.state); TurnNode newNode = new TurnNode(state, killingValue, level);//чем больше уровень рекурсии, тем меньше if (checker.CurrentCoord.Row == 4 && checker.CurrentCoord.Column == 3 && level == 0) { } bool a = (side == Lib.PlayersSide.BLACK && checker is BlackChecker) || (side == Lib.PlayersSide.WHITE && checker is WhiteChecker); if (level == 0 && a) { KillingCommand command = new KillingCommand(game, checker, checker.GetVictim(destination, gameField), destination); newNode.turnCommand = command; //присваеваем команду на первом уровне рекурсии TurnNodes.Add(newNode); } state = ExecuteOperationToKill(state, state[checker.CurrentCoord.Row][checker.CurrentCoord.Column], checker.GetVictim(destination, gameField), destination); MakeTurnNodes(newNode, AnotherPlayer(whoseTurn), level + 1); return(newNode); }
public Player(IGame game, Lib.PlayersSide side) { this.side = side; this.game = game; }
private Lib.PlayersSide AnotherPlayer(Lib.PlayersSide playerside) { return((playerside == Lib.PlayersSide.BLACK) ? Lib.PlayersSide.WHITE : Lib.PlayersSide.BLACK); }