using System; using System.Windows.Forms; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Text; namespace KnightsTour2 { public partial class KnightsTour : Form { public Button[,] buttons = new Button[8, 8]; public Point lastMove; private Bitmap knight = new Bitmap(typeof(KnightsTour), "knight.png"); private Bitmap knightblack = new Bitmap(typeof(KnightsTour), "knightblack.png"); bool firstMove = true; double counter = 0.0; public KnightsTour() { InitializeComponent(); //Button[,] buttons = new Button[8,8]; for (int i = 0; i < 8; i++) 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(); buttons[i, j].Width = 50; buttons[i, j].Height = 50; buttons[i, j].Location = new Point(i * 50, j * 50); // set up size, location, color, etc... // add the click event handler buttons[i, j].Click += new EventHandler(ButtonClick); if ((i + j) % 2 == 0) buttons[i, j].BackColor = Color.Black; else buttons[i, j].BackColor = Color.White; // add the button controls to the form this.Controls.Add(buttons[i, j]); } } private void ButtonClick(object sender, EventArgs e) { double increment = Convert.ToDouble(((double)100.0/64.0).ToString("0.00")); for(int i = 0; i < 8; i++) for (int j = 0; j < 8; j++) { if (buttons[i, j].BackColor == Color.Red) { if ((i + j) % 2 == 0) buttons[i, j].BackColor = Color.Black; else buttons[i, j].BackColor = Color.White; } } // determine which button was clicked B Button b = (Button) sender; int x = Convert.ToInt32(b.Name.TrimStart('b'))/100; int y = Convert.ToInt32(b.Name.TrimStart('b'))%100; if (firstMove == false) { if (x + 2 == lastMove.X && y + 1 == lastMove.Y || x + 2 == lastMove.X && y - 1 == lastMove.Y || x + 1 == lastMove.X && y + 2 == lastMove.Y || x + 1 == lastMove.X && y - 2 == lastMove.Y || x - 2 == lastMove.X && y + 1 == lastMove.Y || x - 2 == lastMove.X && y - 1 == lastMove.Y || x - 1 == lastMove.X && y + 2 == lastMove.Y || x - 1 == lastMove.X && y - 2 == lastMove.Y) { if ((x + y) % 2 == 0) buttons[x, y].Image = knightblack; else buttons[x, y].Image = knight; progressBar.PerformStep(); counter += increment; labelComplete.Text = (counter.ToString() + "%"); lastMove.X = x; lastMove.Y = y; } else { MessageBox.Show("Seems You Have Made An Invalid Move!", "Error: Invalid Move"); return; } } else { if ((x + y) % 2 == 0) buttons[x, y].Image = knightblack; else buttons[x, y].Image = knight; lastMove.X = x; lastMove.Y = y; progressBar.PerformStep(); counter += 100/64; labelComplete.Text = ((double)increment).ToString("0.00") + "%"; firstMove = false; } } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { this.Dispose(); } private void exitToolStripMenuItem1_Click(object sender, EventArgs e) { MessageBox.Show("New Game Started!", "Notice"); Form form = new Form(); this.Hide(); form = new KnightsTour(); form.Show(); } private void buttonHint_Click(object sender, EventArgs e) { /* The mass of lines of code below (between the green lines) * represents my attempt at verification process. * It is an extra function I added. It higlights the users availible moves * by putting a red backround in them. */ ////////////////////////////////////////////////////////////// bool noMoves = true; bool endloop = false; if (lastMove.X + 2 >= 0 && lastMove.X + 2 < 8 && lastMove.Y + 1 >= 0 && lastMove.Y + 1 < 8) buttons[lastMove.X + 2, lastMove.Y + 1].BackColor = Color.Red; if (lastMove.X + 2 >= 0 && lastMove.X + 2 < 8 && lastMove.Y - 1 >= 0 && lastMove.Y - 1 < 8) buttons[lastMove.X + 2, lastMove.Y - 1].BackColor = Color.Red; if (lastMove.X + 1 >= 0 && lastMove.X + 1 < 8 && lastMove.Y + 2 >= 0 && lastMove.Y + 2 < 8) buttons[lastMove.X + 1, lastMove.Y + 2].BackColor = Color.Red; if (lastMove.X + 1 >= 0 && lastMove.X + 1 < 8 && lastMove.Y - 2 >= 0 && lastMove.Y - 2 < 8) buttons[lastMove.X + 1, lastMove.Y - 2].BackColor = Color.Red; if (lastMove.X - 2 >= 0 && lastMove.X - 2 < 8 && lastMove.Y + 1 >= 0 && lastMove.Y + 1 < 8) buttons[lastMove.X - 2, lastMove.Y + 1].BackColor = Color.Red; if (lastMove.X - 2 >= 0 && lastMove.X - 2 <= 8 && lastMove.Y - 1 >= 0 && lastMove.Y - 1 < 8) buttons[lastMove.X - 2, lastMove.Y - 1].BackColor = Color.Red; if (lastMove.X - 1 >= 0 && lastMove.X - 1 < 8 && lastMove.Y + 2 >= 0 && lastMove.Y + 2 < 8) buttons[lastMove.X - 1, lastMove.Y + 2].BackColor = Color.Red; if (lastMove.X - 1 >= 0 && lastMove.X - 1 < 8 && lastMove.Y - 2 >= 0 && lastMove.Y - 2 < 8) buttons[lastMove.X - 1, lastMove.Y - 2].BackColor = Color.Red; /////////////////////////////////////////////////////////////// for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (endloop == false) if (buttons[i, j].BackColor == Color.Red) { noMoves = false; endloop = true; } } } if (noMoves == true) MessageBox.Show("You Are Out Of Moves... Please Start A New Game!", "No Moves Availible"); } private void aboutToolStripMenuItem_Click(object sender, EventArgs e) { aboutForm about = new aboutForm(); about.ShowDialog(); } private void instructionsToolStripMenuItem_Click(object sender, EventArgs e) { instructionForm instructions = new instructionForm(); instructions.ShowDialog(); } } }