2014-02-06 36 views
1

我想在C中編程康威的生命遊戲,但我很難理解如何存儲ALIVE或DEAD單元格。C中的生命編程遊戲 - 按位運算的問題

電路板存儲在32位無符號長整數(32x32板)的數組中,每一位代表一個單元(1 =活動,0 =無效)。我無法改變這種設計。到目前爲止,我已經確定了一個特定單元有多少個鄰居,但是我需要根據遊戲規則改變它的狀態(一個1可能需要變成0或1,0可能需要是1或0)。

我假設我可以對此使用按位操作(|,&,^),但我不知道如何隔離一行中的特定位並存儲它,因爲我遍歷行的其餘部分,然後存儲新的,重新計算的行作爲一個無符號長整數。

即,如果10011 ... 0需要是01101 ... 1。我怎樣才能做到這一點?

我意識到代碼將需要額外的循環,但我只是試圖圍繞這個特定的問題,繼續前我的頭。

任何幫助表示讚賞。

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

回答

5

你可以設置一個有點或門(|)與有位設置的值。

你可以通過ANDing(&)將值設置爲而不是那個值來置1。

您可以將設置了一位的值變成除了用NOT(~)運算符設置的值之外的每一位的值。

您可以通過與僅設置該位的值進行ANDing(&)來判斷是否設置了位,並查看結果是true還是false。

您可以通過ň地使與第n位的值(從右邊算起,名爲第0位最右邊的位)由左移(<<)的值設置1