Example #1
0
        public void MakeMoveRaisesEvent()
        {
            // Given
            Piece             rook = new Rook(Colour.White);
            StartSquare       a1   = new StartSquare("a1");
            DestinationSquare a4   = new DestinationSquare("a4");

            long gameId = 101;
            Game target = new Game(gameId);

            target.Board.PutPieceOn(a1, rook);

            // When
            MakeMove command = new MakeMove(gameId, new Move(rook.Code, a1, a4));

            target.MakeMove(command);

            // Then
            DomainEvent result = target.Events.Single();

            Assert.IsInstanceOfType(result, typeof(MoveMade));
            MoveMade movemade = (MoveMade)result;

            Assert.AreEqual(gameId, movemade.GameId);
            Assert.AreEqual('R', movemade.PieceCode);
            Assert.AreEqual("a1", movemade.StartSquare);
            Assert.AreEqual("a4", movemade.DestinationSquare);
        }
Example #2
0
        public void MakeMove(Tuple <int, int> coords)
        {
            if (GetAvailableCells().Count == 0)
            {
                ChangedTurn();
                if (IsGameFinished())
                {
                    FinishGame(GetCells());
                }
            }
            else
            {
                PassedMovesCount = 0;
            }

            var cells = MarkCell(CurrentPlayerColor, coords, _board.Cells);

            MoveMade?.Invoke(cells);
            SwitchPlayer();
            AvailableCellsCalculated?.Invoke(GetAvailableCells());
            CalculatePlayersScore(cells);
            if (IsFull(cells) || PassedMovesCount == 2)
            {
                FinishGame(cells);
                return;
            }
            _board = new Board(cells);
        }
Example #3
0
        private void OnPuzzleButtonClick(object sender, RoutedEventArgs e)
        {
            var b = e.Source as Button;

            if (b != null)
            {
                var row = (int)b.GetValue(RowProperty);
                var col = (int)b.GetValue(ColumnProperty);

                var moveStatus = _puzzleLogic.GetMoveStatus(row, col);

                MoveMade?.Invoke(this, new HandledEventArgs(moveStatus != MoveStatus.BadMove));

                if (moveStatus != MoveStatus.BadMove)
                {
                    if (!IsApplyingStyle || !ShouldAnimateInteractions)
                    {
                        // Not templated, just move piece
                        MovePiece(b, row, col);
                    }
                    else
                    {
                        AnimatePiece(b, row, col, moveStatus);
                    }
                }
            }
        }
Example #4
0
        public LichessAgent(string gameId)
        {
            cancellationTokenSource = new CancellationTokenSource();
            GameId = gameId;

            Mutex gameStateReceived = new Mutex();

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            ChessBot.StreamGameState(GameId, evt =>
            {
                switch ((string)evt["type"])
                {
                case "gameFull":
                    //TODO: FixIt
                    break;

                case "gameState":
                    var mvs = ((string)evt["moves"]).Split(" ");
                    if (mvs.Length > (Game.MovesHistory).Count)
                    {
                        var mv   = mvs.Last();
                        var from = new Position('8' - mv[1], mv[0] - 'a');
                        var to   = new Position('8' - mv[3], mv[2] - 'a');
                        var transformToFigure = Figure.FiguresType.None;
                        if (mv.Length > 4)
                        {
                            switch (mv[4])
                            {
                            case 'r':
                                transformToFigure = Figure.FiguresType.Rook;
                                break;

                            case 'n':
                                transformToFigure = Figure.FiguresType.Knight;
                                break;

                            case 'b':
                                transformToFigure = Figure.FiguresType.Bishop;
                                break;

                            case 'q':
                                transformToFigure = Figure.FiguresType.Queen;
                                break;
                            }
                        }
                        MoveWanted.WaitOne();
                        MoveWanted.Reset();
                        passed = new Move(from, to)
                        {
                            transformTo = transformToFigure
                        };
                        MoveMade.Set();
                    }
                    break;
                }
            }, cancellationTokenSource.Token, Tokens.FairBotToken);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
        }
Example #5
0
 public static Move MapToMove(this MoveMade moveMade)
 {
     return(new Move
            (
                PieceCodes.FromChar(moveMade.PieceCode),
                new StartSquare(moveMade.StartSquare),
                new DestinationSquare(moveMade.DestinationSquare)
            ));
 }
Example #6
0
        private void InvokeOnMove(MoveInfo info, ITactics tactics)
        {
            MoveMade move = OnMove;

            if (move != null)
            {
                move(info, tactics);
            }
        }
Example #7
0
        public void Handle(MoveMade moveMade)
        {
            Move move = moveMade.MapToMove();

            Board.ExecuteMove(move);
            //if (!IsReplaying)
            //{
            //    StuurRobotAan(move); // Zijeffect op de echte wereld
            //}
        }
Example #8
0
 public void SelectMove(IPlayer player, Move move)
 {
     if (CurrentPlayer == player && !m_Game.IsFinished)
     {
         CurrentPlayer.MoveProduced -= SelectMove;
         m_Game.ApplyMove(move);
         MoveMade?.Invoke();
         CurrentPlayer.MoveProduced += SelectMove;
         CurrentPlayer.TakeTurn();
     }
 }
Example #9
0
        public void MakeMove(Tuple <int, int> coolds)
        {
            passedMovesCount = 0;
            field.SetCell(currentPlayerColor, coolds);
            MoveMade?.Invoke(field.Cells);

            SwitchPlayer();
            if (field.isFull() || passedMovesCount == 2)
            {
                FinishGame();
                return;
            }
        }
Example #10
0
        public void MakeMove(MakeMove command)
        {
            BusinessRule.ThrowIfNotSatisfied(
                new PieceMustActuallyMove(command.Move)
                & new PieceMustOccupyStartingSquare(Board, command.Move)
                & new MoveIsValidForPiece(Board, command.Move)
                // & new MovePathIsUnobstructed(Board, command.Move)
                );

            MoveMade e = command.Move.MapToMoveMade(this.Id);

            RaiseEvent(e);
        }
Example #11
0
        public void MakeMove(Tuple <int, int> coolds)
        {
            if (GetAvailablePoints().Count == 0)
            {
                Pass();
            }
            else
            {
                passedMovesCount = 0;
            }
            List <List <Point> > points = BoardHandler.SetPoint(currentPlayerColor, coolds, board.Points);

            MoveMade?.Invoke(points);
            SwapTurn();
            AvailablePointsCalculated?.Invoke(GetAvailablePoints());
            CalculatePlayersScore(points);
            if (BoardHandler.isFull(points) || passedMovesCount == 2)
            {
                FinishGame(points);
                return;
            }
            board = new Board(points);
        }
Example #12
0
 private void OnMove(int gridX, int gridY, int X, int Y)
 {
     MoveMade?.Invoke(this, new TicTacToeTurnEventArgs(WhoseTurn, NextMoveX, NextMoveY, gridX, gridY, X, Y));
 }
Example #13
0
 /// <summary>
 /// Raises the <see cref="MoveMade"/> event.
 /// </summary>
 protected virtual void OnMoveMade(CubeMove move)
 {
     MoveMade?.Invoke(this, move);
 }
Example #14
0
        public GameResult PlayGame(ChessGame startingPosition = null)
        {
            var game = startingPosition ?? new ChessGame();

            var gameResult = new GameResult();

            GameStats.GameCount += 1;

            while (game.WhoseTurn != Player.None)
            {
                if (game.DrawCanBeClaimed)
                {
                    gameResult.Result = "Draw";
                    game.ClaimDraw("Boredom");

                    GameStats.DrawCount += 1;

                    break;
                }

                var move = (game.WhoseTurn == Player.White ? WhiteAgent : BlackAgent).GenerateMove(game);

                GameStats.MoveCount  += 1;
                gameResult.MoveCount += 1;

                var gameBeforeMove = new ChessGame(game.GetGameCreationData());
                game.MakeMove(move, true);
                var gameAfterMove = new ChessGame(game.GetGameCreationData());

                // raise event
                MoveMade?.Invoke(gameBeforeMove, move, gameAfterMove);

                if (game.IsInCheck(game.WhoseTurn))
                {
                    GameStats.CheckCount  += 1;
                    gameResult.CheckCount += 1;
                }

                if (game.IsStalemated(game.WhoseTurn))
                {
                    GameStats.StalemateCount += 1;
                    gameResult.Result         = "Stalemate";

                    break;
                }

                if (game.IsCheckmated(game.WhoseTurn))
                {
                    gameResult.Result = "Checkmate";
                    gameResult.Winner = game.WhoseTurn == Player.White ? Player.Black : Player.White;

                    if (gameResult.Winner == Player.White)
                    {
                        GameStats.WhiteCheckmateCount += 1;
                    }
                    else
                    {
                        GameStats.BlackCheckmateCount += 1;
                    }

                    break;
                }
            }

            // raise event
            GameConcluded?.Invoke(new ChessGame(game.GetGameCreationData()), gameResult);

            return(gameResult);
        }