Beispiel #1
0
        public void CreateMapChessPosition()
        {
            Map_Chess = new ChessPiece[8, 8];
            ChessColor color;

            for (int y = 0; y < 8; y++)
            {
                if (y == 0 || y == 1)
                {
                    color = ChessColor.Black;
                }
                else
                {
                    color = ChessColor.White;
                }
                for (int x = 0; x < 8; x++)
                {
                    Map_Chess[x, y] = new PieceClass.None();
                    if (y == 2 || y == 3 || y == 4 || y == 5)
                    {
                        continue;
                    }
                    if (y == 0 || y == 7)
                    {
                        switch (x)
                        {
                        case 0:
                        case 7:
                            Map_Chess[x, y] = new PieceClass.Rook(color); break;

                        case 1:
                        case 6:
                            Map_Chess[x, y] = new PieceClass.Knight(color); break;

                        case 2:
                        case 5:
                            Map_Chess[x, y] = new PieceClass.Bishop(color); break;

                        case 3:
                            Map_Chess[x, y] = new PieceClass.Queen(color); break;

                        case 4:
                            Map_Chess[x, y] = new PieceClass.King(color); break;

                        default: break;
                        }
                        PictureBox pic = LoadPic(color, array_ChessEnum[x], new Point(x * ChessEdge, y * ChessEdge));
                        ChessBoard.Controls.Add(pic);
                        listchess.Add(pic);
                        pic.BringToFront();
                        Map_Chess[x, y].IndexControl = ChessBoard.Controls.Count - 1;
                    }
                    else if (y == 1 || y == 6)
                    {
                        Map_Chess[x, y] = new PieceClass.Pawn(color);
                        PictureBox pic = LoadPic(color, ChessEnum.Pawn, new Point(x * ChessEdge, y * ChessEdge));
                        ChessBoard.Controls.Add(pic);
                        listchess.Add(pic);
                        pic.BringToFront();
                        Map_Chess[x, y].IndexControl = ChessBoard.Controls.Count - 1;
                    }
                }
            }
        }
Beispiel #2
0
        private void Pic_MouseUp(object sender, MouseEventArgs e)
        {
            if (isMove)
            {
                isMove = false;
            }
            else
            {
                return;
            }
            int        _x = 0, _y = 0;
            Point      pos  = new Point();
            ChessColor lose = ChessColor.Die;

            if (CheckDropTrueSquare(ref _x, ref _y) && CheckDropTruePosAllow(_x, _y, ref pos))
            {
                //if eat
                foreach (PictureBox pic in listchess)
                {
                    if (pic.Location == pos)
                    {
                        pic.Visible = false;
                        pic.Dispose();
                        ChessBoard.Controls.Remove(pic);
                        listchess.Remove(pic);
                        if (Map_Chess[pos.X / 57, pos.Y / 57].ChessType != ChessEnum.King)
                        {
                            CheckPieceDie(Map_Chess[pos.X / 57, pos.Y / 57]);
                        }
                        break;
                    }
                }

                ChessPosition pos_map = GetPosMapsChess(((PictureBox)sender).Location);
                ChessPiece    map     = Map_Chess[pos_map.X, pos_map.Y];
                ((PictureBox)sender).Location = pos;
                ChessPosition pos_map_ = GetPosMapsChess(((PictureBox)sender).Location);
                //check king
                if (Map_Chess[pos_map_.X, pos_map_.Y].ChessType == ChessEnum.King)
                {
                    lose = Map_Chess[pos_map_.X, pos_map_.Y].Color;
                }
                //check pawn
                if (Map_Chess[pos_map.X, pos_map.Y].ChessType == ChessEnum.Pawn)
                {
                    if (pos_map_.Y == 0 || pos_map_.Y == 7)
                    {
                        PawnUnderPromotion nform = new PawnUnderPromotion(Map_Chess[pos_map.X, pos_map.Y].Color);
                        nform.ShowDialog();
                        switch (nform.chess)
                        {
                        case ChessEnum.Bishop: map = new PieceClass.Bishop(Map_Chess[pos_map.X, pos_map.Y].Color); break;

                        case ChessEnum.Rook: map = new PieceClass.Rook(Map_Chess[pos_map.X, pos_map.Y].Color); break;

                        case ChessEnum.Knight: map = new PieceClass.Knight(Map_Chess[pos_map.X, pos_map.Y].Color); break;

                        case ChessEnum.Queen: map = new PieceClass.Queen(Map_Chess[pos_map.X, pos_map.Y].Color); break;
                        }
                        ((PictureBox)sender).Image = Image.FromFile(FolderImage + Map_Chess[pos_map.X, pos_map.Y].Color.ToString() + "\\" + nform.chess.ToString() + ".png");
                        ((PictureBox)sender).BringToFront();
                    }
                }
                //overwrite new pos

                Map_Chess[pos_map_.X, pos_map_.Y] = map;

                //clear old pos maps

                Map_Chess[pos_map.X, pos_map.Y] = new PieceClass.None();

                reportmove(pos_map.X, pos_map.Y, pos_map_.X, pos_map_.Y, map);

                //change round
                round = round == ChessColor.Black ? ChessColor.White : ChessColor.Black;
            }
            //clean
            ((PictureBox)sender).Visible = true;
            ChessBoard.Controls.Remove(temp_pic);
            temp_pic.Dispose();
            temp_pic = null;
            foreach (PictureBox pic in listchess)
            {
                if (pic.BackColor == canmove_color)
                {
                    pic.BackColor = DefaultColor;
                }
            }
            foreach (PictureBox pic in listBackground)
            {
                if (pic.BackColor == canmove_color)
                {
                    pic.BackColor = ((pic.Location.X / 57) + (pic.Location.Y / 57)) % 2 == 0 ? Color.Gray : Color.DarkGray;
                }
            }
            if (lose != ChessColor.Die)
            {
                MessageBox.Show(lose.ToString() + " lose.");
                MakeNewRound();
                Application.Exit();
                return;
            }
            playerturn();
            GC.Collect();
        }