Advertisement
Guest User

https://github.com/duckythescientist/obfuscatedLife

a guest
Nov 26th, 2013
2,672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int main()
  2. {
  3.     int board[2048];
  4.     int randNum = (int)&randNum;
  5.     int i;
  6.    
  7.     // Board contains 64 columns, 32 rows.
  8.     // bit 5 (=32) of board[i] is old cell state
  9.     // bit 4 (=16) of board[i] is new cell state
  10.  
  11.     // Generate random initial state.
  12.     for(i = 0; i < 2048; ++i)
  13.     {
  14.         randNum = (randNum*0x41C64E6D + 12345) & 0x7fffffff;
  15.         board[i] = (randNum & 1024) ? 32 : 0;
  16.     }
  17.  
  18.     usleep(102400);
  19.     for(;;)
  20.     {
  21.    
  22.         // For each cell, calculate new board state.
  23.         for(i = 0; i < 2048; ++i)
  24.         {
  25.             // For each neighbor, increase board[i]. This is at max 8 = 2**3, so this won't affect bits 4 and 5.
  26.  
  27.             const int neighborOffset[4] = {1, 63, 64, 65};
  28.  
  29.             int j;
  30.             for (j = 0; j < 4; ++j)
  31.             {
  32.                 if(board[(i + neighborOffset[j]) % 2048] & (1 << 5))
  33.                 {
  34.                     ++board[i];
  35.                 }
  36.                 if(board[(i - neighborOffset[j]) % 2048] & (1 << 5))
  37.                 {
  38.                     ++board[i];
  39.                 }
  40.  
  41.                 if(j==3)
  42.                 {
  43.                     // Apply "game of life" rule.
  44.                     board[i] |= ((board[i] ^ 34) && (board[i] ^ 35)) ? ( (board[i]^3) ? 0 : (1<<4)) : (1<<4);
  45.                 }
  46.             }
  47.         }
  48.        
  49.         // Copy old board state to new board state and print board.
  50.         for (i = 1; i < 2048; ++i)
  51.         {
  52.             // Set new cell state from old state
  53.             board[i] = ( board[i] & (1 << 4) ) << 1;
  54.            
  55.             // Print cell
  56.             putchar(board[i] ? 'X':' ');
  57.             if((i-1)%64==0) //if(!(63^i % 64))
  58.             {
  59.                 putchar('\n');
  60.             }
  61.         }
  62.        
  63.         usleep(102400);
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement