All pastes #476417 Raw Edit

Hex's KnightsTour

public text v1 · immutable
·published 2007-05-08 02:13 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

namespace KnightsTour
{
    public partial class KnightsTour : Form
    {
        Size gridSize = new Size(8, 8);
        Size cellSize = new Size(50, 50);
        // the bitmap images
        private Bitmap knight = new Bitmap(typeof(KnightsTour), "knight.png");
        /// The game internal data representation
        /// If a cell equals:
        /// 0 - no image
        /// 1 - image 1
        /// 2 - image 2
        Bitmap[,] gameData;
        // variables used when an image is moving across the game window
        Point selectedCell = new Point();
        Point mousePt = new Point();
        Point cellOffset = new Point();
        Bitmap selectedImage;
        bool moving = false;

        public KnightsTour()
        {
            InitializeComponent();
            // for flicker free motion
            this.SetStyle(ControlStyles.AllPaintingInWmPaint |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true);

            // set the screen size and window position
            this.ClientSize = new Size(cellSize.Width * gridSize.Width,
                cellSize.Height * gridSize.Height);
            this.StartPosition = FormStartPosition.CenterScreen;

            // initialize the gameData array
            gameData = new Bitmap[gridSize.Width, gridSize.Height];

            // set the gameData values all to null
            for (int i = 0; i < gridSize.Width; i++)
                for (int j = 0; j < gridSize.Height; j++)
                    gameData[i, j] = null;
            // set the left column to img1
            for (int j = 0; j < gridSize.Height; j++)
                gameData[0, j] = knight;

        }
        
        private void KnightsTour_Paint_1(object sender, PaintEventArgs e)
        {
            selectedImage = knight; 
            // the graphics device to draw to
            Graphics gr = e.Graphics;
            // draw the background colour
            //gr.FillRectangle(Brushes.Goldenrod, this.ClientRectangle);
            // draw the game grid.
            for (int i = 0; i < gridSize.Width; i++)
                for (int j = 0; j < gridSize.Height; j++)
                {
                    Rectangle cell = new Rectangle(i * cellSize.Width,j * cellSize.Height,
                        cellSize.Width, cellSize.Height);
                    if ((i + j) % 2 == 0)
                        gr.FillRectangle(Brushes.White, cell);
                    else
                        gr.FillRectangle(Brushes.Black, cell);
                }
           if (moving)
                gr.DrawImage(selectedImage, mousePt.X - cellOffset.X, mousePt.Y - cellOffset.Y);
        }

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

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

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

        private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Starting New Game", "Notice");
            Form form = new Form();
            this.Hide();
            form = new KnightsTour();
            form.Show();
        }

        private void KnightsTour_MouseDown(object sender, MouseEventArgs e)
        {
            //determine which cell was chosen
            selectedCell = new Point((e.X) / cellSize.Width , (e.Y) / cellSize.Height);
            //set the variable which tells if the object is going to be moved
            if (selectedCell.X < 0 || selectedCell.Y < 0 || selectedCell.X >= gridSize.Width ||
                selectedCell.Y >= gridSize.Height)
            {
                MessageBox.Show("Invalid Move", "Warning...");
                moving = false;
            }
            else moving = true;
            if (moving)//if the image is in motion
            {
                //record the current mouse location
                mousePt = new Point(selectedCell.X, selectedCell.Y);
                MessageBox.Show(selectedCell.X.ToString() + "," + selectedCell.Y.ToString(), "Selected Cell...");
                //cause the selected knight square to be redrawn
                gameData[selectedCell.X * 50,selectedCell.Y * 50] = selectedImage;
                RedrawPoint(selectedCell.X, selectedCell.Y);
                RedrawCell(selectedCell);
            }
        }
        private void RedrawPoint(int x, int y)
        {
            Rectangle rect = new Rectangle(x, y, cellSize.Width, cellSize.Height);
            Invalidate(rect);
        }
        private void RedrawCell(Point cell)
        {
            Rectangle rect = new Rectangle(cell.X * cellSize.Width, cell.Y * cellSize.Height, cellSize.Width, cellSize.Height);
            Invalidate(rect);
        }
    }
}