bot/src/beam.cpp
/*
* Predict the future pieces according to the 7 bag rule
* NOTE TO SELF: Must call this in "set_root()" if forecast mode on
*/
Predict the future pieces according to the 7 bag rule
NOTE TO SELF: Must call this in "set_root()" if forecast mode on
Predict the future pieces according to the 7 bag rule NOTE TO SELF : Must call this in " set_root () " if forecast mode on
void beam::forecast()
{
	piece_type bag[7] = { PIECE_I, PIECE_T, PIECE_L, PIECE_O, PIECE_S, PIECE_J, PIECE_Z };
	for (int n = 0; n < 7; ++n) {
		int to_swap_index = rand() % 7;
		piece_type place_holder = bag[n];
		bag[n] = bag[to_swap_index];
		bag[to_swap_index] = place_holder;
	}
	int number_of_piece_from_previous_bag = (forecast_bag_index + (preview % 7)) % 7;
	number_of_piece_from_previous_bag = std::min(number_of_piece_from_previous_bag, preview); 
	piece_type upcoming_piece[7] = {};
	int upcoming_piece_count = 0;
	for (int i = 0; i < 7; ++i) {
		bool already_appeared = false;
		for (int k = preview - number_of_piece_from_previous_bag; k < preview; ++k) {
			if (bag[i] == queue[k]) {
				already_appeared = true;
				break;
			}
		}
		if (!already_appeared) {
			upcoming_piece[upcoming_piece_count] = bag[i];
			upcoming_piece_count++;
		}
	}
	memcpy(queue + preview, upcoming_piece, upcoming_piece_count * sizeof(piece_type));
	queue_count += upcoming_piece_count;
	while (true)
	{
		for (int n = 0; n < 7; ++n) {
			int to_swap_index = rand() % 7;
			piece_type place_holder = bag[n];
			bag[n] = bag[to_swap_index];
			bag[to_swap_index] = place_holder;
		}
		for (int i = 0; i < 7; ++i) {
			queue[queue_count] = bag[i];
			++queue_count;
			if (queue_count >= max_depth) break;
		}
		if (queue_count >= max_depth) break;
	}
	forecast_bag_index = (forecast_bag_index + 1) % 7;
}
void beam :: forecast ( ) { piece_type bag [ 7 ] = { PIECE_I , PIECE_T , PIECE_L , PIECE_O , PIECE_S , PIECE_J , PIECE_Z } ; for ( int n = 0 ; n < 7 ; ++ n ) { int to_swap_index = rand ( ) % 7 ; piece_type place_holder = bag [ n ] ; bag [ n ] = bag [ to_swap_index ] ; bag [ to_swap_index ] = place_holder ; } int number_of_piece_from_previous_bag = ( forecast_bag_index + ( preview % 7 ) ) % 7 ; number_of_piece_from_previous_bag = std :: min ( number_of_piece_from_previous_bag , preview ) ; piece_type upcoming_piece [ 7 ] = { } ; int upcoming_piece_count = 0 ; for ( int i = 0 ; i < 7 ; ++ i ) { bool already_appeared = false ; for ( int k = preview - number_of_piece_from_previous_bag ; k < preview ; ++ k ) { if ( bag [ i ] == queue [ k ] ) { already_appeared = true ; break ; } } if ( ! already_appeared ) { upcoming_piece [ upcoming_piece_count ] = bag [ i ] ; upcoming_piece_count ++ ; } } memcpy ( queue + preview , upcoming_piece , upcoming_piece_count * sizeof ( piece_type ) ) ; queue_count += upcoming_piece_count ; while ( true ) { for ( int n = 0 ; n < 7 ; ++ n ) { int to_swap_index = rand ( ) % 7 ; piece_type place_holder = bag [ n ] ; bag [ n ] = bag [ to_swap_index ] ; bag [ to_swap_index ] = place_holder ; } for ( int i = 0 ; i < 7 ; ++ i ) { queue [ queue_count ] = bag [ i ] ; ++ queue_count ; if ( queue_count >= max_depth ) break ; } if ( queue_count >= max_depth ) break ; } forecast_bag_index = ( forecast_bag_index + 1 ) % 7 ; }
Predict the future pieces according to the 7 bag rule
NOTE TO SELF: Must call this in "set_root()" if forecast mode on
Predict the future pieces according to the 7 bag rule NOTE TO SELF : Must call this in " set_root () " if forecast mode on
// Init bag and shuffle bag randomly
 // Find the number of piece remained from the previoud bag
 // Make sure that it doesn't exceed preview count
 // Find piece that haven't appeared yet
 // Add upcoming piece to the queue
 // If the queue isn't full, then keep adding pieces from bag randomly
 // Shuffle bag randomly
 // Keep adding piece to queue, if queue is full, then stop
 // If queue is full, then stop
 // Update bag index
