SHOW:
|
|
- or go back to the newest paste.
1 | int main() | |
2 | { | |
3 | - | int world[2048]; |
3 | + | int board[2048]; |
4 | - | int O = (int)&O; |
4 | + | int randNum = (int)&randNum; |
5 | int i; | |
6 | ||
7 | - | // bit 5 (=32) of world[i] is old cell state |
7 | + | // Board contains 64 columns, 32 rows. |
8 | - | // bit 4 (=16) of world[i] is new cell state |
8 | + | // bit 5 (=32) of board[i] is old cell state |
9 | - | // World contains 64 columns, 32 rows |
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 | - | O = (O*0x41C64E6D + 12345) & 0x7fffffff; |
13 | + | |
14 | - | world[i] = (O & 1024) ? 32 : 0; |
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 neighbor, increase world[i]. This is at max 8 = 2**3, so this won't affect bits 4 and 5. |
22 | + | // For each cell, calculate new board state. |
23 | - | const int diffNeighbor[4] = {1, 63, 64, 65}; |
23 | + | |
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 | - | if(world[(i + diffNeighbor[j]) % 2048] & (1 << 5)) |
28 | + | |
29 | int j; | |
30 | - | ++world[i]; |
30 | + | |
31 | { | |
32 | - | if(world[(i - diffNeighbor[j]) % 2048] & (1 << 5)) |
32 | + | if(board[(i + neighborOffset[j]) % 2048] & (1 << 5)) |
33 | { | |
34 | - | ++world[i]; |
34 | + | ++board[i]; |
35 | } | |
36 | if(board[(i - neighborOffset[j]) % 2048] & (1 << 5)) | |
37 | { | |
38 | ++board[i]; | |
39 | } | |
40 | - | world[i] |= ((world[i] ^ 34) && (world[i] ^ 35)) ? ( (world[i]^3) ? 0 : (1<<4)) : (1<<4); |
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 | - | world[i] = ( world[i] & (1 << 4) ) << 1; // set new call state from old state |
47 | + | |
48 | - | putchar(world[i] ? 'X':' '); |
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 | } |