Exemple #1
0
        private void CheckForWin()
        {
            // TODO: extract it into Rules class - because there are a lot of draw cases (pretty complex)
            var validEnemyMoves = _rulesProvider.GetMoves(Board, SideUtil.Opposite(SideUtil.Convert(CurrentPlayer.Side)));

            if (validEnemyMoves.Count == 0)
            {
                _winnerIndex = CurrentPlayerIndex;
                Status       = _winnerIndex == 0 ? GameStatus.Player1Wins : GameStatus.Player2Wins;
                //_isRunning = false;
                //TODO: potentially it's ok to undo move after game is over?
                // - don't stop the game with _isRunning = false
                // - then Undo should unmark it?
                // - then to be able to make Redo should store it in History
                // etc.
            }
        }
Exemple #2
0
        public IGameMove Choose(IGameMove[] moves, SquareBoard board)
        {
            var walkMoves = moves.Select(x => x as WalkGameMove).Where(x => x != null).ToArray();

            if (walkMoves.Length == 0)
            {
                return(null);
            }

            _cts = new CancellationTokenSource(TimeoutPerMoveMilliseconds);

            var depth = 8 * Math.Pow(TimeoutPerMoveMilliseconds / 1000, 0.4);
            var move  = _bot.FindBestMove(board, SideUtil.Convert(Side), _cts.Token, new NegaMaxBot.BotOptions {
                MaxDepth = (int)depth
            });

            return(walkMoves.FirstOrDefault(x => x.Figure == move.Figure && x.MoveSequence == move.Sequence));
        }
Exemple #3
0
        private async Task GameLoop()
        {
            do
            {
                var validMoves = _rulesProvider.GetMoves(Board, SideUtil.Convert(CurrentPlayer.Side));
                var gameMoves  = new List <IGameMove>(validMoves.Values.Count);
                if (validMoves.Count > 0)
                {
                    gameMoves.AddRange(validMoves.Flatten((f, m) => new WalkGameMove(this, f, m)));
                }
                if (_currentHistoryItem.Previous != null && _currentHistoryItem.Previous.Previous != null)
                {
                    gameMoves.Add(UndoMove);
                }
                if (_currentHistoryItem.Next != null && _currentHistoryItem.Next.Next != null)
                {
                    gameMoves.Add(RedoMove);
                }

                try
                {
                    var move = await TryChooseMove(gameMoves);

                    if (move == null)
                    {
                        return;               //gameLoop was interrupted
                    }
                    move.Execute();
                    _gameStatistics.Append(move, this);
                }
                catch (Exception ex)
                {
                    Error      = ex?.InnerException ?? ex;
                    Status     = GameStatus.Error;
                    _isRunning = false;
                    break;
                }

                foreach (var player in _players)
                {
                    player.GameUpdated(this);
                }
            } while (_isRunning);
        }
Exemple #4
0
 public Cell(Figure figure, GameSide playerSide)
 {
     _figure = figure;
     _active = figure.Side == SideUtil.Convert(playerSide);
 }