From f8f054c985d6c14bef933f2e04c8c07c8b54acc2 Mon Sep 17 00:00:00 2001 From: blzimme000 Date: Wed, 16 Feb 2022 14:40:39 -0600 Subject: [PATCH] Done with all but Game.java --- .idea/.gitignore | 3 + .idea/description.html | 1 + .idea/encodings.xml | 6 + .idea/misc.xml | 12 ++ .idea/modules.xml | 8 ++ .idea/project-template.xml | 3 + .idea/vcs.xml | 6 + Assignment7.iml | 11 ++ .../Assignment7/com/company/Game.class | Bin 0 -> 384 bytes .../Assignment7/com/company/GameWheel.class | Bin 0 -> 2301 bytes .../Assignment7/com/company/Slice.class | Bin 0 -> 1155 bytes .../Assignment7/com/company/readme.md | 92 ++++++++++++++ .../com/company/runner_GameWheel.class | Bin 0 -> 2929 bytes src/com/company/Game.java | 10 ++ src/com/company/GameWheel.java | 116 ++++++++++++++++++ src/com/company/Slice.java | 34 +++++ src/com/company/readme.md | 92 ++++++++++++++ src/com/company/runner_GameWheel.java | 66 ++++++++++ 18 files changed, 460 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/description.html create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/project-template.xml create mode 100644 .idea/vcs.xml create mode 100644 Assignment7.iml create mode 100644 out/production/Assignment7/com/company/Game.class create mode 100644 out/production/Assignment7/com/company/GameWheel.class create mode 100644 out/production/Assignment7/com/company/Slice.class create mode 100644 out/production/Assignment7/com/company/readme.md create mode 100644 out/production/Assignment7/com/company/runner_GameWheel.class create mode 100644 src/com/company/Game.java create mode 100644 src/com/company/GameWheel.java create mode 100644 src/com/company/Slice.java create mode 100644 src/com/company/readme.md create mode 100644 src/com/company/runner_GameWheel.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/description.html b/.idea/description.html new file mode 100644 index 0000000..db5f129 --- /dev/null +++ b/.idea/description.html @@ -0,0 +1 @@ +Simple Java application that includes a class with main() method \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..b5b16a9 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..bf4a6f9 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/project-template.xml b/.idea/project-template.xml new file mode 100644 index 0000000..1f08b88 --- /dev/null +++ b/.idea/project-template.xml @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Assignment7.iml b/Assignment7.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Assignment7.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/Assignment7/com/company/Game.class b/out/production/Assignment7/com/company/Game.class new file mode 100644 index 0000000000000000000000000000000000000000..4aa9dee78b8c9ed4ec0788a40b356d64a4462ace GIT binary patch literal 384 zcmZ`#!A`P`{r~^~ literal 0 HcmV?d00001 diff --git a/out/production/Assignment7/com/company/GameWheel.class b/out/production/Assignment7/com/company/GameWheel.class new file mode 100644 index 0000000000000000000000000000000000000000..3b2cd13198f7318ff62b6056038cb1e4ba03f7e0 GIT binary patch literal 2301 zcmZuzT~iZh6n-|l$tGci5C|#>)^9^lVwL&2K&&XZjPIj+Q>}n>55KOm^R|bIx;~^PD&N?Vlf?14yCl zherVd(BReKGoZt-Ad+)RNq#D3`P1Yjvt-Q|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PP>av<}JFe5wX9; zM}Jdl9*Sv2Mik8G@Vtbf@JbNhPR;P&G6_NEnNaUf!K-R2D#UO+DvIBkh(^2BzX~&J zh()CUws9qt7YiL?55E>>WUu_55UjK}i^cjav0)1Q6=1WHD{P8YBvE2REMXju#NEfx iI;Gxd3@?$cvW}2-?1J|dbWy^-kp4H_DJXKwgVMj{fw)Bg literal 0 HcmV?d00001 diff --git a/out/production/Assignment7/com/company/Slice.class b/out/production/Assignment7/com/company/Slice.class new file mode 100644 index 0000000000000000000000000000000000000000..9d82efbcb7cbd54a6174f9e9276ab3a46b467175 GIT binary patch literal 1155 zcmaJ=+fLg+5Iqx;IGB(?pxjDnXhR8s;$B`Vf-1Fe!BoOQOo!-P!S+Gc(8IU%#(@0@%k>6Ddp>NavA3mN0WF&V}U) zuVcMEK9#m6WcMA<(a#AJ)vcpRLI$2M-elGslM-gk}rB{;BgXambji$9&?x{EcV2iY$(qb`n{vOYz10)Iv{LT zM_HR&qdvmI*vKP7x}i?E%wo&&aAbd`WG7OAKUkUHTl|iMY z)Y+h4A5}Er`3HD-(U)VwKPKYiNvbCm7*aHZ>$V*&>pAQYRwA+X%Cu9C)GWu-u~)7T zA>Z^o={H;v1TrvS5jIA89@mP2$Aq>2hw_leePf*`G{uvc;)f?ZvUptiOoK-ow|HOR z+lTPC;j>u#0jh0Z!loFc9<2{yjo056RNum1%Pw{OS*|)Y<3sOvof;ovNO`eeS*IJm-1N zJ>6ga@ag9O2Jm4N4QMpbWTF`@3hfu{i*_<=dy~l%=Po!ItFoz zgo}#W%_fI4w&yuPE23Csz%;QMYnFH!o-Jr6r_kVf3TubLovxoud-*FgfNsP3j6I}+@n&`$lh2D&xOY+Lw-fS``3Wo7V?VK|@JsKrYuZcUbNul{t)!n+G71yUCuoNIW`b^w~Eea9WE9jsoyv3@) zLoS)A>rHa~-3n?ZiZ~Jm`c2$}?MvAT*O7B)rf6pi1X|;xPLWjNDWQ9Zi6ri$Dt?i{ z7MVTCNDItHuuI|4soA_^_2v64?OXY*J!?&psO?st4lUX#XzLaQR?g8={zQS(GvV}1 z(bYSm*o_AaJZRz}>>;fSD%QnPF!B;PD>B!EuBDzB5t;XzNMV39dCqfs$n_kBu6SZ4 zqF8S#AH@M2G;qkoAP$qLe21y07F^xQdTLQ+3&;&I8lIm}QjCxwZY2%eA$ic3U@V1&FaxDgOP6%c2Z z(Oo^YDam_`>lcDaX`eMQCZNaVd@!`wY!l;R%dn6<=aR7Gm^hC~vYoT1ondVU+6fK@ zfzaF(PmHByn=8ll0vU;qr6m-PTt{Gg6Mk--9L+i7!mv2=O!z{%!P!%x&A^O_0IY38 ziXiQ=df8b*w?z{dafz@e9X*_NGc1qpcsj8}+tALQF^f3^KQr-jan(^@8MU2j=!6A2 z=N!!WMNh}r?|y0GdAy*oPEs56y^O6#T|LE7LED}V&l|%n_P|>4GV?)yf@s>LW*;x+ z&N;y;n;Oa~_bKp*9k}vbGdAfdm(DgV0lvITrRY_pmK?`!kC~v{O?EIzKbR*%>j?FX!wx9eL$ZDG~vQI<>A zDQp??{pn&pbvprdmL(hIpsldHo}-0BaJ!2^9>!r;J8Z>&BT0(=+f2*bV=!wM3Qobm zKNPm!mfK2@4SYa}e|#xBc{M+5=3!`H2{y8I^9{yPRNffjo$ngno5Ayh7XH7;rzG!u zx1hQI2GlhU8t@YTWm=ZL%zv{szKmD+)QVT}D_Ugp*9;@;=01aQW*)6K&~_cM&#?Z? z4fI^c#@Lzs7Iq=eN8JH1{UkV-YgQc3w?ctz$U(eX@w^Uow0m%MV@k{%8VF7qATZR7$T~S)hN5aR5`gQ?;#QcetUGYA~xYp5@lf*YiB?DnCo45 zk`;3nTiKxR#tdg)^D9oV)JD5zz`WCCW?FuIw b@CM%GJ;L1H;`LX&$EPSqf5-c{iVgn**hkT9 literal 0 HcmV?d00001 diff --git a/src/com/company/Game.java b/src/com/company/Game.java new file mode 100644 index 0000000..aa68a11 --- /dev/null +++ b/src/com/company/Game.java @@ -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(); + } +} diff --git a/src/com/company/GameWheel.java b/src/com/company/GameWheel.java new file mode 100644 index 0000000..ea0616d --- /dev/null +++ b/src/com/company/GameWheel.java @@ -0,0 +1,116 @@ +package com.company; + +import java.util.ArrayList; +import java.util.Collections; + +public class GameWheel +{ + private final ArrayList 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; + } +} diff --git a/src/com/company/Slice.java b/src/com/company/Slice.java new file mode 100644 index 0000000..3cd163d --- /dev/null +++ b/src/com/company/Slice.java @@ -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; + } +} diff --git a/src/com/company/readme.md b/src/com/company/readme.md new file mode 100644 index 0000000..86a4da9 --- /dev/null +++ b/src/com/company/readme.md @@ -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. \ No newline at end of file diff --git a/src/com/company/runner_GameWheel.java b/src/com/company/runner_GameWheel.java new file mode 100644 index 0000000..c7e5d55 --- /dev/null +++ b/src/com/company/runner_GameWheel.java @@ -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