您好,欢迎光临本网站![请登录][注册会员]  
文件名称: c#拼图游戏
  所属分类: C#
  开发工具:
  文件大小: 1mb
  下载次数: 0
  上传时间: 2012-01-30
  提 供 者: hgwsyw******
 详细说明: using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Xml; using System.Text.RegularExpressions; using System.Security.Cryptography; using System.Security.Cryptography.Xml; using System.Xml.Serialization; namespace PinTu { public partial class Form1 : Form { #region 全局变量 Image im; Bitmap bm; Bitmap empt; ArrayList arrrect = new Ar rayList(); //ArrayList emptpt = new ArrayList(); ArrayList arrpic = new ArrayList(); //ArrayList change = new ArrayList(); PictureBox[] pb; Random rand = new Random(); int Picsize; int gzs; int bushu = 0; int bu = 0; int[] a; int[] b; bool isfirst = true; bool canclick = true; string filename = ""; string juhao = ""; string change = ""; Bitmap bmpg; #endregion public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { button4.Enabled = false; label2.Text = ""; label4.Text = ""; filename = "Autumn.jpg"; int tempi = rand.Next(1, 1001); Readinfo(tempi.ToString()); textBox1.Text = tempi.ToString(); } private void Creatpic(int[] inta) { panel1.Controls.Clear(); im = Image.FromFile(filename); bm = new Bitmap(320, 320); Graphics gg = Graphics.FromImage(bm); gg.DrawImage(im, new Rectangle(0, 0, 320, 320)); panel2.BackgroundImage = im; pb = new PictureBox[gzs * gzs]; for (int i = 0; i < gzs * gzs; i++) { pb[i] = new PictureBox(); pb[i].Parent = panel1; pb[i].Size = new Size(Picsize, Picsize); pb[i].Location = new Point((i % gzs) * Picsize, (i / gzs) * Picsize); pb[i].BorderStyle = BorderStyle.FixedSingle; pb[i].Name = a[i].ToString(); pb[i].SizeMode = PictureBoxSizeMode.CenterImage; Bitmap bmp = new Bitmap(Picsize, Picsize); Graphics g = Graphics.FromImage(bmp); g.DrawImage(bm, new Rectangle(0, 0, Picsize, Picsize), (Rectangle)arrrect[inta[i]], GraphicsUnit.Pixel); pb[i].Image = bmp; pb[i].Click += new EventHandler(Picture_Click); if (a[i] == gzs * gzs - 1) { empt = bmp; //pb[i].Image = null; //pb[i].BackColor = Color.Blue; Image img = pb[i].Image; bmpg = new Bitmap(img); for (int k = 0; k < bmpg.Width; k++) { for (int j = 0; j < bmpg.Height; j++) { Color bmpcolor = bmpg.GetPixel(k, j); byte A = bmpcolor.A; byte R = bmpcolor.R; byte G = bmpcolor.G; byte B = bmpcolor.B; bmpcolor = Color.FromArgb(128, R, G, B); bmpg.SetPixel(k, j, bmpcolor); } } pb[i].Image = bmpg; } } } private void Picture_Click(object sender, EventArgs e) { if (canclick == false) return; PictureBox pbox = (PictureBox)sender; if (pbox.Image == null) return; Point opt = pbox.Location; Point ptleft = new Point(opt.X - Picsize, opt.Y); Point ptright = new Point(opt.X + Picsize, opt.Y); Point ptup = new Point(opt.X, opt.Y - Picsize); Point ptdown = new Point(opt.X, opt.Y + Picsize); foreach (PictureBox p in panel1.Controls) { if (p.Location == ptleft && p.Image == bmpg) { change += pbox.Name + "$" + p.Name + "|"; pbox.Location = ptleft; p.Location = opt; bushu++; textBox1.Enabled = false; button1.Enabled = false; } if (p.Location == ptright && p.Image == bmpg) { change += pbox.Name + "$" + p.Name + "|"; pbox.Location = ptright; p.Location = opt; bushu++; textBox1.Enabled = false; button1.Enabled = false; } if (p.Location == ptup && p.Image == bmpg) { change += pbox.Name + "$" + p.Name + "|"; pbox.Location = ptup; p.Location = opt; bushu++; textBox1.Enabled = false; button1.Enabled = false; } if (p.Location == ptdown && p.Image == bmpg) { change += pbox.Name + "$" + p.Name + "|"; pbox.Location = ptdown; p.Location = opt; bushu++; textBox1.Enabled = false; button1.Enabled = false; } } label2.Text = "已走的步数:" + bushu.ToString(); if (IsWin()) { foreach (PictureBox p in panel1.Controls) { if (p.Image == bmpg) { p.Image = empt; break; } } Updatainfo(textBox1.Text.Trim()); int tempi = rand.Next(1, 1001); Readinfo(tempi.ToString()); textBox1.Enabled = true; textBox1.Text = tempi.ToString(); bushu = 0; label2.Text = ""; button1.Enabled = true; } } private void Creatrect() { Picsize = 320 / gzs; arrrect.Clear(); Rectangle[] rect = new Rectangle[gzs * gzs]; for (int i = 0; i < gzs * gzs; i++) { rect[i] = new Rectangle((i % gzs) * Picsize, (i / gzs) * Picsize, Picsize, Picsize); arrrect.Add(rect[i]); } } private void button1_Click(object sender, EventArgs e) { try { OpenFileDialog dial = new OpenFileDialog(); dial.Title = "open the file"; dial.Filter = "Image File(*.jpg)|*.jpg"; dial.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory; if (dial.ShowDialog() == DialogResult.OK) { filename = dial.FileName; Creatpic(a); } } catch(Exception ex) { MessageBox.Show(ex.Message); } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox1.SelectedIndex == 0) gzs = 2; if (comboBox1.SelectedIndex == 1) gzs = 3; if (comboBox1.SelectedIndex == 2) gzs = 4; if (comboBox1.SelectedIndex == 3) gzs = 5; if (!isfirst) { Creatrect(); DaluanPic(); Creatpic(a); } } private void DaluanPic() { //a = new int[gzs * gzs]; int rpt = gzs * gzs - 1; for (int i = 0; i < gzs * gzs; i++) a[i] = i; for (int i = 0; i < 200; i++) { int p = rand.Next(0, 4); switch (p) { case 0: if (rpt % gzs < gzs - 1) { int temp1 = a[rpt + 1]; a[rpt + 1] = a[rpt]; a[rpt] = temp1; rpt = rpt + 1; } break; case 1: if (rpt % gzs > 0) { int temp2 = a[rpt - 1]; a[rpt - 1] = a[rpt]; a[rpt] = temp2; rpt = rpt - 1; } break; case 2: if (rpt > gzs - 1) { int temp3 = a[rpt - gzs]; a[rpt - gzs] = a[rpt]; a[rpt] = temp3; rpt = rpt - gzs; } break; case 3: if (rpt < gzs * (gzs - 1)) { int temp4 = a[rpt + gzs]; a[rpt + gzs] = a[rpt]; a[rpt] = temp4; rpt = rpt + gzs; } break; } } //b = new int[a.Length]; } private bool IsWin() { b = new int[a.Length]; for (int i = 0; i < gzs * gzs; i++) { Point pt = new Point((i % gzs) * Picsize, (i / gzs) * Picsize); foreach (PictureBox picbox in panel1.Controls) { if (picbox.Location == pt) b[i] = Convert.ToInt32(picbox.Name); } } for (int i = 0; i < b.Length; i++) { if (i != b[i]) return false; } return true; } private void button3_Click(object sender, EventArgs e) { change = ""; arrpic.Clear(); bushu = 0; label2.Text = ""; Creatpic(a); //CrRect(); } private void CrRect() { ArrayList alist = new ArrayList(); gzs = 4; a = new int[16]; for (int m = 0; m < 1050; m++) { DaluanPic(); string s = ""; for (int i = 0; i < a.Length; i++) s += a[i].ToString() + "$"; if (!alist.Contains(s)) alist.Add(s); } Saveinfo(alist); MessageBox.Show("ok"); } private void ReadFile(string s) { StreamReader sr = new StreamReader("abc.txt"); string ss = sr.ReadLine(); while (ss != null) { string[] splitstr = ss.Split('#'); string ps = ""; if (splitstr[0] == s) { juhao = ss; ps = splitstr[1]; string[] split = ps.Split('$'); if (Convert.ToInt32(s) > 1000) { gzs = 4; a = new int[16]; } if (Convert.ToInt32(s) <= 1000) { gzs = 3; a = new int[9]; } for (int w = 0; w < split.Length - 1; w++) a[w] = Convert.ToInt32(split[w]); Creatrect(); Creatpic(a); break; } ss = sr.ReadLine(); } sr.Dispose(); } private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { Regex regex = new Regex("^[0-9]*[1-9][0-9]*$"); if (e.KeyChar == 13) { if (textBox1.Text == "") { MessageBox.Show("请选择局号!"); return; } if (!regex.IsMatch(textBox1.Text.Trim())) { MessageBox.Show("您输入的不是整数!"); return; } if (textBox1.Text.Trim().Substring(0, 1) == "0") { MessageBox.Show("第一位数字不能为零!"); return; } if (int.Parse(textBox1.Text.Trim()) > 2000) { MessageBox.Show("输入的局号超出了范围!"); return; } button4.Enabled = false; Readinfo(textBox1.Text.Trim()); textBox1.Enabled = false; button1.Enabled = false; change = ""; arrpic.Clear(); } } private void button2_Click(object sender, EventArgs e) { label2.Text = ""; bushu = 0; textBox1.Enabled = true; button1.Enabled = true; } private void Saveinfo(ArrayList a) { XmlDocument xmldoc = new XmlDocument(); try { XmlElement root = null, pic = null, theElme = null; xmldoc.Load("abc.xml"); root = xmldoc.DocumentElement; for (int i = 0; i < a.Count; i++) { pic = xmldoc.CreateElement("Pictrue"); theElme = xmldoc.CreateElement("JS"); theElme.InnerText = (i + 1001).ToString(); pic.AppendChild(theElme); theElme = xmldoc.CreateElement("ID"); theElme.InnerText = a[i].ToString(); pic.AppendChild(theElme); theElme = xmldoc.CreateElement("Scores"); theElme.InnerText = "初次"; pic.AppendChild(theElme); theElme = xmldoc.CreateElement("Author"); theElme.InnerText = "WBJ"; pic.AppendChild(theElme); theElme = xmldoc.CreateElement("Date"); theElme.InnerText = DateTime.Now.ToString("yyyy-MM-dd"); pic.AppendChild(theElme); root.AppendChild(pic); } xmldoc.Save("abc.xml"); } catch(Exception e) { MessageBox.Show(e.Message); } } private void Readinfo(string s) { bu = 0; change = ""; arrpic.Clear(); XmlDocument xmldoc = new XmlDocument(); try { string[] ppl; XmlElement root=null; xmldoc.Load("abc.xml"); root = xmldoc.DocumentElement; XmlNodeList nodeList = root.ChildNodes; foreach (XmlNode xn in nodeList) { if (xn.SelectSingleNode("JS").InnerText == s) { ppl = xn.SelectSingleNode("ID").InnerText.Split('$'); XmlNode ff = xn.SelectSingleNode("Scores"); if (ff.InnerText == "初次") label4.Text = "目前本局最少步数:" + ff.InnerText; else { XmlNode kk = xn.SelectSingleNode("Author"); XmlNode date = xn.SelectSingleNode("Date"); XmlNode ys = xn.SelectSingleNode("Yanshi"); label4.Text = "目前本局最少步数:" + ff.InnerText; label4.Text += "\n纪录创造者:" + kk.InnerText; label4.Text += "\n记录创造日期:" + date.InnerText; if (ys.InnerText != "xyz") button4.Enabled = true; } if (Convert.ToInt32(s) <= 1000) { gzs = 3; a = new int[9]; } else { gzs = 4; a = new int[16]; } for (int w = 0; w < ppl.Length - 1; w++) a[w] = Convert.ToInt32(ppl[w]); Creatrect(); Creatpic(a); break; } } } catch (Exception e) { MessageBox.Show(e.Message); } } private void Updatainfo(string s) { XmlDocument xmldoc = new XmlDocument(); try { XmlElement root = null; xmldoc.Load("abc.xml"); root = xmldoc.DocumentElement; XmlNodeList nodeList = root.ChildNodes; foreach (XmlNode xn in nodeList) { if (xn.SelectSingleNode("JS").InnerText == s) { XmlNode xx = xn.SelectSingleNode("Scores"); XmlNode author = xn.SelectSingleNode("Author"); XmlNode date = xn.SelectSingleNode("Date"); XmlNode yanshn = xn.SelectSingleNode("Yanshi"); if (xx.InnerText != "初次") { int zxbs = Convert.ToInt32(xx.InnerText); if (zxbs > bushu) { Form2 f2 = new Form2(); f2.Text = "您创造了记录,请输入您的名字"; if (f2.ShowDialog() == DialogResult.OK) { author.InnerText = f2.textBox1.Text.Trim(); xx.InnerText = bushu.ToString(); date.InnerText = DateTime.Now.ToString("yyyy-MM-dd"); yanshn.InnerText = change; f2.Close(); } } } else { Form2 f2 = new Form2(); f2.Text = "您创造了记录,请输入您的名字"; if (f2.ShowDialog() == DialogResult.OK) { author.InnerText = f2.textBox1.Text.Trim(); xx.InnerText = bushu.ToString(); date.InnerText = DateTime.Now.ToString("yyyy-MM-dd"); yanshn.InnerText = change; f2.Close(); } } break; } } xmldoc.Save("abc.xml"); } catch (Exception e) { MessageBox.Show(e.Message); } } private void button4_Click(object sender, EventArgs e) { timer1.Start(); canclick = false; button2.Enabled = false; button3.Enabled = false; button4.Enabled = false; XmlDocument xmldoc = new XmlDocument(); XmlElement root = null; xmldoc.Load("abc.xml"); root = xmldoc.DocumentElement; XmlNodeList nodeList = root.ChildNodes; foreach (XmlNode xn in nodeList) { if (xn.SelectSingleNode("JS").InnerText == textBox1.Text.Trim()) { string yshpic = xn.SelectSingleNode("Yanshi").InnerText; string[] picstr = yshpic.Split('|'); for (int j = 0; j < picstr.Length; j++) arrpic.Add(picstr[j]); break; } } } private void FileEncryption() { //RijndaelManaged key = new RijndaelManaged(); //byte[] bytekey=Encoding.Unicode.GetBytes("我心飞扬穿越时空"); //key.Key = bytekey; //XmlDocument xmlDoc = new XmlDocument(); //xmlDoc.PreserveWhitespace = true; //xmlDoc.Load("abc.xml"); //Encrypt } private void timer1_Tick(object sender, EventArgs e) { if (bu < arrpic.Count) { string[] picname = arrpic[bu].ToString().Split('$'); foreach (PictureBox p in panel1.Controls) { if (p.Name == picname[0]) { Point ppt = p.Location; foreach (PictureBox pp in panel1.Controls) { if (pp.Name == picname[1]) { p.Location = pp.Location; pp.Location = ppt; } } } } bu++; } else { timer1.Stop(); button2.Enabled = true; button3.Enabled = true; canclick = true; } } } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: 习作
 输入关键字,在本站1000多万海量源码库中尽情搜索: