Example #1
0
 internal static void InitializeGame()
 {
     Board.InitializeBoard();
     Turn = Color.White;
     WhitePlayer.setKing();
     BlackPlayer.setKing();
 }
Example #2
0
        async private void NextTurn()
        {
            ICommand   command;
            FieldState state;

            switch (GameField.Turn)
            {
            case Lib.PlayersSide.WHITE:
                BlockBlack();
                command = await WhitePlayer.MakeStep(GameField);

                if (command != null)
                {
                    state = GameField.SaveState();
                    states.Add(command, state);
                    command.Execute();
                }
                break;

            case Lib.PlayersSide.BLACK:
                BlockWhite();
                command = await BlackPlayer.MakeStep(GameField);

                if (command != null)
                {
                    state = GameField.SaveState();
                    states.Add(command, state);
                    command.Execute();
                }
                break;
            }
        }
Example #3
0
        private PlayerMove GetMoveFromPlayer(IUserInput input, IUserResponse userResponse, Player player, PieceColor currentColor)
        {
            bool isValidSelection = false;
            var  move             = player.MakeMove(input);

            while (!isValidSelection)
            {
                if (ChessBoard.IsLegalBoardPosition(move.PieceSelectionXCoordinate, move.PieceSelectionYCoordinate) &&
                    ChessBoard.IsPieceAt(move.PieceSelectionXCoordinate, move.PieceSelectionYCoordinate))
                {
                    if (ChessBoard.PieceAt(move.PieceSelectionXCoordinate, move.PieceSelectionYCoordinate).PieceColor == currentColor)
                    {
                        isValidSelection = true;
                    }
                    else
                    {
                        userResponse.BadPieceSelection("You must select your own piece to move.");
                        move = WhitePlayer.MakeMove(input);
                    }
                }
                else
                {
                    userResponse.BadPieceSelection("You must select a piece at a valid location.");
                    move = WhitePlayer.MakeMove(input);
                }
            }

            return(move);
        }
Example #4
0
        private void ReplaceBoard(SaveData _data)
        {
            BlackPlayer.Reset();
            WhitePlayer.Reset();
            Pawns.Clear();
            int number = 0;

            foreach (var color in _data.Pawns)
            {
                if (color == PawnColor.Black)
                {
                    Pawns.Add(new Pawn(BlackPlayer, number));
                }
                else if (color == PawnColor.White)
                {
                    Pawns.Add(new Pawn(WhitePlayer, number));
                }
                else
                {
                    Pawns.Add(new Pawn(null, number));
                }
                number++;
            }
            BlackPlayer.Time = _data.BlackTime;
            WhitePlayer.Time = _data.WhiteTime;
            CurrentPlayer    = WhitePlayer;
            if (_data.CurrentPlayer == PawnColor.Black)
            {
                CurrentPlayer = BlackPlayer;
            }
            GetLegalMove(CurrentPlayer);
            UpdateScore();
        }
Example #5
0
        public Game Clone()
        {
            var game = new Game
            {
                Id            = Id,
                BlackPlayer   = BlackPlayer.Clone(),
                WhitePlayer   = WhitePlayer.Clone(),
                Points        = Points.Select(p => p.Clone()).ToList(),
                BlackStarts   = BlackStarts,
                WhiteStarts   = WhiteStarts,
                Created       = Created,
                CurrentPlayer = CurrentPlayer,
                PlayState     = PlayState,
                Roll          = Roll.Select(r => new Dice {
                    Used = r.Used, Value = r.Value
                }).ToList(),
                ThinkStart     = ThinkStart,
                GoldMultiplier = GoldMultiplier,
                IsGoldGame     = IsGoldGame,
                LastDoubler    = LastDoubler,
                Stake          = Stake
            };

            game.Bars = new Point[2];
            game.Bars[(int)Player.Color.Black] = game.Points[0];
            game.Bars[(int)Player.Color.White] = game.Points[25];

            return(game);
        }
Example #6
0
        public Lobby()
        {
            InitializeComponent();
            ApplicationManager.Instance.Lobby = this;

            PlayerDataExport playersInfo = ApplicationManager.Instance.GetPlayers();

            string BlackPlayerName = playersInfo.Color1 == Color.Black ? playersInfo.Name1 : playersInfo.Name2;

            BlackPlayer.SetValue(PlayerVisualiserBlack.PropertyPseudo, BlackPlayerName);

            string WhitePlayerName = playersInfo.Color1 == Color.White ? playersInfo.Name1 : playersInfo.Name2;

            WhitePlayer.SetValue(PlayerVisualiserWhite.PropertyPseudo, WhitePlayerName);

            if (ApplicationManager.Instance.GameType == GameType.Local)
            {
                KeyUp += PlayerPicker.OnKeyDownLocal;
            }
            else
            {
                if (playersInfo.Color1 == Color.Black)
                {
                    KeyUp += PlayerPicker.OnKeyDownOnlineBlack;
                }
                else
                {
                    KeyUp += PlayerPicker.OnKeyDownOnlineWhite;
                }
            }
            // Bind key event to playerpicker
        }
    void Start()
    {
        fixedPosY = transform.position.y;
        fixedRotX = transform.rotation.eulerAngles.x;
        fixedRotZ = transform.rotation.eulerAngles.z;

        white = player.GetComponent <WhitePlayer>();
    }
Example #8
0
        /** This is called after the end of every turn. It calculates the squares for each player's pieces.
         * Then it changes the turn to be the color opposite the player that just moved. It also generates
         * the moves for the player that is about to move. If a player is in check then it also checks for
         * checkmate.
         * @author Thomas Hooper
         * @date March 2019
         */
        public void ChangeTurns()
        {
            PreviousMove = CurrentMove;         //This was after calcvalidsquares b4 so idk why i did that
            CalculateValidSquares();            //This calculates the valid squares for all the pieces both players have.
            #region Changing Turns and Updating History and Generating Moves
            if (Turn == Color.White)
            {
                Turn         = Color.Black;
                MovingPlayer = BlackPlayer;
                WhitePlayer.UpdateHistory();
                BlackPlayer.GenerateMoves(BlackPlayer, ChessBoard, PreviousMove);
            }
            else
            {
                Turn         = Color.White;
                MovingPlayer = WhitePlayer;
                BlackPlayer.UpdateHistory();
                WhitePlayer.GenerateMoves(WhitePlayer, ChessBoard, PreviousMove);
            }
            #endregion
            #region Checking for check and checkmate
            if (WhitePlayer.Check)
            {
                Game game = SerializeGame();
                if (game.CheckForCheckmate())                //Might cause problems
                {
                    Checkmate = true;
                }
            }
            else if (BlackPlayer.Check)
            {
                Game game = SerializeGame();
                if (game.CheckForCheckmate())                //Might cause problems
                {
                    Checkmate = true;
                }
            }
            #endregion

            #region Checking For Draws

            /*
             * if (!Checkmate)
             * {
             *      if (CheckForStalemate(MovingPlayer))
             *      {
             *              Draw = true;
             *      }
             *      else if (CheckForRepetition())
             *      {
             *              Draw = true;
             *      }
             * }
             */
            #endregion
        }
Example #9
0
 public void GetMoveFromPlayer()
 {
     try
     {
         Move move = WhiteTurn ? WhitePlayer.GetMove(Board) : BlackPlayer.GetMove(Board);
         Execute(move);
     }
     catch (ArgumentException)
     {
         GetMoveFromPlayer();
     }
 }
Example #10
0
 public void NewGame()
 {
     PlayerPassName = null;
     BlackPlayer.Reset();
     WhitePlayer.Reset();
     Reset(BlackPlayer, WhitePlayer);
     CurrentPlayer = BlackPlayer;
     GetLegalMove(CurrentPlayer);
     UpdateScore();
     IsCreated = true;
     StartGame();
 }
Example #11
0
        public void Player_cannot_make_a_move_if_it_is_not_the_players_turn()
        {
            var part1 = new BlackPlayer("p1");
            var part2 = new WhitePlayer("p2");

            _sut.RegisterPlayer(part1);
            _sut.RegisterPlayer(part2);
            var gameState = _sut.CreateNewGame(part1);

            _sut.JoinGame(gameState.GameId, part2);

            var flippedPieces = _sut.MakeMove(gameState.GameId, part2, new Position(5, 3));

            flippedPieces.Should().BeNull();
        }
Example #12
0
        public static Player choosePlayer(this Alliance alliance,
                                          WhitePlayer whitePlayer,
                                          BlackPlayer blackPlayer)
        {
            switch (alliance)
            {
            case Alliance.WHITE:
                return(whitePlayer);

            case Alliance.BLACK:
                return(blackPlayer);

            default:
                return(null);
            }
        }
Example #13
0
        public void UndoStep(ICommand command)
        {
            if (GameField.Turn == Lib.PlayersSide.WHITE)
            {
                WhitePlayer.CancelStep();
            }
            else
            {
                BlackPlayer.CancelStep();
            }

            GameField.RestoreState(states[command]);
            states.Remove(command);

            NextTurn();
        }
Example #14
0
        public void A_player_cannot_make_a_move_when_he_is_not_part_of_the_game()
        {
            var part1 = new BlackPlayer("p1");
            var part2 = new WhitePlayer("p2");
            var part3 = new BlackPlayer("p3");

            _sut.RegisterPlayer(part1);
            _sut.RegisterPlayer(part2);
            _sut.RegisterPlayer(part3);
            var gameState = _sut.CreateNewGame(part1);

            _sut.JoinGame(gameState.GameId, part2);

            Action moveOnWrongGame = () => _sut.MakeMove(gameState.GameId, part3, new Position(5, 3));

            moveOnWrongGame.ShouldThrow <ArgumentException>();
        }
Example #15
0
        public void When_game_is_finished_it_is_removed()
        {
            var positions = new char[8, 8]
            {
                { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },

                { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },

                { ' ', ' ', 'X', ' ', 'X', ' ', ' ', ' ' },

                { ' ', ' ', 'X', 'O', 'X', ' ', ' ', ' ' },

                { ' ', ' ', 'X', 'X', 'X', ' ', ' ', ' ' },

                { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },

                { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },

                { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
            };

            bool isFinished  = false;
            var  board       = new Board(positions.Translate());
            var  blackPlayer = new BlackPlayer("black");
            var  whitePlayer = new WhitePlayer("white");

            _sut.RegisterPlayer(blackPlayer);
            _sut.RegisterPlayer(whitePlayer);
            var game = new Game(blackPlayer, board);

            game.JoinOpponent(whitePlayer);
            _sut.AddGame(game);
            _sut.GameStateChanged += (sender, participant, args) =>
            {
                if (game.GameId == args.CurrentState.GameId)
                {
                    isFinished = args.CurrentState.GameState == GameState.Finished;
                }
            };

            var finishedState = _sut.MakeMove(game.GameId, blackPlayer, new Position(3, 2));

            finishedState.Should().NotBeEmpty();
            isFinished.Should().BeTrue();
            _sut.ActiveGames.Should().NotContain(x => x.GameId == game.GameId);
        }
Example #16
0
        private Board(Builder builder)
        {
            //Create new gameboard with builder
            this.gameBoard = createGameBoard(builder);
            //white and black active pieces
            this.whitePieces   = calculateActivePieces(this.gameBoard, Alliance.WHITE);
            this.blackPieces   = calculateActivePieces(this.gameBoard, Alliance.BLACK);
            this.enPassantPawn = builder.getEnPassantPawn();
            //white and black initial legal moves calculation
            IReadOnlyCollection <Move> whiteStandardLegalMoves = calculateLegalMoves(this.whitePieces);
            IReadOnlyCollection <Move> blackStandardLegalMoves = calculateLegalMoves(this.blackPieces);

            //Player instantiation
            this.whitePlayer    = new WhitePlayer(this, whiteStandardLegalMoves, blackStandardLegalMoves);
            this.blackPlayer    = new BlackPlayer(this, whiteStandardLegalMoves, blackStandardLegalMoves);
            this.currentPlayer  = builder.getNextMoveMaker().choosePlayer(this.whitePlayer, this.blackPlayer);
            this.transitionMove = builder.getMoveTransition() != null?builder.getMoveTransition() : MoveFactory.getNullMove();
        }
 private void SetWinner()
 {
     if (BlackStones > WhiteStones)
     {
         WinnerClass = Player.BLACK.ToString();
         this.Winner = BlackPlayer.ToString();
     }
     else if (BlackStones < WhiteStones)
     {
         WinnerClass = Player.WHITE.ToString();
         this.Winner = WhitePlayer.ToString();
     }
     else if (BlackStones == WhiteStones)
     {
         WinnerClass = "Draw";
         this.Winner = "Draw";
     }
     Debug.WriteLine(WinnerClass);
 }
Example #18
0
        public static void checkWhiteKingStatus(Board board, Player player, List <Piece> blackPlayerActivePieces, List <Move> blackPlayerLegalMoves, List <Piece> whitePlayerActivePieces, List <Move> whitePlayerLegalMoves)
        {
            blackPlayerActivePieces = player.findActivePlayerPieces(board);
            blackPlayerLegalMoves   = player.calculateActivePlayerMoves(board, blackPlayerActivePieces);
            player = new WhitePlayer();
            whitePlayerActivePieces = player.findActivePlayerPieces(board);
            whitePlayerLegalMoves   = player.calculateActivePlayerMoves(board, whitePlayerActivePieces);
            Tuple <int, int> whitePlayerKingPosition = board.getPlayerKingPosition(pieceColor.White);

            if (player.kingInCheck(whitePlayerKingPosition, blackPlayerLegalMoves))
            {
                System.Media.SoundPlayer playersound = new System.Media.SoundPlayer(Properties.Resources.record2M_mp3cut);
                playersound.Play();
            }
            if (whitePlayerLegalMoves.Count() == 0)
            {
                MessageBox.Show("The game is over black player wins");
            }
        }
Example #19
0
        public void When_An_Invitation_Is_Sent_To_A_Player_And_The_Player_Already_Has_Sent_An_Invitation_Starts_A_Game()
        {
            bool gameStarted = false;
            var  blackPlayer = new BlackPlayer("invA");
            var  whitePlayer = new WhitePlayer("invB");

            _sut.RegisterPlayer(blackPlayer);
            _sut.RegisterPlayer(whitePlayer);
            _sut.GameStarted += (sender, participant, args) =>
            {
                if (participant == blackPlayer || participant == whitePlayer)
                {
                    gameStarted = true;
                }
            };

            _sut.TryInvitePlayerToGame(blackPlayer, whitePlayer);
            _sut.TryInvitePlayerToGame(whitePlayer, blackPlayer);

            gameStarted.Should().BeTrue();
        }
        private void StartGameImmediately()
        {
            if (!Validate())
            {
                return;
            }

            GamePlayer blackPlayer = BlackPlayer.Build(StoneColor.Black, TimeControl, BlackPlayerSettings);
            GamePlayer whitePlayer = WhitePlayer.Build(StoneColor.White, TimeControl, WhitePlayerSettings);

            BlackPlayerSettings.SaveAsInterfaceMementos();
            WhitePlayerSettings.SaveAsInterfaceMementos();

            LocalGame game = GameBuilder.CreateLocalGame().
                             BoardSize(SelectedGameBoardSize).
                             Ruleset(SelectedRuleset).
                             Komi(float.Parse(CompensationString, CultureInfo.InvariantCulture)).
                             Handicap(Handicap).
                             HandicapPlacementType(
                IsHandicapFixed ?
                HandicapPlacementType.Fixed :
                HandicapPlacementType.Free).
                             WhitePlayer(whitePlayer).
                             BlackPlayer(blackPlayer).
                             Build();

            Mvx.RegisterSingleton <IGame>(game);

            // Navigate to specific View Model
            if (_bundle.Style == GameCreationFormStyle.LocalGame)
            {
                OpenInNewActiveTab <LocalGameViewModel>();
            }
            else
            {
                OpenInNewActiveTab <OnlineGameViewModel>();
            }
        }
Example #21
0
 // Start is called before the first frame update
 void Start()
 {
     white          = GetComponent <WhitePlayer>();
     Cursor.visible = false;
 }
Example #22
0
 private void SetActualPlayer()
 {
     ActualPlayer = WhitePlayer.Equals(ActualPlayer) ? DarkPlayer : WhitePlayer;
 }
Example #23
0
 public GameSpecs()
 {
     _blackPlayer = new BlackPlayer("player1");
     _whitePlayer = new WhitePlayer("player2");
 }