int main(int argc, char *argv[]) { int *array; int width, height; int value; char **stringPtr; int **matrix; /* testing for part 1 */ printf("Testing Part 1\n"); width = 5; height = 6; array = create2DArray(height, width); printf("Store value 7 at [3,4].\n"); set2DElement(array, 3, 4, 7); value = get2DElement(array, 3, 4); printf("Retrieve value %d from [3,4]\n\n", value); free2DArray(array); /* testing for part 2 */ printf("Testing Part 2\n"); stringPtr = createStringArray(100); printf("Store string - fred\n"); setStringArray(stringPtr, 44, "fred"); printf("Store string - barney\n"); setStringArray(stringPtr, 80, "barney"); printf("Get string - %s\n", getStringArray(stringPtr, 44)); printf("Get string - %s\n", getStringArray(stringPtr, 80)); /* test with NULL string */ printf("Get string - %s\n\n", getStringArray(stringPtr, 3)); freeStringArray(stringPtr, 100); /* testing for part 3 */ printf("Testing Part 3\n"); matrix = createArray(100, 100); printf("Store 33 44 55\n"); matrix[22][76] = 33; matrix[83][29] = 44; matrix[99][65] = 55; printf("Retrieve %d %d %d\n", matrix[22][76], matrix[83][29], matrix[99][65]); freeArray(matrix, 100); return(1); }
void ShowMainMenu() { int choice; DisplayInfo *mainMenu=(DisplayInfo *)malloc(sizeof(DisplayInfo)); mainMenu->curPos = 0; mainMenu->size = 4; init2DArray(&(mainMenu->words), MainStr, mainMenu->size); InitDisplay(mainMenu); for(;;) { switch(keyValue) { case Up: { keyValue = Null; CursorMoveUp(mainMenu); break; } case Down: { keyValue = Null; CursorMoveDown(mainMenu); break; } case Enter: { keyValue = Null; choice = mainMenu->curPos; switch(choice) { case 0:ParaMenu();break; case 1:ComMenu();break; case 2:ConstMenu();break; case 3:BacklistMenu();break; } ResumeDisplay(mainMenu); break; } default: { break; } } } free2DArray(&(mainMenu->words),mainMenu->size); free(mainMenu); }
Grid::~Grid() { free2DArray(value); }
void destroyDungeon(dungeon_t dungeon) { turnDestroy(&dungeon); monstersDestroy(&dungeon); free2DArray((void **) dungeon.grid, HEIGHT); free(dungeon.rooms); }
void tetrisSinglePlayer(bool** ledArray) { /*Setting values of the global variables for Tetris:*/ TOP_MARGIN = 0.0; BOT_MARGIN = 0.0; LEFT_MARGIN = 26.0; RIGHT_MARGIN = 26.0; BOT_END = ARRAY_HEIGHT - BOT_MARGIN - 1.0; RIGHT_END = ARRAY_WIDTH - RIGHT_MARGIN - 1.0; /*Tetris-specific constants:*/ const int PIECE_WIDTH = 8; /*Must be evenly divisible by 4*/ const int SQUARE_WIDTH = PIECE_WIDTH / 4; const int LEFT_BORDER = 2; /*Should be a multiple of SQUARE_WIDTH*/ const int RIGHT_BORDER = 2; const int TOP_BORDER = 0; const int BOT_BORDER = 2; const int INIT_X = LEFT_MARGIN + (RIGHT_END - LEFT_MARGIN - PIECE_WIDTH) / 2 + 1; const int INIT_Y = TOP_BORDER; srand(time(NULL)); /*"Bag" of upcoming piece types: ("double" indicating two sets)*/ int* doubleBag = make1DArray(14); refillBag(doubleBag, true); /*Tetris-specific variables:*/ float curX = INIT_X; float curY = INIT_Y; float projX = curX; float projY = curY; float shadY = curY; int curType = pluckBag(doubleBag); int pieceOrien = 1; /*1-4 corresponds to north, east, south, west*/ int score = 0; int input = 0; int timer = 1; int dropTime = 15; /*Should be > 1; may be decreased for difficulty, but decreasing it might also reduce responsiveness*/ int garbageLines = 0; /*Solid borders:*/ drawCheckerboard(ledArray, TOP_MARGIN, LEFT_MARGIN, BOT_END - TOP_MARGIN + 1, LEFT_BORDER); drawCheckerboard(ledArray, TOP_MARGIN, RIGHT_END + 1 - RIGHT_BORDER, BOT_END - TOP_MARGIN + 1, RIGHT_BORDER); drawCheckerboard(ledArray, TOP_MARGIN, LEFT_MARGIN, TOP_BORDER, RIGHT_END - LEFT_MARGIN + 1); drawCheckerboard(ledArray, BOT_END + 1 - BOT_BORDER, LEFT_MARGIN, BOT_BORDER, RIGHT_END - LEFT_MARGIN + 1); /*Current piece state and its next projected state:*/ bool** curPiece = make2DArray(PIECE_WIDTH, PIECE_WIDTH); importPiece(curPiece, curType, pieceOrien, PIECE_WIDTH); bool** projPiece = make2DArray(PIECE_WIDTH, PIECE_WIDTH); copyPiece(projPiece, curPiece, PIECE_WIDTH); while(checkOverlap(ledArray, projPiece, curPiece, shadY + SQUARE_WIDTH, curX, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false) == 0) { shadY += SQUARE_WIDTH; } while(1) { drawPiece(ledArray, curPiece, curType, false, curY, curX, PIECE_WIDTH); drawShadow(ledArray, curPiece, curType, false, shadY, curX, PIECE_WIDTH); drawPiece(ledArray, projPiece, curType, true, projY, projX, PIECE_WIDTH); copyPiece(curPiece, projPiece, PIECE_WIDTH); curX = projX; curY = projY; shadY = curY; while(checkOverlap(ledArray, projPiece, curPiece, shadY + SQUARE_WIDTH, curX, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false) == 0) { shadY += SQUARE_WIDTH; } drawShadow(ledArray, curPiece, curType, true, shadY, curX, PIECE_WIDTH); frameTest(ledArray, TOP_MARGIN, LEFT_MARGIN, BOT_MARGIN, RIGHT_MARGIN ); input = getLeftInput(); if (input == 1) { /*Hard drop*/ while(checkOverlap(ledArray, projPiece, curPiece, projY + SQUARE_WIDTH, projX, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false) == 0) { projY += SQUARE_WIDTH; drawPiece(ledArray, curPiece, curType, false, curY, curX, PIECE_WIDTH); copyPiece(curPiece, projPiece, PIECE_WIDTH); curX = projX; curY = projY; drawPiece(ledArray, curPiece, curType, true, curY, curX, PIECE_WIDTH); frameTest(ledArray, TOP_MARGIN, LEFT_MARGIN, BOT_MARGIN, RIGHT_MARGIN ); } score = checkLines(ledArray, LEFT_MARGIN + LEFT_BORDER, RIGHT_END - RIGHT_BORDER, BOT_END - BOT_BORDER - garbageLines * SQUARE_WIDTH, TOP_MARGIN + TOP_BORDER, score, curY, PIECE_WIDTH, SQUARE_WIDTH); /* if(some measure of time has passed) { if (addGarbage(ledArray, LEFT_MARGIN + LEFT_BORDER, RIGHT_END - RIGHT_BORDER, BOT_END - BOT_BORDER, TOP_MARGIN + TOP_BORDER, SQUARE_WIDTH)) { break; //Game loss } else { garbageLines++; } } */ projX = INIT_X; projY = INIT_Y; curX = projX; curY = projY; shadY = curY; curType = pluckBag(doubleBag); pieceOrien = 1; timer = 1; importPiece(curPiece, curType, pieceOrien, PIECE_WIDTH); copyPiece(projPiece, curPiece, PIECE_WIDTH); if(checkOverlap(ledArray, projPiece, curPiece, projY, projX, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, true)) { break; /*Game loss*/ } } else if(input == 5 || timer++ % dropTime == 0) { /*Soft drop*/ if(checkOverlap(ledArray, projPiece, curPiece, projY + SQUARE_WIDTH, projX, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false)) { score = checkLines(ledArray, LEFT_MARGIN + LEFT_BORDER, RIGHT_END - RIGHT_BORDER, BOT_END - BOT_BORDER - garbageLines * SQUARE_WIDTH, TOP_MARGIN + TOP_BORDER, score, curY, PIECE_WIDTH, SQUARE_WIDTH); /* if(some measure of time has passed) { if (addGarbage(ledArray, LEFT_MARGIN + LEFT_BORDER, RIGHT_END - RIGHT_BORDER, BOT_END - BOT_BORDER, TOP_MARGIN + TOP_BORDER, SQUARE_WIDTH)) { break; //Game loss } else { garbageLines++; } } */ projX = INIT_X; projY = INIT_Y; curX = projX; curY = projY; shadY = curY; curType = pluckBag(doubleBag); pieceOrien = 1; timer = 1; importPiece(curPiece, curType, pieceOrien, PIECE_WIDTH); copyPiece(projPiece, curPiece, PIECE_WIDTH); if(checkOverlap(ledArray, projPiece, curPiece, projY, projX, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, true)) { break; /*Game loss*/ } } else { projY += SQUARE_WIDTH; timer = 1; } } else if(input == 9) { /*Spin clockwise*/ pieceOrien++; if(pieceOrien > 4) { pieceOrien -= 4; } importPiece(projPiece, curType, pieceOrien, PIECE_WIDTH); if(checkOverlap(ledArray, projPiece, curPiece, projY, projX, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false)) { if(!checkOverlap(ledArray, projPiece, curPiece, projY, projX + SQUARE_WIDTH, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false)) { projX += SQUARE_WIDTH; } else if(curType == 0 && !checkOverlap(ledArray, projPiece, curPiece, projY, projX + 2 * SQUARE_WIDTH, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false)) { projX += 2 * SQUARE_WIDTH; } else if(!checkOverlap(ledArray, projPiece, curPiece, projY, projX - SQUARE_WIDTH, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false)) { projX -= SQUARE_WIDTH; } else if(curType == 0 && !checkOverlap(ledArray, projPiece, curPiece, projY, projX - 2 * SQUARE_WIDTH, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false)) { projX -= 2 * SQUARE_WIDTH; } else { pieceOrien--; if(pieceOrien < 1) { pieceOrien += 4; } importPiece(projPiece, curType, pieceOrien, PIECE_WIDTH); } } } else if(input == 10) { /*Spin counterclockwise*/ pieceOrien--; if(pieceOrien < 1) { pieceOrien += 4; } importPiece(projPiece, curType, pieceOrien, PIECE_WIDTH); if(checkOverlap(ledArray, projPiece, curPiece, projY, projX, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false)) { if(!checkOverlap(ledArray, projPiece, curPiece, projY, projX + SQUARE_WIDTH, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false)) { projX += SQUARE_WIDTH; } else if(curType == 0 && !checkOverlap(ledArray, projPiece, curPiece, projY, projX + 2 * SQUARE_WIDTH, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false)) { projX += 2 * SQUARE_WIDTH; } else if(!checkOverlap(ledArray, projPiece, curPiece, projY, projX - SQUARE_WIDTH, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false)) { projX -= SQUARE_WIDTH; } else if(curType == 0 && !checkOverlap(ledArray, projPiece, curPiece, projY, projX - 2 * SQUARE_WIDTH, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false)) { projX -= 2 * SQUARE_WIDTH; } else { pieceOrien++; if(pieceOrien > 4) { pieceOrien -= 4; } importPiece(projPiece, curType, pieceOrien, PIECE_WIDTH); } } } else if(input == 3) { /*Move right*/ if(checkOverlap(ledArray, projPiece, curPiece, projY, projX + SQUARE_WIDTH, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false) == 0) { projX += SQUARE_WIDTH; } } else if(input == 7) { /*Move left*/ if(checkOverlap(ledArray, projPiece, curPiece, projY, projX - SQUARE_WIDTH, curY, curX, PIECE_WIDTH, SQUARE_WIDTH, false) == 0) { projX -= SQUARE_WIDTH; } } } drawPiece(ledArray, curPiece, curType, true, curY, curX, PIECE_WIDTH); frameTest(ledArray, TOP_MARGIN, LEFT_MARGIN, BOT_MARGIN, RIGHT_MARGIN ); free(doubleBag); free2DArray(curPiece, PIECE_WIDTH); free2DArray(projPiece, PIECE_WIDTH); return; }
int main(int argc, char* argv[]) { srand((unsigned)time(NULL)); int N = 5; if (argc > 1) N = atoi(argv[1]); int i, j; //To be used in future loops Complex** c = create2DComplexArray(N); //Counters for stats: int correctFirstTime = 0, correctNTime = 0, correctWithHints = 0; //Main loop1: int iteratorCounter1 = 1, maxIterations1 = 1000000; //To guarantee no infinite loops do { int x = rand() % N, y = rand() % N; Complex* c_Origin = &c[0][0]; Complex* c_Selected = &c[x][y]; int hintsUsed = 0; //Main loop2: int iteratorCounter2 = 1, maxIterations2 = 1000000; //To guarantee no infinite loops do { printf("M[0][0]=%p. M[i][j]=%p. What's i and j? (or Q or H or HH or HHH): ", c_Origin, c_Selected); char* userInput = malloc(36 * sizeof(char)); fgets(userInput, 36, stdin); if (userInput[0] == 'Q' || userInput[0] == 'q') { free(userInput); goto exit; } else if (userInput[0] == 'H' || userInput[0] == 'h') { int hCount = 0; for (i = 0; i < 32; i++) { if (userInput[i] == 'H' || userInput[i] == 'h') hCount++; else break; } free(userInput); hintsUsed++; showHints(hCount, c, N); } else { int* parsedResult = parseGuess(userInput); int _x = parsedResult[0]; int _y = parsedResult[1]; free(parsedResult); free(userInput); if (_x == -1 || y == -1) continue; if (x == _x && y == _y) { printf("RIGHT\n"); if (hintsUsed > 0) correctWithHints++; else if (iteratorCounter2 > 1) correctNTime++; else correctFirstTime++; break; } else { printf("WRONG\n"); continue; } } } while (iteratorCounter2++ < maxIterations2); } while (iteratorCounter1++ < maxIterations1); exit: free2DArray(c, N); printStats(--iteratorCounter1, correctFirstTime, correctNTime, correctWithHints); return 0; }