emulate draw - pcaston2/tupo GitHub Wiki
#Emulate Draw
The emulated draw is called after the shuffle. Two maps are required, as a result of the possibility of duplicates.
Cards which are in play already (such as commanders, fortress, etc.) are not included in the draw algorithm. They are played in the order that they appear in the field array.
The first map contains the desired ordering. In order to emulate a hand, the map must be traversed sequentially, starting from the front, and working towards the back. If the card following the current element is smaller, they swap positions. This must be done twice to emulate a hand of 3 cards. It may be possible to do both traversals in parallel and thus reduce the traversal time by almost half. (two swaps must occur before the second pass can continue).
The second map contains values to find and replace after a particular value has been found. This way duplicate cards won't seek to be in the same location. The desired order number for duplicates is the lowest order number. The second map would hold the desired second order number at the index of the first, so it can be fetched and replace all following entries. If no new mapping exists, the second map would hold an invalid value, to signify that there is no mapping. To illustrate, an example:
Cards with IDs (the IDs are in the order they wish to be ordered in, for convenience):
1 2 3 4 2 6 7 8 2
Map:
! 5 ! ! 9 ! ! ! !
Note: Two appears multiple times. This is because, although the card at 2 is supposed to be in positions two, five, and nine ideally, we can't account for which order they will appear. They will be updated using the remap as they are selected. Each iteration, first the current item is checked to see if it is re-mapped, then it is swapped.
We randomize the order and get:
6 2 4 8 2 7 2 1 3
*Note: we must check the first element to see if it has a remap, and then apply it.
2<6 4 8 5 7 5 1 3
Map:
! ! ! ! 9 ! ! ! !
Note: When we see two being evaluated to swap in the first position, we check the map and see two should now be five. The remaining elements update any twos to fives.
2 4<6 8 5 7 5 1 3
2-4 6-8 5 7 5 1 3
Note: There are now two concurrent swaps occurring
2 4-6 5<8 7 9 1 3
Map:
! ! ! ! ! ! ! ! !
Note: The map is now complete, the numbers just need to be sorted
2 4 5<6 7<8 9 1 3
2 4 5 6-7 8-9 1 3
2 4 5 6 7-8 1<9 3
2 4 5 6 7 1<8 3<9
2 4 5 6 7 1 3<8 9
2 4 5 6 7 1 3 8-9
2 4 5 6 7 1 3 8 9
Note: Emulated draw is complete, note that 2, 5 and 9 are all the same card