Exemple #1
0
int main(void) {
    int in[NSTR];
    int i, j;
    unsigned seed = 0;

    for (i = 0; i < NSTR; i++) in[i] = 0;
    array = NULL;
    arraylen = arraysize = 0;
    tree = newtree234(mycmp);
    cmp = mycmp;

    verify();
    for (i = 0; i < 10000; i++) {
        j = randomnumber(&seed);
        j %= NSTR;
        printf("trial: %d\n", i);
        if (in[j]) {
            printf("deleting %s (%d)\n", strings[j], j);
            deltest(strings[j]);
            in[j] = 0;
        } else {
            printf("adding %s (%d)\n", strings[j], j);
            addtest(strings[j]);
            in[j] = 1;
        }
    }

    while (arraylen > 0) {
        j = randomnumber(&seed);
        j %= arraylen;
        deltest(array[j]);
    }

    return 0;
}
int main(void) {
    int in[NSTR];
    int i, j, k;
    unsigned seed = 0;

    for (i = 0; i < NSTR; i++) in[i] = 0;
    array = NULL;
    arraylen = arraysize = 0;
    tree = newtree234(mycmp);
    cmp = mycmp;

    verify();
    for (i = 0; i < 10000; i++) {
        j = randomnumber(&seed);
        j %= NSTR;
        printf("trial: %d\n", i);
        if (in[j]) {
            printf("deleting %s (%d)\n", strings[j], j);
            deltest(strings[j]);
            in[j] = 0;
        } else {
            printf("adding %s (%d)\n", strings[j], j);
            addtest(strings[j]);
            in[j] = 1;
        }
	findtest();
    }

    while (arraylen > 0) {
        j = randomnumber(&seed);
        j %= arraylen;
        deltest(array[j]);
    }

    freetree234(tree);

    tree = newtree234(NULL);
    cmp = NULL;
    verify();
    for (i = 0; i < 1000; i++) {
	printf("trial: %d\n", i);
	j = randomnumber(&seed);
	j %= NSTR;
	k = randomnumber(&seed);
	k %= count234(tree)+1;
	printf("adding string %s at index %d\n", strings[j], k);
	addpostest(strings[j], k);
    }
    while (count234(tree) > 0) {
	printf("cleanup: tree size %d\n", count234(tree));
	j = randomnumber(&seed);
	j %= count234(tree);
	printf("deleting string %s from index %d\n", array[j], j);
	delpostest(j);
    }

    return 0;
}
Exemple #3
0
int main( )
{
	struct airport a ;
	int i, pri, curtime, endtime ;
	double expectarrive, expectdepart ;
	struct plane temp ;

  system ( "cls" ) ;

    initairport ( &a );

	start ( &endtime, &expectarrive, &expectdepart ) ;

	for ( curtime = 1 ; curtime <= endtime ; curtime++ )
	{
		pri = randomnumber ( expectarrive ) ;

		for ( i = 1 ; i <= pri ; i++ )
		{
			newplane ( &a, curtime, ARRIVE ) ;
			if ( apfull ( a, 'l' ) )
				 refuse ( &a, ARRIVE ) ;
			else
				apaddqueue( &a, 'l' ) ;
		}

		pri = randomnumber ( expectdepart ) ;
		for ( i = 1 ; i <= pri ; i++ )
		{
			newplane ( &a, curtime, DEPART ) ;
			if ( apfull ( a, 't' ) )
			   refuse ( &a, DEPART ) ;
			else
			   apaddqueue ( &a, 't' ) ;
		}

		if (  ! ( apempty ( a, 'l' ) ) )
		{
			temp = apdelqueue ( &a, 'l' ) ;
			land ( &a, temp, curtime ) ;
		}
		else
		{
			if ( ! ( apempty ( a, 't' ) ) )
			{
				temp = apdelqueue ( &a, 't' ) ;
				fly ( &a, temp, curtime ) ;
			}
			else
				idle ( &a, curtime ) ;
		}
	}

	conclude ( &a, endtime ) ;

    return 0 ;
}
Exemple #4
0
int main(int argc, char *argv[]){
	
	int pass;

	srand(time(0));
	for (pass = 0; pass < atoi(argv[1]); pass++)
		printf("%d: %d\n", pass, (int) (randomnumber(atoi(argv[2]), atoi(argv[3]))));

	return 0;
}
Exemple #5
0
int main(void) {
    int in[NSTR];
    int i, j, k;
    unsigned seed = 0;

    for (i = 0; i < NSTR; i++) in[i] = 0;
    array = NULL;
    arraylen = arraysize = 0;
    tree = newtree234(mycmp);
    cmp = mycmp;

    verify();
    for (i = 0; i < 10000; i++) {
        j = randomnumber(&seed);
        j %= NSTR;
        printf("trial: %d\n", i);
        if (in[j]) {
            printf("deleting %s (%d)\n", strings[j], j);
            deltest(strings[j]);
            in[j] = 0;
        } else {
            printf("adding %s (%d)\n", strings[j], j);
            addtest(strings[j]);
            in[j] = 1;
        }
	findtest();
    }

    while (arraylen > 0) {
        j = randomnumber(&seed);
        j %= arraylen;
        deltest(array[j]);
    }

    freetree234(tree);

    /*
     * Now try an unsorted tree. We don't really need to test
     * delpos234 because we know del234 is based on it, so it's
     * already been tested in the above sorted-tree code; but for
     * completeness we'll use it to tear down our unsorted tree
     * once we've built it.
     */
    tree = newtree234(NULL);
    cmp = NULL;
    verify();
    for (i = 0; i < 1000; i++) {
	printf("trial: %d\n", i);
	j = randomnumber(&seed);
	j %= NSTR;
	k = randomnumber(&seed);
	k %= count234(tree)+1;
	printf("adding string %s at index %d\n", strings[j], k);
	addpostest(strings[j], k);
    }
    while (count234(tree) > 0) {
	printf("cleanup: tree size %d\n", count234(tree));
	j = randomnumber(&seed);
	j %= count234(tree);
	printf("deleting string %s from index %d\n", array[j], j);
	delpostest(j);
    }

    return 0;
}
Exemple #6
0
int sudoku(int mode)
{
	clear_window();
	int level = 1;
	/* we get a random number to select a sudoku */
	int n = randomnumber();
	int numbers[16];
	int numbers_def[16];
	/* get sudoku */
	for (int i = 0; i < 16; i++) {
		numbers_def[i] = make_sudoku(i, n, mode);
	}
	for (int i = 0; i < 16; i++) {
		numbers[i] = numbers_def[i];
	}
	int sudoku4x4[4][4];
	int subregions[4][4];
	char option = 'r';
	while (option != 'c' && option != 'C') {
		print_sudoku(numbers);
		for (int i = 0; i < 16; i++) {
			if (numbers[i] == 0) {
				printf("Introduce el valor a la X%d: ", i+1);
				numbers[i] = getnum();
				if (numbers[i] == 0) {
					numbers[i] = 1;
					i--;
				}
				/* if you press return key */
				if (numbers[i] == -1) {
					/* put numbers[i] and numbers[i-1] equal to 0
					 * if not you can't rewrite it 
					 */
					numbers[i] = 0;
					i--;
					if (numbers[i] != numbers_def[i]) {
						numbers[i] = 0;
						i--;
					}
				}
				printf("%c\n", numbers[i]);
				print_sudoku(numbers);
			}
		}
		for (int i = 0, k = 0, p=0; k < 4; i++) {
			sudoku4x4[k][i] = numbers[p];
			if ((i+1) % 4 == 0) {
				k++;
				i = -1;
			}
			p++;
		}
		for(int i = 0, k = 0; i < 4; i++) {
			for(int j = 0; j < 4; j++) {
				subregions[i][j] = numbers[k];
				k++;
			}
		}
		int gamestate = WIN;
		/* check rows and columns */
		for (int i = 0, k = 0; k < 4; i++) {
			if (sudoku4x4[k][i] == sudoku4x4[k][0] && i != 0) {
				gamestate = LOST;
			}
			if (sudoku4x4[k][i] == sudoku4x4[k][1] && i != 1) {
				gamestate = LOST;
			}
			if (sudoku4x4[k][i] == sudoku4x4[k][2] && i != 2) {
				gamestate = LOST;
			}
			if (sudoku4x4[k][i] == sudoku4x4[k][3] && i != 3) {
				gamestate = LOST;
			}
			if ((i+1) % 4 == 0) {
				k++;
				i = -1;
			}
		}
		for (int i = 0, k = 0; i < 4; k++) {
			if (sudoku4x4[k][i] == sudoku4x4[0][i] && k != 0) {
				gamestate = LOST;
			}
			if (sudoku4x4[k][i] == sudoku4x4[1][i] && k != 1) {
				gamestate = LOST;
			}
			if (sudoku4x4[k][i] == sudoku4x4[2][i] && k != 2) {
				gamestate = LOST;
			}
			if (sudoku4x4[k][i] == sudoku4x4[3][i] && k != 3) {
				gamestate = LOST;
			}
			if ((k+1) % 4 == 0) {
				i++;
				k = -1;
			}
		}
		/* check subregions */
		for (int i = 0, k = 0; k < 4; i++) {
			if (subregions[k][i] == subregions[k][0] && i != 0) {
				gamestate = LOST;
			}
			if (subregions[k][i] == subregions[k][1] && i != 1) {
				gamestate = LOST;
			}
			if (subregions[k][i] == subregions[k][2] && i != 2) {
				gamestate = LOST;
			}
			if (subregions[k][i] == subregions[k][3] && i != 3) {
				gamestate = LOST;
			}
			if ((i+1) % 4 == 0) {
				k++;
				i = -1;
			}
		}
		if (gamestate == WIN && mode < 2) {
			printf("Nivel %d COMPLETADO\n", level);
			do {
				printf("[N]ivel %d o [C]errar\n", level+1);
				option = getchar();
			} while (option != 'n' && option != 'N' && option != 'c' && option != 'C');
			n = randomnumber();
			level++;
		}
		if (gamestate == LOST && mode < 2) {
			printf("Nivel %d NO COMPLETADO\n", level);
			do {
				printf("[R]eintentar o [C]errar\n");
				option = getchar();
			} while (option != 'r' && option != 'R' && option != 'c' && option != 'C');
		}
		if (option == 'r' || option == 'R' || option == 'n' || option == 'N') {
			for (int i = 0; i < 16; i++) {
				numbers[i] = make_sudoku(i, n, mode);
			}
		}
		/* multiplayer mode */
		if (mode == COMPETITION_EASY && gamestate == WIN) {
			return 1;
		}
		if (mode == COMPETITION_DIFF && gamestate == WIN) {
			return 3;
		}
		if (mode >= COMPETITION_EASY && gamestate == LOST) {
			return 0;
		}
	}
	return 0;
}
Exemple #7
0
int sudoku(int mode)
{
	clear_window();
	int level = 1;
	/* we get a random number from 0 to 9 as a char value */
	char n[2] = {randomnumber(), '\0'};
	char path[256] = "/etc/sudoku/sudoku_";
	if (mode == LOAD_SUDOKU) {
		printf("Sudoku File Path: ");
		gets_s(path, 256);
	}
	else {
		if (mode == 0 || mode == 2) {
			strcat(path, "easy");
		}
		if (mode == 1 || mode == 3) {
			strcat(path, "difficult");
		}
		strcat(path, n);
	}
	int numbers[16];
	int numbers_def[16];
	/* get sudoku */
	for (int i = 0; i < 16; i++) {
		numbers_def[i] = make_sudoku(i, path);
		if (numbers_def[i] < 0 || numbers_def[i] > 4) {
			clear_window();
			printf("Error: El archivo '%s' es erroneo o corrupto\n", path);
			printf("Pulsa una tecla para continuar.\n");
			getchar();
			return -1;
		}
	}
	for (int i = 0; i < 16; i++) {
		numbers[i] = numbers_def[i];
	}
	int sudoku4x4[4][4];
	int subregions[4][4];
	char option = 'r';
	while (option != 'c' && option != 'C') {
		print_sudoku(numbers);
		for (int i = 0; i < 16; i++) {
			if (numbers[i] == 0) {
				printf("Introduce el valor a la X%d: ", i+1);
				numbers[i] = getnum();
				if (numbers[i] > 4 || numbers[i] == 0) {
					numbers[i] = 1;
				}
				/* if you press return key */
				if (numbers[i] == -1) {
					/* put numbers[i] and numbers[i-1] equal to 0
					 * if not you can't rewrite it 
					 */
					numbers[i] = 0;
					i--;
					if (numbers[i] != numbers_def[i] && i > 0) {
						numbers[i] = 0;
						i--;
					}
				}
				printf("%c\n", numbers[i]);
				print_sudoku(numbers);
			}
		}
		for (int i = 0, k = 0, p=0; k < 4; i++) {
			sudoku4x4[k][i] = numbers[p];
			if ((i+1) % 4 == 0) {
				k++;
				i = -1;
			}
			p++;
		}
		for(int i = 0, k = 0; i < 4; i++) {
			int action = 0;
			for(int j = 0; j < 4; j++) {
				subregions[i][j] = numbers[k];
				if ((k+1) % 2 == 0) {
					if ((k+1) == 8) {
						k = 5;
						action = 0;
					}
					if (action == 0) {
						k += 3;
						action++;
					}
					else {
						k -= 3;
						action--;
					}
				}
				else {
					k++;
				}
			}
		}
		int gamestate = WIN;
		/* check rows and columns */
		for (int i = 0, k = 0; k < 4; i++) {
			for (int x = 0; x < 4; x++) {
				if (sudoku4x4[k][i] == sudoku4x4[k][x] && i != x) {
					gamestate = LOST;
				}
			}
			if ((i+1) % 4 == 0) {
				k++;
				i = -1;
			}
		}
		for (int i = 0, k = 0; i < 4; k++) {
			for (int x = 0; x < 4; x++) {
				if (sudoku4x4[k][i] == sudoku4x4[x][i] && k != x) {
					gamestate = LOST;
				}
			}
			if ((k+1) % 4 == 0) {
				i++;
				k = -1;
			}
		}
		/* check subregions */
		for (int i = 0, k = 0; k < 4; i++) {
			for (int x = 0; x < 4; x++) {
				if (subregions[k][i] == subregions[k][x] && i != x) {
					gamestate = LOST;
				}
			}
			if (i+1 == 4) {
				k++;
				i = -1;
			}
		}
		if (gamestate == WIN && mode < 2) {
			printf("Nivel %d COMPLETADO\n", level);
			do {
				printf("[N]ivel %d o [C]errar\n", ++level);
				option = getchar();
			} while (option != 'n' && option != 'N' && option != 'c' && option != 'C');
			n[0] = randomnumber();
		}
		if (gamestate == LOST && mode < 2) {
			printf("Nivel %d NO COMPLETADO\n", level);
			do {
				printf("[R]eintentar o [C]errar\n");
				option = getchar();
			} while (option != 'r' && option != 'R' && option != 'c' && option != 'C');
		}
		if (mode == LOAD_SUDOKU) {
			if (gamestate == WIN) {
				printf("Sudoku COMPLETADO\n");
			}
			else {
				printf("Sudoku NO COMPLETADO\n");
			}
			printf("[R]eintentar o [C]errar\n");
			option = getchar();
		}
		if (option == 'r' || option == 'R' || option == 'n' || option == 'N') {
			for (int i = 0; i < 16; i++) {
				numbers[i] = make_sudoku(i, path);
			}
		}
		/* multiplayer mode */
		if (mode == COMPETITION_EASY && gamestate == WIN) {
			return 1;
		}
		if (mode == COMPETITION_DIFF && gamestate == WIN) {
			return 3;
		}
		if (mode == COMPETITION_EASY || mode == COMPETITION_DIFF) {
			if (gamestate == LOST) {
				return 0;
			}
		}
	}

	return 0;
}