Example #1
0
bool MazeBoard::addTreasures(int treasure_count)
{
    if (board_size == 0)
        return false;

    std::vector<int> temp_coords; //board.size() - 4

    for (int x = 1; x <= board_size; x++)
    {
        for (int y = 1; y <= board_size; y++)
        {
            if (is_corner(x,y, board_size))
                continue;
            temp_coords.push_back(INDEX(x,y));
        }
    }

    // shuffle coordinates where treasures will be placed in
    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
    auto engine = std::default_random_engine{seed};
    std::shuffle(std::begin(temp_coords), std::end(temp_coords), engine);

    // fill first 'treasure_count' coordinates with treasures
    for (int x = 0; x < treasure_count; x++)
    {
        // treasures are numbered from 1
        board[temp_coords[x]].treasure = x + 1;
    }
    return true;
}
int main(){
	int i, j, a, b, c, direction; // for loop, another for loop, first user input, second user input, max of user input, direction hex is going in
	int magnitude = 1;	// magnitude of hex
	int** hex;			// mother array, holds all the coordintes
	
	printf("Please type the first hex number : ");
	scanf_s("%d", &a);
	getchar(); // swallows the /n from the scan_f
	printf("Please type the second hex number: ");
	scanf_s("%d", &b);
	getchar();

	if ( a > b )
		c = a;
	else
		c = b;
	
	// creating hex. Dimensions of max(userinput)*3, because there are 3 coordinates
	hex = (int**) malloc(c*sizeof(int*));
	for (i = 0; i < c; i++){
		hex[i] = (int*) calloc(3,sizeof(int));
	}
	hex[1][1] = -1;
	hex[1][2] = 1; // Results in hex[] = {0,0,0},{0,-1,1} which are the initial conditions necessary to get goin!

	for (i=2; i<c; i++){
		int corner = is_corner(hex[i - 1]);
		
		// if this is a corner...
		// case/switch values were determined looking at patterns in the spiral
		if (corner != 0){
			direction = corner; // establish direction for non-corners
			switch(corner){
			case 1: hex[i][0] = -1;
					hex[i][1] = -magnitude + 1;
					hex[i][2] = magnitude;
					break;
			case 2: hex[i][0] = -magnitude;
					hex[i][1] = 1;
					hex[i][2] = magnitude - 1;
					break;
			case 3: hex[i][0] = -magnitude + 1;
					hex[i][1] = magnitude;
					hex[i][2] = -1;
					break;
			case 4: hex[i][0] = 1;
					hex[i][1] = magnitude -1;
					hex[i][2] = -magnitude;
					break;
			case 5: hex[i][0] = magnitude;
					hex[i][1] = -1;
					hex[i][2] = -magnitude +1;
					break;
			case 6: hex[i][0] = magnitude;
					hex[i][1] = -magnitude -1;
					hex[i][2] = 1;
					magnitude++; // moving onto the next ring of the spiral
					break;
			}
		}

		// if not a corner... (a straight segment)
		else{
			switch(direction){ // grab corner as determined earlier
			case 6: hex[i][0] = hex[i-1][0] - 1;
					hex[i][1] = hex[i-1][1];
					hex[i][2] = hex[i-1][2] + 1;
					break;
			case 1: hex[i][0] = hex[i-1][0] - 1;
					hex[i][1] = hex[i-1][1] + 1;
					hex[i][2] = hex[i-1][2];
					break;
			case 2: hex[i][0] = hex[i-1][0];
					hex[i][1] = hex[i-1][1] + 1;
					hex[i][2] = hex[i-1][2] - 1;
					break;
			case 3: hex[i][0] = hex[i-1][0] + 1;
					hex[i][1] = hex[i-1][1];
					hex[i][2] = hex[i-1][2] - 1;
					break;
			case 4: hex[i][0] = hex[i-1][0] + 1;
					hex[i][1] = hex[i-1][1] - 1;
					hex[i][2] = hex[i-1][2];
					break;
			case 5: hex[i][0] = hex[i-1][0];
					hex[i][1] = hex[i-1][1] - 1;
					hex[i][2] = hex[i-1][2] + 1;
					break;
			}
		}
	}
	
	//  this prints the coordinates of every hex. Used for debugging.
	/*for (i=0; i<c; i++){
		printf("%2d: ",i+1);
		printArray(hex[i],3);
	}*/

	//printf("Coordinates of %3d are: (%3d, %3d, %3d)\n", a, hex[a-1][0], hex[a-1][1], hex[a-1][2] );
	//printf("Coordinates of %3d are: (%3d, %3d, %3d)\n", b, hex[b-1][0], hex[b-1][1], hex[b-1][2] );
	printf("Minimum hexagons between them is %d\n", ( abs(hex[a-1][0] - hex[b-1][0]) + abs(hex[a-1][1] - hex[b-1][1]) + abs(hex[a-1][2] - hex[b-1][2]))/2);

	// just for practice! Better to learn good technique now
	for (i = 0; i < c; i++)
	   free(hex[i]);
	free(hex);
	
	getchar();
	return 0;
}
Example #3
0
void MazeBoard::initialize(int _board_size)
{
    board_size = _board_size; // adding board_size to class variable

    /* initialize random seed: */
    srand(time(NULL));

    std::vector<int> ratio (3, 0); // ratio between counts of Stone types
    board.resize(board_size * board_size);

    // adding corners to the board (4x L)
    board[INDEX(1,          1         )] = Stone(L, 1);
    board[INDEX(1,          board_size)] = Stone(L, 2);
    board[INDEX(board_size, 1         )] = Stone(L, 0);
    board[INDEX(board_size, board_size)] = Stone(L, 3);
    ratio[L] += 4;

    // adding T stones (odd x and odd y coordinates - but not corners)
    // count is ((N/2+1)*(N/2+1)-4)
    for (int x = 1; x <= board_size; x+=2)
    {
        for (int y = 1; y <= board_size; y+=2)
        {
            if (is_corner(x, y, board_size)) continue;
            board[INDEX(x,y)] = Stone(T, rand()%4);
            if      (x == 1)          (board[INDEX(x,y)]).rotation = 0; // first row
            else if (x == board_size) (board[INDEX(x,y)]).rotation = 2; // last row
            else if (y == 1)          (board[INDEX(x,y)]).rotation = 3; // first column
            else if (y == board_size) (board[INDEX(x,y)]).rotation = 1; // last column
            (ratio[T])++;
            //std::cout << "ratio.. I: " << ratio[I] << " L: " << ratio[L] << " T: " << ratio[T] << std::endl;
        }
    }

    // placing remaining stones (with random shape and rotation)
    // it is trying to keep ratio 1:1:1
    int remaining_stones_cnt = (board_size/2)*board_size + (board_size/2+1)*(board_size/2);
    std::vector<Stone> temp_board (remaining_stones_cnt);
    StoneType min_ratio; // stone type with the smallest count of stones
    for (int x = 0; x < remaining_stones_cnt; x++)
    {
        min_ratio = minimum(ratio);
        temp_board[x].type = min_ratio;
        temp_board[x].rotation = rand() % 4;
        (ratio[min_ratio])++;
        //std::cout << "ratio.. I: " << ratio[I] << " L: " << ratio[L] << " T: " << ratio[T] << std::endl;
    }

    // shuffle remaining stones
    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
    auto engine = std::default_random_engine{seed};
    std::shuffle(std::begin(temp_board), std::end(temp_board), engine);

    // adding remaining stones to the even rows (N/2)*N
    for (int x = 2; x <= board_size; x+=2)
    {
        for (int y = 1; y <= board_size; y++)
        {
            board[INDEX(x,y)] = temp_board.back();
            temp_board.pop_back();
        }
    }

    // adding remaining stones to the odd rows, where column is even ((N/2+1)*(N/2))
    for (int x = 1; x <= board_size; x+=2)
    {
        for (int y = 2; y <= board_size; y+=2)
        {
            board[INDEX(x,y)] = temp_board.back();
            temp_board.pop_back();
        }
    }

    free_stone.type = minimum(ratio);
}