From 5cf897c2ff7329441304468c9ed1d8ea433060de Mon Sep 17 00:00:00 2001 From: tiera3 <87589219+tiera3@users.noreply.github.com> Date: Wed, 2 Oct 2024 14:42:06 +1000 Subject: [PATCH] Moving Striped Collation code into CardRun --- .../src/main/java/mage/collation/CardRun.java | 45 +++++++++++++++ .../src/main/java/mage/collation/Rotater.java | 55 ++----------------- 2 files changed, 51 insertions(+), 49 deletions(-) diff --git a/Mage/src/main/java/mage/collation/CardRun.java b/Mage/src/main/java/mage/collation/CardRun.java index c34876bf9d6..7391b158417 100644 --- a/Mage/src/main/java/mage/collation/CardRun.java +++ b/Mage/src/main/java/mage/collation/CardRun.java @@ -1,11 +1,56 @@ package mage.collation; +import mage.util.RandomUtil; + /** * @author TheElk801 */ public class CardRun extends Rotater { + 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; + } + } } diff --git a/Mage/src/main/java/mage/collation/Rotater.java b/Mage/src/main/java/mage/collation/Rotater.java index b659d4809ca..617fc97bcaa 100644 --- a/Mage/src/main/java/mage/collation/Rotater.java +++ b/Mage/src/main/java/mage/collation/Rotater.java @@ -25,61 +25,18 @@ public class Rotater { 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(); - 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; }