Beispiel #1
0
        public void resetBoard(Player up, Player down)
        {
            // initialize black pieces
            boxes[0, 0] = new Spot(0, 0, new Rook(up));
            boxes[0, 1] = new Spot(0, 1, new Knight(up));
            boxes[0, 2] = new Spot(0, 2, new Bishop(up));
            boxes[0, 3] = new Spot(0, 3, new Queen(up));
            boxes[0, 4] = new Spot(0, 4, new King(up));
            boxes[0, 5] = new Spot(0, 5, new Bishop(up));
            boxes[0, 6] = new Spot(0, 6, new Knight(up));
            boxes[0, 7] = new Spot(0, 7, new Rook(up));

            //...
            boxes[1, 0] = new Spot(1, 0, new Pawn(up));
            boxes[1, 1] = new Spot(1, 1, new Pawn(up));
            boxes[1, 2] = new Spot(1, 2, new Pawn(up));
            boxes[1, 3] = new Spot(1, 3, new Pawn(up));
            boxes[1, 4] = new Spot(1, 4, new Pawn(up));
            boxes[1, 5] = new Spot(1, 5, new Pawn(up));
            boxes[1, 6] = new Spot(1, 6, new Pawn(up));
            boxes[1, 7] = new Spot(1, 7, new Pawn(up));
            //...

            // initialize white pieces
            boxes[7, 0] = new Spot(7, 0, new Rook(down));
            boxes[7, 1] = new Spot(7, 1, new Knight(down));
            boxes[7, 2] = new Spot(7, 2, new Bishop(down));
            boxes[7, 3] = new Spot(7, 3, new Queen(down));
            boxes[7, 4] = new Spot(7, 4, new King(down));
            boxes[7, 5] = new Spot(7, 5, new Bishop(down));
            boxes[7, 6] = new Spot(7, 6, new Knight(down));
            boxes[7, 7] = new Spot(7, 7, new Rook(down));
            //...
            boxes[6, 0] = new Spot(6, 0, new Pawn(down));
            boxes[6, 1] = new Spot(6, 1, new Pawn(down));
            boxes[6, 2] = new Spot(6, 2, new Pawn(down));
            boxes[6, 3] = new Spot(6, 3, new Pawn(down));
            boxes[6, 4] = new Spot(6, 4, new Pawn(down));
            boxes[6, 5] = new Spot(6, 5, new Pawn(down));
            boxes[6, 6] = new Spot(6, 6, new Pawn(down));
            boxes[6, 7] = new Spot(6, 7, new Pawn(down));
            //...
            for (int i = 2; i < 6; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    boxes[i, j] = new Spot(i, j, null);
                    DrawTool.DrawSpotColor(getBox(i, j));
                }
            }
            foreach (Spot spot in boxes)
            {
                if (spot == null || spot.Piece == null)
                {
                }
                else
                {
                    DrawTool.DrawSpotColor(spot);
                    DrawTool.DrawPieceInit(spot.X, spot.Y, spot.Piece.Name, spot.Piece.Player.IsWhite);
                    Game.piecesAlive[spot.Piece] = spot;
                    //Form1.form1.game.movPoss.Add(spot.Piece,new List<Spot>());
                }
            }
            // initialize remaining boxes without any piece
        }
Beispiel #2
0
        public static void DrawPiece(Spot startPoint, Spot endPoint, string pieceName, bool isWhite)
        {
            int endx = endPoint.X;
            int endy = endPoint.Y;
            int sx   = startPoint.X;
            int sy   = startPoint.Y;

            Image     image     = null;
            Graphics  gr        = Graphics.FromImage(Form1.bm);
            Rectangle rectEnd   = new Rectangle(endy * Form1.hgt / 8, endx * Form1.wid / 8, Form1.wid / 8, Form1.hgt / 8);
            Rectangle rectStart = new Rectangle(sy * Form1.hgt / 8, sx * Form1.wid / 8, Form1.wid / 8, Form1.hgt / 8);

            SolidBrush startBrush = new SolidBrush(startPoint.SpotColor);
            SolidBrush endBrush   = new SolidBrush(endPoint.SpotColor);

            if (pieceName == "P" && isWhite == true)
            {
                image = Image.FromFile("whiteP.png");
            }
            if (pieceName == "P" && isWhite == false)
            {
                image = Image.FromFile("blackP.png");
            }
            if (pieceName == "R" && isWhite == true)
            {
                image = Image.FromFile("whiteR.png");
            }
            if (pieceName == "R" && isWhite == false)
            {
                image = Image.FromFile("blackR.png");
            }
            if (pieceName == "Kn" && isWhite == true)
            {
                image = Image.FromFile("whiteKn.png");
            }
            if (pieceName == "Kn" && isWhite == false)
            {
                image = Image.FromFile("blackKn.png");
            }
            if (pieceName == "B" && isWhite == true)
            {
                image = Image.FromFile("whiteB.png");
            }
            if (pieceName == "B" && isWhite == false)
            {
                image = Image.FromFile("blackB.png");
            }
            if (pieceName == "K" && isWhite == true)
            {
                image = Image.FromFile("whiteK.png");
            }
            if (pieceName == "K" && isWhite == false)
            {
                image = Image.FromFile("blackK.png");
            }
            if (pieceName == "Q" && isWhite == true)
            {
                image = Image.FromFile("whiteQ.png");
            }
            if (pieceName == "Q" && isWhite == false)
            {
                image = Image.FromFile("blackQ.png");
            }
            //KILL PIECE
            if (endPoint.Piece != null)
            {
                gr.FillRectangle(endBrush, rectEnd);
            }

            gr.DrawImage(image, rectEnd);
            gr.FillRectangle(startBrush, rectStart);

            /* Form1.form1.boardPicture.Invalidate(rectStart);
             * Form1.form1.boardPicture.Invalidate(rectEnd);*/
            Form1.form1.boardPicture.Invalidate();
            gr.Dispose();
            startBrush.Dispose();
            endBrush.Dispose();
        }
Beispiel #3
0
 public void replaceBox(int x, int y, Piece piece)
 {
     boxes[x, y] = new Spot(x, y, piece);
 }
Beispiel #4
0
        private void BoardPicture_MouseClick(object sender, MouseEventArgs e)
        {
            Point p = new Point(MousePosition.X, MousePosition.Y);
            Point q = boardPicture.PointToClient(p);

            // Rien de sélectionner
            if (mouseClick == false)
            {
                game.getSpotList();
                startSpot = game.Brd.getBox(q.Y / (hgt / 8), q.X / (wid / 8));

                if (startSpot.Piece == null || startSpot.Piece.Player != game.currentTurn)
                {
                    return;
                }
                // DRAW CLICKED OUTLINE
                Graphics  gr        = Graphics.FromImage(Form1.bm);
                Rectangle rectStart = new Rectangle(startSpot.Y * Form1.hgt / 8, startSpot.X * Form1.wid / 8, Form1.wid / 8, Form1.hgt / 8);
                //gr.DrawRectangle(startPen, rectStart);
                gr.FillRectangle(startBrush, rectStart);
                DrawTool.DrawPieceInit(startSpot.X, startSpot.Y, startSpot.Piece.Name, game.currentTurn.IsWhite);


                //boardPicture.Invalidate(rectStart);
                boardPicture.Invalidate(rectStart);
                mouseClick = true;
                gr.Dispose();

                if (game.movPoss[startSpot.Piece].Count == 0)
                {
                    System.Diagnostics.Debug.WriteLine(startSpot.Piece.Name + " cannot move!");
                }
                foreach (Spot spot in game.movPoss[startSpot.Piece])
                {
                    System.Diagnostics.Debug.WriteLine(startSpot.Piece.Name + " can move to: x: " + spot.X.ToString() + ", y: " + spot.Y.ToString() + " Count: " + game.movPoss[startSpot.Piece].Count.ToString());
                }
            }
            else if (mouseClick == true)
            {
                game.getSpotList();

                //REMOVE START OUTLINE

                /*Graphics gr = Graphics.FromImage(Form1.bm);
                 * gr.DrawRectangle(startPen, new Rectangle(startSpot.Y * Form1.hgt / 8, startSpot.X * Form1.wid / 8, Form1.wid / 8, Form1.hgt / 8));
                 * boardPicture.Invalidate(new Rectangle(startSpot.Y * Form1.hgt / 8, startSpot.X * Form1.wid / 8, Form1.wid / 8, Form1.hgt / 8));*/
                endSpot = game.Brd.getBox(q.Y / (hgt / 8), q.X / (wid / 8));

                if (endSpot.Piece != null && endSpot.Piece.Player == game.currentTurn)
                {
                    if (game.movPoss[endSpot.Piece].Count == 0)
                    {
                        System.Diagnostics.Debug.WriteLine(endSpot.Piece.Name + " cannot move!");
                    }
                    foreach (Spot spot in game.movPoss[endSpot.Piece])
                    {
                        System.Diagnostics.Debug.WriteLine(endSpot.Piece.Name + " can move to: x: " + spot.X.ToString() + ", y: " + spot.Y.ToString() + " Count: " + game.movPoss[endSpot.Piece].Count.ToString());
                    }
                    Graphics   gr         = Graphics.FromImage(Form1.bm);
                    Pen        blankPen   = new Pen(startSpot.SpotColor, 1);
                    SolidBrush blankBrush = new SolidBrush(startSpot.SpotColor);
                    //gr.DrawRectangle(blankPen, new Rectangle(startSpot.Y * Form1.hgt / 8, startSpot.X * Form1.wid / 8, Form1.wid / 8-1, Form1.hgt / 8-1));
                    //boardPicture.Invalidate(new Rectangle(startSpot.Y * Form1.hgt / 8, startSpot.X * Form1.wid / 8, Form1.wid / 8, Form1.hgt / 8));
                    gr.FillRectangle(blankBrush, new Rectangle(startSpot.Y * Form1.hgt / 8, startSpot.X * Form1.wid / 8, Form1.wid / 8, Form1.hgt / 8));
                    DrawTool.DrawPieceInit(startSpot.X, startSpot.Y, startSpot.Piece.Name, game.currentTurn.IsWhite);
                    //boardPicture.Invalidate(new Rectangle(startSpot.Y * Form1.hgt / 8, startSpot.X * Form1.wid / 8, Form1.wid / 8, Form1.hgt / 8));
                    startSpot = endSpot;
                    gr.FillRectangle(startBrush, new Rectangle(startSpot.Y * Form1.hgt / 8, startSpot.X * Form1.wid / 8, Form1.wid / 8, Form1.hgt / 8));
                    DrawTool.DrawPieceInit(startSpot.X, startSpot.Y, startSpot.Piece.Name, game.currentTurn.IsWhite);
                    //gr.DrawRectangle(startPen, new Rectangle(startSpot.Y * Form1.hgt / 8, startSpot.X * Form1.wid / 8, Form1.wid / 8-1, Form1.hgt / 8-1));
                    //boardPicture.Invalidate(new Rectangle(startSpot.Y * Form1.hgt / 8, startSpot.X * Form1.wid / 8, Form1.wid / 8, Form1.hgt / 8));
                    //boardPicture.Invalidate(new Rectangle(startSpot.Y * Form1.hgt / 8, startSpot.X * Form1.wid / 8, Form1.wid / 8, Form1.hgt / 8));
                    boardPicture.Invalidate();
                    blankBrush.Dispose();
                    gr.Dispose();
                    return;
                }

                //Graphics g = Graphics.FromImage(Form1.bm);
                //g.FillRectangle(endBrush, new Rectangle(endSpot.Y * Form1.hgt / 8, endSpot.X * Form1.wid / 8, Form1.wid / 8, Form1.hgt / 8));
                if (game.makeMove(startSpot, endSpot) == false)
                {
                    //g.FillRectangle(blankBrush, new Rectangle(endSpot.Y * Form1.hgt / 8, endSpot.X * Form1.wid / 8, Form1.wid / 8, Form1.hgt / 8));
                    return;
                }
                else if (game.moveList.Count > 0)
                {
                    SolidBrush blankBrush    = new SolidBrush(endSpot.SpotColor);
                    Graphics   g             = Graphics.FromImage(Form1.bm);
                    Spot       lastSpotStart = game.moveList.Last().StartSpot;
                    Spot       lastSpotEnd   = game.moveList.Last().EndSpot;
                    Piece      piece         = game.moveList.Last().MovedPiece;

                    g.FillRectangle(endBrush, new Rectangle(lastSpotEnd.Y * Form1.hgt / 8, lastSpotEnd.X * Form1.wid / 8, Form1.wid / 8, Form1.hgt / 8));
                    DrawTool.DrawPieceInit(lastSpotEnd.X, lastSpotEnd.Y, piece.Name, piece.Player.IsWhite);
                    //boardPicture.Invalidate(new Rectangle(lastSpotEnd.Y * Form1.hgt / 8, lastSpotEnd.X * Form1.wid / 8, Form1.wid / 8, Form1.hgt / 8));
                    g.FillRectangle(startBrush, new Rectangle(lastSpotStart.Y * Form1.hgt / 8, lastSpotStart.X * Form1.wid / 8, Form1.wid / 8, Form1.hgt / 8));
                    //boardPicture.Invalidate(new Rectangle(lastSpotStart.Y * Form1.hgt / 8, lastSpotStart.X * Form1.wid / 8, Form1.wid / 8, Form1.hgt / 8));
                    boardPicture.Invalidate();
                    if (turn.IsWhite != game.currentTurn.IsWhite)
                    {
                        if (game.moveList.Count - 2 >= 0)
                        {
                            Graphics gm            = Graphics.FromImage(Form1.bm);
                            int      lastlastIndex = game.moveList.Count - 2;
                            Move     lastlastMove  = game.moveList[lastlastIndex];

                            Spot       lastlastEndSpot     = lastlastMove.EndSpot;
                            Spot       lastlastStartSpot   = lastlastMove.StartSpot;
                            Piece      pieceLastlast       = lastlastMove.MovedPiece;
                            Piece      pieceLastlastKilled = lastlastMove.KilledPiece;
                            SolidBrush lastEndBrush        = new SolidBrush(lastlastEndSpot.SpotColor);
                            SolidBrush lastStartBrush      = new SolidBrush(lastlastStartSpot.SpotColor);
                            // COLOR BLANK MOVED PIECE OPPOSITE ADVERSE
                            if (endSpot.Piece == null)
                            {
                                //System.Diagnostics.Debug.WriteLine("endSpot Piece == null"+" last last move piece moved: "+pieceLastlast.Name);
                                gm.FillRectangle(lastEndBrush, new Rectangle(lastlastEndSpot.Y * Form1.hgt / 8, lastlastEndSpot.X * Form1.wid / 8, Form1.wid / 8, Form1.hgt / 8));
                                DrawTool.DrawPieceInit(lastlastEndSpot.X, lastlastEndSpot.Y, pieceLastlast.Name, pieceLastlast.Player.IsWhite);
                                //boardPicture.Invalidate(new Rectangle(lastlastEndSpot.Y * Form1.hgt / 8, lastlastEndSpot.X * Form1.wid / 8, Form1.wid / 8, Form1.hgt / 8));
                                boardPicture.Invalidate();
                            }
                            else
                            {
                                //System.Diagnostics.Debug.WriteLine("endSpot Piece NOT null");
                                gm.FillRectangle(endBrush, new Rectangle(lastSpotEnd.Y * Form1.hgt / 8, lastSpotEnd.X * Form1.wid / 8, Form1.wid / 8, Form1.hgt / 8));
                                DrawTool.DrawPieceInit(lastSpotEnd.X, lastSpotEnd.Y, piece.Name, piece.Player.IsWhite);
                                ////boardPicture.Invalidate(new Rectangle(lastSpotEnd.Y * Form1.hgt / 8, lastSpotEnd.X * Form1.wid / 8, Form1.wid / 8, Form1.hgt / 8));
                                boardPicture.Invalidate();
                            }

                            //boardPicture.Invalidate();



                            //DrawTool.DrawPiece(lastlastStartSpot, lastlastEndSpot, pieceLastlast.Name, pieceLastlast.Player);



                            // COLOR BLANK START PIECE OPPOSITE ADVERSE
                            if (endSpot.X == lastlastStartSpot.X && endSpot.Y == lastlastStartSpot.Y)
                            {
                            }
                            else
                            {
                                g.FillRectangle(lastStartBrush, new Rectangle(lastlastStartSpot.Y * Form1.hgt / 8, lastlastStartSpot.X * Form1.wid / 8, Form1.wid / 8, Form1.hgt / 8));
                            }

                            //boardPicture.Invalidate(new Rectangle(lastlastStartSpot.Y * Form1.hgt / 8, lastlastStartSpot.X * Form1.wid / 8, Form1.wid / 8, Form1.hgt / 8));
                            boardPicture.Invalidate();
                            g.Dispose();
                            gm.Dispose();
                            lastEndBrush.Dispose();
                        }
                        turn = game.currentTurn;
                    }
                    else
                    {
                        g.Dispose();
                        boardPicture.Invalidate();
                    }
                }



                boardPicture.Invalidate();
                mouseClick = false;
                // game.makeMove(startSpot, endSpot);
            }
        }