Exercise 2 Guidelines This assignment is to familiarize yourself with WPF Applic
ID: 3662701 • Letter: E
Question
Exercise 2 Guidelines This assignment is to familiarize yourself with WPF Applications using C#. You will need to create a WPF application, handle event listeners, GUI components, and event driven code. Instructions You will be creating an application called the Game of Life. This is a simulation of cellular growth by John Horton Conway. The game uses a two dimensional array of possible cells to run the game. After each iteration called a generation, the display of the array will be updated on the WPF form. Every cell will interact with its eight immediate neighbors within a generation. See Figure 1, C- cell; N- neighbor. Each cell with continue to live, die, or be born based on this interaction. NININ NCIN NININ Figure 1 Requirements Create a WPF application Create a 2D array for tracking each cell with dimensions of 30x80. Create a textbox that allows the user to specify the character to use and validate the input. o If the user does not specify a valid character use an 'x'. Create a textbox that allows the user to specify the number of generations to run and validate the input. The simulation is to run this number of generations o If the user does not specify a valid number, use 100 Create two buttons that allows the user to start the simulation. The first starts the simulation using the pattern shown in Figure 2 when this button is pressed The second starts the simulation with randomly placed living cells when pressed o o Traverse each element of the 2D array to calculate if the cell should continue living if alive, die if necessary, or come to life if it meets the following rules * o o o o Any living cell that has less than two living neighbors dies, as if caused by under-population Any living cell that has two or three living neighbors continues to live on to the next generation. Any living cell with more than three living neighbors dies, as if by over-population. Any non-living cell with exactly three live neighbors becomes a living cell, as if by reproduction Hint: you will need to traverse the entire array before you update whether a cell is living or dead. You should have at least the following methods: count the number of neighbors, update the game board on the form, fill the game board with the fixed pattern, fill the game board with random cells, and update game board values based on number of neighbors count. * It would be a wise idea to use a monospaced font, such as Courier New. The application needs to look professional Remember method comments, good variable & method names, good use of whitespace, block comments, and proper formatting * *Explanation / Answer
Game of Life:
Program:
public class GameOfLifeViewModel
{
public Thread GameThread { get; private set; }
private World World { get; set; }
private TaskFactory TaskFactory { get; set; }
private bool IsRunning { get; set; }
public GameOfLifeViewModel(World world)
{
this.TaskFactory = new TaskFactory(TaskScheduler.FromCurrentSynchronizationContext());
this.World = world;
this.GameThread = new Thread(GameOfLife);
}
public void Start()
{
GameThread.Start();
}
private void GameOfLife()
{
while (true)
{
for (int i = 0; i < World.Grid2DArray.GetLength(0); i++)
{
for (int j = 0; j < World.Grid2DArray.GetLength(1); j++)
{
int aliveNeighbours = CheckAliveNeighbours(i, j);
if (!World.Grid2DArray[i, j] && aliveNeighbours == 3)
{
World.Grid2DArray[i, j] = true;
}
else if (World.Grid2DArray[i, j] && (aliveNeighbours > 3 || aliveNeighbours < 2))
{
World.Grid2DArray[i, j] = false;
}
}
}
Thread.Sleep(200);
TaskFactory.StartNew((Action)(() => SetWorldVisuals()));
}
}
private void SetWorldVisuals()
{
int count = 0;
for (int i = 0; i < World.Grid2DArray.GetLength(0); i++)
{
for (int j = 0; j < World.Grid2DArray.GetLength(1); j++)
{
if (World.Grid2DArray[i, j])
{
World.Grid[count].Visibility = Visibility.Visible;
}
else
{
World.Grid[count].Visibility = Visibility.Hidden;
}
count++;
}
}
}
private int CheckAliveNeighbours(int xPos, int yPos)
{
int aliveCells = 0;
for (int i = 0; i < 9; i++)
{
int x = (i % 3) - 1;
int y = (i / 3) - 1;
if (x == 0 && y == 0) continue;
int xx = xPos + x;
int yy = yPos + y;
if (xx < 0 || yy < 0) continue;
if (xx < World.xRows && yy < World.yRows && World.Grid2DArray[xx, yy])
{
aliveCells++;
}
}
return aliveCells;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.