Esempio n. 1
0
 private async Task NextTurn()
 {
     currentPlayerIndex = (currentPlayerIndex + 1) % _players.Count;
     currentPlayer      = _players[currentPlayerIndex];
     StatusCallback?.Invoke($" Tura gracza: {currentPlayer.Name}");
     await NextMove();
 }
Esempio n. 2
0
        private Position TranslatePlayerPosition(QuoridorPlayer player, Position pos)
        {
            if (player is AiPlayer || player.StartingPosition == Bottom)
            {
                return(pos);
            }

            if (player.StartingPosition == Top)
            {
                return new Position {
                           X = BoardSize - pos.X, Y = BoardSize - pos.Y
                }
            }
            ;
            else if (player.StartingPosition == Left)
            {
                return new Position {
                           X = BoardSize - pos.Y, Y = BoardSize - pos.X
                }
            }
            ;                                                                        //check
            else if (player.StartingPosition == Right)
            {
                return new Position {
                           X = pos.Y, Y = pos.X
                }
            }
            ;                                                //check
            else
            {
                throw new Exception();
            }
        }
Esempio n. 3
0
        private void MovePlayer(QuoridorPlayer currentPlayer, Position position)
        {
            if (currentPlayer == null)
            {
                return;
            }

            this[currentPlayer.CurrentPosition] = BoardElementType.Empty;
            BoardMatrix[currentPlayer.CurrentPosition.Y][currentPlayer.CurrentPosition.X].Player = null;

            currentPlayer.CurrentPosition = position;

            this[currentPlayer.CurrentPosition] = BoardElementType.Player;
            BoardMatrix[currentPlayer.CurrentPosition.Y][currentPlayer.CurrentPosition.X].Player = currentPlayer;
        }
Esempio n. 4
0
 private Move TryMove(Position position, QuoridorPlayer currentPlayer)
 {
     if (MoveValidator.IsValid(GetBoardArray(), currentPlayer.CurrentPosition,
                               Players.Select(p => p.CurrentPosition), position))
     {
         //if (changeAllowed)
         {
             MovePlayer(currentPlayer, position);
         }
         return(new Move {
             Destination = position
         });
     }
     return(null);
 }
Esempio n. 5
0
        public void MovePlayer(QuoridorPlayer player, Move move)
        {
            if (!Players.Contains(player))
            {
                throw new ArgumentException();
            }

            if (move.IsMove)
            {
                var position = TranslatePlayerPosition(player, move.Destination);
#if _USE_ADDITIONAL_VALIDATION
                if (!MoveValidator.IsValid(GetBoardArray(), player.CurrentPosition, Players.Select(p => p.CurrentPosition), position))
                {
                    //this is kind of second time validation - we dont neceserry need it in production
                    throw new ArgumentException();
                }
#endif
                var playerBoardElement = this[player.CurrentPosition];

                this[player.CurrentPosition] = BoardElementType.Empty;
                BoardMatrix[player.CurrentPosition.Y][player.CurrentPosition.X].Player = null;

                player.CurrentPosition = position;

                this[player.CurrentPosition] = playerBoardElement;
                BoardMatrix[player.CurrentPosition.Y][player.CurrentPosition.X].Player = player;
            }
            else if (move.IsWallPlacement)
            {
                var positions =
                    move.WallPlacementPositions.Select(p => TranslatePlayerPosition(player, p));
#if _USE_ADDITIONAL_VALIDATION
                if (!WallValidator.AreValid(GetBoardArray(), Players, player, positions.ToArray()))
                {
                    //this is kind of second time validation - we dont neceserry need it in production
                    throw new ArgumentException();
                }
#endif
                foreach (var position in positions)
                {
                    this[position] = BoardElementType.Wall;
                }
            }
            else
            {
                throw new ArgumentException();
            }
        }
Esempio n. 6
0
        public Game(List <PlayerParameters> playersParameters) : this()
        {
            localPlayer  = null;
            gameLocation = GameLocation.LocalGame;
            board        = new Board(gameLocation);
            foreach (var playersParameter in playersParameters)
            {
                if (playersParameter.PlayerType == PlayerType.AI)
                {
                    _players.Add(new AiPlayer(board, playersParameter));
                }
                else if (playersParameter.PlayerType == PlayerType.Human)
                {
                    _players.Add(new HumanPlayer(playersParameter));
                }
            }

            SetNumberOfWallPerPlayer();

            board.PlacePlayers(_players);
        }
Esempio n. 7
0
        public Game(GameType gameType, Player localUser, IEnumerable <Player> connectedUsers) : this()
        {
            switch (gameType)
            {
            case GameType.PlayerVsAi:
                localPlayer = new HumanPlayer(null, localUser);
                _players.Add(localPlayer);
                _players.Add(new AiPlayer(board));
                break;

            case GameType.PlayerVsPlayer:
                localPlayer = new HumanPlayer(quoridorWebService, localUser);
                _players.Add(localPlayer);
                foreach (var user in connectedUsers)
                {
                    if (user != localUser)
                    {
                        _players.Add(new HumanPlayer(quoridorWebService, user));
                    }
                }
                break;

            case GameType.AiVsAi:
                localPlayer = null;
                _players.Add(new AiPlayer(board));
                _players.Add(new AiPlayer(board));
                _players.Add(new AiPlayer(board));
                _players.Add(new AiPlayer(board));
                break;
            }

            SetPlayersOrderAndPositions();

            SetNumberOfWallPerPlayer();

            board.PlacePlayers(_players);
        }
Esempio n. 8
0
        private Move TryPlaceWall(Position position, Point clickPoint, Point centerOfElement, QuoridorPlayer currentPlayer)
        {
            if (this[position] != BoardElementType.EmptyForWall || currentPlayer.NumberOfWallsAvalaible <= 0)
            {
                return(null);
            }
            Position wall1Position = null; // = new Position(1, 0);
            Position wall2Position = null; // = new Position(2, 0);
            var      boardArray    = GetBoardArray();

            if (BoardMatrix[position.Y][position.X].IsHorizontalWall)
            {
                wall1Position = new Position(1, 0);
                wall2Position = new Position(2, 0);
                if (clickPoint.X <= centerOfElement.X)
                {
                    wall1Position = -wall1Position;
                    wall2Position = -wall2Position;
                }
            }
            else if (BoardMatrix[position.Y][position.X].IsVerticalWall)
            {
                wall1Position = new Position(0, 1);
                wall2Position = new Position(0, 2);
                if (clickPoint.Y <= centerOfElement.Y)
                {
                    wall1Position = -wall1Position;
                    wall2Position = -wall2Position;
                }
            }
            else if (BoardMatrix[position.Y][position.X].IsMicroWall)
            {
                //TODO: handle this rare case when user clicks exactly on micro wall
                return(null);
            }

            if (WallValidator.AreValid(boardArray, Players, currentPlayer, position, position + wall2Position,
                                       position + wall1Position))
            {
                //  if (changeAllowed)
                {
                    PlaceWall(position);
                    PlaceWall(position + wall1Position);
                    PlaceWall(position + wall2Position);
                }
                return(new Move
                {
                    IsWallPlacement = true,
                    WallPlacementPositions = new[] { position, position + wall2Position, position + wall1Position }
                });
            }
            if (WallValidator.AreValid(boardArray, Players, currentPlayer, position, position - wall2Position,
                                       position - wall1Position))
            {
                //  if (changeAllowed)
                {
                    PlaceWall(position);
                    PlaceWall(position - wall1Position);
                    PlaceWall(position - wall2Position);
                }
                return(new Move
                {
                    IsWallPlacement = true,
                    WallPlacementPositions = new[] { position, position - wall2Position, position - wall1Position }
                });
            }
            return(null);
        }
Esempio n. 9
0
        public Move TryClick(Position position, Point clickPoint, Point centerOfElement, QuoridorPlayer currentPlayer)
        {
            switch (this[position])
            {
            case BoardElementType.Empty:
                return(TryMove(position, currentPlayer));

            case BoardElementType.EmptyForWall:
                return(TryPlaceWall(position, clickPoint, centerOfElement, currentPlayer));

            default:
                return(null);
            }
        }