From e11852c92e0aa3560d5a37d8c56ec10075dcf6fc Mon Sep 17 00:00:00 2001 From: Gaming32 Date: Sun, 22 Nov 2020 14:59:06 -0600 Subject: [PATCH] add shuffle method for scrambled odds This resolves src/utils/Shuffles.java:220 (which is now gone) --- src/main/ArrayManager.java | 2 +- src/prompts/ShufflePrompt.java | 5 ++++- src/utils/Shuffles.java | 24 +++++++++++++----------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/main/ArrayManager.java b/src/main/ArrayManager.java index 1bd8b51f..faacabbf 100644 --- a/src/main/ArrayManager.java +++ b/src/main/ArrayManager.java @@ -35,7 +35,7 @@ of this software and associated documentation files (the "Software"), to deal final public class ArrayManager { private int[] presortedArray; private utils.Shuffles[] shuffleTypes; - private String[] shuffleIDs = {"Randomly", "Backwards", "Few Unique", "Almost Sorted", "Already Sorted"}; + private String[] shuffleIDs = {"Randomly", "Shuffled Odds", "Backwards", "Few Unique", "Almost Sorted", "Already Sorted"}; private volatile boolean MUTABLE; diff --git a/src/prompts/ShufflePrompt.java b/src/prompts/ShufflePrompt.java index 6ee7827d..d27d6510 100644 --- a/src/prompts/ShufflePrompt.java +++ b/src/prompts/ShufflePrompt.java @@ -124,7 +124,7 @@ public void valueChanged(javax.swing.event.ListSelectionEvent evt) { .addComponent(this.jLabel1) .addGap(5, 5, 5)) .addGroup(layout.createSequentialGroup() - .addComponent(this.jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addComponent(this.jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) @@ -160,6 +160,9 @@ private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) throws case 4: ArrayManager.setShuffle(ArrayManager.getShuffles()[4]); break; + case 5: + ArrayManager.setShuffle(ArrayManager.getShuffles()[5]); + break; default: break; } diff --git a/src/utils/Shuffles.java b/src/utils/Shuffles.java index 8f325d99..1852514a 100644 --- a/src/utils/Shuffles.java +++ b/src/utils/Shuffles.java @@ -58,6 +58,19 @@ public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays De */ } }, + SHUFFLED_ODDS { + @Override + public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { + int currentLen = ArrayVisualizer.getCurrentLength(); + + for(int i = 1; i < currentLen; i += 2){ + int randomIndex = (((int) ((Math.random() * (currentLen - i)) / 2)) * 2) + i; + Writes.swap(array, i, randomIndex, 0, true, false); + + if(ArrayVisualizer.shuffleEnabled()) Delays.sleep(2); + } + } + }, REVERSE { @Override public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays Delays, Highlights Highlights, Writes Writes) { @@ -138,7 +151,6 @@ public void shuffleArray(int[] array, ArrayVisualizer ArrayVisualizer, Delays De } /* - int currentLen = ArrayVisualizer.getCurrentLength(); Writes.write(array, 0, 0, 1, true, false); for(int i = 1; i < currentLen; i++) { int log = (int) (Math.log(i) / Math.log(2)); @@ -203,16 +215,6 @@ else if(k == n) { else return concat(circleGen(n-1, k, Writes), addToAll(circleGen(n-1, k-1, Writes), 1 << (n-1), Writes), Writes); } - */ - /* - //TODO: Consider separate method - for(int i = 1; i < currentLen; i += 2){ - int randomIndex = (((int) ((Math.random() * (currentLen - i)) / 2)) * 2) + i; - Writes.swap(array, i, randomIndex, 0, true, false); - - if(ArrayVisualizer.shuffleEnabled()) Delays.sleep(2); - } - } */ /* int j = 0, k = currentLen;