Moving Striped Collation code into CardRun

This commit is contained in:
tiera3 2024-10-02 14:42:06 +10:00 committed by GitHub
parent ae75e99794
commit 5cf897c2ff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 51 additions and 49 deletions

View file

@ -1,11 +1,56 @@
package mage.collation;
import mage.util.RandomUtil;
/**
* @author TheElk801
*/
public class CardRun extends Rotater<String> {
private int cardPos,stripeLen=0,stripeWidth,stripeDepth;
// cardPos is used in place of private super.position for Striped Collation
public CardRun(boolean keepOrder, String... numbers) {
super(keepOrder, numbers);
}
public CardRun(int sLen, String... numbers) {
super(true, numbers);
cardPos= RandomUtil.nextInt( this.numItems() );
stripeWidth= nextWidth();
stripeDepth= 1+ RandomUtil.nextInt( stripeWidth );
// assert sLen >0;
// assert this.numItems() % sLen == 0;
}
private int nextWidth() {
return 2+ RandomUtil.nextInt(4);
}
public int iterate() {
if( stripeLen ==0 ){
return super.iterate();
}else{
int i = cardPos;
if( stripeDepth < stripeWidth ){
++stripeDepth;
cardPos -= 10;
if (cardPos <0 ){
cardPos += this.numItems();
}
}else{
stripeDepth= 1;
if( (cardPos % stripeLen) >0 ){
cardPos += stripeLen * (stripeWidth-1);
cardPos %= this.numItems();
}else{
this.stripeWidth= this.nextWidth();
}
cardPos -= 1;
if (cardPos <0 ){
cardPos += this.numItems();
}
}
return i;
}
}
}

View file

@ -25,61 +25,18 @@ public class Rotater<T> {
this(true, item1, item2);
}
public Rotater(boolean keepOrder, T... items) {
if (keepOrder) {
this.items = Arrays.asList(items);
this.position = RandomUtil.nextInt(this.items.size());
} else {
this.items = new ArrayList<T>();
Collections.addAll(this.items, items);
Collections.shuffle(this.items, RandomUtil.getRandom());
this.position = 0;
}
public int numItems() {
return items.size();
}
// for striped collation
public Rotater(int sLen, T... items) {
// should there be an error check?
// assert ( items.size() % sLen ) == 0;
this.stripeLen = sLen;
this.items = Arrays.asList(items);
this.position = RandomUtil.nextInt(this.items.size());
this.stripeWidth= this.nextWidth();
this.stripeDepth= 1+ RandomUtil.nextInt(this.stripeWidth);
}
// choose a stripe width between 2 & 5 inclusive
// ToDo when data available: enable different widths to have different likelihoods
private int nextWidth() {
return 2+ RandomUtil.nextInt(4);
public String getItem(int i) {
return items.get(i);
}
public int iterate() {
int i = position;
if( stripeLen >0 ){
if( stripeDepth < stripeWidth ){
++stripeDepth;
position -= 10;
if (position <0 ){
position += items.size();
}
}else{
stripeDepth= 1;
if( (position % stripeLen) >0 ){
position += stripeLen * (stripeWidth-1);
position %= items.size();
}else{
this.stripeWidth= this.nextWidth();
}
position -= 1;
if (position <0 ){
position += items.size();
}
}
}else{
position++;
position %= items.size();
}
position++;
position %= items.size();
return i;
}