標籤:c# 圖形 介面 l型骨牌 蘭州交通大學
</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
C#圖形化介面--L型骨牌