、だから、タイトルのような は言う、私はのコンウェイのゲームでスレッド実現するプログラムを作成する必要があり、各セルはスレッドにコンウェイのゲーム
やあみんなであるとライフゲームそれぞれの死んだ生きている細胞は糸である。 私の最初の目標は、単純にゲームを働かせることでした。(かなり楽しいチャレンジ) 20x20のグリッドを印刷することができ、各セルは1または0の乱数として初期化され、1は生存しています。デッド。 私は、3つのクラス、メイン、セル、およびCellRulesを持っています 私はスレッドを使用する方法のビデオを見てきましたが、私はまだ各セルのためにこれを実装するはずです...私のセルのクラスには、次のようになります。
public class Cell implements Runnable
{
public static int myCount = 0;
private String name;
private int neighbors;
private int alive; // 0 is dead; 1 is alive.
Random rand = new Random();
public Cell (String nm)
{
name = nm;
myCount = rand.nextInt(999);
neighbors = 0;
// 2 because it is exclusive
this.setLife(rand.nextInt(2));
}
public void run()
{
while(Cell.myCount <= 10){
try
{
System.out.println("Expl Thread: " + (++Cell.myCount));
Thread.sleep(100);
} catch (InterruptedException iex)
{
System.out.println("Exception in thread:
"+iex.getMessage());
}
}
}
簡単にするために実際にあるそこにいくつか他のものは、ありますが、私は彼らが示すことが必要であると考えて、および細胞ルールクラスの同じものはありません。セルルールは次のようになります。
/**
* This function simply gets the number of neighbors for each cell, and saves the future generation
* based on the number neighbors from arr to future array.
*
* @param arr Arr that will be checked for neighbors
* @param futureGen This array will keep track of the future generation
* @param columns numbers of columns
* @param rows numbers of rows
*/
public void checkN(Cell [][] arr, Cell [][] futureGen, int columns, int rows)
{
for (int x = 0; x < rows; x++)
{
for (int y = 0; y < columns; y++)
{
arr[x][y].setNeighbors(0);
// Upper left corner
if (x == 0 && y == 0)
for (int i = 0; i <= 1; i++)
for (int j = 0; j <= 1; j++)
if (arr[x + i][y + j].getLife() == 1)
arr[x][y].addNeighbor();
// Upper margin checks
if ((x == 0) && (y != 0 && y <= columns - 2))
for(int i = 0; i <= 1; i++)
for(int j = -1; j <= 1; j++)
if (arr[x + i][y + j].getLife() == 1)
arr[x][y].addNeighbor();
// Upper right corner
if ((x == 0) && (y == columns - 1))
for(int i = 0; i <= 1; i++)
for(int j = -1; j <= 0; j++)
if (arr[x + i][y + j].getLife() == 1)
arr[x][y].addNeighbor();
// Left margin checks
if ((y == 0) && (x != 0 && x <= rows - 2))
for (int i = -1; i <= 1; i++)
for (int j = 0; j <= 1; j++)
if (arr[x + i][y + j].getLife() == 1)
arr[x][y].addNeighbor();
// Lower left corner
if ((x == rows - 1) && y == 0)
for (int i = -1; i <= 0; i++)
for (int j = 0; j <= 1; j++)
if (arr[x + i][y + j].getLife() == 1)
arr[x][y].addNeighbor();
// Bottom margin checks
if ((x == rows - 1) && (y != 0 && y <= columns - 2))
for (int i = -1; i <= 0; i++)
for (int j = -1; j <= 1; j++)
if (arr[x + i][y + j].getLife() == 1)
arr[x][y].addNeighbor();
// Lower right corner
if ((x == rows - 1) && (y == columns - 1))
for (int i = -1; i <= 0; i++)
for (int j = -1; j <= 0; j++)
if (arr[x + i][y + j].getLife() == 1)
arr[x][y].addNeighbor();
// Right margin checks
if ((y == columns - 1) && (x != 0 && x <= rows - 2))
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 0; j++)
if (arr[x + i][y + j].getLife() == 1)
arr[x][y].addNeighbor();
// Middle area checks (can check all around now)!
if ((x > 0) && (x < rows - 1) && (y > 0) && (y < columns - 1))
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++)
if (arr[x + i][y + j].getLife() == 1)
arr[x][y].addNeighbor();
// Do not add yourself!
if (arr[x][y].getLife() == 1)
arr[x][y].subNeighbor();
// Get the new generation through the neighbors
if ((arr[x][y].getLife() == 1) &&
(arr[x][y].getNeighbors() < 2))
futureGen[x][y].setLife(0);
else if ((arr[x][y].getLife() == 1) &&
(arr[x][y].getNeighbors() > 3))
futureGen[x][y].setLife(0);
else if ((arr[x][y].getLife() == 0) &&
(arr[x][y].getNeighbors() == 3))
futureGen[x][y].setLife(1);
else
futureGen[x][y].setLife(arr[x][y].getLife());
}
}
}
スレッドの実装が本当に行っているかわかりません。どのようなガイダンスや説明をいただければ幸いです。 良い一生を過ごしてください:)
このような詳細な応答をありがとうございます! 私はcheckN機能がどのくらい短くなったかにとても驚いています!どのくらい私が学ばなければならないかを示しています。 私は本当に有益ではないと私は各セルのスレッドを作るのは非効率だと思いますが、それは宿題です。私たちは決してスレッドをしておらず、これが学ぶ方法でした。しかし、私はそれがたくさんのビデオを見た後でもかなり混乱していることがわかります。 もう一度、ありがとうございます。私はそれをまだ実装していませんが、それでも私は壮大な答えに本当に感謝しています! – Nano