Exemplo n.º 1
0
int getBestMoveHuyAI(int **x, int row, int col){
	//Function pointers
	void (* funcs[4])(int **, int, int,int *) = {&up, &down, &left, &right};
	int (* tests[4])(int **, int, int) = {&upTest,&downTest,&leftTest,&rightTest};

	//First try every move and get their scores 
	//and the number of empty spaces they create
	int *scores = malloc(sizeof(int)*4);
	int *empty = malloc(sizeof(int)*4);
	int *monotonic = malloc (sizeof(int)*4);
	
	for (int i = 0; i < 4; i++) {
		// Clone Array
			int **clone = malloc(sizeof(int *) * row);

			for(int k = 0; k < row; k++){
				clone[k] = malloc(sizeof(int) *col);
				copy_int_array(x[k],clone[k], col);
			}
        
        scores[i] = 0;
        empty[i] = countEmpty(clone,row, col);
        monotonic[i] = 0;
		if (simplifyTest(tests[i],clone,row, col)==1) {
			//Calculate scores and merges of the move
			simplify(funcs[i], clone , row, col,&scores[i]);
			//Add 1 to seperate legal and illegal moves
			//To choose a move when there are no merges
			scores[i]++;
			empty[i] = countEmpty(clone,row, col) - empty[i];	

			//Check monotonic preserverance of the move
			monotonic[i] += MonotonicTest(clone,row,col);
		} else empty[i] = 0;

		//Formular to calculate the score of each move to choose the best move
		//based on the priority of the three properties above
		//monotonic > merge > score

		scores[i] += monotonic[i]*100 + empty[i]*10;

		//Free clone array
		for(int k = 0; k < row; k++){
				free (clone[k]);
			}
		free (clone);
	}
	
	int bestMove = 0;
	for (int i = 0; i<4;i++) {
		if (scores[i]>=scores[bestMove]) 
			bestMove = i;
	}
	//Free memory
	free (scores);
	free (empty);
	free (monotonic);

	return bestMove;
}
Exemplo n.º 2
0
bool board_2048::gameEnded() {
    bool ended = true;
    if(winGame())
    {
         ui->curScore->setText("WIN");
         return ended;
    }
    if (countEmpty() > 0) {
        return false;
    }
    if (findPairDown()) {
        return false;
    }

    transpose();
    if (findPairDown()) {
        ended = false;
    }
    transpose();
    transpose();
    transpose();
    if(ended==true)
    ui->curScore->setText("END");
    return ended;
}
Exemplo n.º 3
0
bool gameEnded(uint8_t board[SIZE][SIZE]) {
  bool ended = true;
  if (countEmpty(board)>0) return false;
  if (findPairDown(board)) return false;
  rotateBoard(board);
  if (findPairDown(board)) ended = false;
  rotateBoard(board);
  rotateBoard(board);
  rotateBoard(board);
  return ended;
}
Exemplo n.º 4
0
int main()
{
    //Performance Calculation Variables
    clock_t begin, end;
    double processTime;
    /////////////////////////

    hashTable* hTable = NULL;
    wList* wordList = NULL;

	int i=0;
    char** fileNames = getCWDtxtFiles();
    FILE* file[NumOftxtFiles];

	for(i=0;i<NumOftxtFiles;++i)
    {
        file[i] = fopen(fileNames[i], "r");
    }

	FILE* out = fopen("Analysis/output.txt", "a");
    char lol[50] = {'\0'};

    begin = clock();

	for(i=0;i<NumOftxtFiles;++i)
    {
        while(!feof(file[i]))
        {
            fscanf(file[i], "%s", lol);
            trimWord(lol);
            if(lol[0] == '\0')
                continue;
            wordList = addWordToList(wordList, lol, i+1);
        }
    }
	storeBST();
	//Performance Calculation--
    end  = clock();
    processTime = (double)(end-begin)/CLOCKS_PER_SEC;
    //Performance Calculation--

	int numWords = countWords(wordList);
    hTable = createHashTable(numWords);
    addToTableFromList(hTable, wordList);

    //printHashTable(hTable);
    //printHashTableToFile(hTable, out);
    fclose(out);
	fclose(out);

	for(i=0;i<2;++i)
    {
        fclose(file[i]);
    }

	FILE* record = fopen("Analysis/record.txt", "a");
    int emptyCount = countEmpty(hTable);
	fprintf(record, "WordCount : %d Empty : %d ProcessTime : %f s\n", numWords, emptyCount, processTime);

	fclose(record);

    printf("\nNo in list = %d %d", countWords(wordList), numWords);
    int c = 0;
    for(i=0;i<numWords;i++){
        bucket* b = hTable->table[i];
        while(b != NULL){
            ++c;
            b = b->next;
        }
    }

	//Driver Loop
	char ans = 'y';
    while(ans == 'y'){
        printBannner(processTime);
        printf("\n\t"); printf("__________________________________________________________\n\n");
        printf("\t"); printf("                        Enter the Query                              \n\n");
        printf("\t\t\t\t"); scanf("%s",lol);
        printf("\t"); printf("__________________________________________________________\n");
        if(lol[strlen(lol)-1] == '*'){
            makeList(wordList, lol);
            char* word = NULL;
            while((word = getNextWord())!= NULL){
                printf("\n\t\t");printf("%s : \n", word);
                searchText(word, hTable, fileNames, NumOftxtFiles);
            }
        }
        else{
            trimWord(lol);
        	char* word = getCorrectWord(lol);
        	if(strcmp(word, lol) != 0)
        	{
				printf("\n\t\t\t");printf("Corrected Word : %s\n", word);
            	searchText(word, hTable, fileNames, NumOftxtFiles);
            	printf("\n\t\t\t");printf("Do you want to search for original word? y/n : ");
				char a = 'y';
				while(getchar() != '\n');
				scanf("%c", &a);
				if(a == 'y')
					searchText(lol, hTable, fileNames, NumOftxtFiles);
        	}
        	else
        		searchText(lol, hTable, fileNames, NumOftxtFiles);
        }

        printf("\n\n\t\t\t");printf("Search Again? y/n : ");
        while(getchar() != '\n');
        scanf("%c", &ans);
    }

    return 0;
}