示例#1
0
 private void fillRow(int y, ChessBoxState state)
 {
     foreach (var x in Enumerable.Range(0, desk.Size.Width))
     {
         desk.PlayingDesk[x, y] = state;
     }
 }
示例#2
0
 public IEnumerable <Move> GetAllValidMoves(ChessBoxState actualPlayer)
 {
     foreach (var position in GetPositionsByPlayer(actualPlayer))
     {
         foreach (var move in getValidMovesFromPosition(position))
         {
             yield return(move);
         }
     }
 }
示例#3
0
        public void AddChange(ChessBoxPosition checkBox, ChessBoxState oldState, ChessBoxState newState)
        {
            ChessBoxChange change = new ChessBoxChange
            {
                Position = checkBox,
                OldState = oldState,
                NewState = newState
            };

            Changes.Add(change);
        }
示例#4
0
 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ý.");
 }
示例#5
0
        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);
                }
            }
        }
示例#6
0
 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
         });
     }
 }
示例#7
0
        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);
        }
示例#8
0
        private void setPlayerType(ChessBoxState playerColor, IPlayer player)
        {
            switch (playerColor)
            {
            case ChessBoxState.Black:
                Latrunculi.BlackPlayer = player;
                break;

            case ChessBoxState.White:
                Latrunculi.WhitePlayer = player;
                break;
            }
            ;
            TryTurn();
        }
示例#9
0
        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());
        }
示例#10
0
        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));
        }
示例#11
0
        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");
            }
        }
示例#12
0
 public Move Turn(LatrunculiApp latrunculi, ChessBoxState player, CancellationToken ct = default)
 {
     return(getBestMove(latrunculi, player, ct));
 }
示例#13
0
 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);
 }
示例#14
0
 private ChessBoxState getEnemyPlayer(ChessBoxState player) => player == ChessBoxState.Black ? ChessBoxState.White : ChessBoxState.Black;
示例#15
0
 public IEnumerable <ChessBoxPosition> GetPositionsByPlayer(ChessBoxState actualPlayer)
 {
     return(from ChessBoxPosition position in allPositions
            where desk.GetState(position) == actualPlayer
            select position);
 }
示例#16
0
 public void SetGamePlayerToHuman(ChessBoxState playerColor)
 {
     setPlayerType(playerColor, null);
 }
示例#17
0
 public void SetGamePlayerToMedium(ChessBoxState playerColor)
 {
     setPlayerType(playerColor, new MiniMaxPlayer(2));
 }
示例#18
0
 public void SetGamePlayerToEasy(ChessBoxState playerColor)
 {
     setPlayerType(playerColor, new RandomPlayer());
 }
示例#19
0
        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());
        }
示例#20
0
 public void SetGamePlayerToExpert(ChessBoxState playerColor)
 {
     setPlayerType(playerColor, new MiniMaxPlayer(3));
 }
示例#21
0
 public EndOfGameException(string message, ChessBoxState winner)
     : base(message)
 {
     Winner = winner;
 }
示例#22
0
 private string getPlayerName(ChessBoxState player)
 {
     return(player == ChessBoxState.Black ? "černý" : "bílý");
 }
示例#23
0
 private IEnumerable <ChessBoxPosition> getEnemiesToRemove(ChessBoxState actualPlayer, Move move)
 {
     return(getEnemiesToRemoveByDirection(actualPlayer, move.To, NeighborDirection.Horizontal)
            .Concat(getEnemiesToRemoveByDirection(actualPlayer, move.To, NeighborDirection.Vertical)));
 }