Commit db66a6db authored by salix5's avatar salix5 Committed by GitHub

update shuffle_vector() (#437)

parent 9acf492c
......@@ -957,14 +957,14 @@ void field::shuffle(uint8 playerid, uint8 location) {
}
}
if(location == LOCATION_HAND || !(core.duel_options & DUEL_PSEUDO_SHUFFLE)) {
uint32 s = (uint32)svector.size();
int32 s = (int32)svector.size();
if(location == LOCATION_EXTRA)
s = s - player[playerid].extra_p_count;
s = s - (int32)player[playerid].extra_p_count;
if(s > 1) {
if (core.duel_options & DUEL_OLD_REPLAY)
pduel->random.shuffle_vector_old(svector);
pduel->random.shuffle_vector_old(svector, 0, s - 1);
else
pduel->random.shuffle_vector(svector);
pduel->random.shuffle_vector(svector, 0, s - 1);
reset_sequence(playerid, location);
}
}
......
......@@ -44,20 +44,26 @@ public:
return result;
}
// Fisher-Yates shuffle
// Fisher-Yates shuffle v[a]~v[b]
template<typename T>
void shuffle_vector(std::vector<T>& v) {
int n = (int)v.size();
for (int i = 0; i < n - 1; ++i) {
int r = get_random_integer(i, n - 1);
void shuffle_vector(std::vector<T>& v, int a = -1, int b = -1) {
if (a < 0)
a = 0;
if (b < 0)
b = (int)v.size() - 1;
for (int i = a; i < b; ++i) {
int r = get_random_integer(i, b);
std::swap(v[i], v[r]);
}
}
template<typename T>
void shuffle_vector_old(std::vector<T>& v) {
int n = (int)v.size();
for (int i = 0; i < n - 1; ++i) {
int r = get_random_integer_old(i, n - 1);
void shuffle_vector_old(std::vector<T>& v, int a = -1, int b = -1) {
if (a < 0)
a = 0;
if (b < 0)
b = (int)v.size() - 1;
for (int i = a; i < b; ++i) {
int r = get_random_integer_old(i, b);
std::swap(v[i], v[r]);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment