简体   繁体   English

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

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

I'm trying to program Conway's Game of Life in C but I am stumped as to how to store an ALIVE or DEAD cell. 我正在尝试用C编写Conway的《生命游戏》,但是我对如何存储ALIVE或DEAD单元感到困惑。

The board is stored in an array of 32 unsigned longs (32x32 board) with each bit representing a cell (1 = alive, 0 = dead). 该评估板存储在32个无符号长数组(32x32评估板)的阵列中,每个位代表一个单元(1 =有效,0 =无效)。 I can't change this design. 我无法更改此设计。

So far, I have the code determining how many neighbors a particular cell has but I need to change its state based on the game's rules (a 1 may need to become either 0 or 1, a 0 may need to be either 1 or 0). 到目前为止,我有确定特定单元格有多少个邻居的代码,但是我需要根据游戏规则更改其状态(1可能需要变为0或1,0可能需要等于1或0) 。

I assume I can use bitwise operations for this (|, &, ^) but I don't know how to isolate a particular bit in a row and store it as I iterate through the rest of the row and then store the new, re-calculate row as one unsigned long. 我假设我可以对此(|,&,^)使用按位运算,但是我不知道如何在一行中隔离特定的位并将其存储,因为我要遍历该行的其余部分,然后再存储新的-将行计算为一个无符号长。

ie if 10011...0 needs to be 01101...1. 即,如果10011 ... 0需要为01101 ... 1。 How can I do this? 我怎样才能做到这一点?

I realize the code will need additional for loops but I am just trying to wrap my head around this particular problem before proceeding. 我意识到代码将需要附加的for循环,但是我只是想在继续之前解决这个特定问题。

Any help is appreciated. 任何帮助表示赞赏。

#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;
    }
 }

You can set a bit by ORing ( | ) with a value that has that bit set. 您可以通过对( | )与设置了该位的值进行设置。

You can unset a bit by ANDing ( & ) with a value that has every bit set except that one. 您可以使用与运算( & )取消设置某位的值,该值设置了该位以外的所有位。

You can turn a value that has one bit set into a value that has every bit except that one set with the NOT ( ~ ) operator. 您可以使用NOT( ~ )运算符将设置为1的值转换为设置为1的值。

You can tell if a bit is set by ANDing ( & ) with a value that only has that bit set, and seeing whether the result is true or false. 您可以通过“ & ”( & )设置仅设置了该位的值,并判断结果是true还是false来判断是否设置了该位。

You can make a value with the nth bit (counting from the right, with the rightmost bit named the 0th bit) set by left-shifting ( << ) the value 1 by n places. 你可以用第n位的值(从右边算起,名为第0位最右边的位)由左移(设置<< )的值1n位。

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

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