Ejemplo n.º 1
0
        public void MovePiece(ChessMove M)
        {
            if (slow)
            {
                Thread.Sleep(1000);
            }

            ChessPoint from = M.From;
            ChessPoint to   = M.To;

            ChessPiece        FromPiece        = GetChessPieceFromPoint(from);
            ChessPlayer       MovePlayer       = PlayerWhoHasTheMove();
            List <ChessPoint> AllPossibleMoves = new List <ChessPoint>(GetAllPossibleMovesForPiece(from));

            if (FromPiece.Parent == MovePlayer && AllPossibleMoves.Contains(to))
            {
                Turns++;

                if (Pieces[to.X, to.Y] == TopKing)
                {
                    EndGameNormally(PlayerBottom, from);

                    if (slow)
                    {
                        Thread.Sleep(3000);
                    }
                }
                if (Pieces[to.X, to.Y] == BottomKing)
                {
                    EndGameNormally(PlayerTop, from);

                    if (slow)
                    {
                        Thread.Sleep(3000);
                    }
                }

                Pieces[to.X, to.Y]          = Pieces[from.X, from.Y];
                Pieces[from.X, from.Y]      = null;
                Pieces[to.X, to.Y].HasMoved = true;

                if (IsThreefoldRepetitionCheckActive)
                {
                    ThreefoldRepetitionCheck[to.X, to.Y, (int)Pieces[to.X, to.Y].Type + (Pieces[to.X, to.Y].Parent == PlayerBottom ? 0 : 6)]++;
                    if (ThreefoldRepetitionCheck[to.X, to.Y, (int)Pieces[to.X, to.Y].Type + (Pieces[to.X, to.Y].Parent == PlayerBottom ? 0 : 6)] >= AllowedRepetitions)
                    {
                        EndGameBecauseOfRecurrence(PlayerWhoHasTheMove(), from);
                    }
                }

                Turn = !Turn;
                PlayerWhoHasTheMove().TurnStarted();
            }
            else
            {
                throw new ArgumentException();
            }
        }
Ejemplo n.º 2
0
 void EndGameNormally(ChessPlayer Winner, ChessPoint from)
 {
     GameEnded = true;
     OnGameEnd();
     this.Winner = Winner;
     GameLengths.Add(Turns);
     NormallyEndedGames++;
     Turns = 0;
 }
Ejemplo n.º 3
0
        public ChessBoard(ChessPlayer PlayerOne, ChessPlayer PlayerTwo)
        {
            playerTop    = PlayerOne;
            playerBottom = PlayerTwo;

            PlayerTop.Parent    = this;
            PlayerBottom.Parent = this;

            SetUpNewGame();
        }
Ejemplo n.º 4
0
 public ChessPlayer GetOponent(ChessPlayer you)
 {
     if (PlayerTop == you)
     {
         return(PlayerBottom);
     }
     else
     {
         return(PlayerTop);
     }
 }
Ejemplo n.º 5
0
        public void SetUpNewGame(ChessPlayer PlayerTop, ChessPlayer PlayerBottom)
        {
            ClearPieces();
            ClearThreefoldRepetitionCheck();

            this.playerTop      = PlayerTop;
            this.playerBottom   = PlayerBottom;
            PlayerTop.Parent    = this;
            PlayerBottom.Parent = this;

            Turn      = false;
            Winner    = null;
            GameEnded = false;

            PlayerTop.NewMatchStarted(true);
            PlayerBottom.NewMatchStarted(false);

            // Pawns
            for (int i = 0; i < 8; i++)
            {
                Pieces[i, 1] = new ChessPiece(PlayerTop, ChessPieceType.Pawn);
            }
            for (int i = 0; i < 8; i++)
            {
                Pieces[i, 6] = new ChessPiece(PlayerBottom, ChessPieceType.Pawn);
            }

            // Top Rest
            Pieces[0, 0] = new ChessPiece(PlayerTop, ChessPieceType.Rook);
            Pieces[1, 0] = new ChessPiece(PlayerTop, ChessPieceType.Knight);
            Pieces[2, 0] = new ChessPiece(PlayerTop, ChessPieceType.Bishop);
            Pieces[3, 0] = new ChessPiece(PlayerTop, ChessPieceType.King);
            Pieces[4, 0] = new ChessPiece(PlayerTop, ChessPieceType.Queen);
            Pieces[5, 0] = new ChessPiece(PlayerTop, ChessPieceType.Bishop);
            Pieces[6, 0] = new ChessPiece(PlayerTop, ChessPieceType.Knight);
            Pieces[7, 0] = new ChessPiece(PlayerTop, ChessPieceType.Rook);

            // Bottom Rest
            Pieces[0, 7] = new ChessPiece(PlayerBottom, ChessPieceType.Rook);
            Pieces[1, 7] = new ChessPiece(PlayerBottom, ChessPieceType.Knight);
            Pieces[2, 7] = new ChessPiece(PlayerBottom, ChessPieceType.Bishop);
            Pieces[3, 7] = new ChessPiece(PlayerBottom, ChessPieceType.King);
            Pieces[4, 7] = new ChessPiece(PlayerBottom, ChessPieceType.Queen);
            Pieces[5, 7] = new ChessPiece(PlayerBottom, ChessPieceType.Bishop);
            Pieces[6, 7] = new ChessPiece(PlayerBottom, ChessPieceType.Knight);
            Pieces[7, 7] = new ChessPiece(PlayerBottom, ChessPieceType.Rook);

            TopKing    = Pieces[3, 0];
            BottomKing = Pieces[3, 7];
        }
Ejemplo n.º 6
0
        public ChessMove[] GetAllMoves(ChessBoard Board, ChessPlayer Player)
        {
            List <ChessMove> moves = new List <ChessMove>(8 * 8);

            for (int x = 0; x < 8; x++)
            {
                for (int y = 0; y < 8; y++)
                {
                    if (Board.GetChessPieceFromPoint(x, y) != null && Board.GetChessPieceFromPoint(x, y).Parent == Player)
                    {
                        ChessPoint[] targets = Board.GetAllPossibleMovesForPiece(x, y);
                        for (int i = 0; i < targets.Length; i++)
                        {
                            moves.Add(new ChessMove(new ChessPoint(x, y), targets[i]));
                        }
                    }
                }
            }
            return(moves.ToArray());
        }
Ejemplo n.º 7
0
        public bool CanPlayerMoveThere(ChessPlayer CurrentPlayer, ChessPoint P)
        {
            ChessPlayer OtherPlayer = null;

            if (CurrentPlayer == PlayerBottom)
            {
                OtherPlayer = PlayerTop;
            }
            else if (CurrentPlayer == PlayerTop)
            {
                OtherPlayer = PlayerBottom;
            }
            else
            {
                throw new ArgumentException();
            }

            return(IsFieldInBounds(P) && GetChessPieceFromPoint(P) == null ||
                   IsFieldInBounds(P) && GetChessPieceFromPoint(P) != null && GetChessPieceFromPoint(P).Parent == OtherPlayer);
        }
Ejemplo n.º 8
0
        void EndGameBecauseOfRecurrence(ChessPlayer FaultyPlayer, ChessPoint from)
        {
            GameEnded = true;
            OnGameEnd();
            GameLengths.Add(Turns);
            Turns = 0;
            EndedGameBecauseOfRecurrance++;

            if (FaultyPlayer == PlayerTop)
            {
                Winner = PlayerBottom;
            }
            else if (FaultyPlayer == PlayerBottom)
            {
                Winner = PlayerTop;
            }
            else
            {
                throw new Exception("wat");
            }
        }
Ejemplo n.º 9
0
 public ChessPiece(ChessPlayer Parent, ChessPieceType Type)
 {
     this.Parent = Parent;
     this.Type   = Type;
 }