All pastes #503847 Raw Edit

Queens C# Source

public text v1 · immutable
·published 2007-05-23 01:51 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Queens
{
    public partial class Queens : Form
    {
        public Button[,] buttons = new Button[8, 8];//this is the multidimensional
        //array of buttons that the user can click on (either white/black)
        public Point lastMove;//this is the location of the last move the user makes.
        public int queenCount = 0;
        public int openSquares = 64;
        ////End Globals////
        /*The two Bitmap variables below are the images of the knights. Knight.png is
         * the image that appears in a regular white button thats clicked. Knightblack
         * is the image that appears in a black square. This was necessery because 
         * I had problems getting the software to accept the transparent backround. */

        private Bitmap queenWhite = new Bitmap(typeof(Queens), "QueenWhite.png");
        private Bitmap queenBlack = new Bitmap(typeof(Queens), "QueenBlack.png");
        private Bitmap dotWhite = new Bitmap(typeof(Queens), "DotWhite.png");
        private Bitmap dotBlack = new Bitmap(typeof(Queens), "DotBlack.png");
        bool firstMove = true;//this bool value stays true until the user clicks button
        double counter = 0.0;//value that stores the percentage the user has completed
        public Queens()    
        {
            InitializeComponent();//loads the form
            for (int i = 0; i < 8; i++)
                /*This for loop creates each of the buttons in the grid and then fills
                 * in their properties. Including name, width, height, location and
                 * their backround colour */ 
                for (int j = 0; j < 8; j++)
                {
                    // create each of the buttons
                    buttons[i, j] = new Button();
                    // initialize each button here
                    buttons[i, j].Name = "b" + (i * 100 + j).ToString();//button name
                    buttons[i, j].Width = 50;//sets button width
                    buttons[i, j].Height = 50;//sets button height
                    buttons[i, j].Location = new Point(i * 50, j * 50);//sets location
                    // adds the click event handler
                    buttons[i, j].Click += new EventHandler(ButtonClick);//if button is
                    //clicked then go to the function ButtonClick().
                    //the if/else statement below creates a chessboard pattern by
                    //painting alternate squares white and black.
                    if ((i + j) % 2 == 0)//if i + j equals an even number
                        buttons[i, j].BackColor = Color.Black;//sets backround colour
                    else//if i + j equals an odd number
                        buttons[i, j].BackColor = Color.White;//sets backround colour
                        // add the button controls to the form
                    this.Controls.Add(buttons[i, j]);//adds the button to the form
                }
        }

        private void ButtonClick(object sender, EventArgs e)
        {
            Button b = (Button)sender;//determines which button was clicked 
            int x = Convert.ToInt32(b.Name.TrimStart('b')) / 100; //gets the position (horizontal)
            int y = Convert.ToInt32(b.Name.TrimStart('b')) % 100;// gets the position (verticle)
            // add the button controls to the form
            for (int i = 0; i < 8; i++)
            {
                //////PRINTS//VERTICAL//LINES//////
                if ((x + i) % 2 == 0)
                    buttons[x, i].Image = dotBlack;
                else
                    buttons[x, i].Image = dotWhite;
                //////PRINTS//HORIZONTAL//LINES/////
                if ((i + y) % 2 == 0)
                    buttons[i, y].Image = dotBlack;
                else
                    buttons[i, y].Image = dotWhite;
                //////PRINTS//DIAGONAL//LINES//////
                if (x + i < 8 && y + i < 8)
                {
                    if (buttons[x + i, y + i].BackColor == Color.Black)
                        buttons[x + i, y + i].Image = dotBlack;
                    else
                        buttons[x + i, y + i].Image = dotWhite;
                }
                for (int j = 8; j >= 0; j--)
                {
                    if (x - i >= 0 && y - i >= 0)
                    {
                        if (buttons[x - i, y - i].BackColor == Color.Black)
                            buttons[x - i, y - i].Image = dotBlack;
                        else
                            buttons[x - i, y - i].Image = dotWhite;
                    }

                    if (x + i >= 0 && y - i >= 0 && x + i < 8 && y - i < 8)
                    {
                        if (buttons[x + i, y - i].BackColor == Color.Black)
                            buttons[x + i, y - i].Image = dotBlack;
                        else
                            buttons[x + i, y - i].Image = dotWhite;
                    }

                    if (x - i >= 0 && y + i >= 0 && x - i < 8 && y + i < 8)
                    {
                        if (buttons[x - i, y + i].BackColor == Color.Black)
                            buttons[x - i, y + i].Image = dotBlack;
                        else
                            buttons[x - i, y + i].Image = dotWhite;
                    }
                }
            }
            /////////SETS QUEEN IMAGE//////////
            if ((x + y) % 2 == 0)//if x + y equals an even number
                b.Image = queenBlack;
            else//if x + y equals an odd number
                b.Image = queenWhite;
            queenCount++;
            openSquares = 0;
            for (int h = 0; h < 8; h++)
            {
                for (int j = 0; j < 8; j++)
                {
                    if (buttons[h, j].Image == null)
                        openSquares++;
                }
                labelCount.Text = "Queens: " + queenCount + " || Open Squares: " + openSquares;
                Invalidate();
            }
            if (openSquares == 0)
            {
                MessageBox.Show("Good Job You Completed The Game Using Only: " + queenCount.ToString() + " Queens!", "Game Over...");
                NewGame();
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            aboutForm about = new aboutForm();
            about.ShowDialog();
        }

        private void instructionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            instructForm instructions = new instructForm();
            instructions.ShowDialog();
        }

        private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewGame();
            MessageBox.Show("New Game Started", "Notice...");
        }

        private void NewGame()
        {
            Form form = new Form();//creates a new form
            this.Hide();//hides the current form
            form = new Queens();//sets new form equal to current
            form.Show();//shows the new form
        }
    }
}