我正在使用2x2主板进行游戏,该主板将扩展至7x6。
我现在正在进行获胜检测,但我认为我正在做很长的路。必须有一个更短的方式。
winn
这是游戏板的照片:
这就是我目前正在检测胜利者的方式
if (btns[0, col].BackColor.Equals(Color.Red) && btns[1, col].BackColor.Equals(Color.Red))
{
MessageBox.Show("Red Win");
}
if (btns[0, col].BackColor.Equals(Color.Blue) && btns[1, col].BackColor.Equals(Color.Blue))
{
MessageBox.Show("Blue Win");
}
这种方式似乎我必须列出所有组合,当我扩展到7x6时它不是很理想。
这是程序的整个代码
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Button[,] btns;
public Form1()
{
InitializeComponent();
btns = new Button[,] { { button2 , button1 },
{ button4 , button3 }};
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (var btn in btns)
{
btn.Enabled = false;
}
}
int cc = 0;
private void button5_Click(object sender, EventArgs e)
{
// Button[] row1 = new Button[] {button2, button1};
for (int col = 0; col < btns.GetLength(1); ++col)
{
var btn = btns[0, col];
if (!btn.Enabled)
{
btn.Enabled = true;
if (cc == 0)
{
cc = 1;
btn.BackColor = Color.Red;
}
else
{
cc = 0;
btn.BackColor = Color.Blue;
}
if (btns[0, col].BackColor.Equals(Color.Red) && btns[1, col].BackColor.Equals(Color.Red))
{
MessageBox.Show("Red Win");
}
if (btns[0, col].BackColor.Equals(Color.Blue) && btns[1, col].BackColor.Equals(Color.Blue))
{
MessageBox.Show("Blue Win");
}
return;
}
}
}
private void button6_Click(object sender, EventArgs e)
{
// Button[] row2 = new Button[] { button4, button3 };
for (int col = 0; col < btns.GetLength(1); ++col)
{
var btn = btns[1, col];
if (!btn.Enabled)
{
btn.Enabled = true;
if (cc == 0)
{
cc = 1;
btn.BackColor = Color.Red;
}
else
{
cc = 0;
btn.BackColor = Color.Blue;
}
if (btns[1, col].BackColor.Equals(Color.Red) && btns[0, col].BackColor.Equals(Color.Red))
{
MessageBox.Show("Red Win");
}
if (btns[1, col].BackColor.Equals(Color.Blue) && btns[0, col].BackColor.Equals(Color.Blue))
{
MessageBox.Show("Blue Win");
}
return;
}
}
}
}
}
我尝试了很多其他方法,但似乎无法让它发挥作用。
答案 0 :(得分:2)
也许这个答案很复杂,而且我会得到许多downvotes但我无法抗拒解决这个问题尽可能最佳。尝试检查此代码的详细信息:
int n; //dimension of the matrix
Button [,] btns;
public Form1()
{
InitializeComponent();
n = 2;/*You should set here the dimension of your matix. I considered it nxn because of diagonals. If you want nxm matrix than the code is a little bit complicated but not too much*/
btns = new Button[n, n];
for(int i = 0;i<n;i++)
for(int j = 0; j<n; j++)
{
Button btn = new Button();
btn.Location = new Point(i*20,j*40);
btn.Size = new Size(18,38);
btns[i,j] = btn;
this.Controls.Add(btn);
}
}
private void button1_Click(object sender, EventArgs e)
{
int mainDiag = 0;
int secDiag = 0;
int i = 0;
int j = 0;
int [] cols = new int[n];
int winner = 0; //no winner
while(winner == 0 && i<n)
{
int row = 0;
j = 0;
while(j<n)
{
if (btns[i, j].BackColor == Color.Blue)
{
if (i == j)
mainDiag++;//inrement main diagonal
if(i + j == n-1)
secDiag++;//increment second diagonal
row++; //increment row
cols[i]++; //increment column
}
else if (btns[i, j].BackColor == Color.Red)
{
if (i == j)
mainDiag--;
if(i + j == n-1)
secDiag--;
row--;
cols[i]++;
}
j++;
}
if(row == n) //if row value == n whole row is blue and blue player wins
winner = 1;
else if(row == -n)
winner = -1; //if row value == -n whole row is red and red player wins
i++;
}
if(winner == 0)
{
if(mainDiag == n)
winner = 1; //similar for the diagonal
else if(mainDiag == -n)
winner = -1;
else if(secDiag == n)
winner = 1;//similar for the second diagonal
else if(secDiag == -n)
winner = -1;
else
{
i = 0;
while (winner == 0 && i < n)
{
if (cols[i] == n)
winner = 1; //i-th column is whole blue and blue player wins
else if (cols[i] == -n)
winner = -1; //i-th column is whole red and red player wins
}
}
}
if (winner == 1)
MessageBox.Show("Blue wins");
else if(winner == -1)
MessageBox.Show("Red wins");
}
答案 1 :(得分:1)
正如人们在评论中所说,你可能想要使用循环来做到这一点。
我不会在这里详细介绍,但我会描述一些算法来完成你的胜利条件检测。
算法1:
对于每个单元格,检测它是否是水平,垂直或对角线的一部分。其时间复杂度为O(n ^(3/2))。如果您愿意,您可以考虑重复检查,但您不需要。
算法2:
检查所有垂直行,所有水平行和所有对角线行,并查看这些行是否有任何获胜条件。该方法的时间复杂度应大致为O(n)。你可以通过不检查不足以保持胜利条件的对角行来节省一点时间,但你不必这样做。
答案 2 :(得分:1)
如果您想要一个非常基本的答案,我会考虑以下
将每一行添加到数组中
将每列添加到数组中
对于数组中的每个项目,如果它们都等于一种颜色,那么你就赢了
当您使用更大的数字(即6x6网格)时,您可能需要使用多维数组