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

@ -24,9 +24,9 @@ You will implement three different public methods in the GameWheel class.
| | 19 - Color: red, Prize Amount: $190 | | | 19 - Color: red, Prize Amount: $190 |
| | You may find the toString method helpful when testing your other GameWheel methods. | | | You may find the toString method helpful when testing your other GameWheel methods. |
*** ***
| scramble | The second method you will write is the scramble method, which randomizes the positions of the slices that are in the wheel, but without changing the pattern of the colors (i.e. the black slices will still be at multiples of 5, the red slices at all other odd indices and the blue slices at all other even indices). No individual slice should be changed, rather the slices should be re-ordered. | | | scramble | The second method you will write is the scramble method, which randomizes the positions of the slices that are in the wheel, but without changing the pattern of the colors (i.e. the black slices will still be at multiples of 5, the red slices at all other odd indices and the blue slices at all other even indices). No individual slice should be changed, rather the slices should be re-ordered. |
|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------| |----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| sort | The third method to implement is the sort method. This is similar to the scramble method in that it reorders the slices in the GameWheel so that the pattern of the colors remains the same. After the sort method is called however, the slices of each color should be sorted in increasing order of prize amount. So for example, the black slice with the lowest | prize value will be at index 0, and the black slice with the highest prize value will be at index 15. | | sort | The third method to implement is the sort method. This is similar to the scramble method in that it reorders the slices in the GameWheel so that the pattern of the colors remains the same. After the sort method is called however, the slices of each color should be sorted in increasing order of prize amount. So for example, the black slice with the lowest prize value will be at index 0, and the black slice with the highest prize value will be at index 15|
### Part 2: Game/play ### Part 2: Game/play

View file

@ -14,11 +14,11 @@ public class GameWheel
*/ */
public String toString(){ public String toString(){
//Implement the toString method here //Implement the toString method here
StringBuilder thing = new StringBuilder(); String thing = "";
for (int i = 0; i < slices.size(); i++){ 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() public void scramble()
{ {
//Implement the scramble method here //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. * but without changing the pattern of the colors.
*/ */
public void sort(){ public void sort(){
//Implement the sort method here /*
for (int i = 1; i < slices.size(); i++) Implement the sort method here
{ - Need to use colorSplit(ArrayList<Slice>) and sort(ArrayList<Slice>) to sort
Slice toInsert = new Slice("toInsert", slices.get(i).getPrizeAmount()); individual lists, then combine into big list similar to scramble method.
int j; */
for (j = i; j > 0; j--)
{
if (toInsert.getPrizeAmount() >= slices.get(j-1).getPrizeAmount()){
break;
}
}
slices.add(j, slices.remove(i));
}
} }
/* COMPLETED METHODS - YOU DO NOT NEED TO CHANGE THESE */ /* COMPLETED METHODS - YOU DO NOT NEED TO CHANGE THESE */
@ -113,4 +125,31 @@ public class GameWheel
} }
return arr; 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;
}
} }