繁体   English   中英

用C语言编写生活游戏-按位运算问题

[英]Programming Game of Life in C - Issue with bitwise operations

我正在尝试用C编写Conway的《生命游戏》,但是我对如何存储ALIVE或DEAD单元感到困惑。

该评估板存储在32个无符号长数组(32x32评估板)的阵列中,每个位代表一个单元(1 =有效,0 =无效)。 我无法更改此设计。

到目前为止,我有确定特定单元格有多少个邻居的代码,但是我需要根据游戏规则更改其状态(1可能需要变为0或1,0可能需要等于1或0) 。

我假设我可以对此(|,&,^)使用按位运算,但是我不知道如何在一行中隔离特定的位并将其存储,因为我要遍历该行的其余部分,然后再存储新的-将行计算为一个无符号长。

即,如果10011 ... 0需要为01101 ... 1。 我怎样才能做到这一点?

我意识到代码将需要附加的for循环,但是我只是想在继续之前解决这个特定问题。

任何帮助表示赞赏。

#include <stdio.h>
#include <stdlib.h>
unsigned long columnMask;
int compass = 0;
int totAliveNeighbors = 0;
int iterator = 0;
int iterator2 = 0;

#define ARRAY_SIZE 32
#define NEIGHBORS 8

unsigned long grid[ARRAY_SIZE];
unsigned long copyGrid[ARRAY_SIZE];
unsigned long neighbors[NEIGHBORS];
unsigned long init32;
unsigned long holder = 0;

int main(void){
    srand(time(NULL));
    printf("\n");

    /** Seeds the grid with random numbers **/
    for(iterator = 0; iterator < 32; iterator++){
        init32 = ((double)rand()/RAND_MAX)*0xFFFFFFFF;
        grid[iterator] = init32;
    }

    /** Displays the binary representation of the grid elements **/
    for(iterator = 0; iterator < 32; iterator++){  
        displayBinary(grid[iterator]);     
        printf("\n");
    }
    printf("\n");

    /** Calculate and sum neighbors for 'x' cell **/
    /** Will need to iterate through each column by shifting the mask **/
    /** Will need to iterate through each row **/
            iterator= 0; //example use
            neighbors[0] = north(iterator);
            neighbors[1] = south(iterator);
            neighbors[2] = east(iterator);
            neighbors[3] = west(iterator);
            neighbors[4] = northWest(iterator);
            neighbors[5] = northEast(iterator);
            neighbors[6] = southWest(iterator);
            neighbors[7] = SouthEast(iterator);

            columnMask = 0x80000000//need to shift by iterator value later on
            for(compass =0; compass < 8; compass++){
                totAliveNeighbors += ((columnMask & neighbors[compass])?1:0);
            }

}//end main

void displayBinary(unsigned long x){
    unsigned long MASK = 0x80000000;
    do {
        //printf("%c",(x & MASK)?'X':0x20);
        printf("%s", (x & MASK)?"1":"0");
    } while ((MASK >>=1)!=0);
}

unsigned long north(int rowNum){
    if(rowNum == 0){
        return 0;
    }
    else    
        return grid[rowNum-1];
}

unsigned long west(int rowNum){
    holder = grid[rowNum] >>1;
    return holder;
}

unsigned long east(int rowNum){
    holder = grid[rowNum] <<1;
    return holder;
}

unsigned long south(int rowNum){
    if(rowNum == 31)
        return 0;
    else    
        return grid[rowNum+1];
}

unsigned long northWest(int rowNum){
    if(rowNum == 0)
        return 0;
    else{
        holder = grid[rowNum-1] >>1;
        return holder;
    }
}

unsigned long northEast(int rowNum){
    if(rowNum == 0)
        return 0;
    else{
        holder = grid[rowNum-1] <<1;
        return holder;
    }    
}

unsigned long southWest(int rowNum){
    if(rowNum == 31)
        return 0;
    else{
        holder = grid[rowNum+1] >>1;
        return holder;
    }
}

unsigned long SouthEast(int rowNum){
    if(rowNum == 31)
        return 0;
    else{
        holder = grid[rowNum+1] <<1;
        return holder;
    }
 }

您可以通过对( | )与设置了该位的值进行设置。

您可以使用与运算( & )取消设置某位的值,该值设置了该位以外的所有位。

您可以使用NOT( ~ )运算符将设置为1的值转换为设置为1的值。

您可以通过“ & ”( & )设置仅设置了该位的值,并判断结果是true还是false来判断是否设置了该位。

你可以用第n位的值(从右边算起,名为第0位最右边的位)由左移(设置<< )的值1n位。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM