Esempio n. 1
0
 private GraphicCell[,] copyGraphicGrid() //NEWLY ADDED
 {
     GraphicCell[,] gc = new GraphicCell[8, 8];
     for (int i = 0; i < 8; i++)
     {
         for (int j = 0; j < 8; j++)
         {
             gc[i, j]       = new GraphicCell(i, j, this);
             gc[i, j].Image = grid[i, j].Image;
         }
     }
     return(gc);
 }
Esempio n. 2
0
        private void InitGrid()
        {
            var images = new Dictionary <Color, Dictionary <Type, Bitmap> >();

            #region filling up images
            var whites = new Dictionary <Type, Bitmap>();
            var blacks = new Dictionary <Type, Bitmap>();
            images[Color.White] = whites;
            images[Color.Black] = blacks;

            whites[Type.Bishop] = Properties.Resources.BishopW;
            whites[Type.Pawn]   = Properties.Resources.PawnW;
            whites[Type.Knight] = Properties.Resources.KnightW;
            whites[Type.Queen]  = Properties.Resources.QueenW;
            whites[Type.King]   = Properties.Resources.KingW;
            whites[Type.Rook]   = Properties.Resources.RookW;

            blacks[Type.Bishop] = Properties.Resources.BishopB;
            blacks[Type.Pawn]   = Properties.Resources.PawnB;
            blacks[Type.Knight] = Properties.Resources.KnightB;
            blacks[Type.Queen]  = Properties.Resources.QueenB;
            blacks[Type.King]   = Properties.Resources.KingB;
            blacks[Type.Rook]   = Properties.Resources.RookB;
            #endregion

            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    grid[i, j] = new GraphicCell(i, j, this);

                    Piece piece = logic.getCellContent(i, j);
                    if (piece != null)
                    {
                        grid[i, j].Image = images[piece.GetColor()][piece.GetType()];
                    }
                }
            }
        }
Esempio n. 3
0
        private void graphicallyMovePiece(GraphicCell source, GraphicCell target)
        {
            int i, j;

            Image temp = source.Image;

            source.Image = null;
            target.Image = temp;

            if (logic.grid[source.I, source.J].piece != null)
            {
                i = source.I;
                j = source.J;
            }

            else
            {
                i = target.I;
                j = target.J;
            }
            if (logic.getCellContent(i, j).GetType() == Type.King && Math.Abs(target.J - source.J) == 2)
            {
                if (target.J < 4)
                {
                    temp = grid[target.I, 0].Image;
                    grid[target.I, 0].Image = null;

                    grid[target.I, 3].Image = temp;
                }

                if (target.J > 4)
                {
                    temp = grid[target.I, 7].Image;
                    grid[target.I, 7].Image = null;

                    grid[target.I, 5].Image = temp;
                }
            }


            //En Passant
            if (logic.getCellContent(i, j).GetType() == Type.Pawn)
            {
                if (logic.getCellContent(i, j).GetColor() == Color.White && logic.WhitePerformingEnPassant(target.I, target.J, source.J))
                {
                    grid[target.I + 1, target.J].Image = null;
                }
                else if (logic.getCellContent(i, j).GetColor() == Color.Black && logic.BlackPerformingEnPassant(target.I, target.J, source.J))
                {
                    grid[target.I - 1, target.J].Image = null;
                }
            }

            //Promotion needed?
            if (logic.getCellContent(i, j).GetType() == Type.Pawn && (target.I == 0 || target.I == 7))
            {
                string promotion = Promote(target.I);
                switch (promotion)
                {
                case "Knight":
                    logic.Promotion(source.I, source.J, Type.Knight);

                    if (target.I == 0)
                    {
                        grid[target.I, target.J].Image = Properties.Resources.KnightW;
                    }

                    else
                    {
                        grid[target.I, target.J].Image = Properties.Resources.KnightB;
                    }
                    break;

                case "Rook":
                    logic.Promotion(source.I, source.J, Type.Rook);

                    if (target.I == 0)
                    {
                        grid[target.I, target.J].Image = Properties.Resources.RookW;
                    }

                    else
                    {
                        grid[target.I, target.J].Image = Properties.Resources.RookB;
                    }
                    break;

                case "Bishop":
                    logic.Promotion(source.I, source.J, Type.Bishop);

                    if (target.I == 0)
                    {
                        grid[target.I, target.J].Image = Properties.Resources.BishopW;
                    }

                    else
                    {
                        grid[target.I, target.J].Image = Properties.Resources.BishopB;
                    }
                    break;

                case "Queen":
                    logic.Promotion(source.I, source.J, Type.Queen);

                    if (target.I == 0)
                    {
                        grid[target.I, target.J].Image = Properties.Resources.QueenW;
                    }

                    else
                    {
                        grid[target.I, target.J].Image = Properties.Resources.QueenB;
                    }
                    break;
                }
            }
        }
Esempio n. 4
0
        public void CellClicked(int i, int j) // todo there's a bug - after choosing a target once, then choosing another target, it won't show valid moves
        {
            if (lastChosen == null)           // choosing source
            {
                ValidMoves = logic.GetValidMoves(i, j);
                // would return null if no piece
                if (ValidMoves != null)
                {
                    foreach (Cell cell in ValidMoves)
                    {
                        if (cell.piece == null && logic.grid[i, j].piece.GetType() == Type.King && Math.Abs(j - cell.J) == 2)
                        {
                            grid[cell.I, cell.J].BackColor = System.Drawing.Color.Aqua;
                        }

                        else if (cell.piece == null)
                        {
                            grid[cell.I, cell.J].BackColor = System.Drawing.Color.SandyBrown;
                        }

                        else
                        {
                            grid[cell.I, cell.J].BackColor = System.Drawing.Color.Red;
                        }
                    }

                    lastChosen = grid[i, j];
                }
            }

            else // choosing target
            {
                foreach (Cell cell in ValidMoves)
                {
                    grid[cell.I, cell.J].BackColor = grid[cell.I, cell.J].OriginalColor;
                }

                if (!ValidMoves.Contains(this.logic.GetCell(i, j)))
                {
                    if (logic.getCellContent(i, j) != null && logic.getCellContent(i, j).GetColor() == logic.GetTurn())
                    {
                        lastChosen = null;
                        CellClicked(i, j);
                        return;
                    }

                    lastChosen = null;
                    return;
                }

                showEaten(i, j);

                //Save in undo stacks.
                undoStackLogic.Push(logic.copyLogicGrid());
                undoStackGraphic.Push(copyGraphicGrid());

                graphicallyMovePiece(lastChosen, grid[i, j]);         //BREAKPOINT

                if (logic.MakeMove(i, j, lastChosen.I, lastChosen.J)) // means if checkmate
                {
                    this.InitGrid();                                  // todo if checkmate, leave the board!
                    this.InitEaten();
                    EndGame();
                }

                lastChosen = null;

                // todo
                // if move was successfully done, and not checkmate... blah blah if's
                if (ai != null)
                {
                    letAIplay();
                }
            }
        }