示例#1
0
 private void Clear_Click(object sender, EventArgs e)
 {
     for (int i = 0; i < 64; i++)
     {
         space drawSpace = ((space)coordinates[i]);
         drawSpace.setOccupied(false);
         totalQueens = 0;
     }
     Invalidate();
 }
示例#2
0
 private void Form1_MouseClick(object sender, MouseEventArgs e)
 {
     if (e.Button == MouseButtons.Left)
     {
         for (int i = 0; i < 64; i++)
         {
             space drawSpace = ((space)coordinates[i]);
             if (drawSpace.isClicked(e.X, e.Y))
             {
                 if (drawSpace.isValid(coordinates))
                 {
                     drawSpace.setOccupied(true);
                     totalQueens++;
                 }
                 else
                 {
                     System.Media.SystemSounds.Beep.Play();
                 }
             }
         }
     }
     if (e.Button == MouseButtons.Right)
     {
         for (int i = 0; i < 64; i++)
         {
             space drawSpace = ((space)coordinates[i]);
             if (drawSpace.isClicked(e.X, e.Y))
             {
                 if (drawSpace.isOccupied())
                 {
                     drawSpace.setOccupied(false);
                     totalQueens--;
                 }
             }
         }
     }
     Invalidate();
 }
示例#3
0
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;

            g.DrawString("Queen Count: " + totalQueens, Font, Brushes.Black, 140, 17);
            for (int i = 0; i < 64; i++)
            {
                space     drawSpace = ((space)coordinates[i]);
                Rectangle rect      = drawSpace.getRect();
                int       X         = drawSpace.getX();
                int       Y         = drawSpace.getY();
                if ((i / 8) % 2 == 0)
                {
                    if (i % 2 == 0)
                    {
                        if (!hints)
                        {
                            g.FillRectangle(Brushes.Black, rect);
                            g.DrawRectangle(Pens.Black, rect);
                            if (drawSpace.isOccupied())
                            {
                                Font useThis = new Font("Arial", 32);
                                g.DrawString("Q", useThis, Brushes.White, X, Y);
                            }
                        }
                        else
                        {
                            if (!drawSpace.isValid(coordinates))
                            {
                                g.FillRectangle(Brushes.Red, rect);
                                g.DrawRectangle(Pens.Black, rect);
                                if (drawSpace.isOccupied())
                                {
                                    Font useThis = new Font("Arial", 32);
                                    g.DrawString("Q", useThis, Brushes.White, X, Y);
                                }
                            }
                            else
                            {
                                g.FillRectangle(Brushes.Black, rect);
                                g.DrawRectangle(Pens.Black, rect);
                                if (drawSpace.isOccupied())
                                {
                                    Font useThis = new Font("Arial", 32);
                                    g.DrawString("Q", useThis, Brushes.White, X, Y);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (!hints)
                        {
                            g.FillRectangle(Brushes.White, rect);
                            g.DrawRectangle(Pens.Black, rect);
                            if (drawSpace.isOccupied())
                            {
                                Font useThis = new Font("Arial", 32);
                                g.DrawString("Q", useThis, Brushes.Black, X, Y);
                            }
                        }
                        else
                        {
                            if (!drawSpace.isValid(coordinates))
                            {
                                g.FillRectangle(Brushes.Red, rect);
                                g.DrawRectangle(Pens.Black, rect);
                                if (drawSpace.isOccupied())
                                {
                                    Font useThis = new Font("Arial", 32);
                                    g.DrawString("Q", useThis, Brushes.White, X, Y);
                                }
                            }
                            else
                            {
                                g.FillRectangle(Brushes.White, rect);
                                g.DrawRectangle(Pens.Black, rect);
                                if (drawSpace.isOccupied())
                                {
                                    Font useThis = new Font("Arial", 32);
                                    g.DrawString("Q", useThis, Brushes.Black, X, Y);
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (i % 2 == 1)
                    {
                        if (!hints)
                        {
                            g.FillRectangle(Brushes.Black, rect);
                            g.DrawRectangle(Pens.Black, rect);
                            if (drawSpace.isOccupied())
                            {
                                Font useThis = new Font("Arial", 32);
                                g.DrawString("Q", useThis, Brushes.White, X, Y);
                            }
                        }
                        else
                        {
                            if (!drawSpace.isValid(coordinates))
                            {
                                g.FillRectangle(Brushes.Red, rect);
                                g.DrawRectangle(Pens.Black, rect);
                                if (drawSpace.isOccupied())
                                {
                                    Font useThis = new Font("Arial", 32);
                                    g.DrawString("Q", useThis, Brushes.White, X, Y);
                                }
                            }
                            else
                            {
                                g.FillRectangle(Brushes.Black, rect);
                                g.DrawRectangle(Pens.Black, rect);
                                if (drawSpace.isOccupied())
                                {
                                    Font useThis = new Font("Arial", 32);
                                    g.DrawString("Q", useThis, Brushes.White, X, Y);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (!hints)
                        {
                            g.FillRectangle(Brushes.White, rect);
                            g.DrawRectangle(Pens.Black, rect);
                            if (drawSpace.isOccupied())
                            {
                                Font useThis = new Font("Arial", 32);
                                g.DrawString("Q", useThis, Brushes.Black, X, Y);
                            }
                        }
                        else
                        {
                            if (!drawSpace.isValid(coordinates))
                            {
                                g.FillRectangle(Brushes.Red, rect);
                                g.DrawRectangle(Pens.Black, rect);
                                if (drawSpace.isOccupied())
                                {
                                    Font useThis = new Font("Arial", 32);
                                    g.DrawString("Q", useThis, Brushes.White, X, Y);
                                }
                            }
                            else
                            {
                                g.FillRectangle(Brushes.White, rect);
                                g.DrawRectangle(Pens.Black, rect);
                                if (drawSpace.isOccupied())
                                {
                                    Font useThis = new Font("Arial", 32);
                                    g.DrawString("Q", useThis, Brushes.Black, X, Y);
                                }
                            }
                        }
                    }
                }
            }
            if (totalQueens == 8)
            {
                for (int i = 0; i < 64; i++)
                {
                    space drawSpace = ((space)coordinates[i]);
                    drawSpace.setOccupied(false);
                    totalQueens = 0;
                }
                MessageBox.Show("You win!");
            }
        }