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
}
Invalidate();
}
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)
if (buttons[x, y].Image != null)
{
MessageBox.Show("This Spot Is Already Taken...", "Error:");
return;
}
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//////
/*The following if statements basically just draw all your
* diagonal lines. The reason I used several if statements is
* because the diagonal line has to be drawn in 4 different
* directions. */
if (x + i < 8 && y + i < 8)
{ //this if statement draws diagonal lines up and to the right
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)
{ //this if statement draws diagonal lines down and left
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)
{ //this one draws them up and to the left.
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)
{ //this one draws them down and to the right
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;//black image of queen is used (blacksquare)
else//if x + y equals an odd number
b.Image = queenWhite;//white image of queen is used (whitesquare)
queenCount++;//increases number of queens that have been played
openSquares = 0;//resets the number of open squares to 0.
for (int h = 0; h < 8; h++)
{ //this simple for loop counts up how many availible squares the
//usr has. If there are no images it counts it as an open square
//and then increments the interger value.
for (int j = 0; j < 8; j++)
{
if (buttons[h, j].Image == null)//if no image
openSquares++;//increments count
}//end for
labelCount.Text = "Queens: " + queenCount + " || Open Squares: " + openSquares;//updates display
Invalidate();//updates form
}//end for
if (openSquares == 0)
{ //if there are no free squares then display a message telling user
//they won.
MessageBox.Show("Good Job You Completed The Game Using Only: " + queenCount.ToString() + " Queens!", "Game Over...");
NewGame();//send to form => newgame
}//end if
}//end buttonClick()
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{/*When user wants to click File>Exit */
this.Dispose();//shuts down form and ends all processes
}//end exitToolStrip
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
//displays a form telling the user about the program
aboutForm about = new aboutForm(); //aboutForm is aboutForm.cs
about.ShowDialog();//displays the form
}//end
private void instructionsToolStripMenuItem_Click(object sender, EventArgs e)
{
//displays a form telling the user how to use the program
instructForm instructions = new instructForm();//instructForm.cs
instructions.ShowDialog();//displays instructions
}//end
private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
{
NewGame();//calls newGame function
MessageBox.Show("New Game Started", "Notice...");//notifies usr :(
}//end newGameToolStrip()
private void NewGame()
{ //this function resets the form to it's original state.
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
}//end NewGame()
}//end public Queens
}//end namespace