Example #1
0
 public Bishop(Position pos, Side side, ChessField chessfield)
     : base(pos, side, chessfield)
 {
     FigureType = FigureTypes.Bishop;
     movepolitics = new MovePolitics[] {
                                 chessfield.DiagMovePolitics};
 }
Example #2
0
 public void SetPosition(Position pos)
 {
     if (x > 8 | x < 1 | y > 8 | y < 1)
         throw new ArgumentException();
     x = pos.x;
     y = pos.y;
 }
Example #3
0
 public Knight(Position pos, Side side, ChessField chessfield)
     : base(pos, side, chessfield)
 {
     FigureType = FigureTypes.Knight;
     movepolitics = new MovePolitics[] {
                                 chessfield.KnightMovePolitics};
 }
Example #4
0
 public Pawn(Position pos, Side side, ChessField chessfield)
     : base(pos,side, chessfield)
 {
     FigureType = FigureTypes.Pawn;
     movepolitics = new MovePolitics[] { chessfield.VerticalMovePolitics};
     attackpolitics = new MovePolitics[] { chessfield.DiagMovePolitics};
 }
Example #5
0
        /// <summary>
        /// Вернет список бьющихся фигур на проходе
        /// </summary>
        /// <returns></returns>
        public MyList<Position> GetInMoveAttacks()
        {
            int delta = (Side == Side.Black) ? +1 : -1;
            Position pos;
            Side oppside = GetEnemySide();
            Figure fig;
            MyList<Position> moves = new MyList<Position>();
            foreach (Position attack in GetAttacks())
            {
                try{
                    pos = new Position(attack.GetX(), attack.GetY() + delta);
                    fig = chessfield.GetFigureAt(pos);
                    if (fig != null && fig.GetFigureType() == FigureTypes.Pawn &&
                        fig.Side == oppside &&
                        chessfield.GetlastMoved(oppside) == fig)
                        moves.Add(pos);
                    {

                    }

                }
                catch {}

            }
            return moves;
        }
Example #6
0
 public Rook(Position pos, Side side, ChessField chessfield)
     : base(pos, side, chessfield)
 {
     FigureType = FigureTypes.Rook;
     movepolitics = new MovePolitics[] {
                                 chessfield.VerticalMovePolitics,
                                 chessfield.HorizontalMovePolitics};
 }
Example #7
0
 public void NewMove(Position from, Position to)
 {
     //MessageBox.Show("new move");
     lock (lockobj)
     {
         newmove = true;
         this.from = from;
         this.to = to;
     }
 }
Example #8
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;
        }
Example #9
0
 /// <summary>
 ///  Установить фигуру позиции pos
 /// </summary>
 /// <param name="pos"></param>
 /// <param name="fig"></param>
 private void SetFigureAt(Position pos, Figure fig)
 {
     try
     {
         field[pos.GetX0(), pos.GetY0()] = fig;
     }
     catch
     {
         return;
     }
 }
Example #10
0
 /// <summary>
 /// Создать фигуру заданного типа
 /// </summary>
 /// <param name="figtype">Тип фигуры</param>
 /// <param name="pos">Позиция</param>
 /// <returns></returns>
 Figure CreateFigure(FigureTypes figtype, Position pos, Side side)
 {
     switch (figtype)
     {
         case FigureTypes.Bishop:
             {
                 return new Bishop(pos, side, this);
             }
         case FigureTypes.King:
             {
                 return new King(pos, side, this);
             }
         case FigureTypes.Knight:
             {
                 return new Knight(pos, side, this);
             }
         case FigureTypes.Pawn:
             {
                 return new Pawn(pos, side, this);
             }
         case FigureTypes.Queen:
             {
                 return new Queen(pos, side, this);
             }
         case FigureTypes.Rook:
             {
                 return new Rook(pos, side, this);
             }
     }
     return null;
 }
Example #11
0
 /// <summary>
 /// Поменять пешку на фигуру заданного типа
 /// </summary>
 /// <param name="pos"></param>
 /// <param name="figuretype"></param>
 public Figure TransformPawn(Position pos, FigureTypes figuretype)
 {
     Figure fig;
     Side side;
     side = GetFigureAt(pos).Side;
     Figure oldfig = SideToPlayer(side).alivefigures[SideToPlayer(side).alivefigures.IndexOf(GetFigureAt(pos))];
     fig = CreateFigure(figuretype, pos, side);
     SetFigureAt(pos, fig);
     SideToPlayer(side).alivefigures.Remove(oldfig);
     SideToPlayer(side).alivefigures.Add(fig);
     fig.MoveEvent += MoveFigureHandler;
     fig.KillEvent += SideToPlayer(side).KillFigureHandler;
     // надо восстановить старые обработчики для пешки FIX IT
     return fig;
 }
Example #12
0
 /// <summary>
 /// Проверяет находится ли позиция под ударом
 /// </summary>
 /// <param name="enemyside"></param>
 /// <param name="pos"></param>
 /// <param name="originalpos"></param>
 /// <returns></returns>
 public bool isDangerPosition(Side enemyside, Position pos, Position originalpos = null, Figure disabledfigure=null )
 {
     bool danger = false;
     Figure oldfig = null;
     if (originalpos != null)
     {
         oldfig = GetFigureAt(originalpos);
         SetFigureAt(originalpos, null);
     }
     Player pl = this.SideToPlayer(enemyside);
     // iterate over alive figures
     foreach (Figure fig in pl.alivefigures)
     {
         if (disabledfigure != null && disabledfigure == fig)
             continue;
         if (fig.GetFigureType() == FigureTypes.King)
         {
             if (((King)fig).GetMovesWithOutChecks().Contains(pos))
             {
                 danger = true;
                 break;
             }
         }
         else
         {
             if (fig.GetAttacks().Contains(pos))
             {
                 danger = true;
                 break;
             }
         }
     }
     if (originalpos != null)
         SetFigureAt(originalpos, oldfig);
     return danger;
 }
Example #13
0
 public bool isCorrectInMoveAttack(Position pos)
 {
     if (!isHighlighted())
         return false;
     if (inmoveattacks.Contains(pos))
         return true;
     else
         return false;
 }
Example #14
0
        public void Cell_Click(Position pos)
        {
            MyList<Position> moves, attacks,castlings, inmoveattacks;
            Figure fig = Field.GetFigureAt(pos);
            if (!isHighlighted())
            {
                if (Hightlight(pos, out moves, out attacks, out castlings, out inmoveattacks))
                {
                    foreach (Position move in moves)
                    {
                        view.CellMove(move);
                    }
                    view.CellMove(pos);
                    foreach (Position move in attacks)
                    {
                        view.CellAttack(move);
                    }
                    foreach (Position castle in castlings)
                    {
                        view.CellCastling(castle);
                    }
                    foreach (Position attack in inmoveattacks)
                    {
                        view.CellAttack(attack);
                    }
                }

            }
            else
            {
                if (isCorrectMove(pos))
                {
                    view.AddToLog(Field.GetFigureAt(highlightedfigurepos).GetImage() + " " + highlightedfigurepos.ToString() + "-" + pos.ToString());
                    Move(pos);
                    view.DrawField();
                }

                if (isCorrectCastling(pos))
                {
                    view.AddToLog(Field.GetFigureAt(highlightedfigurepos).GetImage() + " Рокировка " + pos.ToString());
                    Castle(pos);
                    view.DrawField();
                }

                if (isCorrectInMoveAttack(pos))
                {
                    view.AddToLog(Field.GetFigureAt(highlightedfigurepos).GetImage() + " " + highlightedfigurepos.ToString() + "-" + pos.ToString());
                    InMoveAttack(pos);
                    view.DrawField();
                }

                // снять выделение
                if (isHighlightedFigure(pos))
                {
                    MyList<Position> needunhighlight = Escape();
                    foreach (Position unhpos in needunhighlight)
                    {
                        view.CellDefault(unhpos);
                    }
                }

            }

            view.SetTurnText();

            view.WhiteCount(player1.GetCount());
            view.BlackCount(player2.GetCount());
        }
Example #15
0
 /// <summary>
 /// Подсветка хода
 /// </summary>
 /// <param name="pos"></param>
 /// <param name="moves"></param>
 /// <param name="attacks"></param>
 /// <returns></returns>
 public bool Hightlight(Position pos, out MyList<Position> moves, out MyList<Position> attacks,
     out MyList<Position> castlings, out MyList<Position> inmoveattacks)
 {
     moves = new MyList<Position>();
     attacks = new MyList<Position>();
     castlings = new MyList<Position>();
     inmoveattacks = new MyList<Position>();
     MyList<Position> moves0, attacks0, inmoveattacks0;
     if (state != GameState.WaitWhite && state!= GameState.WaitBlack)
         return false;
     Figure fig = Field.GetFigureAt(pos);
     if (fig != null)
     {
         if ((state == GameState.WaitBlack && fig.Side == Side.Black) ||
             (state == GameState.WaitWhite && fig.Side == Side.White))
         {
             // highlighting code
             attacks0 = GetAttacks(fig);
             moves0 = GetMoves(fig, attacks0);
             foreach (Position move in attacks0)
                 if (!Field.IsBadMove(pos, move, fig.Side))
                     attacks.Add(move);
             foreach (Position move in moves0)
                 if (!Field.IsBadMove(pos, move, fig.Side))
                     moves.Add(move);
             if (fig.GetFigureType() == FigureTypes.King)
             {
                 castlings = (fig as King).GetCastling();
             }
             if (fig.GetFigureType() == FigureTypes.Pawn)
             {
                 inmoveattacks0 = (fig as Pawn).GetInMoveAttacks();
                 foreach (Position move in inmoveattacks0)
                     if (!Field.IsBadMove(pos, move, fig.Side))
                         inmoveattacks.Add(move);
             }
             // Если король под шахом и возможных ходов нет то конец игры FIX IT
             //if (fig.GetFigureType() == FigureTypes.King & moves.Count == 0 & attacks.Count == 0)
             //    EndGame(fig);
             if (state == GameState.WaitBlack)
                 state = GameState.HighlightedBlack;
             if (state == GameState.WaitWhite)
                 state = GameState.HighlightedWhite;
             this.moves = moves;
             this.attacks = attacks;
             this.castlings = castlings;
             this.inmoveattacks = inmoveattacks;
             highlightedfigurepos = pos;
             return true;
         }
         else
             return false;
     }
     else
     {
         return false;
     }
 }
Example #16
0
 protected void SendSuperiority(NetworkStream ns, FigureTypes figtype,Position pos)
 {
     hassuperiority = false;
     WriteString(ns, comsuperiority);
     BinaryFormatter formatter = new BinaryFormatter();
     formatter.Serialize(ns, figtype);
     formatter.Serialize(ns, pos);
 }
Example #17
0
 public void ReplacePawn(Position pos)
 {
     Figure fig = Field.GetFigureAt(pos);
     FigureTypes figtype;
     if (isRemoteJob())
     {
         figtype = FigureTypes.Queen;
         blockmove = true;
     }
     else
     {
         blockmove = false;
         figtype = view.SelectFigure();
         Field.TransformPawn(pos, figtype);
         if (GameType != GameType.LocalGame)
         {
             switch (GameType)
             {
                 case GameType.ClientGame:
                     client.NewSuperiority(figtype, pos);
                     break;
                 case GameType.ServerGame:
                     server.NewSuperiority(figtype, pos);
                     break;
             }
         }
         view.DrawField();
         Field.ShahCheck(Field.GetFigureAt(pos));
     }
 }
Example #18
0
        public bool Move(Position pos)
        {
            if (!isHighlighted())
                return false;
            if (!isCorrectMove(pos))
                return false;

            if (moves.Contains(pos) || attacks.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);
                fig.SetPosition(pos);
            }
            if (!blockmove)
            {
                switch (state)
                {
                    case GameState.HighlightedBlack:
                        state = GameState.WaitWhite;
                        break;
                    case GameState.HighlightedWhite:
                        state = GameState.WaitBlack;
                        break;
                }
            }
            return true;
        }
Example #19
0
 /// <summary>
 /// Является ли фигура той которая инициировала выделение?
 /// </summary>
 /// <param name="pos"></param>
 /// <returns></returns>
 public bool isHighlightedFigure(Position pos)
 {
     if (!isHighlighted())
         return false;
     if (highlightedfigurepos == pos)
         return true;
     else
         return false;
 }
Example #20
0
 /// <summary>
 /// Эта клетка была выделена?
 /// </summary>
 /// <param name="pos"></param>
 /// <returns></returns>
 public bool isCorrectMove(Position pos)
 {
     if (!isHighlighted())
         return false;
     if (moves.Contains(pos) || attacks.Contains(pos))
         return true;
     else
         return false;
 }
Example #21
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;
     }
 }
Example #22
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;
        }
Example #23
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;
        }
Example #24
0
 public void CellMove(Position pos)
 {
     Button btn = buttons[pos.GetX0(), pos.GetY0()];
     btn.FlatAppearance.BorderSize = 2;
     btn.FlatAppearance.BorderColor = Color.Green;
 }
Example #25
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;
        }
Example #26
0
 /// <summary>
 /// Проверит приведет ли ход к шаху
 /// </summary>
 public bool IsBadMove(Position frompos, Position topos, Side side)
 {
     Figure oldfig1, oldfig2;
     bool isKing;
     bool flag;
     oldfig1 = GetFigureAt(frompos);
     oldfig2 = GetFigureAt(topos);
     // проверим не просчитываем ли мы ходы самого короля
     if (oldfig1.GetFigureType() == FigureTypes.King)
         isKing = true;
     else
         isKing = false;
     SetFigureAt(frompos, null);
     SetFigureAt(topos, oldfig1);
     if (isKing)
         // заменим короля
         oldfig1.SetPositionNoCheck(topos);
     flag = isShahedKing(side, oldfig2);
     if (isKing)
         // поставим как было
         oldfig1.SetPositionNoCheck(frompos);
     SetFigureAt(frompos, oldfig1);
     SetFigureAt(topos, oldfig2);
     return flag;
 }
Example #27
0
 public void CellCastling(Position pos)
 {
     Button btn = buttons[pos.GetX0(), pos.GetY0()];
     btn.FlatAppearance.BorderSize = 2;
     btn.FlatAppearance.BorderColor = Color.Yellow;
 }
Example #28
0
 public bool isCorrectCastling(Position pos)
 {
     if (!isHighlighted())
         return false;
     if (castlings.Contains(pos))
         return true;
     else
         return false;
 }
Example #29
0
 public void NewSuperiority(FigureTypes figtype, Position pos)
 {
     lock (lockobj)
     {
         hassuperiority = true;
         superiorityfigtype = figtype;
         superioritypos = pos;
     }
 }
Example #30
0
 public void CellDefault(Position pos)
 {
     Button btn = buttons[pos.GetX0(), pos.GetY0()];
     btn.FlatAppearance.BorderSize = 0;
     btn.FlatAppearance.BorderColor = Color.White;
 }