Học lập trình với Jason - Bài 2
:: Góc Học Tập :: Lập trình mạng
Trang 1 trong tổng số 1 trang
Học lập trình với Jason - Bài 2
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)
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- Khởi đầu
- Tổng số bài gửi : 48
Join date : 18/09/2009
Similar topics
» Học lập trình với Jason - Bài 1
» Học lập trình với Jason - Bài 3
» Học lập trình với Jason - Bài 4
» Lập trình C từ A đến Z
» Bài thuyết trình bài tập 1
» Học lập trình với Jason - Bài 3
» Học lập trình với Jason - Bài 4
» Lập trình C từ A đến Z
» Bài thuyết trình bài tập 1
:: Góc Học Tập :: Lập trình mạng
Trang 1 trong tổng số 1 trang
Permissions in this forum:
Bạn không có quyền trả lời bài viết
|
|
10/12/2020, 01:06 by Ti3uThucodon93
» Tiểu thuyết : Yêu anh là sai lầm của cuộc đời em
24/2/2019, 11:49 by Ti3uThucodon93
» Tiểu thuyết : Yêu sai thời điểm
23/2/2019, 16:39 by Ti3uThucodon93
» Thơ Trung Quốc !
15/10/2018, 10:05 by Ti3uThucodon93
» Câu nói hay !
22/1/2017, 15:59 by Ti3uThucodon93
» Thơ tình dài !
15/11/2016, 11:45 by Ti3uThucodon93
» Thơ 4 câu !
1/9/2016, 16:14 by Ti3uThucodon93
» Thơ hài hước !
9/8/2016, 09:04 by Ti3uThucodon93
» Cách chữa trị bệnh hôi nách dành cho sinh viên
30/7/2016, 16:02 by thammybacsihathanh