Exemple #1
0
        public void ArrangeFigures(ChessField chessfield)
        {
            // расстановка на доске
            for (int i = 0; i < 8; i++)
            {
                alivefigures.Add(new Pawn(SidePosition(i, 1), side, chessfield));
            }
            alivefigures.Add(new Rook(SidePosition(0, 0), side, chessfield));
            alivefigures.Add(new Rook(SidePosition(7, 0), side, chessfield));
            alivefigures.Add(new Knight(SidePosition(1, 0), side, chessfield));
            alivefigures.Add(new Knight(SidePosition(6, 0), side, chessfield));
            alivefigures.Add(new Bishop(SidePosition(2, 0), side, chessfield));
            alivefigures.Add(new Bishop(SidePosition(5, 0), side, chessfield));
            // король с ферзем ставяться зеркально
            Position pos = SidePosition(3, 0);     // отображаем координату y если надо

            king = new King(new Position(4 + 1, pos.GetY()), side, chessfield);
            alivefigures.Add(king);
            alivefigures.Add(new Queen(new Position(3 + 1, pos.GetY()), side, chessfield));

            foreach (Figure figure in alivefigures)
            {
                // устанавливаем обработчики
                figure.MoveEvent += chessfield.MoveFigureHandler;
                figure.KillEvent += KillFigureHandler;
            }
            this.chessfield = chessfield;
            shahsituation   = false;
        }
Exemple #2
0
        public bool Castle(Position pos)
        {
            if (!isHighlighted())
            {
                return(false);
            }
            if (!isCorrectCastling(pos))
            {
                return(false);
            }

            if (gametype == GameType.ServerGame && state == GameState.HighlightedWhite)
            {
                server.NewMove(highlightedfigurepos, pos);
            }

            if (gametype == GameType.ClientGame && state == GameState.HighlightedBlack)
            {
                client.NewMove(highlightedfigurepos, pos);
            }
            Figure fig  = Field.GetFigureAt(highlightedfigurepos);
            Figure rook = Field.GetFigureAt(pos);

            if (pos.GetX() == 1)
            {
                // длинная рокировка
                int y = pos.GetY();
                fig.SetPosition(new Position(pos.GetX() + 2, y));
                rook.SetPosition(new Position(pos.GetX() + 3, y));
            }
            if (pos.GetX() == 8)
            {
                // короткая рокировка
                int y = pos.GetY();
                fig.SetPosition(new Position(pos.GetX() - 1, y));
                rook.SetPosition(new Position(pos.GetX() - 2, y));
            }

            switch (state)
            {
            case GameState.HighlightedBlack:
                state = GameState.WaitWhite;
                break;

            case GameState.HighlightedWhite:
                state = GameState.WaitBlack;
                break;
            }
            return(true);
        }
Exemple #3
0
        /// <summary>
        /// Возвращает позицию инвертированную по Y
        /// </summary>
        /// <param name="pos"></param>
        /// <returns></returns>
        public Position Reverse(Position pos)
        {
            byte x = pos.GetX();
            byte y = pos.GetY();

            y = (byte)(9 - y);
            return(new Position(x, y));
        }
Exemple #4
0
 /// <summary>
 ///  Получить фигуру в позиции pos
 /// </summary>
 /// <param name="pos"></param>
 /// <returns></returns>
 public Figure GetFigureAt(Position pos)
 {
     try
     {
         return(field[pos.GetX() - 1, pos.GetY() - 1]);
     }
     catch
     {
         return(null);
     }
 }
Exemple #5
0
        public MyList <Position> GetMoves(Figure figure, ChessField cf)
        {
            Position          pos = figure.Position;
            MyList <Position> l = new MyList <Position>();
            int x = pos.GetX(), y = pos.GetY();

            try
            {
                l.Add(new Position(x + 1, y + 2));
            }
            catch {}
            try
            {
                l.Add(new Position(x + 1, y + -2));
            }
            catch { }
            try
            {
                l.Add(new Position(x - 1, y + 2));
            }
            catch { }
            try
            {
                l.Add(new Position(x - 1, y - 2));
            }
            catch { }
            try
            {
                l.Add(new Position(x + 2, y + 1));
            }
            catch { }
            try
            {
                l.Add(new Position(x + 2, y - 1));
            }
            catch { }
            try
            {
                l.Add(new Position(x - 2, y + 1));
            }
            catch { }
            try
            {
                l.Add(new Position(x - 2, y - 1));
            }
            catch { }
            return(l);
        }
Exemple #6
0
        /// <summary>
        /// Установить новую позицию
        /// </summary>
        /// <param name="pos"></param>
        public void SetPosition(Position pos)
        {
            Position oldpos = Position;

            Position = pos;
            hasmoved = true;
            OnMoveEvent(oldpos, pos);
            //chessfield.PositionChanged(this, pos);
            if (this.GetFigureType() == FigureTypes.Pawn)
            {
                Position midpos = pos;
                if (Side == Side.Black)
                {
                    midpos = Reverse(midpos);
                }
                if (midpos.GetY() == 8)
                {
                    OnPawnSuperiorityEvent();
                }
            }
        }
Exemple #7
0
        public bool InMoveAttack(Position pos)
        {
            if (!isHighlighted())
            {
                return(false);
            }
            if (!isCorrectInMoveAttack(pos))
            {
                return(false);
            }
            if (inmoveattacks.Contains(pos))
            {
                if (gametype == GameType.ServerGame && state == GameState.HighlightedWhite)
                {
                    server.NewMove(highlightedfigurepos, pos);
                }

                if (gametype == GameType.ClientGame && state == GameState.HighlightedBlack)
                {
                    client.NewMove(highlightedfigurepos, pos);
                }
                Figure fig   = Field.GetFigureAt(highlightedfigurepos);
                int    delta = (fig.Side == Side.Black) ? +1 : -1;
                fig.SetPosition(new Position(pos.GetX(), pos.GetY() - delta));
                Figure killedfig = Field.GetFigureAt(pos);
                Field.Kill(killedfig);
            }

            switch (state)
            {
            case GameState.HighlightedBlack:
                state = GameState.WaitWhite;
                break;

            case GameState.HighlightedWhite:
                state = GameState.WaitBlack;
                break;
            }
            return(true);
        }
Exemple #8
0
        public MyList <Position> GetMoves(Figure figure, ChessField cf)
        {
            Position          pos = figure.Position;
            MyList <Position> l   = new MyList <Position>();

            switch (figure.GetFigureType())
            {
            case FigureTypes.King:
            {
                if (pos.GetX() > 1)
                {
                    l.Add(new Position(pos.GetX() - 1, pos.GetY()));
                }
                if (pos.GetX() < 8)
                {
                    l.Add(new Position(pos.GetX() + 1, pos.GetY()));
                }
                break;
            }

            default:     // Bishop, Queen & etc.
            {
                // +
                for (int i = pos.GetX() + 1; i <= 8; i++)
                {
                    l.Add(new Position(i, pos.GetY()));
                    if (cf.GetFigureAt(new Position(i, pos.GetY())) != null)
                    {
                        break;
                    }
                }
                // --
                for (int i = pos.GetX() - 1; i > 0; i--)
                {
                    l.Add(new Position(i, pos.GetY()));
                    if (cf.GetFigureAt(new Position(i, pos.GetY())) != null)
                    {
                        break;
                    }
                }
                break;
            }
            }
            return(l);
        }
Exemple #9
0
        public MyList <Position> GetMoves(Figure figure, ChessField cf)
        {
            Position          pos = figure.Position, tmppos;
            MyList <Position> l = new MyList <Position>();

            switch (figure.GetFigureType())
            {
            case FigureTypes.Pawn:
            {
                if (figure.Side == Side.Black)
                {
                    pos = figure.Reverse(pos);
                }
                if (pos.GetY() < 8)
                {
                    if (pos.GetX() < 8)
                    {
                        tmppos = new Position(pos.GetX() + 1, pos.GetY() + 1);
                        l.Add(figure.Side == Side.White ? tmppos : figure.Reverse(tmppos));
                    }
                    if (pos.GetX() > 1)
                    {
                        tmppos = new Position(pos.GetX() - 1, pos.GetY() + 1);
                        l.Add(figure.Side == Side.White ? tmppos : figure.Reverse(tmppos));
                    }
                }
                break;
            }

            case FigureTypes.King:
            {
                int x, y;
                x = pos.GetX();
                y = pos.GetY();
                if (x < 8)
                {
                    if (y < 8)
                    {
                        l.Add(new Position(x + 1, y + 1));
                    }
                    if (y > 1)
                    {
                        l.Add(new Position(x + 1, y - 1));
                    }
                }
                if (x > 1)
                {
                    if (y < 8)
                    {
                        l.Add(new Position(x - 1, y + 1));
                    }
                    if (y > 1)
                    {
                        l.Add(new Position(x - 1, y - 1));
                    }
                }
                break;
            }

            default:
            {
                for (int i = pos.GetX() + 1, j = pos.GetY() + 1; i <= 8 && j <= 8; i++, j++)
                {
                    l.Add(new Position(i, j));
                    if (cf.GetFigureAt(new Position(i, j)) != null)
                    {
                        break;
                    }
                }

                for (int i = pos.GetX() - 1, j = pos.GetY() - 1; i >= 1 && j >= 1; i--, j--)
                {
                    l.Add(new Position(i, j));
                    if (cf.GetFigureAt(new Position(i, j)) != null)
                    {
                        break;
                    }
                }
                for (int i = pos.GetX() + 1, j = pos.GetY() - 1; i <= 8 && j >= 1; i++, j--)
                {
                    l.Add(new Position(i, j));
                    if (cf.GetFigureAt(new Position(i, j)) != null)
                    {
                        break;
                    }
                }
                for (int i = pos.GetX() - 1, j = pos.GetY() + 1; i >= 1 && j <= 8; i--, j++)
                {
                    l.Add(new Position(i, j));
                    if (cf.GetFigureAt(new Position(i, j)) != null)
                    {
                        break;
                    }
                }
                break;
            }
            }
            return(l);
        }
Exemple #10
0
 /// <summary>
 ///  Получить фигуру в позиции pos
 /// </summary>
 /// <param name="pos"></param>
 /// <returns></returns>
 public Figure GetFigureAt(Position pos)
 {
     try
     {
         return field[pos.GetX() - 1, pos.GetY() - 1];
     }
     catch
     {
         return null;
     }
 }
Exemple #11
0
        public MyList <Position> GetMoves(Figure figure, ChessField cf)
        {
            Position          pos = figure.Position;
            Position          tmppos;
            MyList <Position> l = new MyList <Position>();

            switch (figure.GetFigureType())
            {
            case FigureTypes.Pawn:
            {
                if (figure.Side == Side.Black)
                {
                    pos = figure.Reverse(pos);
                }
                if (pos.GetY() == 2)
                {
                    Position middlepos = new Position(pos.GetX(), pos.GetY() + 1);
                    middlepos = figure.Side == Side.White ? middlepos : figure.Reverse(middlepos);
                    if (cf.GetFigureAt(middlepos) == null)
                    {
                        tmppos = new Position(pos.GetX(), pos.GetY() + 2);
                        l.Add(figure.Side == Side.White ? tmppos : figure.Reverse(tmppos));
                    }
                }

                int y = pos.GetY();
                if (y + 1 < 9)
                {
                    tmppos = new Position(pos.GetX(), y + 1);
                    l.Add(figure.Side == Side.White ? tmppos : figure.Reverse(tmppos));
                }
                break;
            }

            case FigureTypes.King:
            {
                if (pos.GetY() > 1)
                {
                    l.Add(new Position(pos.GetX(), pos.GetY() - 1));
                }
                if (pos.GetY() < 8)
                {
                    l.Add(new Position(pos.GetX(), pos.GetY() + 1));
                }
                break;
            }

            default:     // Bishop, Queen & etc.
            {
                // +
                for (int i = pos.GetY() + 1; i <= 8; i++)
                {
                    l.Add(new Position(pos.GetX(), i));
                    if (cf.GetFigureAt(new Position(pos.GetX(), i)) != null)
                    {
                        break;
                    }
                }
                //--
                for (int i = pos.GetY() - 1; i > 0; i--)
                {
                    l.Add(new Position(pos.GetX(), i));
                    if (cf.GetFigureAt(new Position(pos.GetX(), i)) != null)
                    {
                        break;
                    }
                }
                break;
            }
            }
            return(l);
        }
Exemple #12
0
        public bool Castle(Position pos)
        {
            if (!isHighlighted())
                return false;
            if (!isCorrectCastling(pos))
                return false;

            if (gametype == GameType.ServerGame && state == GameState.HighlightedWhite)
            {
                server.NewMove(highlightedfigurepos, pos);
            }

            if (gametype == GameType.ClientGame && state == GameState.HighlightedBlack)
            {
                client.NewMove(highlightedfigurepos, pos);
            }
            Figure fig = Field.GetFigureAt(highlightedfigurepos);
            Figure rook = Field.GetFigureAt(pos);
            if (pos.GetX() == 1)
            {
                // длинная рокировка
                int y = pos.GetY();
                fig.SetPosition(new Position(pos.GetX() + 2, y));
                rook.SetPosition(new Position(pos.GetX() + 3, y));
            }
            if (pos.GetX() == 8)
            {
                // короткая рокировка
                int y = pos.GetY();
                fig.SetPosition(new Position(pos.GetX() - 1, y));
                rook.SetPosition(new Position(pos.GetX() - 2, y));
            }

            switch (state)
            {
                case GameState.HighlightedBlack:
                    state = GameState.WaitWhite;
                    break;
                case GameState.HighlightedWhite:
                    state = GameState.WaitBlack;
                    break;
            }
            return true;
        }
Exemple #13
0
        public bool InMoveAttack(Position pos)
        {
            if (!isHighlighted())
                return false;
            if (!isCorrectInMoveAttack(pos))
                return false;
            if (inmoveattacks.Contains(pos))
            {

                if (gametype == GameType.ServerGame && state == GameState.HighlightedWhite)
                {
                    server.NewMove(highlightedfigurepos, pos);
                }

                if (gametype == GameType.ClientGame && state == GameState.HighlightedBlack)
                {
                    client.NewMove(highlightedfigurepos, pos);
                }
                Figure fig = Field.GetFigureAt(highlightedfigurepos);
                int delta = (fig.Side == Side.Black) ? +1 : -1;
                fig.SetPosition(new Position(pos.GetX(), pos.GetY()-delta));
                Figure killedfig = Field.GetFigureAt(pos);
                Field.Kill(killedfig);
            }

            switch (state)
            {
                case GameState.HighlightedBlack:
                    state = GameState.WaitWhite;
                    break;
                case GameState.HighlightedWhite:
                    state = GameState.WaitBlack;
                    break;
            }
            return true;
        }