</pre>这个是兰州交大的CShap(C#)作业题,刚开始自己陷入误区了,第一天做时没耐心了,今天花了2个多小时写了这个L型骨牌的覆盖程序<p></p><p>命令行的骨牌实现可以在我的博客里找到</p><p>下面是图形化界面的L型骨牌实现</p><p>具体实现思路:</p><p>利用矩形数组和二维数组,核心的算法是二维数组也就是命令行的L型骨牌,然后利用数值的不同画出不同颜色的方块</p><p></p><p>核心的算法</p><p></p><pre name="code" class="csharp">using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace L骨牌 { public partial class Form1 : Form { Graphics g; //定义画刷数组 // SolidBrush[] brush = { new SolidBrush(Color.Gold), new SolidBrush(Color.Black), new SolidBrush(Color.Yellow), new SolidBrush(Color.DarkBlue), new SolidBrush(Color.Purple), new SolidBrush(Color.Red), new SolidBrush(Color.Blue) ,new SolidBrush(Color.DarkCyan),new SolidBrush(Color.Crimson),new SolidBrush(Color.Green),new SolidBrush(Color.Honeydew),new SolidBrush(Color.LawnGreen)}; //定义一个数计数brush // int count = 0; //二位数组 int[,] board; //对应的方块 Rectangle[,] rect; //k int k; //x int x; //y int y; //2^K int num; //画线开始的坐标 int x0, y0; //递归层数 int n = 1; //每次画出的L型骨牌 int next; //定时器 System.Timers.Timer t; //获取一个不同的画刷 public SolidBrush getSolidBrush() { long tick = DateTime.Now.Ticks; Random ran = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32)); SolidBrush sb = new SolidBrush(Color.FromArgb(ran.Next(255),ran.Next(255),ran.Next(255))); return sb; } //构造函数 public Form1() { InitializeComponent(); g = this.CreateGraphics(); } public void resetX0Y0() { x0 = 10; y0 = 10; } public void initValue() { k = Int32.Parse(textBox_K.Text); x = Int32.Parse(textBox_x.Text); y = Int32.Parse(textBox_Y.Text); num = (int)Math.Pow(2, k); //二维数组 board = new int[num, num]; //对应的方块 rect = new Rectangle[num, num]; //重置x0和y0 resetX0Y0(); // next = 1; } public void drawInitLine() { //横向画线 for (int j = 0; j <=num; j++) { Pen p = new Pen(new SolidBrush(Color.Brown)); g.DrawLine(p,x0,y0,x0+400,y0); y0 += 400 / num; } //重置x0,y0 resetX0Y0(); //竖向画线 for (int j = 0; j <= num; j++) { Pen p = new Pen(new SolidBrush(Color.Brown)); g.DrawLine(p, x0, y0, x0, y0+400); x0 += 400 / num; } //重置x0,y0 resetX0Y0(); } public void initArray() { //每个方格的宽度 int size = 400/num; //重置x0,y0 for (int i = 0; i < num; i++) { for (int j = 0; j < num; j++) { rect[i, j] = new Rectangle(x0,y0,size,size); y0 += size; } x0 += size; y0 = 10; } ////////////// for (int i = 0; i < num; i++) { for (int j = 0; j < num; j++) { board[i, j] = 0; } } } //public void chess_Board( int dr, int dt, int pr, int pt, int size) //{ // if (size == 1) return;//当规模为一时,不需要做任何的改变了 // int t = n++; // int tsize = size / 2;//每一次棋盘大小变成原来的1/4 // //棋盘分为四大区域 // //最先处理了特殊方块位于左上方的 // if (pr < dr + tsize && pt < dt + tsize) // { // //左上方的 // chess_Board(dr, dt, pr, pt, tsize); // } // else // { // //左上方的 // board[dr + tsize - 1,dt + tsize - 1] = t; // chess_Board(dr, dt, dr + tsize - 1, dt + tsize - 1, tsize); // } // //特殊方块位于右上方 // if (pr < dr + tsize && pt >= dt + tsize) // { // //右上方 // chess_Board(dr, dt + size, pr, pt, tsize); // } // else // { // //右上方 // board[dr + tsize - 1,dt + tsize] = t; // chess_Board(dr, dt + tsize, dr + tsize - 1, dt + tsize, tsize); // } // //特殊方块位于左下方 // if (pr >= dr + tsize && pt < dt + tsize) // { // //左下方 // chess_Board(dr + tsize, dt, pr, pt, tsize); // } // else // { // //左下方 // board[dr + tsize,dt + tsize - 1] = t; // chess_Board( dr + tsize, dt, dr + tsize, dt + tsize - 1, tsize); // } // //特殊方块位于右下方 // if (pr >= dr + tsize && pt >= dt + tsize) // { // //右下方 // chess_Board(dr + tsize, dt + tsize, pr, pt, tsize); // } // else // { // //右下方 // board[dr + tsize,dt + tsize] = t; // chess_Board( dr + tsize, dt + tsize, dr + tsize, dt + tsize, tsize); // } //} private void ChessBoard(int tr, int tc, int x, int y, int s) { //算法主体 if (s == 1) return; int si = s / 2, t = n++; //覆盖左上角子棋盘 if (x < tr + si && y < tc + si) //特殊方格在此棋盘中 ChessBoard(tr, tc, x, y, si); else { //此棋盘中无特殊方格 //用L形骨牌覆盖左上角 board[tr + si - 1, tc + si - 1] = t; //覆盖其余方格 ChessBoard(tr, tc, tr + si - 1, tc + si - 1, si); } //覆盖右上角子棋盘 if (x < tr + si && y >= tc + si) //特殊方格在此棋盘中 ChessBoard(tr, tc + si, x, y, si); else { //此棋盘中无特殊方格 //用L形骨牌覆盖左下角 board[tr + si - 1, tc + si] = t; //覆盖其余方格 ChessBoard(tr, tc + si, tr + si - 1, tc + si, si); } //覆盖左下角子棋盘 if (x >= tr + si && y < tc + si) //特殊方格在此棋盘中 ChessBoard(tr + si, tc, x, y, si); else { //此棋盘中无特殊方格 //用L形骨牌覆盖右上角 board[tr + si, tc + si - 1] = t; //覆盖其余方格 ChessBoard(tr + si, tc, tr + si, tc + si - 1, si); } //覆盖右下角子棋盘 if (x >= tr + si && y >= tc + si) //特殊方格在此棋盘中 ChessBoard(tr + si, tc + si, x, y, si); else { //此棋盘中无特殊方格 //用L形骨牌覆盖左上角 board[tr + si, tc + si] = t; //覆盖其余方格 ChessBoard(tr + si, tc + si, tr + si, tc + si, si); } } public void drawSpecialRect() { g.FillRectangle(getSolidBrush(), rect[x - 1, y - 1]); } private void button1_Click(object sender, EventArgs e) { initValue(); drawInitLine(); initArray(); drawSpecialRect(); ChessBoard(0, 0, x - 1, y - 1, num); next = getMin(); // MessageBox.Show("" + board[4, 5]); showArray(); } public int getMax() { int max = board[0, 0]; for (int i = 0; i < num; i++) { for (int j = 0; j < num; j++) { if (board[i, j] > max) { max = board[i, j]; } } } return max; } public int getMin() { int min = board[0, 0]; for (int i = 0; i < num; i++) { for (int j = 0; j < num; j++) { if (board[i, j] < min) { min = board[i, j]; } } } return min; } public void showArray() { for (int i = 0; i < num; i++) { for (int j = 0; j < num; j++) { Console.Write("" + board[i, j]+"\t"); } Console.WriteLine(); } Console.WriteLine("///////////////////////////////"); } public void drawRectNext() { int max = getMax(); if (next > max) { MessageBox.Show("客官,已经画完了"); return; } // int count = 1; //bool t = true; SolidBrush sb = getSolidBrush(); for (int i = 0; i < num; i++) { for (int j = 0; j < num; j++) { if (board[i, j] == next) { // MessageBox.Show("I:" + i + "\n J:"+j); if (i == x-1 && j == y-1) { continue; } //if (count>3&&t==true) //{ // sb = getSolidBrush(); // t = false; //} g.FillRectangle(sb, rect[i, j]); //count++; } } } } private void button2_Click(object sender, EventArgs e) { drawRectNext(); next++; } public bool drawRectNextOnTimer() { int max = getMax(); if (next > max) { //drawInitLine(); t.Enabled = false; MessageBox.Show("动态绘制完毕"); return true; } // int count = 1; //bool t = true; SolidBrush sb = getSolidBrush(); for (int i = 0; i < num; i++) { for (int j = 0; j < num; j++) { if (board[i, j] == next) { // MessageBox.Show("I:" + i + "\n J:"+j); if (i == x - 1 && j == y - 1) { continue; } //if (count>3&&t==true) //{ // sb = getSolidBrush(); // t = false; //} g.FillRectangle(sb, rect[i, j]); //count++; } } } return false; } private void button3_Click(object sender, EventArgs e) { t= new System.Timers.Timer(1000); t.Elapsed += new System.Timers.ElapsedEventHandler(drawL); t.Enabled = true; t.AutoReset = true; int min = getMin(); int max = getMax(); next = min; } public void drawL(object source, System.Timers.ElapsedEventArgs e) { //MessageBox.Show("时间到"); drawRectNextOnTimer(); next++; } } }
我使用的是VS2013
下面是下载连接
http://download.csdn.net/detail/jackhall/8817479
时间: 2024-10-05 05:00:46