Question
Assignment #2 1. In the MLT-3, there are three transition rules , and three levels (-V,0, and +V). Those rules are: 1. If the next bit is 0, there is no transition . 2. If the next bit is 1 and the current level is not 0, the next level is 0. 3. If the next bit is 1 and the current level is 0, the next level is the opposite of the last nonzero level. Your task is to write a set of programming codes that simulate the MLT-3 scheme with the given sequence shown in the figure below (the second one (b.)). The first one (a.) is for the lustrative purpose (refer to the Lecture 04, pages 39 through 42). The second one (b.) is for your task. The third one (c.) is for the rules to be specified. 0 1 1 1 0 1 1 1 1 0 1 1 1 1 1 IIIIIIII +V Next bit: 0 Next bit: 1 Next bit: 1 OV II Time Next bit: 1 a. Typical case Last Last non-zero non-zero Next bit: 0 level: +V level: V Next bit: 0 C. Transition states 1 1 0 1 0 1 1 1 0 1 1 0 1 1 1 II III +V II III IIIIIIII OV IIIIIIII IIIIIIII Time IIIIIIII IIIIIIII
Explanation / Answer
#define _XOPEN_SOURCE 500 #include #include #include #include // constants #define DIM_MIN 3 #define DIM_MAX 9 // board int board[DIM_MAX][DIM_MAX]; // dimensions int d; // prototypes void clear(void); void greet(void); void init(void); void draw(void); bool move(int tile); bool won(void); int main(int argc, string argv[]) { // ensure proper usage if (argc != 2) { printf("Usage: fifteen d "); return 1; } // ensure valid dimensions d = atoi(argv[1]); if (d DIM_MAX) { printf("Board must be between %i x %i and %i x %i, inclusive. ", DIM_MIN, DIM_MIN, DIM_MAX, DIM_MAX); return 2; } // open log FILE *file = fopen("log.txt", "w"); if (file == NULL) { return 3; } // greet user with instructions greet(); // initialize the board init(); // accept moves until game is won while (true) { // clear the screen clear(); // draw the current state of the board draw(); // log the current state of the board (for testing) for (int i = 0; i