void testMatrixMulti(void) { int **matrixA, **matrixB; int **matrixResult; int rowNumOfA, colNumOfA; int rowNumOfB, colNumOfB; int rowNumOfR, colNumOfR; rowNumOfA = 2; colNumOfA = 3; rowNumOfB = 3; colNumOfB = 2; matrixA = generateMatrix(rowNumOfA, colNumOfA); matrixB = generateMatrix(rowNumOfB, colNumOfB); matrixResult = make2DArray(rowNumOfA, colNumOfA); printf("----------- test: matrix multiplication ------------------\n"); if(!matrixMulti(matrixResult, matrixA, rowNumOfA, colNumOfA, matrixB, rowNumOfB, colNumOfB)) { printf("matrixA\n"); printMatrix(matrixA, rowNumOfA, colNumOfA); printf("matrixB\n"); printMatrix(matrixB, rowNumOfB, colNumOfB); printf("matrixResult = matrixA x matrixB\n"); rowNumOfR = rowNumOfA; colNumOfR = colNumOfB; printMatrix(matrixResult, rowNumOfR, colNumOfR); } else printf("Fail to matrix multiplication\n"); }
void testMatrixSum(void) { int **matrixA, **matrixB; int **matrixResult; int rowNum, colNum; rowNum = colNum = 3; matrixA = generateMatrix(rowNum, colNum); matrixB = generateMatrix(rowNum, colNum); matrixResult = make2DArray(rowNum, colNum); matrixSum(matrixResult, matrixA, matrixB, rowNum, colNum); printf("----------- test: matrix addition ------------------\n"); printf("matrixA\n"); printMatrix(matrixA, rowNum, colNum); printf("matrixB\n"); printMatrix(matrixB, rowNum, colNum); printf("matrixResult = matrixA + matrixB\n"); printMatrix(matrixResult, rowNum, colNum); }
void testMake2DArray(void) { int **twoDimArray; int rowNum, colNum; int rowIdx, colIdx; printf("start testMake2DArray()\n"); rowNum = 5; colNum = 5; twoDimArray = make2DArray(rowNum, colNum); for(rowIdx = 0; rowIdx < rowNum; rowIdx++) for(colIdx = 0; colIdx < colNum; colIdx++) twoDimArray[rowIdx][colIdx] = rand() % 10; print2DArray(twoDimArray, rowNum, colNum); return; }
// // 테스트 함수: 동적 할당된 2차원 배열 메모리의 함수 전달 및 출력 // void testMake2DArray(void) { int **twoDimArray; int rowNum, colNum; int rowIdx, colIdx; printf("start testMake2DArray()\n"); // 행과 열의 개수 지정 rowNum = 5; colNum = 5; // 지정된 크기의 matrix 메모리 생성 twoDimArray = make2DArray(rowNum, colNum); // matrix의 각 원소에 임의의 값 저장 for(rowIdx = 0; rowIdx < rowNum; rowIdx++) for(colIdx = 0; colIdx < colNum; colIdx++) twoDimArray[rowIdx][colIdx] = rand() % 10; print2DArray(twoDimArray, rowNum, colNum); return; }
void testMatrixTranspose(void) { int **matrixA; int **matrixAT; int rowNumOfA, colNumOfA; int rowNumOfAT, colNumOfAT; rowNumOfA = 2; colNumOfA = 3; matrixA = generateMatrix(rowNumOfA, colNumOfA); rowNumOfAT = colNumOfA; colNumOfAT = rowNumOfA; matrixAT = make2DArray(rowNumOfAT, colNumOfAT); printf("----------- test: matrix transpose ------------------\n"); matrixTranspose(matrixAT, matrixA, rowNumOfA, colNumOfA); printf("matrixA\n"); printMatrix(matrixA, rowNumOfA, colNumOfA); printf("matrixAT\n"); printMatrix(matrixAT, rowNumOfAT, colNumOfAT); }
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; }