<html>
<head>
<style type="text/css">
.haha {
border-width: 2;font-size: 50;font-weight: bold;color: white;text-align: center;color: white;background-color: black;
}
</style>
</head>
<h4 id='de'></h4>
<script>
var xsize = 3, ysize = 3;
var size = xsize * ysize;
var a = new Array(size);
var spaceX, spaceY;
newGame(xsize, ysize);
function newGame() {
size = xsize * ysize;
a = new Array(size);
clear();
shuffle();
init();
draw();
}
onkeydown = keyDown;
function print(s) {
document.getElementById('de').innerHTML = s;
}
function clear() {
for (var i = 0; i < size - 1; i++)
a[i] = i + 1;
a[size - 1] = 0;
spaceX = xsize - 1;
spaceY = ysize - 1;
}
function shuffle() {
for (var i = 0; i < 100; i++) {
var x, y;
do {
x = Math.floor(Math.random() * (size - 1));
y = Math.floor(Math.random() * (size - 1));
} while (x == y);
var temp = a[x];
a[x] = a[y];
a[y] = temp;
}
}
function keyDown(e) {
var x = spaceX, y = spaceY;
switch (e.keyCode) {
case 37:
y++;
break;
case 38:
x++;
break;
case 39:
y--;
break;
case 40:
x--;
break;
}
if (x >= 0 && y >= 0 && x < xsize && y < ysize) {
var temp = a[x * ysize + y];
a[x * ysize + y] = a[spaceX * ysize + spaceY];
a[spaceX * ysize + spaceY] = temp;
spaceX = x;
spaceY = y;
draw();
if (over()) {
confirm('you win!');
clear();
shuffle();
draw();
}
}
}
function over() {
for (var i = 0; i < size - 1; i++) {
if (a[i] != i + 1)
return false;
}
return true;
}
function draw() {
for (var i = 0; i < size; i++) {
var img = document.getElementById('pic' + i);
img.innerHTML = (a[i] == 0 ? "" : a[i]);
}
}
function cout(s) {
document.write(s);
}
function init() {
var w = screen.availWidth / xsize;
var h = (screen.availHeight - 100) / ysize;
w=h=Math.min(w,h);
cout("<table align='center'>");
for (var i = 0; i < xsize; i++) {
cout("<tr>");
for (var j = 0; j < ysize; j++) {
cout("<td width=" + Math.floor(w) + " height=" + Math.floor(h)
+ " class='haha' id=pic" + (i * ysize + j) + "></td>");
}
cout("</tr>");
}
cout("</table>");
}
</script>
<html>
C#版:需要创建几张外部图片
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Media;
class form : Form
{
int[,] a = new int[8, 2];
int x, y;//空格所在的位置
Random r = new Random();
static void Main()
{
Application.Run(new form());
}
form()
{
Text = "智能拼图 made by weidiao.neu";
ClientSize = new Size(200, 800);
MaximumSize = Size;
FormBorderStyle = FormBorderStyle.None;
Paint += draw;
KeyUp += key;
init();
}
void key(object o, KeyEventArgs e)
{
int p=-1, q=-1;
switch (e.KeyCode)
{
case Keys.Up: p = x+1; q = y; break;
case Keys.Down: p = x-1; q = y ; break;
case Keys.Left: p = x; q = y+1; break;
case Keys.Right: p = x ; q = y-1; break;
}
if (!valid(p, q)) return;
new SoundPlayer("move.wav").Play();
swap(ref a[x, y], ref a[p, q]);
x = p; y = q;
draw(null,null);
if (over()) win();
}
bool over()
{
int i, j;
for (i = 0; i < 8; i++)
for (j = 0; j < 2; j++)
if (a[i, j] != (i * 2 + j+1)%16)
return false;
return true;
}
void win()
{
new SoundPlayer("win.wav").Play();
DialogResult o = MessageBox.Show("您赢了,好牛逼的样子.", "就是吊", MessageBoxButtons.RetryCancel);
if (o == DialogResult.Retry)
{
init();
draw(null, null);
}
else Application.Exit();
}
void draw(object o, EventArgs e)
{
int w = ClientSize.Width/2;
int h = ClientSize.Height/8;
Bitmap bit = new Bitmap(ClientSize.Width, ClientSize.Height);
Graphics.FromImage(bit).Clear(Color.AliceBlue);
int i,j;
for (i = 0; i < 8; i++)
for (j = 0; j < 2; j++)
Graphics.FromImage(bit).DrawImage(Image.FromFile(a[i, j].ToString() + ".bmp"), new Rectangle(new Point(j * w,i * h), new Size(w - 1, h - 1)));
CreateGraphics().DrawImage(bit,0,0);
}
void init()
{
x = 7;
y = 1;
int i, j;
for (i = 0; i < 8; i++)
for (j = 0; j < 2; j++)
a[i,j] = i * 2 + j+1;
a[7, 1] = 0;
shuffle0();
//shuffle1();
}
bool valid(int x,int y)
{
return x >= 0 && y >= 0 && x < 8 && y < 2;
}
void swap(ref int m, ref int n)
{
int i = m;
m = n;
n = i;
}
//shuffle0用于洗牌,也能保证有解
//这是一种巧妙的算法,只要保证交换偶数次就一定有解
//只要交换奇数次,就一定无解.
//交换时不允许动空格
void shuffle0()
{
int i;
int p, q;
for (i = 0; i < 800; i++)
{
p = r.Next() % 15;
q = r.Next()% 15;
if (p == q) {
i--;
continue;
}
swap(ref a[p / 2, p % 2], ref a[q / 2, q % 2]);
}
}
//shuffle1也用于洗牌,能保证绝对有解
void shuffle1()
{
int i;
int p=-1, q=-1;
for (i = 0; i < 500; i++)
{
switch (r.Next()%4)
{
case 0: p = x + 1; q = y; break;
case 1: p = x - 1; q = y; break;
case 2: p = x; q = y + 1; break;
case 3: p = x; q = y - 1; break;
}
if (!valid(p, q)) continue ;
swap(ref a[x, y], ref a[p, q]);
x = p; y = q;
}
}
}