static bool testCalcDepth(int imageWidth, int imageHeight, int featureWidth, int featureHeight, int maximumDisplacement) { float* leftImage = (float*)malloc(sizeof(float)* imageWidth * imageHeight); if (leftImage == NULL) { allocationFailed(); } float* rightImage = (float*)malloc(sizeof(float)* imageWidth * imageHeight); if (rightImage == NULL) { free(leftImage); allocationFailed(); } fillRandomFloat(leftImage, imageWidth * imageHeight); fillRandomFloat(rightImage, imageWidth * imageHeight); float* depthNaive = (float*)malloc(sizeof(float)* imageWidth * imageHeight); if (depthNaive == NULL) { free(leftImage); free(rightImage); allocationFailed(); } float* depthOptimized = (float*)malloc(sizeof(float)* imageWidth * imageHeight); if (depthOptimized == NULL) { free(leftImage); free(rightImage); free(depthNaive); allocationFailed(); } calcDepthNaive(depthNaive, leftImage, rightImage, imageWidth, imageHeight, featureWidth, featureHeight, maximumDisplacement, NULL); calcDepthOptimized(depthOptimized, leftImage, rightImage, imageWidth, imageHeight, featureWidth, featureHeight, maximumDisplacement); for (size_t i = 0; i < imageWidth * imageHeight; i++) { if (!floatsWithinTolerance(depthNaive[i], depthOptimized[i])) { free(leftImage); free(rightImage); free(depthNaive); free(depthOptimized); return false; } } free(leftImage); free(rightImage); free(depthNaive); free(depthOptimized); return true; }
int isEndGame(){ int canPlayerMove = Board_possibleMovesExist(&board, turn); if (canPlayerMove == -1){ allocationFailed(); } //losing scenario if (Board_isInCheck(&board, turn)){ isInCheck = 1; if (!canPlayerMove){ gameEnded = 1; return 1; } else{ if (displayMode == CONSOLE){ printf("Check!\n"); } } } else{ isInCheck = 0; } //tie scenario if(!canPlayerMove){ gameEnded = 1; return 1; } return 0; }
int main(int argc, char* argv[]){ displayMode = CONSOLE; if(argc>1){ if (str_equals(argv[1], "gui")){ displayMode = GUI; } } int initializationError = initialize(); if (initializationError == 1){ // allocation error occured allocationFailed(); } else if (initializationError == 2){ // SDL failed to initialize exit(0); } display(); while (1){ if (isEndGame()){ if (displayMode == CONSOLE){ break; } else{ gameEnded = 1; } } if (turn != player1 && gameMode == SINGLE_PLAYER_MODE && !gameEnded){ if (computerTurn()){ allocationFailed(); } } else{ short error = humanTurn(turn); if (error != 0 && error != 2){ //error occured or "start" command entered return error; } } } if (displayMode == CONSOLE){ printEndGameResults(); } return 0; }
/* * Prints relevant error message. * * @params: (error) - the exitcode of the error */ void printError(int error){ switch (error){ case 1: allocationFailed(); case 0: break; case -1: printf("Illegal command, please try again\n"); break; case -2: printf("Invalid position on the board\n"); break; case -3: printf("Wrong game mode\n"); break; case -4: printf("Wrong value for minimax depth. The value should be between 1 to 4\n"); break; case -5: printf("The specified position does not contain your piece\n"); break; case -6: printf("Illegal move\n"); break; case -7: printf("Wrong board initialization\n"); break; case -8: printf("Setting this piece creates an invalid board\n"); break; case -9: printf("Wrong file name\n"); break; case -10:printf("Error: standard function fprintf has failed\n"); break; case -11:printf("Wrong position for a rook\n"); break; case -12:printf("Illegal castling move\n"); break; } }
/* * The computer turn procedure. */ int computerTurn(){ PossibleMove* bestMove = getBestMove(); if (!bestMove){ allocationFailed(); } if (displayMode == CONSOLE){ printf("Computer: move "); PossibleMove_print(bestMove); } Board_update(&board, bestMove); PossibleMove_free(bestMove); turn = !turn; if (display()){ return 1; } return 0; }
static bool benchmarkMatrix(int imageWidth, int imageHeight, int featureWidth, int featureHeight, int maximumDisplacement) { float* left = (float*)malloc(sizeof(float)* imageWidth * imageHeight); if (left == NULL) { allocationFailed(); } float* right = (float*)malloc(sizeof(float)* imageWidth * imageHeight); if (right == NULL) { free(left); allocationFailed(); } fillRandomFloat(left, imageWidth * imageHeight); fillRandomFloat(right, imageWidth * imageHeight); float* depthNaive = (float*)malloc(sizeof(float)* imageWidth * imageHeight); if (depthNaive == NULL) { free(left); free(right); allocationFailed(); } float* depthOptimized = (float*)malloc(sizeof(float)* imageWidth * imageHeight); if (depthOptimized == NULL) { free(left); free(right); free(depthNaive); allocationFailed(); } double speedup; uint64_t startTSC, endTSC; // Get total clock cycles for optimised startTSC = __rdtsc(); calcDepthOptimized(depthOptimized, left, right, imageWidth, imageHeight, featureWidth, featureHeight, maximumDisplacement); endTSC = __rdtsc(); double optimizedTSC = endTSC - startTSC; // Get total clock cycles for naive startTSC = __rdtsc(); calcDepthNaive(depthNaive, left, right, imageWidth, imageHeight, featureWidth, featureHeight, maximumDisplacement); endTSC = __rdtsc(); double naiveTSC = endTSC - startTSC; // Speedup is just naive / optimised speedup = naiveTSC / optimizedTSC; printf("%.4f Speedup Ratio\r\n", speedup); for (size_t i = 0; i < imageWidth * imageHeight; i++) { if (!floatsWithinTolerance(depthNaive[i], depthOptimized[i])) { free(left); free(right); free(depthNaive); free(depthOptimized); return false; } } free(left); free(right); free(depthNaive); free(depthOptimized); return true; }
static bool testMatrix(int imageWidth, int imageHeight, int featureWidth, int featureHeight, int maximumDisplacement) { float* left = (float*)malloc(sizeof(float)* imageWidth * imageHeight); if (left == NULL) { allocationFailed(); } float* right = (float*)malloc(sizeof(float)* imageWidth * imageHeight); if (right == NULL) { free(left); allocationFailed(); } fillRandomFloat(left, imageWidth * imageHeight); fillRandomFloat(right, imageWidth * imageHeight); float* depthNaive = (float*)malloc(sizeof(float)* imageWidth * imageHeight); if (depthNaive == NULL) { free(left); free(right); allocationFailed(); } float* depthOptimized = (float*)malloc(sizeof(float)* imageWidth * imageHeight); if (depthOptimized == NULL) { free(left); free(right); free(depthNaive); allocationFailed(); } size_t floatOps = 0; calcDepthNaive(depthNaive, left, right, imageWidth, imageHeight, featureWidth, featureHeight, maximumDisplacement, &floatOps); calcDepthOptimized(depthOptimized, left, right, imageWidth, imageHeight, featureWidth, featureHeight, maximumDisplacement); int result = true; for (size_t i = 0; i < imageWidth * imageHeight; i++) { if (!floatsWithinTolerance(depthNaive[i], depthOptimized[i])) { result = false; } } if (true || !result) { printf("Left image: \n"); printFloatImg(left, imageWidth, imageHeight); printf("Right iamge: \n"); printFloatImg(right, imageWidth, imageHeight); printf("Expected: \n"); printFloatImg(depthNaive, imageWidth, imageHeight); printf("Actual: \n"); printFloatImg(depthOptimized, imageWidth, imageHeight); } free(left); free(right); free(depthNaive); free(depthOptimized); return result; }