Пример #1
0
/**
 * If tile borders empty space, moves tile and returns true, else
 * returns false. 
 */
bool move(int tile)
{
    // Check whether tile is on the board
    if (findTile(tile))
    {
        // Check to see if blank tile's row position is equal to selected tile's row position
        if (blank_row + 1 == found_row || blank_row - 1 == found_row)
        {
            // To ensure only selecting tile below or above blank tile (column position stays the same)
            if (blank_col == found_col)
            {
                swap_tiles();
                return true;
            }
        }
        // Check to see if blank tile's column position is equal to selected tile's column position   
        if (blank_col + 1 == found_col || blank_col - 1 == found_col)
        {
            // To ensure only selecting tiles left or right of blank tile (row position stays the same)
            if (blank_row == found_row)
            {
                swap_tiles();
                return true;
            }
        }
    }
    return false;
}
Пример #2
0
/*
Inicjalizuj grę
*/
void init() {

	short i, j, index, temp, temp2;
	short len;
	short* random;
	tile temp_tile;

	/* Wyczyść obydwie tablice */
	for(i=0;i<TILES_COUNT;i++) {
		for(j=0;j<TILES_COUNT;j++) {
			game[i][j].top = -1;
			game[i][j].bottom = -1;
			game[i][j].left = -1;
			game[i][j].right = -1;
			pool[i][j].top = -1;
			pool[i][j].bottom = -1;
			pool[i][j].left = -1;
			pool[i][j].right = -1;
		}
	}

	/* Obliczamy liczbę unikalnych wartości dla danej układanki */
	len = 2*TILES_COUNT*(TILES_COUNT-1);
	random = (short*)malloc(sizeof(short)*len);

	/* Generujemy losowe liczby z zakresu 0-9 */
	for(i=0;i<len;i++)
		random[i] = rand() % 10;

	/* Kopiujemy wartości do odpowiednich komórek w puli (pool) */

	index = 0;

	for(i=0;i<TILES_COUNT;i++) {
		for(j=0;j<TILES_COUNT;j++) {

			if(j < TILES_COUNT-1) {
				temp = random[index++];
				pool[j][i].right = temp;
				pool[j+1][i].left = temp;
			}

			if(i < TILES_COUNT-1) {
				temp = random[index++];
				pool[j][i].bottom = temp;
				pool[j][i+1].top = temp;
			}

		}
	}

	free(random);

	/* Uzupełniamy nieistotnymi wartościami */
	for(i=0;i<TILES_COUNT;i++) {
		for(j=0;j<TILES_COUNT;j++) {
			if(pool[i][j].top == -1)
				pool[i][j].top = rand() % 10;
			if(pool[i][j].bottom == -1)
				pool[i][j].bottom = rand() % 10;
			if(pool[i][j].left == -1)
				pool[i][j].left = rand() % 10;
			if(pool[i][j].right == -1)
				pool[i][j].right = rand() % 10;
		}
	}

	/* Dokonujemy losowych modyfikacji */
	for(i=0;i<RANDOM_ITERATIONS;i++) {

		temp = rand() % (TILES_COUNT*TILES_COUNT);
		temp2 = rand() % (TILES_COUNT*TILES_COUNT);

		if(temp != temp2)
			swap_tiles(&pool[temp/TILES_COUNT][temp%TILES_COUNT], &pool[temp2/TILES_COUNT][temp2%TILES_COUNT]);
		else continue;
		
	}

}