// 目前0位在座標[3,4]上面 將與位在座標[3,3]的20交換位置
==============================================================================================
// 兩個數字0和20 成功執行位置交換的程式指令 目前0位在座標[3,3] 而20位在座標[3,4]
==============================================================================================
※數字推盤遊戲_進度:
「數字推盤遊戲」的規格不論是3X3,4X4,5X5…等,各種類型的推盤都符合一項要件,那就是他們都擁有一個1x1的空缺,好讓各個數字能藉著這個空缺穿梭在推盤的每個角落。而在我利用C Sharp所呈現的數字推盤遊戲中,數字0所象徵的正是那1x1的空缺。現在我已經成功讓推盤上的數字打亂了,所以接下來我們要面臨的難題就是該如何讓0周邊(上、下、左、右)的任意數字與0作“交換”的動作?
==============================================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Button[,] buttons = new Button[30, 30];
int[] array = new int[30];
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 25; i++)
{
array[i] = i;
}
/* for (int j = 1; j < 10; j++)
{
buttons[1, j] = new Button();
buttons[1, j].Location = new Point(j * 50, 50);
buttons[1, j].Size = new Size(50, 50);
buttons[1, j].Text = array[j].ToString();
this.Controls.Add(buttons[1, j]);
} */
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
buttons[i, j] = new Button();
buttons[i, j].Location = new Point(j * 50, i * 50);
buttons[i, j].Size = new Size(50, 50);
buttons[i, j].Text = j.ToString();
this.Controls.Add(buttons[i, j]);
buttons[i, j].Click += new EventHandler(Button1_Click);
}
}
}
private void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (buttons[i, j].Text == "0")
{
MessageBox.Show("i=" + i + ",j=" + j + "" + buttons[i, j].Text);
}
}
}
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (sender == buttons[i, j])
{
movement(i, j, buttons[i, j].Text);
}
}
}
}
private void movement(int i, int j, string s)
{
//MessageBox.Show("i=" + i + ",j=" + j + "" + s);
//MessageBox.Show("i=" + i + ",j-1=" + (j - 1) + "" + buttons[i, j - 1].Text);
String stmp;
if (buttons[i, j - 1].Text == "0")
{
stmp = buttons[i, j].Text;
buttons[i, j].Text = buttons[i, j - 1].Text;
buttons[i, j - 1].Text = stmp;
}
else if (buttons[i, j + 1].Text == "0")
{
stmp = buttons[i, j].Text;
buttons[i, j].Text = buttons[i, j + 1].Text;
buttons[i, j + 1].Text = stmp;
}
else if (buttons[i - 1, j].Text == "0")
{
stmp = buttons[i, j].Text;
buttons[i, j].Text = buttons[i - 1, j].Text;
buttons[i - 1, j].Text = stmp;
}
else if (buttons[i + 1, j].Text == "0")
{
stmp = buttons[i, j].Text;
buttons[i, j].Text = buttons[i + 1, j].Text;
buttons[i + 1, j].Text = stmp;
}
}
private void button1_Click(object sender, EventArgs e)
{
int d1, tmp, k;
Random irand = new Random();
d1 = irand.Next(1, 26);
label1.Text = d1.ToString();
for (int j = 1; j < 26; j++)
{
k = 25 - j + 1;
tmp = array[d1];
array[d1] = array[k];
array[k] = tmp;
}
//buttons[1, 1].Text = array[1].ToString();
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
/* buttons[i, j] = new Button();*/
buttons[i, j].Location = new Point(j * 50, i * 50);
buttons[i, j].Size = new Size(50, 50);
buttons[i, j].Text = array[i*5+j+1].ToString();
this.Controls.Add(buttons[i, j]);
}
}
/* for (int j = 1; j < 10; j++)
{
// buttons[1, j] = new Button();
buttons[1, j].Location = new Point(50 * j, 50);
buttons[1, j].Text = array[j].ToString();
this.Controls.Add(buttons[1, j]);
} */
label2.Text = array[d1].ToString();
}
}
}
留言列表