Scramble method done

This commit is contained in:
blzimme000 2022-02-18 07:35:12 -06:00
parent f8f054c985
commit 6ae8461d8a
2 changed files with 59 additions and 20 deletions

View file

@ -14,11 +14,11 @@ public class GameWheel
*/
public String toString(){
//Implement the toString method here
StringBuilder thing = new StringBuilder();
String thing = "";
for (int i = 0; i < slices.size(); i++){
thing.append(i).append(" - Color: ").append(slices.get(i).getColor()).append(", PrizeAmount: $").append(slices.get(i).getPrizeAmount());
thing += i + " - " + slices.get(i).toString() + "\n";
}
return thing.toString();
return thing;
}
@ -28,7 +28,27 @@ public class GameWheel
public void scramble()
{
//Implement the scramble method here
Collections.shuffle(slices);
ArrayList<Slice> newSlices = new ArrayList<>();
ArrayList<Slice> redStuff = new ArrayList<>(shuffle(colorSplit(slices, "red")));
ArrayList<Slice> blueStuff = new ArrayList<>(shuffle(colorSplit(slices, "blue")));
ArrayList<Slice> blackStuff = new ArrayList<>(shuffle(colorSplit(slices, "black")));
int blackCount = 0;
int blueCount = 0;
int redCount = 0;
for (int i = 0; i < 20; i++){
if (i % 5 == 0){
newSlices.add(blackStuff.get(blackCount));
blackCount++;
} else if (i % 2 == 0){
newSlices.add(blueStuff.get(blueCount));
blueCount++;
} else {
newSlices.add(redStuff.get(redCount));
redCount++;
}
}
slices.subList(0, 20).clear();
slices.addAll(newSlices);
}
@ -36,19 +56,11 @@ public class GameWheel
* but without changing the pattern of the colors.
*/
public void sort(){
//Implement the sort method here
for (int i = 1; i < slices.size(); i++)
{
Slice toInsert = new Slice("toInsert", slices.get(i).getPrizeAmount());
int j;
for (j = i; j > 0; j--)
{
if (toInsert.getPrizeAmount() >= slices.get(j-1).getPrizeAmount()){
break;
}
}
slices.add(j, slices.remove(i));
}
/*
Implement the sort method here
- Need to use colorSplit(ArrayList<Slice>) and sort(ArrayList<Slice>) to sort
individual lists, then combine into big list similar to scramble method.
*/
}
/* COMPLETED METHODS - YOU DO NOT NEED TO CHANGE THESE */
@ -113,4 +125,31 @@ public class GameWheel
}
return arr;
}
private ArrayList<Slice> colorSplit(ArrayList<Slice> list, String color){
ArrayList<Slice> newList = new ArrayList<>();
for (Slice e : list){
if (e.getColor().equals(color)){
newList.add(e);
}
}
return newList;
}
private ArrayList<Slice> shuffle(ArrayList<Slice> list){
Collections.shuffle(list);
return list;
}
private ArrayList<Slice> sort(ArrayList<Slice> list){
for (int i = 1; i < list.size(); i++){
Slice toInsert = new Slice("toInsert", list.get(i).getPrizeAmount());
int j;
for (j = i; j > 0; j--)
{
if (toInsert.getPrizeAmount() >= list.get(j-1).getPrizeAmount()){
break;
}
}
list.add(j, list.remove(i));
}
return list;
}
}