【文件属性】:
文件名称:c#拼图游戏
文件大小:1.62MB
文件格式:RAR
更新时间:2015-02-19 15:23:53
习作
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 ArrayList();
//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;
}
}
}
}
【文件预览】:
PinTu
----bin()
--------Debug()
----Form1.Designer.cs(9KB)
----Form3.cs(945B)
----Form2.resx(6KB)
----Form3.Designer.cs(2KB)
----GamePanel.cs(610B)
----Program.cs(486B)
----obj()
--------Debug()
----Form3.resx(6KB)
----ab.xml(71B)
----Form2.cs(778B)
----Form1.cs(22KB)
----PinTu.csproj(4KB)
----Form2.Designer.cs(4KB)
----Form1.resx(6KB)
----Properties()
--------Resources.resx(5KB)
--------Settings.settings(249B)
--------AssemblyInfo.cs(1KB)
--------Settings.Designer.cs(1KB)
--------Resources.Designer.cs(3KB)
网友评论
- 试试 不错 还可用
- 为啥不能运行。。。。。。。。。。。郁闷
- 试试 不错 还可用
- 能运行,效果很一般。
- 在2010调试时,出现GamePanel does not contain a constructor that takes 1 arguments
- 不能用,没法运行,一调试就是错误
- 才有对象化设计,封装较好,好使……
- 可以参考,谢谢分享.运行成功
- 不能用,没法运行
- 功能不够强大
- 转成2010都不行,没有解决方案。打不开游戏界面
- 这个资源可以用的。
- 32位64位的都有
- 能玩,还不错
- 非常的不错 可惜我是初学者 对我来说有些难了
- 代码有帮助
- 不错,非常符合我的需要,非常有用,谢谢
- 我的也是在2010不能运行
- 程序在vs2010没法运行 不过代码读一下还是很有帮助的
- 没有注释其它还行
- 资源简介也太长了吧
- 打不开PinTu的应用程序。就信封水平划过就没了。