Done with all but Game.java

This commit is contained in:
blzimme000 2022-02-16 14:40:39 -06:00
commit f8f054c985
18 changed files with 460 additions and 0 deletions

3
.idea/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

1
.idea/description.html Normal file
View file

@ -0,0 +1 @@
<html>Simple <b>Java</b> application that includes a class with <code>main()</code> method</html>

6
.idea/encodings.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>

12
.idea/misc.xml Normal file
View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="ProjectKey">
<option name="state" value="project://e2804f05-5315-4fc6-a121-c522a6c26470" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_16" default="true" project-jdk-name="16" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Assignment7.iml" filepath="$PROJECT_DIR$/Assignment7.iml" />
</modules>
</component>
</project>

View file

@ -0,0 +1,3 @@
<template>
<input-field default="com.company">IJ_BASE_PACKAGE</input-field>
</template>

6
.idea/vcs.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

11
Assignment7.iml Normal file
View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,92 @@
For this assignment, you will work on a multi-file program which represents a game using a spinnable Game Wheel, a bit like the one seen on the show Wheel Of Fortune (below).
![](https://files.projectstem.org/CSA/CSA_Resources/Unit7/Images/Wheel_of_Fortune.png)
Your Game Wheel will have 20 evenly cut colored “slices”, each marked with a cash prize amount. Two classes are used to create this wheel: GameWheel and Slice. You will not need to edit the Slice class, but you will implement some of the methods for the GameWheel class. You will also write a method in a third class named Game which can be used to play the game.
As this assignment uses pre-written methods which you will need to use in your code, brief documentation of the constructors and public methods of the GameWheel and Slice classes is included at the end of the assignment brief.
### Part 1: GameWheel
When a new GameWheel is created it always contains 20 slices, which are represented in the GameWheel class by an ArrayList of Slice objects. The wheel is initialized so that every slice with an odd index is red and every slice with an even index is blue, with the exception of the slices at positions 0, 5, 10 and 15 which are all black. Every slice is initialized with a different prize value.
You will implement three different public methods in the GameWheel class.
**Methods to write**
| toString | The first of these methods is the toString method, which returns a String consisting of a numbered list of all slices of GameWheel (using the slice toString method), one on each line. This should be in the format shown below: |
|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| | 0 - Color: black, Prize Amount: $4000 |
| | 1 - Color: red, Prize Amount: $10 |
| | 2 - Color: blue, Prize Amount: $200 |
| | 3 - Color: red, Prize Amount: $30 |
| | ... |
| | 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. |
### Part 2: Game/play
When you have done the above, you will write a method in the Game class named play. This method has a single GameWheel parameter. When run, this method will simulate a game in which the game wheel represented by this parameter is spun three times and the results are used to calculate a total prize. The total prize is found by adding the values on the three slices. If all three slices are the same color, then this sum should be doubled (and an additional line should be printed at the end explaining this). For the exact format of the output of the program, see the sample runs below (as this program involves some randomization, the actual output will vary).
### Sample run 1
Total prize money: $7000
Spin 1 - Color: black, Prize Amount: $4000
Spin 2 - Color: blue, Prize Amount: $2400
Spin 3 - Color: blue, Prize Amount: $600
### Sample run 2
Total prize money: $820
Spin 1 - Color: red, Prize Amount: $50
Spin 2 - Color: red, Prize Amount: $130
Spin 3 - Color: red, Prize Amount: $230
Three reds = double your money!
### Testing your code
The fourth class named Runner has a pre-written main method which will let you test your code. You can choose to play the game by calling the play method from your game class or test individual methods from the GameWheel class. Please do not write a main method in any of the other classes as these will stop your code from being scored correctly.
### Documentation
**Slice class**
| Slice(String c, int p) | Creates a slice with color c, and cash prize p. |
|------------------------|--------------------------------------------------------------------------------------------------------|
| int getPrizeAmount() | Returns the cash prize in dollars for this slice. |
| String getColor() | Returns the color of this slice as a string. |
| String toString() | Returns a string representation of the slice in the following format: “Color: red, Prize amount: $50”. |
**GameWheel class**
| GameWheel() | Creates a wheel with 20 slices with preset prize amounts. |
|---------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| GameWheel(int\[\] prizes) | Creates a wheel with the prize amounts given in the prizes array. |
| Slice spinWheel() | Spins the wheel so that a different slice is selected. Return that slice (Note: the 10 slices following the current slice are more likely to be returned than the other 10). |
Milestones
----------
As you work on this assignment, you can use the milestones below to inform your development process:
**Milestone 1:** Set up the toString method for the gameWheel class so you can test your methods as you write them. Plan how you will decompose the scramble and sort methods (i.e. are there helper methods which can be used by both).
**Milestone 2:** Write the scramble and sort methods in the gameWheel class. Use the runner class and the toString methods to test these methods work exactly as intended.
**Milestone 3:** Implement the play method in the game class. Make sure you run this multiple times to ensure it works under a variety of conditions.
Decomposition hints
----------------------
More than any exercise or assignment you've seen so far, this assignment requires you to carefully decompose what needs to be done into steps. This is especially true when implementing the `scramble` and `sort` methods. There is no one right way to do this, and very different approaches can still end up working. Below are some hints which might help you get started, but do feel free to experiment and come up with your own way.
* One way to implement the `scramble` and `sort` methods might be to split the `slices` list up into separate lists by color, then put them back together into the `slices` list once they are scrambled/sorted.
* You could sort/scramble these separate lists, or be even smarter and sort/scramble them either as you put them into the list or as you put them back into `slices`.
* Because there is a lot of repetition in these methods you should think about writing helper methods that prevent you from having to write code which is almost the same several times.
* Examples of where helper methods could be useful include splitting slices into lists by color, sorting a list, scrambling a list, and recombining separate lists to make a new `slices` list.

10
src/com/company/Game.java Normal file
View file

@ -0,0 +1,10 @@
package com.company;
public class Game
{
public static void play(GameWheel g)
{
// Implement the play method here
String slice1 = GameWheel.spinWheel().toString();
}
}

View 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;
}
}

View file

@ -0,0 +1,34 @@
package com.company;
public class Slice
{
private final String color;
private final int prizeAmount;
// Creates a slice with color c, and cash prize p
public Slice(String c, int p) {
color = c;
prizeAmount = p;
}
// Returns the cash prize in dollars for this slice
public int getPrizeAmount() {
return prizeAmount;
}
// Returns the color of this slice as a string
public String getColor() {
return color;
}
/* Returns a string representation of the slice in the following format:
* "Color: red, prize amount: $50".
*/
public String toString() {
return "Color: " + color + ", Prize Amount: $" + prizeAmount;
}
}

92
src/com/company/readme.md Normal file
View file

@ -0,0 +1,92 @@
For this assignment, you will work on a multi-file program which represents a game using a spinnable Game Wheel, a bit like the one seen on the show Wheel Of Fortune (below).
![](https://files.projectstem.org/CSA/CSA_Resources/Unit7/Images/Wheel_of_Fortune.png)
Your Game Wheel will have 20 evenly cut colored “slices”, each marked with a cash prize amount. Two classes are used to create this wheel: GameWheel and Slice. You will not need to edit the Slice class, but you will implement some of the methods for the GameWheel class. You will also write a method in a third class named Game which can be used to play the game.
As this assignment uses pre-written methods which you will need to use in your code, brief documentation of the constructors and public methods of the GameWheel and Slice classes is included at the end of the assignment brief.
### Part 1: GameWheel
When a new GameWheel is created it always contains 20 slices, which are represented in the GameWheel class by an ArrayList of Slice objects. The wheel is initialized so that every slice with an odd index is red and every slice with an even index is blue, with the exception of the slices at positions 0, 5, 10 and 15 which are all black. Every slice is initialized with a different prize value.
You will implement three different public methods in the GameWheel class.
**Methods to write**
| toString | The first of these methods is the toString method, which returns a String consisting of a numbered list of all slices of GameWheel (using the slice toString method), one on each line. This should be in the format shown below: |
|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| | 0 - Color: black, Prize Amount: $4000 |
| | 1 - Color: red, Prize Amount: $10 |
| | 2 - Color: blue, Prize Amount: $200 |
| | 3 - Color: red, Prize Amount: $30 |
| | ... |
| | 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|
### Part 2: Game/play
When you have done the above, you will write a method in the Game class named play. This method has a single GameWheel parameter. When run, this method will simulate a game in which the game wheel represented by this parameter is spun three times and the results are used to calculate a total prize. The total prize is found by adding the values on the three slices. If all three slices are the same color, then this sum should be doubled (and an additional line should be printed at the end explaining this). For the exact format of the output of the program, see the sample runs below (as this program involves some randomization, the actual output will vary).
### Sample run 1
Total prize money: $7000
Spin 1 - Color: black, Prize Amount: $4000
Spin 2 - Color: blue, Prize Amount: $2400
Spin 3 - Color: blue, Prize Amount: $600
### Sample run 2
Total prize money: $820
Spin 1 - Color: red, Prize Amount: $50
Spin 2 - Color: red, Prize Amount: $130
Spin 3 - Color: red, Prize Amount: $230
Three reds = double your money!
### Testing your code
The fourth class named Runner has a pre-written main method which will let you test your code. You can choose to play the game by calling the play method from your game class or test individual methods from the GameWheel class. Please do not write a main method in any of the other classes as these will stop your code from being scored correctly.
### Documentation
**Slice class**
| Slice(String c, int p) | Creates a slice with color c, and cash prize p. |
|------------------------|--------------------------------------------------------------------------------------------------------|
| int getPrizeAmount() | Returns the cash prize in dollars for this slice. |
| String getColor() | Returns the color of this slice as a string. |
| String toString() | Returns a string representation of the slice in the following format: “Color: red, Prize amount: $50”. |
**GameWheel class**
| GameWheel() | Creates a wheel with 20 slices with preset prize amounts. |
|---------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| GameWheel(int\[\] prizes) | Creates a wheel with the prize amounts given in the prizes array. |
| Slice spinWheel() | Spins the wheel so that a different slice is selected. Return that slice (Note: the 10 slices following the current slice are more likely to be returned than the other 10). |
Milestones
----------
As you work on this assignment, you can use the milestones below to inform your development process:
**Milestone 1:** Set up the toString method for the gameWheel class so you can test your methods as you write them. Plan how you will decompose the scramble and sort methods (i.e. are there helper methods which can be used by both).
**Milestone 2:** Write the scramble and sort methods in the gameWheel class. Use the runner class and the toString methods to test these methods work exactly as intended.
**Milestone 3:** Implement the play method in the game class. Make sure you run this multiple times to ensure it works under a variety of conditions.
Decomposition hints
----------------------
More than any exercise or assignment you've seen so far, this assignment requires you to carefully decompose what needs to be done into steps. This is especially true when implementing the `scramble` and `sort` methods. There is no one right way to do this, and very different approaches can still end up working. Below are some hints which might help you get started, but do feel free to experiment and come up with your own way.
* One way to implement the `scramble` and `sort` methods might be to split the `slices` list up into separate lists by color, then put them back together into the `slices` list once they are scrambled/sorted.
* You could sort/scramble these separate lists, or be even smarter and sort/scramble them either as you put them into the list or as you put them back into `slices`.
* Because there is a lot of repetition in these methods you should think about writing helper methods that prevent you from having to write code which is almost the same several times.
* Examples of where helper methods could be useful include splitting slices into lists by color, sorting a list, scrambling a list, and recombining separate lists to make a new `slices` list.

View file

@ -0,0 +1,66 @@
package com.company;
import java.util.Scanner;
public class runner_GameWheel{
private static Scanner scan;
private static String instruct;
private static GameWheel wheel;
public static void main(String[] args){
scan = new Scanner(System.in);
wheel = new GameWheel();
instruct = "";
while(!instruct.equals("q")){
System.out.println("Type \"p\" to play game, \"t\" to test GameWheel methods, \"q\" to quit.");
instruct = scan.nextLine();
if(instruct.equals("p"))
Game.play(wheel);
else if(instruct.equals("t"))
testMethods();
else if(!instruct.equals("q"))
System.out.print("Instruction not recognized");
System.out.println();
}
}
private static void testMethods(){
while(!instruct.equals("q") && !instruct.equals("m")){
System.out.println("Type GameWheel method to call (toString, scramble, sort). Or type \"m\" for main menu, \"q\" to quit");
instruct = scan.nextLine();
if(instruct.equals("toString"))
System.out.println(wheel);
else if(instruct.equals("scramble"))
wheel.scramble();
else if(instruct.equals("sort"))
wheel.sort();
else if(instruct.equals("new"))
wheel = new GameWheel(makeStarterArray());
else if(instruct.equals("random_prize_test"))
ranTest();
else if(!instruct.equals("q") && !instruct.equals("m"))
System.out.println("Instruction not recognized");
}
}
private static int[] makeStarterArray(){
int[] arr = new int[20];
for(int i = 0; i < 20; i++){
arr[i] = scan.nextInt();
}
scan.nextLine();
return arr;
}
private static void ranTest(){
int numLines = scan.nextInt();
for(int i = 0; i<numLines; i++){
wheel.scramble();
for(int j = 0; j < 20; j++)
System.out.print(wheel.getSlice(j).getPrizeAmount() + " ");
System.out.println();
}
scan.nextLine();
}
}