Conway の Game of Life に基づいて、Ubuntu で GCC を使用して単純な C アプリケーションを作成しています。基本的に必要なコードはすべて揃っていますが、コードの小さな側面に問題があります。
私のCソースファイル:
#include <stdio.h>
#include <stdlib.h>
#define HEIGHT 32
#define WIDTH 32
#define COMPASS 8
#define SPACE '.'
unsigned long mask = 0x80000000;
unsigned long row[WIDTH] = { 0 };
unsigned long copy[WIDTH] = { 0 };
int northWest(int rowNum) {
copy[rowNum - 1] = row[rowNum - 1] >>= 1;
return copy[rowNum - 1] & row[rowNum];
}
int north(int rowNum) {
copy[rowNum - 1] = row[rowNum - 1];
return copy[rowNum - 1] & row[rowNum];
}
int northEast(int rowNum) {
copy[rowNum - 1] = row[rowNum - 1] <<= 1;
return copy[rowNum - 1] & row[rowNum];
}
int west(int rowNum) {
copy[rowNum] = row[rowNum] >>= 1;
return copy[rowNum] & row[rowNum];
}
int east(int rowNum) {
copy[rowNum] = row[rowNum] <<= 1;
return copy[rowNum] & row[rowNum];
}
int southWest(int rowNum) {
copy[rowNum + 1] = row[rowNum + 1] >>= 1;
return copy[rowNum + 1] & row[rowNum];
}
int south(int rowNum) {
copy[rowNum + 1] = row[rowNum];
return copy[rowNum + 1] & row[rowNum];
}
int southEast(int rowNum) {
copy[rowNum + 1] = row[rowNum + 1] <<= 1;
return copy[rowNum + 1] & row[rowNum];
}
void clearRows(unsigned long row[]) {
int i;
system("clear");
for (i = 0; i < HEIGHT; ++i) {
row[i] = 0;
}
}
void displayBinary(unsigned long x) {
int bit;
/*int mask;*/
for (bit = 0; bit < HEIGHT; ++bit)
{
mask = 1 << bit;
printf("%c", (x & mask) ? 'X' : SPACE);
}
printf("\n");
}
int main(void) {
int i, j, alive;
char ch;
unsigned long init32;
srand(time(NULL));
for (i = 0; i < HEIGHT; ++i) {
init32 = ((double)rand() / RAND_MAX) * 0xFFFFFFFF;
row[i] = init32;
displayBinary(row[i]);
}
do {
system("clear");
for (i = 0; i < WIDTH; ++i) {
unsigned long neighbours[COMPASS] = {
north(i),
south(i),
west(i),
east(i),
northEast(i),
northWest(i),
southEast(i),
southWest(i)
};
for (j = 0; j < COMPASS; ++j) {
alive += ((mask & neighbours[j]) ? 1 : 0);
}
displayBinary(row[i]);
}
} while ((ch = getchar()) != 'n');
return EXIT_SUCCESS;
}
アプリケーションが最初に起動したとき、出力は私が期待するもの (ランダムな 'X' および '.' 文字の 32x32 グリッド) ですが、その後の反復ごとに何も変わりません。各ループで、私が持っているメソッド (north()、west() など) に基づいて隣人を再計算し、新しい 'X' と '.' を出力する必要があります。32x32 グリッド上の値。
新しい値を配列に入れて画面に出力する方法について、誰かが何らかの支援を提供できますか? ところで、私はCでプログラミングするのが初めてです。ありがとう。