單純是人和人用來玩用的,沒有連線、AI功能。
一打開程式的樣子
3連線了
完整程式碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication14
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
reSet();
}
//用來裝Button的限列
Button[] btn = new Button[25];
//用來新增賓果用的Button
Button newBtn;
//計算Button位置用
int count = 0;
//設算Buton在那一行
int column = 0;
//計算Button在那一列
int row = 0;
//是否做過第一次初始化(加入25個按鈕)
bool first_init = false;
//紀錄點下的按鈕
int[] click_btn = new int[25];
//是否已連線
bool haveline;
/// <summary>
/// 重設遊戲
/// </summary>
private void reSet()
{
haveline = false;
if (!first_init)
initButtion();
else
initButtionSecond();
getRandom();
}
/// <summary>
/// 第一次初始化Button陣列
/// </summary>
private void initButtion()
{
for (int i = 0; i < 25; i++)
{
Button newBtn = new Button();
newBtn.Location = new Point(12 + (column * 75), 75 + ((row - 1) * 75));
newBtn.Text = (count + 1).ToString();
newBtn.Size = new Size(75, 75);
newBtn.TabIndex = count + 1;
newBtn.Click += click_evn;
newBtn.Name = i.ToString();
btn[i] = newBtn;
this.Controls.Add(newBtn);
count += 1;
column += 1;
if (count % 5 == 0)
{
column = 0;
row += 1;
}
}
first_init = true;
}
//重設Button
private void initButtionSecond()
{
for (int i = 0; i < 25; i++)
{
btn[i].BackColor = default(Color);
btn[i].Enabled = true;
btn[i].UseVisualStyleBackColor = true;
click_btn[i] = 0;
}
}
//Button陣列按鈕事件
private void click_evn(object sender, EventArgs e)
{
if (!haveline)
{
Button btn = (Button)sender;
btn.Enabled = false;
click_btn[Convert.ToInt32(btn.Name)] = 1;
haveline = checkLink();
if (haveline)
MessageBox.Show("You Win");
}
}
//產生1~25不重覆亂數
private void getRandom()
{
Random r = new Random();
int[] num = new int[25];
for (int i = 0; i < 25; i++)
{
num[i] = i + 1;
}
int temp;
for (int i = 0; i < 25; i++)
{
int R = r.Next(25);
temp = num[i];
num[i] = num[R];
num[R] = temp;
}
for (int i = 0; i < 25; i++)
{
btn[i].Text = num[i].ToString();
}
}
/// <summary>
/// 檢查是否有連線
/// </summary>
/// <returns>true (3條連線) / false 未滿3條 </returns>
private bool checkLink()
{
//目前有幾條連線
int totalLine = 0;
//是否已達成3條連線
bool haveLine = false;
//判斷橫五公格
for (int i = 0; i < 5; i++)
{
if (click_btn[0 + i * 5].ToString() + click_btn[1 + i * 5].ToString() + click_btn[2 + i * 5].ToString() + click_btn[3 + i * 5].ToString() + click_btn[4 + i * 5].ToString() == "11111")
{
for (int j = 0; j < 5; j++)
{
btn[j + i * 5].BackColor = Color.Pink;
}
totalLine += 1;
}
}
//判斷直五公格
for (int i = 0; i < 5; i++)
{
if (click_btn[i].ToString() + click_btn[5 + i].ToString() + click_btn[10 + i].ToString() + click_btn[15 + i].ToString() + click_btn[20 + i].ToString() == "11111")
{
for (int j = 0; j < 5; j++)
{
btn[j * 5 + i].BackColor = Color.Pink;
}
totalLine += 1;
}
}
//判斷斜線\
if (click_btn[0].ToString() + click_btn[6].ToString() + click_btn[12].ToString() + click_btn[18].ToString() + click_btn[24].ToString() == "11111")
{
for (int j = 0; j < 5; j++)
{
btn[j * 6].BackColor = Color.Pink;
}
totalLine += 1;
}
//判斷斜線/
if (click_btn[4].ToString() + click_btn[8].ToString() + click_btn[12].ToString() + click_btn[16].ToString() + click_btn[20].ToString() == "11111")
{
for (int j = 1; j < 6; j++)
{
btn[j * 4].BackColor = Color.Pink;
}
totalLine += 1;
}
//是否達成3連線
if (totalLine >= 3)
haveLine = true;
return haveLine;
}
/// <summary>
/// 重設按鈕事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
reSet();
}
}
}
沒有留言:
張貼留言