pattern.h (5608B)
1 /** 2 * @file pattern.h 3 * @author Dimitrije Dobrota 4 * @date 19 June 2022 5 * @brief Pattern interface and types 6 */ 7 8 #ifndef PATTERN_H 9 #define PATTERN_H 10 11 #include <stdlib.h> 12 13 /** 14 * @brief Structure representing one cell pattern to be used in the help menu 15 */ 16 typedef struct pattern_T { 17 char *cells; ///< string where 1 represents a living cell and 0 a dead one, 18 /// space is used to detonate a new row 19 char *name; ///< name of the pattern 20 int height; ///< pattern height , used for cache, no need to fill manually 21 int width; ///< pattern width , used for cache, no need to fill manually 22 } * pattern_T; 23 24 /// Game title using cells 25 struct pattern_T title = { 26 "011111000000000000000000000000000000000000000000010000000000000000000000 " 27 "100000100011000100001011111100000111100111111000010000000101111110111111 " 28 "100000000100100110011010000000001000010100000000010000000101000000100000 " 29 "100111101000010101101011111000001000010111110000010000000101111100111110 " 30 "100000101111110100001010000000001000010100000000010000000101000000100000 " 31 "100000101000010100001010000000001000010100000000010000000101000000100000 " 32 "011111001000010100001011111100000111100100000000011111110101000000111111 ", 33 "Title", 34 0, 35 0, 36 }; 37 38 /// Game keybindings 39 struct pattern_T games[] = { 40 { "", "Game", 0, 0}, 41 { "p", "play/pause", 0, 0}, 42 { "wasd", "move cursor", 0, 0}, 43 {"arrows", "move screen", 0, 0}, 44 { "q", "quit", 0, 0}, 45 { "space", "toggle cell", 0, 0}, 46 { "v", "visual select", 0, 0}, 47 { "-", "decrease generation step", 0, 0}, 48 { "+", "increase generation step", 0, 0}, 49 { "[", "decrease dt step", 0, 0}, 50 { "]", "increase dt step", 0, 0}, 51 { "", "", 0, 0}, 52 { "", "Visual select", 0, 0}, 53 { "enter", "save", 0, 0}, 54 { "x", "delete", 0, 0}, 55 { "t", "toggle", 0, 0}, 56 { "q", "cancel", 0, 0}, 57 }; 58 59 /// Still cell patterns 60 struct pattern_T stills[] = { 61 { "11 11", "block", 0, 0}, 62 { "1011 1101", "Snake", 0, 0}, 63 { "010 101 010", "Tub", 0, 0}, 64 { "110 101 010", "Boat", 0, 0}, 65 { "0110 1001 0110", "Beehive", 0, 0}, 66 { "1100 1001 0011", "Carrier", 0, 0}, 67 { "11000 10011 01101", "Shillelagh", 0, 0}, 68 { "110 1001 1001 0110", "Pond", 0, 0}, 69 { "0010 0101 1010 0100", "Barge", 0, 0}, 70 { "0010 0101 1010 1100", "Long boat", 0, 0}, 71 { "0110 1001 0101 0010", "Loaf", 0, 0}, 72 {"00100 01010 01010 11011", "Hat", 0, 0}, 73 }; 74 75 /// Oscillator cell patterns 76 struct pattern_T oscillators[] = { 77 { "0111 1110", "Toad", 0, 0}, 78 { "010 010 010", "Blinker", 0, 0}, 79 { "0010 1010 0101 0100", "Clock", 0, 0}, 80 { "0011 0001 1000 1100", "Beacon", 0, 0}, 81 { "0010000100 1101111011 0010000100", "Pentadecathlon", 0, 0}, 82 { "110000 110100 000010 010000 001011 000011", "Figure eight", 0, 0}, 83 {"11000011 10100101 00100100 10100101 11000011", "Spark coil", 0, 0}, 84 }; 85 86 /// Spaceship cell patterns 87 struct pattern_T spaceships[] = { 88 { "010 001 111", "Glider", 0}, 89 { "01001 10000 10001 11110", "Lightweight", 0}, 90 { "000100 010001 100000 100001 111110", "Mediumweight", 0}, 91 {"0001100 0100001 1000000 1000001 1111110", "Heavyweight", 0}, 92 }; 93 94 /// Methuselah cell patterns 95 struct pattern_T methuselahs[] = { 96 { "111 101 101", "Piheptomino", 0}, 97 { "1011 1110 0100", "B-Heptomino", 0}, 98 { "11001 10001 10011", "Glider by the dozen", 0}, 99 { "111 000 010 010 010", "Thunderbird", 0}, 100 { "0100000 0001000 1100111", "Acorn", 0}, 101 {"00000010 11000000 01000111", "Diehard", 0}, 102 }; 103 104 /** 105 * @brief A structure representing a group of pattern_T of a certain category 106 */ 107 typedef struct pattern_group_T { 108 char *name; ///< name of the group 109 struct pattern_T *pattern; ///< array of pattern_T that comprise a group 110 int size; ///< number of elements in a group 111 } pattern_group_T; 112 113 /// Pattern group containing all of the patterns for the help menu 114 struct pattern_group_T pattern_groups[] = { 115 {"Key bindings", games, sizeof(games) / sizeof(struct pattern_T)}, 116 { "Still", stills, sizeof(stills) / sizeof(struct pattern_T)}, 117 { "Oscillator", oscillators, sizeof(oscillators) / sizeof(struct pattern_T)}, 118 { "Spaceships", spaceships, sizeof(spaceships) / sizeof(struct pattern_T)}, 119 { "Methuselah", methuselahs, sizeof(methuselahs) / sizeof(struct pattern_T)}, 120 }; 121 122 /// Size of pattern_groups 123 int pattern_groups_s = sizeof(pattern_groups) / sizeof(pattern_group_T); 124 125 /// Get the pattern height 126 int pattern_height(pattern_T pattern) { 127 int count = 0; 128 for (char *p = pattern->cells; *p != '\0'; p++) 129 if (*p == ' ') 130 count++; 131 132 return count; 133 } 134 135 /// Get the pattern width 136 int pattern_width(pattern_T pattern) { 137 int count = 0; 138 for (char *p = pattern->cells; *p != ' ' && *p != '\0'; p++) 139 count++; 140 141 return count; 142 } 143 144 #endif