/* ----------------------------------------------------------------------- * * Life: A game program that emulates the growth of cells in a petri dish * * Author: Suzi Anvin * * Rules: Cells die if lonely or overcrowded. They only live if the 8 * surrounding squares have 2 or 3 other cells. Cells can be created * in any empty square that is adjacent to 3 other cells. * * Operation: (Versions 0.X) Non-operational development stages. Version 0.1 * inputs one line of text, converts it inot a bit array, prints * bits and then prints the line of cells. An error message is * returned if the computer does not recognize the input and asks * for the string again. After displaying correct line, program * asks if the user want's to try again and quits if they answer * 'n' Oterwise it restarts. * * Goal: (Versions 1.X) User enters a string of text on several lines, * representing cells as '@' and empty spaces as '.' This is * converted into an array of bits. "Life" then runs step by step * through the lifecycle of the cells, re-drawing the display each * time the user presses enter. The display quits when the user * types 'Q' and then asks the user if they want to play again. * If the user's answer begins with 'Y' the program restarts. * Otherwise, it quits. * ----------------------------------------------------------------------- */ #include #include inline void set_bit (uint8_t array[], unsigned int bit) { array[bit/8] |= 1<<(bit%8); } inline void clr_bit (uint8_t array[], unsigned int bit) { array[bit/8] &= ~(1<<(bit%8)); } /* test_bit returns 1 if the bit is 1 and 0 if the bit is 0. */ inline int test_bit (const uint8_t array[], unsigned int bit) { return (array[bit/8]>>(bit%8)) & 1; } /* set_bmp_line takes a text line of @'s and .'s. It assigns them to * one line of a bitmap array with @ = 1 and . = 0 * * When passing the array to set_bmp_line, be sure to specify the line * number [in brackets]. * * set_bmp_line returns 0 if all bits are correctly assigned and 1 if * it does not recognize a character from the text line. */ int set_bmp_line (uint8_t array[], char line[]) { int i; /* counter */ clr_bit (array, 0); for (i=1; i <= 38; i++) { switch (line[i-1]) { case '@': set_bit (array, i); break; case '.': clr_bit (array, i); break; default: return (1); } } clr_bit (array, 39); return (0); } /* draw_bmp_line is a preliminary function to draw one line of the bitmap * seperately. Each line of the array is passed individually, and bits 1-38 * are drawn on the screen, with 1 = '@' and 0 = '.'. the comand call needs to * specify the current line # [in brackets] to use the function. * * I'm told I'm "already using pointers" to pass in the 1 * dimensional array. That's fine, I'll wait til I have a better clue before * passing in both dimensions. In version 1.1 or so this function should * be updated to draw the whole bitmap. */ void draw_bmp_line (uint8_t array[]) { int i; /* counter(s) */ putchar(' '); for (i=1; i<= 38; i++) { if (test_bit(array, i)) putchar('@'); else putchar('.'); } putchar('\n'); } /* debug_bits prints out the bits as the appear in the array. This tests that * they have been encoded correctly. */ void debug_bits (uint8_t array[]) { int i; for (i=0; i < 40; i++) { printf("%d", test_bit(array, i)); } putchar('\n'); } /* set up bitmap array as [line #] [byte # in that line] */ /* temporary main function to test the set_bmp_line and draw_bmp_line * functions */ int main() { char line[50]; uint8_t bitmap[5]; /* one dimensional bitmap array */ while (1) { /* loop for debugging */ printf("Welcome to the game of Life, development version 0.1.\n" "Please enter your cells one line at a time,\n" "with no more than 38 characters per line\n" "as indicated by the line of ---'s.\n"); while(1){ printf("Use @ to indicate a cell and a period to " "indicate an empty space.\n" " --------------------------------------\n "); fgets(line, sizeof(line), stdin); line[38] = '\0'; if (set_bmp_line(bitmap, line) == 1) { printf("Error: Unrecognized character in string.\n" "Please enter your cells again.\n"); continue; } else break; } debug_bits(bitmap); draw_bmp_line(bitmap); printf("try again? (y/n)"); fgets(line, sizeof(line), stdin); if (line[0] == 'n') break; } return (0); }