private void fillRow(int y, ChessBoxState state) { foreach (var x in Enumerable.Range(0, desk.Size.Width)) { desk.PlayingDesk[x, y] = state; } }
public IEnumerable <Move> GetAllValidMoves(ChessBoxState actualPlayer) { foreach (var position in GetPositionsByPlayer(actualPlayer)) { foreach (var move in getValidMovesFromPosition(position)) { yield return(move); } } }
public void AddChange(ChessBoxPosition checkBox, ChessBoxState oldState, ChessBoxState newState) { ChessBoxChange change = new ChessBoxChange { Position = checkBox, OldState = oldState, NewState = newState }; Changes.Add(change); }
public void ValidateMove(ChessBoxState actualPlayer, Move move) { if (GetAllValidMoves(actualPlayer).Contains(move)) { return; } if (desk.GetState(move.From) != actualPlayer) { var actualPlayerText = actualPlayer == ChessBoxState.Black ? "černý" : "bílý"; throw new Exception($"Tah není platný, protože na tahu je {actualPlayerText} hráč."); } throw new Exception($"Tah není platný."); }
private IEnumerable <ChessBoxPosition> getEnemiesToRemoveByDirection(ChessBoxState actualPlayer, ChessBoxPosition position, NeighborDirection direction) { var enemyPlayer = getEnemyPlayer(actualPlayer); foreach (var enemy in getNeighborsByPlayer(enemyPlayer, position, direction)) { if (enemy.IsCorner && getNeighborsByPlayer(actualPlayer, enemy).Count() == 1 || getNeighborsByPlayer(actualPlayer, enemy, direction).Count() == 1) { yield return(enemy); } } }
public IEnumerable <ChessBoxChange> MoveEffects(ChessBoxState actualPlayer, Move move) { foreach (var enemyToRemove in getEnemiesToRemove(actualPlayer, move)) { yield return(new ChessBoxChange() { Position = enemyToRemove, OldState = desk.GetState(enemyToRemove), NewState = ChessBoxState.Empty, IsCaptured = true }); } }
public bool Move(ChessBoxState actualPlayer, Move move) { ValidateMove(actualPlayer, move); ChangeSet step = new ChangeSet(move); ChessBoxState stateFrom = desk.GetState(move.From); ChessBoxState stateTo = desk.GetState(move.To); step.AddChange(move.From, stateFrom, ChessBoxState.Empty); step.AddChange(move.To, stateTo, stateFrom); step.AddChangesRange(MoveEffects(actualPlayer, move)); desk.DoStep(step); Moved?.Invoke(this, step); return(true); }
private void setPlayerType(ChessBoxState playerColor, IPlayer player) { switch (playerColor) { case ChessBoxState.Black: Latrunculi.BlackPlayer = player; break; case ChessBoxState.White: Latrunculi.WhitePlayer = player; break; } ; TryTurn(); }
private Move getBestMove(LatrunculiApp latrunculi, ChessBoxState player, CancellationToken ct = default) { ct.ThrowIfCancellationRequested(); logger?.Dispose(); deskLogger?.Dispose(); logger = new FileLogger($"{latrunculi.HistoryManager.ActualRound} - {player} - minimax {depth}"); deskLogger = new FileLogger($"{latrunculi.HistoryManager.ActualRound} - {player} - minimax {depth} - desk"); var validMoves = latrunculi.Rules.GetAllValidMoves(player).ToArray(); IEnumerable <(Move move, int evaluation, int bestMovesCount)> moves = validMoves.Select(move => (minMaxRecursive(latrunculi, depth, move, ct))).ToArray(); if (latrunculi.Debug) { logger.WriteLine("############################# Result"); foreach (var moveInfo in moves) { logger.WriteLine($"{moveInfo.evaluation} {moveInfo.move}, count: {moveInfo.bestMovesCount}"); } } if (latrunculi.BestMovesDebug) { Console.WriteLine(); foreach (var moveInfo in moves) { Console.WriteLine($"{moveInfo.evaluation} {moveInfo.move}, count: {moveInfo.bestMovesCount}"); } Console.WriteLine($"--------------------------------"); } var maxEvaluation = moves.Max(move => move.evaluation); var maxMoves = moves.Where(move => move.evaluation == maxEvaluation).ToArray(); var maxBestCount = maxMoves.Max(move => move.bestMovesCount); var bestMoves = maxMoves.Where(move => move.bestMovesCount == maxBestCount).Select(move => move.move).ToArray(); var moveForwardDirection = player == ChessBoxState.Black ? -1 : 1; var bestForwardMoves = bestMoves.Where(move => move.To.Y - move.From.Y == moveForwardDirection).ToArray(); if (bestForwardMoves.Length == 0) { bestForwardMoves = bestMoves.Where(move => move.From.Y == move.To.Y).ToArray(); } if (bestForwardMoves.Length == 0) { bestForwardMoves = bestMoves; } logger.Dispose(); return(bestForwardMoves.Skip(random.Next(bestForwardMoves.Count()) - 1).First()); }
public IPlayer GetPlayerType(ChessBoxState player) { var playerName = player == ChessBoxState.Black ? "černého" : "bílého"; var playerTypesNames = string.Join(", ", playerTypes.Keys); var playerType = GetValidValue($"Zadejte typ {playerName} hráče ({playerTypesNames}): ", (string input) => { input = input.Trim().ToLower(); if (this.playerTypes.ContainsKey(input)) { return(true); } Program.WriteColoredLine("Zadán chybný typ hráče.", ConsoleColor.Red); return(false); }); return(createPlayerByType(playerType)); }
public Move Turn(LatrunculiApp latrunculi, ChessBoxState player, CancellationToken ct = default) { Console.Write("Váš tah (start cíl): "); string line = Console.ReadLine().Trim().ToLower(); if (commandManager.CheckCommand(line)) { return(null); } var moveMatch = Regex.Match(line, @"^(?<from>[a-zA-Z][1-9])\s*(?<to>[a-zA-Z][1-9])$"); if (moveMatch.Success) { ChessBoxPosition from = ChessBoxPosition.FromString(deskSize, moveMatch.Groups["from"].Value); ChessBoxPosition to = ChessBoxPosition.FromString(deskSize, moveMatch.Groups["to"].Value); return(new Move(from, to)); } else { throw new InvalidCastException("Je vyžadován tah ve formátu start cíl, např. E4 E5"); } }
public Move Turn(LatrunculiApp latrunculi, ChessBoxState player, CancellationToken ct = default) { return(getBestMove(latrunculi, player, ct)); }
private IEnumerable <ChessBoxPosition> getNeighborsByPlayer(ChessBoxState player, ChessBoxPosition position, NeighborDirection direction = NeighborDirection.All) { return(from ChessBoxPosition neighborPosition in position.GetNeighbors(direction) where desk.GetState(neighborPosition) == player select neighborPosition); }
private ChessBoxState getEnemyPlayer(ChessBoxState player) => player == ChessBoxState.Black ? ChessBoxState.White : ChessBoxState.Black;
public IEnumerable <ChessBoxPosition> GetPositionsByPlayer(ChessBoxState actualPlayer) { return(from ChessBoxPosition position in allPositions where desk.GetState(position) == actualPlayer select position); }
public void SetGamePlayerToHuman(ChessBoxState playerColor) { setPlayerType(playerColor, null); }
public void SetGamePlayerToMedium(ChessBoxState playerColor) { setPlayerType(playerColor, new MiniMaxPlayer(2)); }
public void SetGamePlayerToEasy(ChessBoxState playerColor) { setPlayerType(playerColor, new RandomPlayer()); }
public Move Turn(LatrunculiApp latrunculi, ChessBoxState player, CancellationToken ct = default) { var validMoves = latrunculi.Rules.GetAllValidMoves(player); return(validMoves.Skip(random.Next(validMoves.Count()) - 1).First()); }
public void SetGamePlayerToExpert(ChessBoxState playerColor) { setPlayerType(playerColor, new MiniMaxPlayer(3)); }
public EndOfGameException(string message, ChessBoxState winner) : base(message) { Winner = winner; }
private string getPlayerName(ChessBoxState player) { return(player == ChessBoxState.Black ? "černý" : "bílý"); }
private IEnumerable <ChessBoxPosition> getEnemiesToRemove(ChessBoxState actualPlayer, Move move) { return(getEnemiesToRemoveByDirection(actualPlayer, move.To, NeighborDirection.Horizontal) .Concat(getEnemiesToRemoveByDirection(actualPlayer, move.To, NeighborDirection.Vertical))); }