コード例 #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();
            }
        }
コード例 #2
0
 void EndGameNormally(ChessPlayer Winner, ChessPoint from)
 {
     GameEnded = true;
     OnGameEnd();
     this.Winner = Winner;
     GameLengths.Add(Turns);
     NormallyEndedGames++;
     Turns = 0;
 }
コード例 #3
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);
        }
コード例 #4
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");
            }
        }
コード例 #5
0
 public ChessMove()
 {
     this.From   = ChessPoint.Zero;
     this.To     = ChessPoint.Zero;
     this.rating = 0;
 }
コード例 #6
0
 public ChessMove(ChessPoint From, ChessPoint To, int rating)
 {
     this.From   = From;
     this.To     = To;
     this.rating = rating;
 }
コード例 #7
0
 public ChessMove(ChessPoint From, ChessPoint To)
 {
     this.From   = From;
     this.To     = To;
     this.rating = 0;
 }
コード例 #8
0
 public ChessPiece GetChessPieceFromPoint(ChessPoint P)
 {
     return(Pieces[P.X, P.Y]);
 }
コード例 #9
0
 public bool IsFieldInBounds(ChessPoint P)
 {
     return(P.X >= 0 && P.Y >= 0 && P.X < 8 && P.Y < 8);
 }
コード例 #10
0
 public bool IsFieldVacant(ChessPoint P)
 {
     return(P.X >= 0 && P.Y >= 0 && P.X < 8 && P.Y < 8 && Pieces[P.X, P.Y] == null);
 }
コード例 #11
0
        public ChessPoint[] GetAllPossibleMovesForPiece(int fromX, int fromY)
        {
            if (fromX < 0 || fromY < 0 || fromX >= 8 || fromY >= 8 || Pieces[fromX, fromY] == null)
            {
                return(new ChessPoint[0]);
            }

            List <ChessPoint> PossibleMoves = new List <ChessPoint>();
            ChessPoint        UpLeft        = new ChessPoint();
            ChessPoint        UpRight       = new ChessPoint();
            ChessPoint        DownLeft      = new ChessPoint();
            ChessPoint        DownRight     = new ChessPoint();
            ChessPoint        RightUp       = new ChessPoint();
            ChessPoint        RightDown     = new ChessPoint();
            ChessPoint        LeftUp        = new ChessPoint();
            ChessPoint        LeftDown      = new ChessPoint();
            ChessPoint        Right         = new ChessPoint();
            ChessPoint        Down          = new ChessPoint();
            ChessPoint        Up            = new ChessPoint();
            ChessPoint        Left          = new ChessPoint();
            ChessPoint        P             = new ChessPoint();

            switch (Pieces[fromX, fromY].Type)
            {
            case ChessPieceType.Pawn:
                if (GetChessPieceFromPoint(fromX, fromY).Parent == PlayerTop)
                {
                    if (Pieces[fromX, fromY].HasMoved)
                    {
                        if (IsFieldVacant(new ChessPoint(fromX, fromY + 1)))
                        {
                            PossibleMoves.Add(new ChessPoint(fromX, fromY + 1));
                        }
                    }
                    else
                    {
                        if (IsFieldVacant(new ChessPoint(fromX, fromY + 1)))
                        {
                            PossibleMoves.Add(new ChessPoint(fromX, fromY + 1));
                            if (IsFieldVacant(new ChessPoint(fromX, fromY + 2)))
                            {
                                PossibleMoves.Add(new ChessPoint(fromX, fromY + 2));
                            }
                        }
                    }

                    Left  = new ChessPoint(fromX - 1, fromY + 1);
                    Right = new ChessPoint(fromX + 1, fromY + 1);

                    if (IsFieldInBounds(Left) && GetChessPieceFromPoint(Left) != null && Pieces[Left.X, Left.Y].Parent == PlayerBottom)
                    {
                        PossibleMoves.Add(Left);
                    }
                    if (IsFieldInBounds(Right) && GetChessPieceFromPoint(Right) != null && Pieces[Right.X, Right.Y].Parent == PlayerBottom)
                    {
                        PossibleMoves.Add(Right);
                    }
                }
                if (GetChessPieceFromPoint(fromX, fromY).Parent == PlayerBottom)
                {
                    if (Pieces[fromX, fromY].HasMoved)
                    {
                        if (IsFieldVacant(new ChessPoint(fromX, fromY - 1)))
                        {
                            PossibleMoves.Add(new ChessPoint(fromX, fromY - 1));
                        }
                    }
                    else
                    {
                        if (IsFieldVacant(new ChessPoint(fromX, fromY - 1)))
                        {
                            PossibleMoves.Add(new ChessPoint(fromX, fromY - 1));
                            if (IsFieldVacant(new ChessPoint(fromX, fromY - 2)))
                            {
                                PossibleMoves.Add(new ChessPoint(fromX, fromY - 2));
                            }
                        }
                    }

                    Left  = new ChessPoint(fromX - 1, fromY - 1);
                    Right = new ChessPoint(fromX + 1, fromY - 1);

                    if (IsFieldInBounds(Left) && GetChessPieceFromPoint(Left) != null && Pieces[Left.X, Left.Y].Parent == PlayerTop)
                    {
                        PossibleMoves.Add(Left);
                    }
                    if (IsFieldInBounds(Right) && GetChessPieceFromPoint(Right) != null && Pieces[Right.X, Right.Y].Parent == PlayerTop)
                    {
                        PossibleMoves.Add(Right);
                    }
                }
                break;

            case ChessPieceType.Knight:
                UpLeft    = new ChessPoint(fromX - 1, fromY - 2);
                UpRight   = new ChessPoint(fromX + 1, fromY - 2);
                DownLeft  = new ChessPoint(fromX - 1, fromY + 2);
                DownRight = new ChessPoint(fromX + 1, fromY + 2);
                RightUp   = new ChessPoint(fromX + 2, fromY - 1);
                RightDown = new ChessPoint(fromX + 2, fromY + 1);
                LeftUp    = new ChessPoint(fromX - 2, fromY - 1);
                LeftDown  = new ChessPoint(fromX - 2, fromY + 1);

                if (CanPlayerMoveThere(PlayerWhoHasTheMove(), UpLeft))
                {
                    PossibleMoves.Add(UpLeft);
                }
                if (CanPlayerMoveThere(PlayerWhoHasTheMove(), UpRight))
                {
                    PossibleMoves.Add(UpRight);
                }
                if (CanPlayerMoveThere(PlayerWhoHasTheMove(), DownLeft))
                {
                    PossibleMoves.Add(DownLeft);
                }
                if (CanPlayerMoveThere(PlayerWhoHasTheMove(), DownRight))
                {
                    PossibleMoves.Add(DownRight);
                }
                if (CanPlayerMoveThere(PlayerWhoHasTheMove(), RightUp))
                {
                    PossibleMoves.Add(RightUp);
                }
                if (CanPlayerMoveThere(PlayerWhoHasTheMove(), RightDown))
                {
                    PossibleMoves.Add(RightDown);
                }
                if (CanPlayerMoveThere(PlayerWhoHasTheMove(), LeftUp))
                {
                    PossibleMoves.Add(LeftUp);
                }
                if (CanPlayerMoveThere(PlayerWhoHasTheMove(), LeftDown))
                {
                    PossibleMoves.Add(LeftDown);
                }
                break;

            case ChessPieceType.King:
                UpLeft    = new ChessPoint(fromX - 1, fromY - 1);
                UpRight   = new ChessPoint(fromX + 1, fromY - 1);
                DownLeft  = new ChessPoint(fromX - 1, fromY + 1);
                DownRight = new ChessPoint(fromX + 1, fromY + 1);
                Right     = new ChessPoint(fromX + 1, fromY);
                Down      = new ChessPoint(fromX, fromY + 1);
                Up        = new ChessPoint(fromX, fromY - 1);
                Left      = new ChessPoint(fromX - 1, fromY);

                if (CanPlayerMoveThere(PlayerWhoHasTheMove(), UpLeft))
                {
                    PossibleMoves.Add(UpLeft);
                }
                if (CanPlayerMoveThere(PlayerWhoHasTheMove(), UpRight))
                {
                    PossibleMoves.Add(UpRight);
                }
                if (CanPlayerMoveThere(PlayerWhoHasTheMove(), DownLeft))
                {
                    PossibleMoves.Add(DownLeft);
                }
                if (CanPlayerMoveThere(PlayerWhoHasTheMove(), DownRight))
                {
                    PossibleMoves.Add(DownRight);
                }
                if (CanPlayerMoveThere(PlayerWhoHasTheMove(), Right))
                {
                    PossibleMoves.Add(Right);
                }
                if (CanPlayerMoveThere(PlayerWhoHasTheMove(), Down))
                {
                    PossibleMoves.Add(Down);
                }
                if (CanPlayerMoveThere(PlayerWhoHasTheMove(), Up))
                {
                    PossibleMoves.Add(Up);
                }
                if (CanPlayerMoveThere(PlayerWhoHasTheMove(), Left))
                {
                    PossibleMoves.Add(Left);
                }
                break;

            case ChessPieceType.Rook:
                P = new ChessPoint(fromX, fromY + 1);
                while (CanPlayerMoveThere(PlayerWhoHasTheMove(), P))
                {
                    PossibleMoves.Add(new ChessPoint(P.X, P.Y));
                    if (GetChessPieceFromPoint(P) != null && GetChessPieceFromPoint(P).Parent == PlayerWhoHasntTheMove())
                    {
                        break;
                    }
                    P.Y++;
                }

                P = new ChessPoint(fromX, fromY - 1);
                while (CanPlayerMoveThere(PlayerWhoHasTheMove(), P))
                {
                    PossibleMoves.Add(new ChessPoint(P.X, P.Y));
                    if (GetChessPieceFromPoint(P) != null && GetChessPieceFromPoint(P).Parent == PlayerWhoHasntTheMove())
                    {
                        break;
                    }
                    P.Y--;
                }

                P = new ChessPoint(fromX + 1, fromY);
                while (CanPlayerMoveThere(PlayerWhoHasTheMove(), P))
                {
                    PossibleMoves.Add(new ChessPoint(P.X, P.Y));
                    if (GetChessPieceFromPoint(P) != null && GetChessPieceFromPoint(P).Parent == PlayerWhoHasntTheMove())
                    {
                        break;
                    }
                    P.X++;
                }

                P = new ChessPoint(fromX - 1, fromY);
                while (CanPlayerMoveThere(PlayerWhoHasTheMove(), P))
                {
                    PossibleMoves.Add(new ChessPoint(P.X, P.Y));
                    if (GetChessPieceFromPoint(P) != null && GetChessPieceFromPoint(P).Parent == PlayerWhoHasntTheMove())
                    {
                        break;
                    }
                    P.X--;
                }
                break;

            case ChessPieceType.Bishop:
                P = new ChessPoint(fromX + 1, fromY + 1);
                while (CanPlayerMoveThere(PlayerWhoHasTheMove(), P))
                {
                    PossibleMoves.Add(new ChessPoint(P.X, P.Y));
                    if (GetChessPieceFromPoint(P) != null && GetChessPieceFromPoint(P).Parent == PlayerWhoHasntTheMove())
                    {
                        break;
                    }
                    P.X++; P.Y++;
                }

                P = new ChessPoint(fromX - 1, fromY - 1);
                while (CanPlayerMoveThere(PlayerWhoHasTheMove(), P))
                {
                    PossibleMoves.Add(new ChessPoint(P.X, P.Y));
                    if (GetChessPieceFromPoint(P) != null && GetChessPieceFromPoint(P).Parent == PlayerWhoHasntTheMove())
                    {
                        break;
                    }
                    P.X--; P.Y--;
                }

                P = new ChessPoint(fromX + 1, fromY - 1);
                while (CanPlayerMoveThere(PlayerWhoHasTheMove(), P))
                {
                    PossibleMoves.Add(new ChessPoint(P.X, P.Y));
                    if (GetChessPieceFromPoint(P) != null && GetChessPieceFromPoint(P).Parent == PlayerWhoHasntTheMove())
                    {
                        break;
                    }
                    P.X++; P.Y--;
                }

                P = new ChessPoint(fromX - 1, fromY + 1);
                while (CanPlayerMoveThere(PlayerWhoHasTheMove(), P))
                {
                    PossibleMoves.Add(new ChessPoint(P.X, P.Y));
                    if (GetChessPieceFromPoint(P) != null && GetChessPieceFromPoint(P).Parent == PlayerWhoHasntTheMove())
                    {
                        break;
                    }
                    P.X--; P.Y++;
                }
                break;

            case ChessPieceType.Queen:
                P = new ChessPoint(fromX, fromY + 1);
                while (CanPlayerMoveThere(PlayerWhoHasTheMove(), P))
                {
                    PossibleMoves.Add(new ChessPoint(P.X, P.Y));
                    if (GetChessPieceFromPoint(P) != null && GetChessPieceFromPoint(P).Parent == PlayerWhoHasntTheMove())
                    {
                        break;
                    }
                    P.Y++;
                }

                P = new ChessPoint(fromX, fromY - 1);
                while (CanPlayerMoveThere(PlayerWhoHasTheMove(), P))
                {
                    PossibleMoves.Add(new ChessPoint(P.X, P.Y));
                    if (GetChessPieceFromPoint(P) != null && GetChessPieceFromPoint(P).Parent == PlayerWhoHasntTheMove())
                    {
                        break;
                    }
                    P.Y--;
                }

                P = new ChessPoint(fromX + 1, fromY);
                while (CanPlayerMoveThere(PlayerWhoHasTheMove(), P))
                {
                    PossibleMoves.Add(new ChessPoint(P.X, P.Y));
                    if (GetChessPieceFromPoint(P) != null && GetChessPieceFromPoint(P).Parent == PlayerWhoHasntTheMove())
                    {
                        break;
                    }
                    P.X++;
                }

                P = new ChessPoint(fromX - 1, fromY);
                while (CanPlayerMoveThere(PlayerWhoHasTheMove(), P))
                {
                    PossibleMoves.Add(new ChessPoint(P.X, P.Y));
                    if (GetChessPieceFromPoint(P) != null && GetChessPieceFromPoint(P).Parent == PlayerWhoHasntTheMove())
                    {
                        break;
                    }
                    P.X--;
                }

                P = new ChessPoint(fromX + 1, fromY + 1);
                while (CanPlayerMoveThere(PlayerWhoHasTheMove(), P))
                {
                    PossibleMoves.Add(new ChessPoint(P.X, P.Y));
                    if (GetChessPieceFromPoint(P) != null && GetChessPieceFromPoint(P).Parent == PlayerWhoHasntTheMove())
                    {
                        break;
                    }
                    P.X++; P.Y++;
                }

                P = new ChessPoint(fromX - 1, fromY - 1);
                while (CanPlayerMoveThere(PlayerWhoHasTheMove(), P))
                {
                    PossibleMoves.Add(new ChessPoint(P.X, P.Y));
                    if (GetChessPieceFromPoint(P) != null && GetChessPieceFromPoint(P).Parent == PlayerWhoHasntTheMove())
                    {
                        break;
                    }
                    P.X--; P.Y--;
                }

                P = new ChessPoint(fromX + 1, fromY - 1);
                while (CanPlayerMoveThere(PlayerWhoHasTheMove(), P))
                {
                    PossibleMoves.Add(new ChessPoint(P.X, P.Y));
                    if (GetChessPieceFromPoint(P) != null && GetChessPieceFromPoint(P).Parent == PlayerWhoHasntTheMove())
                    {
                        break;
                    }
                    P.X++; P.Y--;
                }

                P = new ChessPoint(fromX - 1, fromY + 1);
                while (CanPlayerMoveThere(PlayerWhoHasTheMove(), P))
                {
                    PossibleMoves.Add(new ChessPoint(P.X, P.Y));
                    if (GetChessPieceFromPoint(P) != null && GetChessPieceFromPoint(P).Parent == PlayerWhoHasntTheMove())
                    {
                        break;
                    }
                    P.X--; P.Y++;
                }
                break;
            }

            return(PossibleMoves.ToArray());
        }