Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.

Học lập trình với Jason - Bài 2

Go down

Học lập trình với Jason - Bài 2 Empty Học lập trình với Jason - Bài 2

Bài gửi by jasonsmith2812 3/2/2010, 09:34

Bài này làm trò chơi xếp số, mình không làm hướng đối tượng

Phần Design : có 3 textbox (Dòng, cột, kích thước), 2 button (Play và Result)

Phần code :

private int dong = 5;
private int cot = 4;
private int _size = 40;
private Button[,] arrBtn;

public Tên class()
{
InitializeComponent();
}

-- Method khởi tạo random mảng button --
private void InitBtnNumber(Panel panelContain)
{
arrBtn = new Button[dong, cot];
panelContain.Controls.Clear();

List<int> listPure = new List<int>();
for (int i = 1; i <= dong * cot; i++)
listPure.Add(i);
List<int> listRan = RandomList(listPure);
listRan.Remove(listRan.Count);
int index = 0;
for (int i = 0; i < dong; i++)
{
for (int j = 0; j < cot; j++)
{
arrBtn[i, j] = new Button();
if (index < listRan.Count)
arrBtn[i, j].Text = listRan[index].ToString();
arrBtn[i, j].Size = new Size(_size, _size);
arrBtn[i, j].Location = new Point(j * _size, i * _size);
arrBtn[i, j].Click += new EventHandler(DoiCho);
panelContain.Controls.Add(arrBtn[i, j]);
index++;
}
}
}

-- Kiểm tra mảng này sau khi sắp xếp đúng hay sai
private void btnResult_Click(object sender, EventArgs e)
{
int index = 1;
for (int i = 0; i < dong; i++)
{
for (int j = 0; j < cot; j++)
{
Button btn = (Button)arrBtn.GetValue(i, j);
if (!index.ToString().Equals(btn.Text) && !btn.Text.Equals(""))
{
MessageBox.Show("Sai");
return;
}
index++;
}
}
MessageBox.Show("Dung");
}

public List<int> RandomList(List<int> listInput)
{
List<int> listRan = new List<int>();
for (int i = listInput.Count; i > 0; i--)
{
int ran = new Random().Next(i);
listRan.Add(listInput[ran]);
listInput.RemoveAt(ran);
}
return listRan;
}

-- Event kiểm tra lúc đổi chỗ các ô --
private void DoiCho(object sender, EventArgs e)
{
if (((Button)sender).Text.Equals(""))
return;
else
{
for (int i = 0; i < dong; i++)
{
for (int j = 0; j < cot; j++)
{
Button btn = (Button)arrBtn.GetValue(i, j);
Button empUp = new Button() { Text = "Loc" }
, empLeft = new Button() { Text = "Loc" }
, empDown = new Button() { Text = "Loc" }
, empRight = new Button() { Text = "Loc" };

if (i > 0) empUp = (Button)arrBtn.GetValue(i - 1, j);
if (j > 0) empLeft = (Button)arrBtn.GetValue(i, j - 1);
if (i < dong - 1) empDown = (Button)arrBtn.GetValue(i + 1, j);
if (j < cot - 1) empRight = (Button)arrBtn.GetValue(i, j + 1);

if (btn.Text.Equals((((Button)sender).Text)) && (empUp.Text == ""
|| empLeft.Text == "" || empDown.Text == "" || empRight.Text == ""))
{
string temp = btn.Text;
btn.Text = "";
if (empUp.Text == "") empUp.Text = temp;
if (empLeft.Text == "") empLeft.Text = temp;
if (empDown.Text == "") empDown.Text = temp;
if (empRight.Text == "") empRight.Text = temp;
}
}
}
}
}

private void btnPlay_Click(object sender, EventArgs e)
{
-- ép kiểu và gắn giá trị cho biến private --
Int32.TryParse(txtDong.Text, out dong);
Int32.TryParse(txtCot.Text, out cot);
Int32.TryParse(txtSize.Text, out _size);

InitBtnNumber(panelMain);
}

Chúc các bạn chơi thắng game này, mình làm xong mà chơi hoài ko thắng (pó tay)
jasonsmith2812
jasonsmith2812
Khởi đầu
Khởi đầu

Tổng số bài gửi : 48
Join date : 18/09/2009

Về Đầu Trang Go down

Về Đầu Trang

- Similar topics

 
Permissions in this forum:
Bạn không có quyền trả lời bài viết