155 lines
4.6 KiB
Java
155 lines
4.6 KiB
Java
package com.company;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
|
|
public class GameWheel
|
|
{
|
|
private final ArrayList<Slice> slices; // List of slices making up the wheel
|
|
private int currentPos; // Position of currently selected slice on wheel
|
|
|
|
|
|
/* Returns string representation of GameWheel with each numbered slice
|
|
* on a new line
|
|
*/
|
|
public String toString(){
|
|
//Implement the toString method here
|
|
String thing = "";
|
|
for (int i = 0; i < slices.size(); i++){
|
|
thing += i + " - " + slices.get(i).toString() + "\n";
|
|
}
|
|
return thing;
|
|
}
|
|
|
|
|
|
/* Randomizes the positions of the slices that are in the wheel, but without
|
|
* changing the pattern of the colors
|
|
*/
|
|
public void scramble()
|
|
{
|
|
//Implement the scramble method here
|
|
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);
|
|
}
|
|
|
|
|
|
/* Sorts the positions of the slices that are in the wheel by prize amount,
|
|
* but without changing the pattern of the colors.
|
|
*/
|
|
public void sort(){
|
|
/*
|
|
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 */
|
|
|
|
/* Creates a wheel with 20 preset slices
|
|
*/
|
|
public GameWheel()
|
|
{
|
|
this(getStandardPrizes());
|
|
}
|
|
|
|
/* Creates a wheel with 20 slices, using values from array parameter
|
|
*/
|
|
public GameWheel(int[] prizes)
|
|
{
|
|
currentPos = 0;
|
|
slices = new ArrayList<>();
|
|
for(int i = 0; i < 20; i++){
|
|
int pa = 0;
|
|
String col = "blue";
|
|
if(i < prizes.length)
|
|
pa = prizes[i];
|
|
if (i%5 == 0)
|
|
col = "black";
|
|
else if (i%2 == 1)
|
|
col = "red";
|
|
slices.add(new Slice(col, pa));
|
|
}
|
|
}
|
|
|
|
/* Spins the wheel by so that a different slice is selected. Returns that
|
|
* slice (Note: the 10 slices following the current slice are more likely to
|
|
* be returned than the other 10).
|
|
*/
|
|
public Slice spinWheel()
|
|
{
|
|
//spin power between range of 1-50 (inclusive)
|
|
int power = (int)(Math.random()*50 + 1);
|
|
currentPos = (currentPos + power) % slices.size();
|
|
return slices.get(currentPos);
|
|
}
|
|
|
|
public Slice getSlice(int i){
|
|
int sliceNum = i;
|
|
if(i < 0 || i > 19)
|
|
sliceNum = 0;
|
|
return slices.get(sliceNum);
|
|
}
|
|
|
|
// Makes an array with a standard list of prizes
|
|
private static int[] getStandardPrizes()
|
|
{
|
|
int[] arr = new int[20];
|
|
for (int i=0; i < 20; i++)
|
|
{
|
|
if (i%5 == 0)
|
|
arr[i] = i*1000;
|
|
else if (i%2 == 1)
|
|
arr[i] = i*100;
|
|
else
|
|
arr[i] = i*200;
|
|
}
|
|
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;
|
|
}
|
|
}
|