Done with all but Game.java
This commit is contained in:
commit
f8f054c985
18 changed files with 460 additions and 0 deletions
116
src/com/company/GameWheel.java
Normal file
116
src/com/company/GameWheel.java
Normal file
|
@ -0,0 +1,116 @@
|
|||
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
|
||||
StringBuilder thing = new StringBuilder();
|
||||
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());
|
||||
}
|
||||
return thing.toString();
|
||||
}
|
||||
|
||||
|
||||
/* 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
|
||||
Collections.shuffle(slices);
|
||||
}
|
||||
|
||||
|
||||
/* 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
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue