From 6ae8461d8ab4d175bb003d328e34108abae6b806 Mon Sep 17 00:00:00 2001 From: blzimme000 Date: Fri, 18 Feb 2022 07:35:12 -0600 Subject: [PATCH] Scramble method done --- .../Assignment7/com/company/readme.md | 6 +- src/com/company/GameWheel.java | 73 ++++++++++++++----- 2 files changed, 59 insertions(+), 20 deletions(-) diff --git a/out/production/Assignment7/com/company/readme.md b/out/production/Assignment7/com/company/readme.md index 04217fd..86a4da9 100644 --- a/out/production/Assignment7/com/company/readme.md +++ b/out/production/Assignment7/com/company/readme.md @@ -24,9 +24,9 @@ You will implement three different public methods in the GameWheel class. | | 19 - Color: red, Prize Amount: $190 | | | 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. | | -|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------| -| 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. | +| 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| ### Part 2: Game/play diff --git a/src/com/company/GameWheel.java b/src/com/company/GameWheel.java index ea0616d..7f5ce60 100644 --- a/src/com/company/GameWheel.java +++ b/src/com/company/GameWheel.java @@ -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 newSlices = new ArrayList<>(); + ArrayList redStuff = new ArrayList<>(shuffle(colorSplit(slices, "red"))); + ArrayList blueStuff = new ArrayList<>(shuffle(colorSplit(slices, "blue"))); + ArrayList 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) and sort(ArrayList) 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 colorSplit(ArrayList list, String color){ + ArrayList newList = new ArrayList<>(); + for (Slice e : list){ + if (e.getColor().equals(color)){ + newList.add(e); + } + } + return newList; + } + private ArrayList shuffle(ArrayList list){ + Collections.shuffle(list); + return list; + } + private ArrayList sort(ArrayList 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; + } }