Life GUI (JAVA)
Добавлено: 19 май 2009, 19:21
Для начала, условие
Задачу, в общем, я выполнил. Но программа работает только 1 шаг. На следующий шаг массив обнуляется. Вот код.LifeGUI (see http://www.math.com/students/wonders/life/life.html)
Mathematician John Conway devised the Game of Life in 1970. It models a very simple world. The Life world is a two-dimensional plane of cells. Each cell may be empty or contain a single creature. Each day, creatures are born or die in each cell according to the number of neighboring creatures on the previous day. A neighbor is a cell that adjoins the cell either horizontally, vertically, or diagonally. The rules in pseudocode style are:
if ( the cell is alive on the previous day)
{
if (the number of live neighbors was 2 or 3)
{
the cell remains alive
}
else
{
the cell dies
}
}
else if (the cell is not alive on the previous day)
{
if (the number of live neighbors was exactly 3)
{
the cell becomes alive
}
else
{
the cell remains dead
{
}
For example, the world displayed as:
0000000000
0000000000
000xxx0000
0000000000
0000000000
0000000000
where X’s indicate living cells, becomes:
0000000000
0000x00000
0000x00000
0000x00000
0000000000
0000000000
For the non-GUI application, use a 20 by 20 grid. To initialize the grid, the application should prompt the user for the coordinates of live cells on the first day. The application should then generate each day’s world as long as the user wishes to continue or until there are no more live cells.
For the GUI application the 20 by 20 grid should be buttons that initially display all 0s. To select the live cells for the first day, the user clicks the buttons on the positions of live cells. When clicked, a button changes from displaying a 0 to displaying an X. A Next button below the grid can be clicked repeatedly to display the next generation until the user quits or there are no more live cells.