Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int main()
- {
- int board[2048];
- int randNum = (int)&randNum;
- int i;
- // Board contains 64 columns, 32 rows.
- // bit 5 (=32) of board[i] is old cell state
- // bit 4 (=16) of board[i] is new cell state
- // Generate random initial state.
- for(i = 0; i < 2048; ++i)
- {
- randNum = (randNum*0x41C64E6D + 12345) & 0x7fffffff;
- board[i] = (randNum & 1024) ? 32 : 0;
- }
- usleep(102400);
- for(;;)
- {
- // For each cell, calculate new board state.
- for(i = 0; i < 2048; ++i)
- {
- // For each neighbor, increase board[i]. This is at max 8 = 2**3, so this won't affect bits 4 and 5.
- const int neighborOffset[4] = {1, 63, 64, 65};
- int j;
- for (j = 0; j < 4; ++j)
- {
- if(board[(i + neighborOffset[j]) % 2048] & (1 << 5))
- {
- ++board[i];
- }
- if(board[(i - neighborOffset[j]) % 2048] & (1 << 5))
- {
- ++board[i];
- }
- if(j==3)
- {
- // Apply "game of life" rule.
- board[i] |= ((board[i] ^ 34) && (board[i] ^ 35)) ? ( (board[i]^3) ? 0 : (1<<4)) : (1<<4);
- }
- }
- }
- // Copy old board state to new board state and print board.
- for (i = 1; i < 2048; ++i)
- {
- // Set new cell state from old state
- board[i] = ( board[i] & (1 << 4) ) << 1;
- // Print cell
- putchar(board[i] ? 'X':' ');
- if((i-1)%64==0) //if(!(63^i % 64))
- {
- putchar('\n');
- }
- }
- usleep(102400);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement