我創建一個小遊戲就像遊戲黑白棋/奧賽羅我已成功地創建了一個2×3板與按鈕改變它。檢查按鈕的顏色,並使用2D陣列
的按鈕改變顏色的人你點擊它們,但我有麻煩來檢測是否有白色黑色顏色2之間,如果是更改白色變成黑色。我希望這是有意義的。這些按鈕位於二維數組中。任何建議,可以幫助我這樣做將不勝感激。
的圖像:
這裏是我的代碼:
![namespace reversitest
{
public partial class Form1 : Form
{
private Button\[,\] squares;
public Form1()
{
InitializeComponent();
squares = new Button\[3, 2\];
squares = new Button\[,\] {{button1, button2, button3},
{button4, button5, button6,}};
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (Button sqrr in squares)
{
sqrr.Click += new System.EventHandler(this.DrawCharacter);
}
}
int _turn = 0;
private void DrawCharacter(object sender, EventArgs e)
{
Button sqrr = (Button)sender;
int col = 0;
if (sqrr.BackColor.Equals(Color.Black) || sqrr.BackColor.Equals(Color.White))
{
MessageBox.Show("Move Not Allowed!");
}
else
{
for (int i = 0; i < squares.GetLongLength(1); ++i)
{
// check othere squares and change color
if (i < 2)
{
for (int f = 0; f < 3; ++f)
{
var ss = squares\[i, f\];
if (ss.BackColor.Equals(Color.Black))
{
MessageBox.Show("we have a black");
//ss = squares\[i, f+1\];
ss.BackColor = Color.Black;
}
else
{
MessageBox.Show("no black");
}
}
}
if (_turn == 0)
{
_turn = 1;
sqrr.BackColor = Color.Black;
}
else
{
_turn = 0;
sqrr.BackColor = Color.White;
}
}
}
}
}
}
那是董事會規模將是固定的(2×3),或有什麼不同? –
@NewDeveloper它會成長爲一個8×8 – Tacit