コード例 #1
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));
        }
コード例 #2
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);
     }
 }
コード例 #3
0
ファイル: KnightMovePolitics.cs プロジェクト: peleccom/chess
        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);
        }
コード例 #4
0
ファイル: King.cs プロジェクト: peleccom/chess
        public MyList <Position> GetCastling()
        {
            MyList <Position> castling = new MyList <Position>();
            Figure            fig;
            int x = Position.GetX();

            if (ismoved())
            {
                return(castling);
            }
            Side oppside = GetEnemySide();
            int  ypos    = (this.Side == Side.White) ? 1 : 8;

            if (chessfield.isDangerPosition(oppside, Position))
            {
                return(castling);
            }

            fig = chessfield.GetFigureAt(new Position(1, ypos));
            if (fig != null &&
                !chessfield.isDangerPosition(oppside, new Position(x - 1, ypos)) &&
                !chessfield.isDangerPosition(oppside, new Position(x - 2, ypos)) &&
                !chessfield.isDangerPosition(oppside, new Position(x - 3, ypos)) &&
                !chessfield.isDangerPosition(oppside, new Position(x, ypos)) &&
                !fig.ismoved() &&
                chessfield.GetFigureAt(new Position(2, ypos)) == null &&
                chessfield.GetFigureAt(new Position(3, ypos)) == null &&
                chessfield.GetFigureAt(new Position(4, ypos)) == null)
            {
                castling.Add(fig.Position);
            }

            fig = chessfield.GetFigureAt(new Position(8, ypos));
            if (fig != null &&
                !chessfield.isDangerPosition(oppside, new Position(x + 1, ypos)) &&
                !chessfield.isDangerPosition(oppside, new Position(x + 2, ypos)) &&
                !chessfield.isDangerPosition(oppside, new Position(x, ypos)) &&
                !fig.ismoved() &&
                chessfield.GetFigureAt(new Position(7, ypos)) == null &&
                chessfield.GetFigureAt(new Position(6, ypos)) == null)
            {
                castling.Add(fig.Position);
            }
            return(castling);
        }
コード例 #5
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);
        }
コード例 #6
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);
        }
コード例 #7
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);
        }
コード例 #8
0
ファイル: DiagMovePolitics.cs プロジェクト: peleccom/chess
        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);
        }
コード例 #9
0
ファイル: ChessField.cs プロジェクト: peleccom/chess
 /// <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;
     }
 }
コード例 #10
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);
        }
コード例 #11
0
ファイル: Game.cs プロジェクト: peleccom/chess
        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;
        }
コード例 #12
0
ファイル: Game.cs プロジェクト: peleccom/chess
        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;
        }