/**************************************************/ /* /* Program: hw5.c /* The fish simulation /* Author: YOUR NAME HERE /* Date: YOUR DATE HERE /* /* /**************************************************/ #include "gp142.h" #include #include /***********************/ /* Program constants */ /***********************/ /* size of the grid */ #define NUMROWS 50 #define NUMCOLS 50 /* fish breeding parameters */ #define MAX_FISH (NUMROWS*NUMCOLS) #define FISH_BREED_TIME 15 #define NUM_INITIAL_FISH 10 /* various constants for figuring out the grid */ /* coordinates */ #define LEFTBORDER 10 #define RIGHTBORDER 10 #define TOPBORDER 10 #define BOTTOMBORDER 10 #define GRIDULX (-GP142_XMAX+LEFTBORDER) #define GRIDULY (GP142_YMAX-TOPBORDER) #define GRIDLRX (GP142_XMAX-RIGHTBORDER) #define GRIDLRY (-GP142_YMAX+BOTTOMBORDER) #define GRIDWIDTH (GRIDLRX-GRIDULX) #define GRIDHEIGHT (GRIDULY-GRIDLRY) #define BOXWIDTH (GRIDWIDTH/NUMCOLS) #define BOXHEIGHT (GRIDHEIGHT/NUMROWS) /* True and false */ #define TRUE 1 #define FALSE 0 /**************************/ /* Structure declarations */ /**************************/ typedef struct { int occupied; int occupantID; } GridSquare; /**************************/ /* Function declarations */ /**************************/ GP142_point convertGridToGP142(int row, int col); int findFreeAdjacentSquare(int row, int col, int *newRow, int *newCol, GridSquare theGrid[][NUMCOLS]); void drawScreen(GridSquare theGrid[][NUMCOLS]); /**************************/ /* main funtion */ /**************************/ int main(void) { /* event handling variables */ int quit, event; char key_pressed; int mouse_x, mouse_y; /*******************/ /* Data structures */ /*******************/ GridSquare theGrid[NUMROWS][NUMCOLS]; /********************/ /* Initialize GP142 */ /********************/ GP142_open(); GP142_logging(LOG_OFF); GP142_animate(ANI_RUN); GP142_clear(); /******************************************/ /* Initialize the random number generator */ /* Uncomment for true randomness */ /******************************************/ /* srand(time(NULL)); */ /**********************************/ /* Initialize the data structures */ /**********************************/ /* Fill this in */ /**************/ /* event loop */ /**************/ quit = FALSE; do { /* Draw the screen */ drawScreen(theGrid); /* grab an event */ event = GP142_await_event(&mouse_x, &mouse_y, &key_pressed); /* handle the event */ switch (event) { case GP142_QUIT: /* Quit selected */ quit = TRUE; break; case GP142_KBD: /* Key pressed. Ignored in this application. */ break; case GP142_MOUSE: /* Mouse click. Ignored in this application */ break; case GP142_PERIODIC: /* Timer interval event. Update the simulation */ break; default: printf("Received unknown event\n"); /* unknown event */ break; } /* end switch */ } while (!quit); /* Close everything down */ GP142_close(); return 0; } /**************************/ /* Function definitions */ /**************************/ /* Given the row and column of a grid square, */ /* returns the GP142 x and y coordinates of the */ /* upper left hand corner of the square */ GP142_point convertGridToGP142(int row, int col) { GP142_point gpPoint; gpPoint.x = (col * BOXWIDTH) + GRIDULX; gpPoint.y = -((row * BOXHEIGHT) - GRIDULY); return gpPoint; } /* Clears and redraws the screen */ void drawScreen(GridSquare theGrid[][NUMCOLS]) { } /* Given the row and column of a grid square, and */ /* the grid itself, finds an random empty square adjacent to the */ /* one passed in. If such a square is found, sets the */ /* pointer parameters newRow and newCol to the row and */ /* column of the free square and returns TRUE. Otherwise, */ /* leaves the parameters unchanged and returns FALSE */ int findFreeAdjacentSquare(int row, int col, int *newRow, int *newCol, GridSquare theGrid[][NUMCOLS]) { int upperRow, lowerRow, leftCol, rightCol; int freeArray[4][3]; int i, j, dir; /* Initialize the status array */ for (i = 0; i < 4; i++) { freeArray[i][0] = FALSE; } /***************** /* Calculate the indices of the surrounding squares /***************** /* upper square */ if (row == 0) { upperRow = NUMROWS - 1; } else { upperRow = row - 1; } /* lower square */ lowerRow = (row + 1) % NUMROWS; /* left square */ if (col == 0) { leftCol = NUMCOLS - 1; } else { leftCol = col - 1; } /* right square */ rightCol = (col + 1) % NUMCOLS; /***************** /* Set the status of all the squares /***************** /* Set the status of the upper square */ if (!theGrid[upperRow][col].occupied) { freeArray[0][0] = TRUE; freeArray[0][1] = upperRow; freeArray[0][2] = col; } /* Set the status of the lower square */ if (!theGrid[lowerRow][col].occupied) { freeArray[1][0] = TRUE; freeArray[1][1] = lowerRow; freeArray[1][2] = col; } /* Set the status of the left square */ if (!theGrid[row][leftCol].occupied) { freeArray[2][0] = TRUE; freeArray[2][1] = row; freeArray[2][2] = leftCol; } /* Set the status of the right square */ if (!theGrid[row][rightCol].occupied) { freeArray[3][0] = TRUE; freeArray[3][1] = row; freeArray[3][2] = rightCol; } /***************** /* Find a random free square /***************** /* pick a random direction */ dir = rand() % 4; /* loop through the array until we find a */ /* free square */ for (i = dir, j = 0; j < 4; i = (i + 1) % 4, j++) { if (freeArray[i][0] == TRUE) { /* return the free square */ *newRow = freeArray[i][1]; *newCol = freeArray[i][2]; return TRUE; } } /* nothing found */ return FALSE; }