摘要:消消樂 遊戲 (三個消掉)Part 3/2
很高興糾結問題終於解決了!!!!
不過請大家別介意我在上班
的時間寫這東東,因為有時候寫程式寫累了,你又不能亂上網,那麼寫寫小程式可以激發腦力,
總比上網去有的沒的網站好多了吧XDDDD
也希望這樣子可以讓自己腦袋更清醒~
終於明白:標記過的不能再標記
這個道理,
我的程式只要改成這樣就OK了:
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;
using ThreeToDisppear.Properties;
namespace ThreeToDisppear
{
public partial class Form1 : Form
{
int[,] pbArray = new int[8, 8];
PictureBox[,] pbControl = new PictureBox[8, 8];
#region forms
public Form1()
{
InitializeComponent();
Random gen = new Random(Guid.NewGuid().GetHashCode());
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
pbArray[i, j] = gen.Next(1, 5);
}
}
#region pbControls
pbControl[0, 0] = pb11;
pbControl[0, 1] = pb12;
pbControl[0, 2] = pb13;
pbControl[0, 3] = pb14;
pbControl[0, 4] = pb15;
pbControl[0, 5] = pb16;
pbControl[0, 6] = pb17;
pbControl[0, 7] = pb18;
pbControl[1, 0] = pb21;
pbControl[1, 1] = pb22;
pbControl[1, 2] = pb23;
pbControl[1, 3] = pb24;
pbControl[1, 4] = pb25;
pbControl[1, 5] = pb26;
pbControl[1, 6] = pb27;
pbControl[1, 7] = pb28;
pbControl[2, 0] = pb31;
pbControl[2, 1] = pb32;
pbControl[2, 2] = pb33;
pbControl[2, 3] = pb34;
pbControl[2, 4] = pb35;
pbControl[2, 5] = pb36;
pbControl[2, 6] = pb37;
pbControl[2, 7] = pb38;
pbControl[3, 0] = pb41;
pbControl[3, 1] = pb42;
pbControl[3, 2] = pb43;
pbControl[3, 3] = pb44;
pbControl[3, 4] = pb45;
pbControl[3, 5] = pb46;
pbControl[3, 6] = pb47;
pbControl[3, 7] = pb48;
pbControl[4, 0] = pb51;
pbControl[4, 1] = pb52;
pbControl[4, 2] = pb53;
pbControl[4, 3] = pb54;
pbControl[4, 4] = pb55;
pbControl[4, 5] = pb56;
pbControl[4, 6] = pb57;
pbControl[4, 7] = pb58;
pbControl[5, 0] = pb61;
pbControl[5, 1] = pb62;
pbControl[5, 2] = pb63;
pbControl[5, 3] = pb64;
pbControl[5, 4] = pb65;
pbControl[5, 5] = pb66;
pbControl[5, 6] = pb67;
pbControl[5, 7] = pb68;
pbControl[6, 0] = pb71;
pbControl[6, 1] = pb72;
pbControl[6, 2] = pb73;
pbControl[6, 3] = pb74;
pbControl[6, 4] = pb75;
pbControl[6, 5] = pb76;
pbControl[6, 6] = pb77;
pbControl[6, 7] = pb78;
pbControl[7, 0] = pb81;
pbControl[7, 1] = pb82;
pbControl[7, 2] = pb83;
pbControl[7, 3] = pb84;
pbControl[7, 4] = pb85;
pbControl[7, 5] = pb86;
pbControl[7, 6] = pb87;
pbControl[7, 7] = pb88;
#endregion
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
switch (pbArray[i, j])
{
case 1:
pbControl[i, j].Image = Resources.red;
break;
case 2:
pbControl[i, j].Image = Resources.green;
break;
case 3:
pbControl[i, j].Image = Resources.blue;
break;
case 4:
pbControl[i, j].Image = Resources.yellow;
break;
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
#endregion
bool[,] isSame = new bool[8, 8];
int row = 0;
int col = 0;
private void PB_Click(object sender, EventArgs e)
{
var pb = sender as PictureBox;
if (pb != null)
{
row = Convert.ToInt16(pb.Tag.ToString().Substring(0, 1))-1;
col = Convert.ToInt16(pb.Tag.ToString().Substring(1, 1))-1;
#region three to dispeared
//三個以上連在一起的判斷
isSame[row, col] = true;
isConnectedTop(row, col);
isConnectedLeft(row, col);
isConnectedDown(row, col);
isConnectedRight(row, col);
int isThreeConnect = connect1 + connect2 + connect3 + connect4;
if (isThreeConnect >= 2)
{
ToDisppeared();
}
else
{
connect1 = 0;
connect2 = 0;
connect3 = 0;
connect4 = 0;
}
#region 歸零
//歸零,給下一個用
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
isSame[i, j] = false;
}
}
connect1 = 0;
connect2 = 0;
connect3 = 0;
connect4 = 0;
row = 0;
col = 0;
#endregion
#endregion
}
}
private void ToDisppeared()
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (isSame[i, j] == true)
{
//標記為true的就消失
pbControl[i, j].Visible = false;
}
}
}
}
int connect1 = 0;
private void isConnectedTop(int rows, int cols)
{
if (rows - 1 >= 0)
{
if (isSame[rows - 1, cols] != true)
{
if (pbArray[rows - 1, cols] == pbArray[row, col])
{
//上方
isSame[rows - 1, cols] = true;
connect1++;
isConnectedTop(rows - 1, cols);
isConnectedLeft(rows - 1, cols);
isConnectedRight(rows - 1, cols);
}
}
}
}
int connect2 = 0;
private void isConnectedLeft(int rows, int cols)
{
if (cols - 1 >= 0)
{
if (isSame[rows, cols - 1] != true)
{
if (pbArray[rows, cols - 1] == pbArray[row, col])
{
//左方
isSame[rows, cols - 1] = true;
connect2++;
isConnectedTop(rows, cols - 1);
isConnectedLeft(rows, cols - 1);
isConnectedDown(rows, cols - 1);
}
}
}
}
int connect3 = 0;
private void isConnectedDown(int rows, int cols)
{
if (rows + 1 <= 7)
{
if (isSame[rows + 1, cols] != true)
{
if (pbArray[rows + 1, cols] == pbArray[row, col])
{
//下方
isSame[rows + 1, cols] = true;
connect3++;
isConnectedLeft(rows + 1, cols);
isConnectedDown(rows + 1, cols);
isConnectedRight(rows + 1, cols);
}
}
}
}
int connect4 = 0;
private void isConnectedRight(int rows, int cols)
{
if (cols + 1 <= 7)
{
if (isSame[rows, cols + 1] != true)
{
if (pbArray[rows, cols + 1] == pbArray[row, col])
{
//右方
isSame[rows, cols + 1] = true;
connect4++;
isConnectedTop(rows, cols + 1);
isConnectedDown(rows, cols + 1);
isConnectedRight(rows, cols + 1);
}
}
}
}
}
}
也就是如果isSame已經是true了,那當然就不用管他啦!
這次是把所有程式貼出來,
請小心取用
那麼就可以思考讓圖案"掉下來"的部份囉~
到目前為止還滿意的已經可以把它分享給你的朋友玩玩囉^^
(雖然只有一次性,還沒到判斷是否都點完)