XAML을 사용하여 Conway의 Game of Life를 C#으로 만들려고합니다. 창을 사용하면 슬라이더를 사용하여 2D 셀 배열의 행 및 열 수를 지정할 수 있습니다. 내 Uniform Grid가 완벽한 정사각형 (10x10, 20x20 또는 심지어 16x16) 일 때, 시뮬레이션은 문제없이 작동합니다. 그러나 사용자가 직사각형의 균일 한 격자 (13x14, 15x26, 24x14)를 지정하려고하면 셀은 차이 (즉, 33x27 격자, 차이 = 6)로 던져 지므로 셀은 적절하게 위로 이동하지만 꺼집니다 (왼쪽/오른쪽) 차이로). 나는 이것이 X 축에서만 발생한다는 것을 좁혔다. 셀은 결코 y 축에서 벗어나지 않습니다. 질문 : 내 배열이 왜 내 x 축에서 벗어나니까? 거기에 문제가 있습니까?Conway의 Game of Life 2D 배열이 일치하지 않습니다
제가 말할 수있는 한, 모든 것이 잘 작동합니다. 내 2D 배열과 제복 그리드의 치수를 확인하기 위해 로그를 설정했습니다. 나는 무엇이 잘못 되었는가에 대해 확신하지 못한다. 그리고 나는 문자 그대로의 날을 바라보고 있었고 디버깅을 해왔다. 나는 끝내 준다. 제발 도와주세요. 나는 단순히 잡히지 않는 뭔가가 있기를 바랍니다.
코드 범례 : unigridOfCells는 XAML의 통일 그리드입니다. slideWidth/slideHeight는 슬라이더입니다. 또한, 내 isAlive 속성을 SolidColorBrush로 변환하는 리소스의 변환기를 사용하고 있습니다.
private Cell[,] cells;
private Cell[,] nextGenCells;
private int codeColumn, codeRow, difference, secondDiff;
public MainWindow()
{
InitializeComponent();
unigridOfCells.Height = 500;
unigridOfCells.Width = 500;
setCellsOnGrid(10, 10);
}
//Sets all the cells on the grid, as well as setting the number of columns and rows to be reset for all arrays in the application
public void setCellsOnGrid(int column, int row)
{
unigridOfCells.Rows = row;
unigridOfCells.Columns = column;
codeColumn = column;
codeRow = row;
time = new Timer(3000);
cells = new Cell[codeColumn, codeRow];
nextGenCells = new Cell[codeColumn, codeRow];
for (int i = 0; i < codeColumn; i++)
{
for (int j = 0; j < codeRow; j++)
{
cells[i, j] = new Cell();
Rectangle block = new Rectangle();
block.Height = 10;
block.Width = 10;
block.DataContext = cells[i, j];
block.MouseLeftButtonDown += cells[i, j].ParentClicked;
//block.MouseLeftButtonDown += blockSpace;
Binding b = new Binding();
b.Source = cells[i, j];
b.Path = new PropertyPath("isAlive");
b.Converter = (BoolColorConverter)Application.Current.FindResource("cellLifeSwitch");
block.SetBinding(Rectangle.FillProperty, b);
unigridOfCells.Children.Add(block);
}
}
}
public void blockSpace(object sender, MouseButtonEventArgs e)
{
int spot = 0;
int pick = 0;
for (int i = 0; i < codeColumn; i++)
{
for (int j = 0; j < codeRow; j++)
{
spot = unigridOfCells.Children.IndexOf((Rectangle)sender);
}
}
MessageBox.Show("" + spot + " : " + pick);
}
//Updates the cells. This is where the rules are applied and the isAlive property is changed (if it is).
public void updateCells()
{
for (int n = 0; n < codeColumn; n++)
{
for (int m = 0; m < codeRow; m++)
{
nextGenCells[n, m] = new Cell();
bool living = cells[n, m].isAlive;
int count = GetLivingNeighbors(n, m);
bool result = false;
if (living && count < 2)
{
result = false;
}
if (living && (count == 2 || count == 3))
{
result = true;
}
if (living && count > 3)
{
result = false;
}
if (!living && count == 3)
{
result = true;
}
nextGenCells[n, m].isAlive = result;
}
}
setNextGenCells();
}
//Resets all the cells in a time step
public void setNextGenCells()
{
for (int f = 0; f < codeColumn; f++)
{
for (int k = 0; k < codeRow; k++)
{
cells[f, k].isAlive = nextGenCells[f, k].isAlive;
}
}
}
//Checks adjacent cells to the cell in the position that was passed in
public int GetLivingNeighbors(int x, int y)
{
int count = 0;
// Check cell on the right.
if (x != codeColumn - 1)
if (cells[x + 1, y].isAlive)
count++;
// Check cell on the bottom right.
if (x != codeColumn - 1 && y != codeRow - 1)
if (cells[x + 1, y + 1].isAlive)
count++;
// Check cell on the bottom.
if (y != codeRow - 1)
if (cells[x, y + 1].isAlive)
count++;
// Check cell on the bottom left.
if (x != 0 && y != codeRow - 1)
if (cells[x - 1, y + 1].isAlive)
count++;
// Check cell on the left.
if (x != 0)
if (cells[x - 1, y].isAlive)
count++;
// Check cell on the top left.
if (x != 0 && y != 0)
if (cells[x - 1, y - 1].isAlive)
count++;
// Check cell on the top.
if (y != 0)
if (cells[x, y - 1].isAlive)
count++;
// Check cell on the top right.
if (x != codeColumn - 1 && y != 0)
if (cells[x + 1, y - 1].isAlive)
count++;
return count;
}
//Fires when the next generation button is clicked. Simply makes the board go through the algorithm
private void nextGenerationClick(object sender, RoutedEventArgs e)
{
updateCells();
}
//Fired when the "Reset Grid" button is pressed, resets EVERYTHING with the new values from the sliders
private void resetGrid(object sender, RoutedEventArgs e)
{
MessageBox.Show("First Slide (width) value: " + slideWidth.Value + "\nSecond Slide (length) value: " + slideHeight.Value + "\nDifference: " + (codeColumn - codeRow) + "\nColumns: " + unigridOfCells.Columns + " \nRows: " + unigridOfCells.Rows + "\nChildren count: " + unigridOfCells.Children.Count + " \nLengths: "
+ "\n\tOf 1D of cells: " + cells.GetLength(0) + "\n\tOf 1D of nextGenCells: " + nextGenCells.GetLength(0) + "\n\tUniform Grid Columns: " + unigridOfCells.Columns + " \nWidths: "
+ "\n\tOf 2D of cells: " + cells.GetLength(1) + "\n\tOf 2D of nextGenCells: " + nextGenCells.GetLength(1) + "\n\tUniform Grid Rows: " + unigridOfCells.Rows);
unigridOfCells.Children.Clear();
setCellsOnGrid((int)slideWidth.Value, (int)slideHeight.Value);
}
안녕하세요 Carlos. 우연히도, 나는 C#/XAML/WPF의 삶에 대한 데모에 대해 최근에 [질문 [질문]했다. (http://stackoverflow.com/questions/16642461/brushes-white-slows-graphics-demo-down). 전체 코드가 게시되면 다른 접근 방식에 대해 궁금합니다. – dharmatech
몇 장의 스크린 샷이 도움이 될 것입니다. – RBarryYoung
'unigridOfCells.Rows = column;'과'unigridOfCells.Columns = row;'주위에'uniGridOfCells'에 대한 행/열 값을 교환 해보십시오. 이것은 x/y 논리를 혼란스럽게 만드는 고전적인 경우입니다 (스크린 샷은 RBarryYoung이 말한 것처럼 좋을 것입니다.) - 이것이 전체적인 문제인지 아닌지 여부는 현재 첫 번째 열을 먼저 추가하는 것이고 첫 번째 행을 채우는 것입니다 (그리고 ' t는 꽤 맞습니다.) 정상적으로 채워진 다음, 다음 행 등에 등을 맞 춥니 다. – VisualMelon