大家好我需要一些帮助,
我正在制作一种“生命游戏”游戏。无论如何,如果当前单元格的邻居与我当前单元格的值相同,我想使用网格视图进行检查。如果是,我想检查空白区域(当前单元格周围)并添加邻居的值但是带有额外的字符。
这是我的代码:
public void check()
{
Textual txt = new Textual();
for (int x = 0; x < dataGridView1.Rows.Count; x++)
{
for (int y = 0; y < dataGridView1.Rows[x].Cells.Count; y++)
{
if (dataGridView1.Rows[x].Cells[y].Value == "VS" ||
dataGridView1.Rows[x].Cells[y].Value == "SL" ||
dataGridView1.Rows[x].Cells[y].Value == "KG" ||
dataGridView1.Rows[x].Cells[y].Value == "KN")
{
if (y - 1 >= 0 &&
y + 1 < dataGridView1.ColumnCount &&
x - 1 >= 0 && x + 1 < dataGridView1.RowCount &&
y + 1 < dataGridView1.ColumnCount &&
x + 1 < dataGridView1.RowCount)
{
string value = dataGridView1.Rows[x].Cells[y].Value.ToString();
int j;
int k;
switch (value)
{
case "VS":
if (dataGridView1.Rows[x].Cells[y + 1].Value == "")
{
dataGridView1.Rows[x].Cells[y + 1].Value = "VS++";
j = x;
k = y + 1;
int[,] temp= new int[j, k];
or.oznaka = "VS";
popis.TryAdd(temp, or);
dataGridView1.Rows[x].Cells[y + 1].Style.BackColor = System.Drawing.Color.Red;
}
else if (dataGridView1.Rows[x - 1].Cells[y].Value == "")
{
dataGridView1.Rows[x - 1].Cells[y].Value = "VS++";
j = x - 1;
k = y;
int[,] temp= new int[j, k];
or.oznaka = "VS";
popis.TryAdd(temp, or);
dataGridView1.Rows[x - 1].Cells[y].Style.BackColor = System.Drawing.Color.Yellow;
}
else if (dataGridView1.Rows[x + 1].Cells[y].Value == "")
{
dataGridView1.Rows[x + 1].Cells[y].Value = "VS++";
j = x + 1;
k = y;
int[,] temp= new int[j, k];
or.oznaka = "VS";
popis.TryAdd(temp, or);
dataGridView1.Rows[x + 1].Cells[y].Style.BackColor = System.Drawing.Color.Gold;
}
else if (dataGridView1.Rows[x + 1].Cells[y + 1].Value == "")
{
dataGridView1.Rows[x + 1].Cells[y + 1].Value = "VS+";
j = x - 1;
k = y + 1;
int[,] temp= new int[j, k];
or.oznaka = "VS";
popis.TryAdd(temp, or);
dataGridView1.Rows[x + 1].Cells[y + 1].Style.BackColor = System.Drawing.Color.Green;
}
else if (dataGridView1.Rows[x - 1].Cells[y - 1].Value == "")
{
dataGridView1.Rows[x - 1].Cells[y - 1].Value = "VS+";
j = x - 1;
k = y - 1;
int[,] temp= new int[j, k];
or.oznaka = "VS";
popis.TryAdd(temp,or);
dataGridView1.Rows[x - 1].Cells[y - 1].Style.BackColor = System.Drawing.Color.Purple;
}
else if (dataGridView1.Rows[x - 1].Cells[y + 1].Value == "")
{
dataGridView1.Rows[x - 1].Cells[y + 1].Value = "VS+";
j = x - 1;
k = y + 1;
int[,] temp= new int[j, k];
or.oznaka = "VS";
popis.TryAdd(temp, or);
dataGridView1.Rows[x - 1].Cells[y + 1].Style.BackColor = System.Drawing.Color.HotPink;
}
else if (dataGridView1.Rows[x].Cells[y - 1].Value == "")
{
dataGridView1.Rows[x].Cells[y - 1].Value = "VS+";
j = x;
k = y - 1;
int[,] temp= new int[j, k];
or.oznaka = "VS";
popis.TryAdd(temp, or);
dataGridView1.Rows[x].Cells[y - 1].Style.BackColor = System.Drawing.Color.Lavender;
}
else if (dataGridView1.Rows[x + 1].Cells[y - 1].Value == "")
{
dataGridView1.Rows[x + 1].Cells[y - 1].Value = "VS+";
j = x + 1;
k = y - 1;
int[,] temp= new int[j, k];
or.oznaka = "VS";
popis.TryAdd(temp, or);
dataGridView1.Rows[x + 1].Cells[y - 1].Style.BackColor = System.Drawing.Color.LightBlue;
}
break;
}
}
}
}
}
}
答案 0 :(得分:3)
这是一个可用于查找匹配邻居的函数。如果你选择二维数组而不是网格,它仍然会非常相似,这将是最好的解决方案,因为控制应该用于与人类交互,数据应该被操纵而不管呈现方式。
要找到给定单元格的匹配值(注意x和y - 你改变了它们的通常含义):
cell = FindCell(new Point(x, y), dataGridView1.Rows[y].Cells[x]);
找到空单元格:
cell = FindCell(new Point(x, y), string.Empty);
编辑:更好的用法示例
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.Value != null && cell.Value.ToString() != string.Empty)
{
DataGridViewCell neighbour = FindCell(new Point(cell.ColumnIndex, row.Index), cell.Value.ToString());
// Found
if (neighbour != null)
{
DataGridViewCell emptyCell = FindCell(new Point(cell.ColumnIndex, row.Index), string.Empty);
if (emptyCell != null)
{
emptyCell.Value = "WHATEVERYOUREQUIREITTOBE";
}
}
}
}
}
编辑2:我从您的代码中假设您的单元格将具有非空值。事实并非如此,请将每个cell.Value.ToString()替换为(cell.Value ?? string.Empty).ToString(),就像我在示例中所做的那样。
确保找到一个单元格后测试null。这是功能:
/// <summary>
/// List of locations around given location. Add to previous value to get next location.
/// </summary>
Point[] neighbours = new Point[]
{
new Point (-1, -1),
new Point (1, 0),
new Point (1, 0),
new Point (-2, 1),
new Point (2, 0),
new Point (-2, 1),
new Point (1, 0),
new Point (1, 0),
};
/// <summary>
/// Finds a cell containing given string value.
/// </summary>
/// <param name="location">Point of search</param>
/// <param name="value">Value to find</param>
/// <returns>Cell containing given value</returns>
DataGridViewCell FindCell(Point location, string value)
{
for (int i = 0; i < neighbours.Length; ++i)
{
// Move location to new point
location.Offset(neighbours[i]);
// Check boundaries
if (location.Y >= 0 && location.Y < dataGridView1.RowCount
&& location.X >= 0 && location.X < dataGridView1.Columns.Count)
{
// Get cell
DataGridViewCell cell = dataGridView1.Rows[location.Y].Cells[location.X];
// If value matches
if ((cell.Value ?? string.Empty).ToString() == value)
{
return cell;
}
}
}
// No match
return null;
}