JavaScript拼图小游戏

<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;
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值