From aab91a6f9c4e33635ddf534107a106791b0310b2 Mon Sep 17 00:00:00 2001 From: Andy James Date: Fri, 4 May 2018 08:07:54 -0700 Subject: [PATCH 01/30] Minigame Factory is now a static class, as it should be --- Test/org/vashonsd/Utils/UtilsTests.java | 5 + src/org/vashonsd/Games/MinigameFactory.java | 2 + src/org/vashonsd/Utils/Cards/Card.java | 166 ++++++++++++++++++++ src/org/vashonsd/Utils/Cards/Deck.java | 90 +++++++++++ src/org/vashonsd/Utils/DiceGame.java | 45 ++++++ src/org/vashonsd/Utils/Minigame.java | 24 +++ src/org/vashonsd/Utils/Utils.java | 7 + 7 files changed, 339 insertions(+) create mode 100644 src/org/vashonsd/Utils/Cards/Card.java create mode 100644 src/org/vashonsd/Utils/Cards/Deck.java create mode 100644 src/org/vashonsd/Utils/DiceGame.java diff --git a/Test/org/vashonsd/Utils/UtilsTests.java b/Test/org/vashonsd/Utils/UtilsTests.java index b6bc965..4e52362 100644 --- a/Test/org/vashonsd/Utils/UtilsTests.java +++ b/Test/org/vashonsd/Utils/UtilsTests.java @@ -12,4 +12,9 @@ public void testIsInteger(){ Assert.assertTrue(Utils.IsInteger("3")); Assert.assertFalse(Utils.IsInteger("th4n")); } + + @Test + public void testRollDie() { + Assert.assertTrue(true); + } } diff --git a/src/org/vashonsd/Games/MinigameFactory.java b/src/org/vashonsd/Games/MinigameFactory.java index 03e547c..e3dee18 100644 --- a/src/org/vashonsd/Games/MinigameFactory.java +++ b/src/org/vashonsd/Games/MinigameFactory.java @@ -9,6 +9,7 @@ import org.vashonsd.Games.RI.RobertGame; import org.vashonsd.Games.SP.SamGame; import org.vashonsd.Games.SR.SeanGame; +import org.vashonsd.Utils.DiceGame; import org.vashonsd.Utils.Minigame; import java.util.HashMap; @@ -34,6 +35,7 @@ public class MinigameFactory { addGame(new SamGame()); addGame(new SeanGame()); addGame(new NabilGame()); + addGame(new DiceGame()); } public static void addGame(Minigame m) { diff --git a/src/org/vashonsd/Utils/Cards/Card.java b/src/org/vashonsd/Utils/Cards/Card.java new file mode 100644 index 0000000..090f221 --- /dev/null +++ b/src/org/vashonsd/Utils/Cards/Card.java @@ -0,0 +1,166 @@ +package org.vashonsd.Utils.Cards; + +public class Card +{ + // These constants represent the possible suits and + // can be used to index into the suits array to get + // their string representation. + private static final int HEARTS = 0; + private static final int DIAMONDS = 1; + private static final int SPADES = 2; + private static final int CLUBS = 3; + + // These constants represent the ranks of the non-number + // cards, or cards above 10. To maintain the ordering after + // 2-10, the integer values are 11, 12, 13, and 14 and + // also allow us to index into the ranks array to get their + // String representation. + private static final int JACK = 11; + private static final int QUEEN = 12; + private static final int KING = 13; + private static final int ACE = 14; + + // Instance variables + + // This represents the rank of the card, the value from 2 to Ace. + private int rank; + + // This represents the suit of the card, one of hearts, diamonds, spades or clubs. + private int suit; + + // This represents the value of the card, which is 10 for face cards or 11 for an ace. + private int value; + + // This String array allows us to easily get the String value of a Card from its rank. + // There are two Xs in the front to provide padding so numbers have their String representation + // at the corresponding index. For example, the String for 2 is at index 2. + private String[] ranks = {"X", "X", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; + + // The String array allow us to easily get the String value of the Card for its suit. This + // is the same order as the suits above so we can index into this array. + private String[] suits = {"H", "D", "S", "C"}; + + /** + * This is the constructor to create a new Card. To create a new card + * we pass in its rank and its suit. + * + * @param r The rank of the card, as an int. + * @param s The suit of the card, as an int. + */ + public Card(int r, int s) + { + rank = r; + suit = s; + } + + // Getter Methods + + /** + * This returns the rank of the card as an integer. + * + * @return rank of card as an int. + */ + public int getRank() + { + return rank; + } + + /** + * This returns the suit of the card as an integer. + * + * @return suit of card as an int. + */ + public int getSuit() + { + return suit; + } + + /** + * This returns the value of the card as an integer. + * + * For facecards the value is 10, which is different + * than their rank underlying value. For aces the default + * value is 11. + * + * @return The value of the card as an int. + */ + public int getValue() + { + int value = rank; + if(rank > 10) + { + value = 10; + } + + if(rank == ACE) + { + value = 11; + } + + return value; + } + + /** + * This utility method converts from a rank integer to a String. + * + * @param r The rank. + * @return String version of rank. + */ + public String rankToString(int r) + { + return ranks[r]; + } + + /** + * This utility method converts from a suit integer to a String. + * + * @param s The suit. + * @return String version of suit. + */ + public String suitToString(int s) + { + return suits[s]; + } + + /** + * Return the String version of the suit. + * + * @return String version of suit. + */ + public String getSuitAsString() + { + return suitToString(suit); + } + + /** + * Return the String version of the rank. + * + * @return String version of the rank. + */ + public String getRankAsString() + { + return rankToString(rank); + } + + /** + * This returns the String representation of a card which + * will be two characters. For example, the two of hearts would + * return 2H. Face cards have a short string so the ace of + * spades would return AS. + * + * @return String representation of Card. + */ + public String toString() + { + // Get a string for rank + String rankString = ranks[rank]; + + // Get a string for the suit + String suitString = suits[suit]; + + // combine those + return rankString + suitString; + } + + +} \ No newline at end of file diff --git a/src/org/vashonsd/Utils/Cards/Deck.java b/src/org/vashonsd/Utils/Cards/Deck.java new file mode 100644 index 0000000..7fd7cfe --- /dev/null +++ b/src/org/vashonsd/Utils/Cards/Deck.java @@ -0,0 +1,90 @@ +package org.vashonsd.Utils.Cards; + +import java.util.*; + +public class Deck +{ + private static final int HEARTS = 0; + private static final int DIAMONDS = 1; + private static final int SPADES = 2; + private static final int CLUBS = 3; + + private static final int JACK = 11; + private static final int QUEEN = 12; + private static final int KING = 13; + private static final int ACE = 14; + + // Instance variables + + // This stores the deck which is a list of the Card objects. + private ArrayList deck; + + /** + * This creates a Deck. A Deck starts as a list of 52 cards. + * We loop through each suit and rank and construct a card + * and add it to the deck. + */ + public Deck() + { + deck = new ArrayList(); + + for(int rank = 2; rank <= ACE; rank++) + { + for(int suit = HEARTS; suit <= CLUBS; suit++) + { + Card card = new Card(rank, suit); + deck.add(card); + } + } + } + + // Getter method + + /** + * This getter method returns the ArrayList of cards. + * @return ArrayList of the Cards. + */ + public ArrayList getCards() + { + return deck; + } + + /** + * This deals the first Card from the deck by removing it. + * @return The first Card in the deck. + */ + public Card deal() + { + return deck.remove(0); + } + + /** + * This prints out the current state of the deck. + */ + public void print() + { + for(Card card: deck) + { + System.out.println(card); + } + } + + /** + * This shuffles the deck by making 52 swaps of + * card positions. + */ + public void shuffle() + { + for(int i = 0; i < deck.size(); i++) + { + Random rand = new Random(); + int randomIndex = rand.nextInt(52); + Card x = deck.get(i); + Card y = deck.get(randomIndex); + + deck.set(i, y); + deck.set(randomIndex, x); + } + } + +} diff --git a/src/org/vashonsd/Utils/DiceGame.java b/src/org/vashonsd/Utils/DiceGame.java new file mode 100644 index 0000000..9cb68ea --- /dev/null +++ b/src/org/vashonsd/Utils/DiceGame.java @@ -0,0 +1,45 @@ +package org.vashonsd.Utils; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by andy on 5/3/18. + */ +public class DiceGame extends Minigame { + List playerDice; + List computerDice; + int round = 1; + + public DiceGame() { + super("Dice Wars", "A battle of wits and luck", "quit"); + playerDice = new ArrayList(); + computerDice = new ArrayList(); + } + + @Override + public String start() { + setUp(); + return "Welcome to Dice Wars!"; + } + + private void setUp() { + for(int i=0; i<3; i++) { + playerDice.add(Utils.rollDie()); + computerDice.add(Utils.rollDie()); + } + } + + @Override + public String handle(String str) { + if(round == 1) { + return "Your dice are " + playerDice.toString(); + } + return null; + } + + @Override + public String quit() { + return "Thanks for playing!"; + } +} diff --git a/src/org/vashonsd/Utils/Minigame.java b/src/org/vashonsd/Utils/Minigame.java index 9efae7a..456106f 100644 --- a/src/org/vashonsd/Utils/Minigame.java +++ b/src/org/vashonsd/Utils/Minigame.java @@ -26,9 +26,33 @@ public String getQuitWord() { return this.quitWord; } + /** + * This method will be called when the game is started. + * + * This method can be used just to return a welcome message, + * or it could also be used to set up the starting state of a game. + * If the game will ever restarting, consider creating a setUp() + * method that can be reused. + * + * @return A String representing a greeting. + */ public abstract String start(); + /** + * This method will be called while the game is in play. + * + * handle() is the sole public method that should be called during + * game interactions. + * + * @param str The String representing input from the user. + * @return The response to the user. + */ public abstract String handle(String str); + /** + * This method will be called with the user signals a quit. + * + * @return A String representing an exit message + */ public abstract String quit(); } diff --git a/src/org/vashonsd/Utils/Utils.java b/src/org/vashonsd/Utils/Utils.java index 5ffb8df..11589b0 100644 --- a/src/org/vashonsd/Utils/Utils.java +++ b/src/org/vashonsd/Utils/Utils.java @@ -1,6 +1,8 @@ package org.vashonsd.Utils; +import java.util.Random; + public class Utils { /** @@ -19,4 +21,9 @@ public static boolean IsInteger(String s) { } return true; } + + public static int rollDie() { + Random rand = new Random(); + return rand.nextInt(5)+1; + } } From 8f2c8dc7174e4a5aabb80e6df20015e16aa0692d Mon Sep 17 00:00:00 2001 From: Andy James Date: Fri, 4 May 2018 08:13:04 -0700 Subject: [PATCH 02/30] Added comments to Minigame. Added a Cards class to utils. --- src/org/vashonsd/Utils/Minigame.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/org/vashonsd/Utils/Minigame.java b/src/org/vashonsd/Utils/Minigame.java index 456106f..0c6ddbe 100644 --- a/src/org/vashonsd/Utils/Minigame.java +++ b/src/org/vashonsd/Utils/Minigame.java @@ -1,7 +1,11 @@ package org.vashonsd.Utils; /** - * Created by andy on 5/2/18. + * A Minigame can be run as a text-based game that interacts with the user. + * + * The mechanics of interacting with the user are left out of this code. + * It uses public methods to start, to finish, and, in between, respond + * to Strings with Strings. */ public abstract class Minigame { protected String name; From 9f9c1afbe5cacd1a9a2db88b776f303c614906f7 Mon Sep 17 00:00:00 2001 From: "profit.samuel" Date: Fri, 18 May 2018 08:36:29 -0700 Subject: [PATCH 03/30] It works, but needs work. --- Minigames.iml | 10 ++++ src/org/vashonsd/Games/SP/SamGame.java | 69 ++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/Minigames.iml b/Minigames.iml index 6372453..296a066 100644 --- a/Minigames.iml +++ b/Minigames.iml @@ -19,5 +19,15 @@ + + + + + + + + + + \ No newline at end of file diff --git a/src/org/vashonsd/Games/SP/SamGame.java b/src/org/vashonsd/Games/SP/SamGame.java index b406123..b519838 100644 --- a/src/org/vashonsd/Games/SP/SamGame.java +++ b/src/org/vashonsd/Games/SP/SamGame.java @@ -1,13 +1,74 @@ package org.vashonsd.Games.SP; +import org.vashonsd.Utils.Minigame; +import org.vashonsd.Utils.Utils; -import org.vashonsd.Utils.Placeholder; +import java.util.Random; +import java.util.Scanner; /** * Created by andy on 5/2/18. */ -public class SamGame extends Placeholder { + public class SamGame extends Minigame { - public SamGame() { - super("Sam"); + Random rn = new Random(); + int answer = rn.nextInt(10) + 1; + int current = answer; + int userInt1 = rn.nextInt(21) + 1; + int userInt = userInt1; + + public SamGame() { + super("Sam", "The saddest game in the world", "quit"); + } + + @Override + public String start() { + return "Welcome to my game, your cards hold a value of " + current + " would you like to hit or stick"; + } + + public void setUp() { + + } + @Override + public String handle(String str) { + if(str.equalsIgnoreCase("hit")) { + int answer2 = rn.nextInt(10)+1; + int newAnswer = current + answer2; + current = newAnswer; + if(current > 21) { + return "You busted you idiot!"; + } + return "You cards hold a value of " + current + " would you like to hit or stick"; + } + if(str.equalsIgnoreCase("stick")) { + if(userInt < current) { + if(current > 21) { + return "You busted you idiot!"; + } + return "You win, and finished with a value of " + current + " the computer had "+userInt; + } else if(userInt > current){ + if(current > 21) { + return "You busted you idiot!"; + } + return "You lose, and you finished with a value of " + current + " the computer had " + userInt; + } else { + return "You pushed"; + } + } + if(!Utils.IsInteger(str)) { + return "try typing hit or stick"; + } + + if(userInt < current) { + return "You win"; + } else if(userInt > current) { + return "You lose"; + } else { + return "PUSH"; + } + } + + @Override + public String quit() { + return null; } } From 61951bd65d618381d16b6acf588e09b5e5953d63 Mon Sep 17 00:00:00 2001 From: BeckersAmaze Date: Fri, 18 May 2018 08:42:06 -0700 Subject: [PATCH 04/30] Works, but I still need to make the method that checks whether each guessed letter is in the word you are guessing. --- Minigames.iml | 10 ++++ Test/org/vashonsd/Utils/UtilsTests.java | 35 ++++++------ src/org/vashonsd/Games/BR/BeckettGame.java | 63 ++++++++++++++++++++- src/org/vashonsd/Games/MinigameFactory.java | 20 +++---- src/org/vashonsd/Utils/Utils.java | 15 +++++ 5 files changed, 113 insertions(+), 30 deletions(-) diff --git a/Minigames.iml b/Minigames.iml index 6372453..064c0ba 100644 --- a/Minigames.iml +++ b/Minigames.iml @@ -19,5 +19,15 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Test/org/vashonsd/Utils/UtilsTests.java b/Test/org/vashonsd/Utils/UtilsTests.java index 4e52362..11d9bee 100644 --- a/Test/org/vashonsd/Utils/UtilsTests.java +++ b/Test/org/vashonsd/Utils/UtilsTests.java @@ -1,20 +1,21 @@ package org.vashonsd.Utils; -import org.junit.Assert; -import org.junit.Test; +//import org.junit.Assert; +//import org.junit.Test; +import org.junit.jupiter.api.Test; -/** - * Created by andy on 5/2/18. - */ -public class UtilsTests { - @Test - public void testIsInteger(){ - Assert.assertTrue(Utils.IsInteger("3")); - Assert.assertFalse(Utils.IsInteger("th4n")); - } - - @Test - public void testRollDie() { - Assert.assertTrue(true); - } -} +///** +// * Created by andy on 5/2/18. +// */ +//public class UtilsTests { +// @Test +// public void testIsInteger(){ +// //Assert.assertTrue(Utils.IsInteger("3")); +// //Assert.assertFalse(Utils.IsInteger("th4n")); +// } +// +// @Test +// public void testRollDie() { +// Assert.assertTrue(true); +// } +//} diff --git a/src/org/vashonsd/Games/BR/BeckettGame.java b/src/org/vashonsd/Games/BR/BeckettGame.java index c1ac84b..2d63b1a 100644 --- a/src/org/vashonsd/Games/BR/BeckettGame.java +++ b/src/org/vashonsd/Games/BR/BeckettGame.java @@ -1,13 +1,70 @@ package org.vashonsd.Games.BR; -import org.vashonsd.Utils.Placeholder; +import org.vashonsd.Utils.Minigame; +import org.vashonsd.Utils.Utils; + +import java.util.ArrayList; +import java.util.List; + /** * Created by andy on 5/2/18. */ -public class BeckettGame extends Placeholder { +public class BeckettGame extends Minigame { + //the letters the user guesses + private String hangLetters; + // Word that is being guessed letter by letter + private String hangWord; + // The amount of guesses someone has based on the hangWord's length + private int guessesLeft; + // A list full of the guesses letters + private ListguessedLetters; + // A list of the guessed letters that match the string + private ListcorrectLetters; + public BeckettGame() { - super("Beckett"); + super("Hangman", "A game where you save a life", "quit"); + guessedLetters = new ArrayList(); + correctLetters = new ArrayList(); + + } + public String start(){ + setUp(); + return "Thank you for choosing HANGMAN ... produced by Beckett \n...Guess a letter"; + } + + private void setUp(){ + hangWord = "word"; + guessesLeft = hangWord.length(); + } + + + public String handle(String str){ + if(str.length() > 1 || str.length() < 1) { + return "You lose, you didn't guess a single letter. Play again!"; + } + if(Utils.IsInteger(str)){ + return "You gotta guess a letter, not a number. Play again!"; + } + if(str.equalsIgnoreCase(hangLetters)){ + return "Right"; + } + if(!str.equalsIgnoreCase(hangLetters)){ + guessedLetters.add(str); + if(guessesLeft == 0){ + return "You ran out of guesses, type " + quitWord + " now"; + } + guessesLeft--; + return "Wrong, guess again... you have " + guessesLeft + " left " + guessedLetters; + } + else{ + return "congrats"; + } + } + + + public String quit(){ + return "You ended the game"; } } diff --git a/src/org/vashonsd/Games/MinigameFactory.java b/src/org/vashonsd/Games/MinigameFactory.java index e3dee18..84cdbe1 100644 --- a/src/org/vashonsd/Games/MinigameFactory.java +++ b/src/org/vashonsd/Games/MinigameFactory.java @@ -25,17 +25,17 @@ public class MinigameFactory { static { games = new HashMap(); - addGame(new TwentyQuestions()); - addGame(new AngelicaGame()); + //addGame(new TwentyQuestions()); + //addGame(new AngelicaGame()); addGame(new BeckettGame()); - addGame(new EmmeGame()); - addGame(new HuthaifaGame()); - addGame(new NoahGame()); - addGame(new RobertGame()); - addGame(new SamGame()); - addGame(new SeanGame()); - addGame(new NabilGame()); - addGame(new DiceGame()); + // addGame(new EmmeGame()); + // addGame(new HuthaifaGame()); + //addGame(new NoahGame()); + //addGame(new RobertGame()); + // addGame(new SamGame()); + // addGame(new SeanGame()); + // addGame(new NabilGame()); + // addGame(new DiceGame()); } public static void addGame(Minigame m) { diff --git a/src/org/vashonsd/Utils/Utils.java b/src/org/vashonsd/Utils/Utils.java index 11589b0..a402f2c 100644 --- a/src/org/vashonsd/Utils/Utils.java +++ b/src/org/vashonsd/Utils/Utils.java @@ -1,6 +1,8 @@ package org.vashonsd.Utils; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Random; public class Utils { @@ -26,4 +28,17 @@ public static int rollDie() { Random rand = new Random(); return rand.nextInt(5)+1; } + +// public static ArrayList isInGoalWord(char letter, String goalWord, String correctGuesses){ +// +// for(int i = 0; i < goalWord.length(); i++){ +// if(goalWord.substring(i).equalsIgnoreCase("" + letter)){ +// correctGuesses.replace(correctGuesses.substring(i, i + 1), letter); +// +// } +// +// } +// +// +// } } From 00f054f1281a9fa0e75d2f97b3f5a367d98f987a Mon Sep 17 00:00:00 2001 From: noahedmonds Date: Fri, 18 May 2018 08:43:18 -0700 Subject: [PATCH 05/30] It works --- Minigames.iml | 10 ++ Test/org/vashonsd/Utils/UtilsTests.java | 16 ++- src/org/vashonsd/Games/NE/Choice.java | 66 ++++++++++ src/org/vashonsd/Games/NE/ChoiceType.java | 5 + src/org/vashonsd/Games/NE/CodeHSReader.java | 24 ++++ src/org/vashonsd/Games/NE/NoahGame.java | 129 +++++++++++++++++++- src/org/vashonsd/Games/NE/Paper.java | 11 ++ src/org/vashonsd/Games/NE/Rock.java | 11 ++ src/org/vashonsd/Games/NE/Scissors.java | 11 ++ 9 files changed, 278 insertions(+), 5 deletions(-) create mode 100644 src/org/vashonsd/Games/NE/Choice.java create mode 100644 src/org/vashonsd/Games/NE/ChoiceType.java create mode 100644 src/org/vashonsd/Games/NE/CodeHSReader.java create mode 100644 src/org/vashonsd/Games/NE/Paper.java create mode 100644 src/org/vashonsd/Games/NE/Rock.java create mode 100644 src/org/vashonsd/Games/NE/Scissors.java diff --git a/Minigames.iml b/Minigames.iml index 6372453..296a066 100644 --- a/Minigames.iml +++ b/Minigames.iml @@ -19,5 +19,15 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Test/org/vashonsd/Utils/UtilsTests.java b/Test/org/vashonsd/Utils/UtilsTests.java index b6bc965..4470696 100644 --- a/Test/org/vashonsd/Utils/UtilsTests.java +++ b/Test/org/vashonsd/Utils/UtilsTests.java @@ -2,6 +2,11 @@ import org.junit.Assert; import org.junit.Test; +import org.vashonsd.Games.NE.Choice; +import org.vashonsd.Games.NE.Paper; +import org.vashonsd.Games.NE.Rock; +import org.vashonsd.Games.NE.Scissors; + /** * Created by andy on 5/2/18. @@ -9,7 +14,16 @@ public class UtilsTests { @Test public void testIsInteger(){ - Assert.assertTrue(Utils.IsInteger("3")); + Assert.assertTrue(Utils.IsInteger("3")); Assert.assertFalse(Utils.IsInteger("th4n")); } + + @Test + public void testCardComparison() { + Choice c = new Rock(); + Assert.assertEquals(c.determineWinner(new Paper()), "lose"); + Assert.assertEquals(c.determineWinner(new Scissors()), "win"); + Assert.assertEquals(c.determineWinner(new Rock()), "tie"); + + } } diff --git a/src/org/vashonsd/Games/NE/Choice.java b/src/org/vashonsd/Games/NE/Choice.java new file mode 100644 index 0000000..b20c1cd --- /dev/null +++ b/src/org/vashonsd/Games/NE/Choice.java @@ -0,0 +1,66 @@ +package org.vashonsd.Games.NE; + +import java.util.Random; + +public class Choice { + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public ChoiceType getWinCase() { + return winCase; + } + + public void setWinCase(ChoiceType winCase) { + this.winCase = winCase; + } + + public ChoiceType getTieCase() { + return tieCase; + } + + public void setTieCase(ChoiceType tieCase) { + this.tieCase = tieCase; + } + + public ChoiceType getLoseCase() { + return loseCase; + } + + public void setLoseCase(ChoiceType loseCase) { + this.loseCase = loseCase; + } + + public ChoiceType getMyType() { + return myType; + } + + public void setMyType(ChoiceType myType) { + this.myType = myType; + } + + private String name; + private ChoiceType myType; + private ChoiceType winCase; + private ChoiceType tieCase; + private ChoiceType loseCase; + + public Choice(String name) { + this.name = name; + } + + public String determineWinner(Choice c) { + if(this.getWinCase() == c.getMyType()) { + return "You Win!"; + } else if (this.getLoseCase() == c.getMyType()) { + return "You Lose!"; + } else { + return "You Tied!"; + } + } +} diff --git a/src/org/vashonsd/Games/NE/ChoiceType.java b/src/org/vashonsd/Games/NE/ChoiceType.java new file mode 100644 index 0000000..c380582 --- /dev/null +++ b/src/org/vashonsd/Games/NE/ChoiceType.java @@ -0,0 +1,5 @@ +package org.vashonsd.Games.NE; + +public enum ChoiceType { + ROCK, PAPER, SCISSORS +} diff --git a/src/org/vashonsd/Games/NE/CodeHSReader.java b/src/org/vashonsd/Games/NE/CodeHSReader.java new file mode 100644 index 0000000..433ba5e --- /dev/null +++ b/src/org/vashonsd/Games/NE/CodeHSReader.java @@ -0,0 +1,24 @@ +package org.vashonsd.Games.NE; + +import java.util.Scanner; + +public class CodeHSReader { + private Scanner input; + + public CodeHSReader() { + this.input = new Scanner(System.in); + } + + public int readInt(String prompt) { + System.out.print(prompt); + int n = this.input.nextInt(); + this.input.nextLine(); + return n; + } + + public String readLine(String prompt) { + System.out.print(prompt); + String str = this.input.nextLine(); + return str; + } +} diff --git a/src/org/vashonsd/Games/NE/NoahGame.java b/src/org/vashonsd/Games/NE/NoahGame.java index a6d52a4..7d3c734 100644 --- a/src/org/vashonsd/Games/NE/NoahGame.java +++ b/src/org/vashonsd/Games/NE/NoahGame.java @@ -1,13 +1,134 @@ package org.vashonsd.Games.NE; +import java.util.Random; +import org.vashonsd.Utils.Minigame; -import org.vashonsd.Utils.Placeholder; /** * Created by andy on 5/2/18. */ -public class NoahGame extends Placeholder { + + public class NoahGame extends Minigame { + int win = 0; + int loss = 0; + int tie = 0; + Choice userChoice; + Choice computerChoice; + public NoahGame() { - super("Noah"); + super("Noah", "A super boring game", "Quit"); + } + + @Override + public String start() { + //setUp(); + return ("Welcome to the game! It's Rock, Paper, Scissors\nIf you don't know how to play, type \"rules\" in the input field." + + "\nType Rock, Paper or Scissors to begin!"); } -} + + /** + * Call this method to get the game into its initial state. + */ + private void setUp() { + + } + + + @Override + public String handle(String str) { + + String score = "You have " + win + " wins \nYou have " + loss + " losses \nYou have " + tie + " ties"; + + if (str.equalsIgnoreCase("rules")) { + return "Paper covers Rock\nRock smashes Scissors\nAnd Scissors cuts Paper\nType 'score' to get the score of the game"; + } else if (str.equalsIgnoreCase("score")) { + return score + "\n---------------\nType to play again:\n"; + } + Random rand = new Random(); + int pick = rand.nextInt(2); + if (pick == 0) { + computerChoice = new Rock(); + } else if (pick == 1) { + computerChoice = new Paper(); + } else { + computerChoice = new Scissors(); + + } + + + //The game has just started, so we need to collect the user's choice. + if (str.equalsIgnoreCase("rock")) { + spin(); + userChoice = new Rock(); + } else if (str.equalsIgnoreCase("paper")) { + spin(); + userChoice = new Paper(); + } else if (str.equalsIgnoreCase("scissors")) { + spin(); + userChoice = new Scissors(); + }else{ + return "I don't recognize "+ str +"\n---------------\nType to play again:\n"; + } + + + //If the use types something illegal, reject it. + + //Otherwise, set userChoice to the correct Choice. + + //Then use a randomizer to set the computerChoice. + + + String winner = userChoice.determineWinner(computerChoice); + System.out.println("\nYou picked: " + userChoice.getName() + ", The computer picked: " + computerChoice.getName() + + "\n\n" + winner); + if (winner.contains("Win")) { + win++; + } + if (winner.contains("Lose")) { + loss++; + } + if (winner.contains("Tie")) { + tie++; + } + return "---------------\nType to play again:\n"; + } + + + + @Override + public String quit () { + return "I bet you're happy you're leaving"; + } + + private void spin() { + System.out.println("Rock..."); + try { + Thread.sleep(500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.println("Paper..."); + try { + Thread.sleep(500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.println("Scissors..."); + try { + Thread.sleep(500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.println("Shoot!"); + try { + Thread.sleep(500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + + + + diff --git a/src/org/vashonsd/Games/NE/Paper.java b/src/org/vashonsd/Games/NE/Paper.java new file mode 100644 index 0000000..8bfc919 --- /dev/null +++ b/src/org/vashonsd/Games/NE/Paper.java @@ -0,0 +1,11 @@ +package org.vashonsd.Games.NE; + +public class Paper extends Choice { + + public Paper() { + super("paper"); + setMyType(ChoiceType.PAPER); + setLoseCase(ChoiceType.SCISSORS); + setWinCase(ChoiceType.ROCK); + } +} diff --git a/src/org/vashonsd/Games/NE/Rock.java b/src/org/vashonsd/Games/NE/Rock.java new file mode 100644 index 0000000..9847159 --- /dev/null +++ b/src/org/vashonsd/Games/NE/Rock.java @@ -0,0 +1,11 @@ +package org.vashonsd.Games.NE; + +public class Rock extends Choice{ + + public Rock() { + super("rock"); + setMyType(ChoiceType.ROCK); + setWinCase(ChoiceType.SCISSORS); + setLoseCase(ChoiceType.PAPER); + } +} diff --git a/src/org/vashonsd/Games/NE/Scissors.java b/src/org/vashonsd/Games/NE/Scissors.java new file mode 100644 index 0000000..a24b50f --- /dev/null +++ b/src/org/vashonsd/Games/NE/Scissors.java @@ -0,0 +1,11 @@ +package org.vashonsd.Games.NE; + +public class Scissors extends Choice{ + + public Scissors() { + super("scissors"); + setMyType(ChoiceType.SCISSORS); + setWinCase(ChoiceType.PAPER); + setLoseCase(ChoiceType.ROCK); + } +} \ No newline at end of file From 49bfed9f16df4f3fc9a52e9653416b0de6cddf6e Mon Sep 17 00:00:00 2001 From: noahedmonds Date: Fri, 18 May 2018 08:46:43 -0700 Subject: [PATCH 06/30] It works, and I cleaned it up a bit --- src/org/vashonsd/Games/NE/CodeHSReader.java | 24 --------------------- 1 file changed, 24 deletions(-) delete mode 100644 src/org/vashonsd/Games/NE/CodeHSReader.java diff --git a/src/org/vashonsd/Games/NE/CodeHSReader.java b/src/org/vashonsd/Games/NE/CodeHSReader.java deleted file mode 100644 index 433ba5e..0000000 --- a/src/org/vashonsd/Games/NE/CodeHSReader.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.vashonsd.Games.NE; - -import java.util.Scanner; - -public class CodeHSReader { - private Scanner input; - - public CodeHSReader() { - this.input = new Scanner(System.in); - } - - public int readInt(String prompt) { - System.out.print(prompt); - int n = this.input.nextInt(); - this.input.nextLine(); - return n; - } - - public String readLine(String prompt) { - System.out.print(prompt); - String str = this.input.nextLine(); - return str; - } -} From 5d9aa9197046f66b3d23cdb7c2966dc09654c02c Mon Sep 17 00:00:00 2001 From: noahedmonds Date: Fri, 18 May 2018 08:50:05 -0700 Subject: [PATCH 07/30] small changes --- src/org/vashonsd/Games/NE/NoahGame.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/org/vashonsd/Games/NE/NoahGame.java b/src/org/vashonsd/Games/NE/NoahGame.java index 7d3c734..93f2f27 100644 --- a/src/org/vashonsd/Games/NE/NoahGame.java +++ b/src/org/vashonsd/Games/NE/NoahGame.java @@ -16,7 +16,7 @@ public class NoahGame extends Minigame { public NoahGame() { - super("Noah", "A super boring game", "Quit"); + super("Noah", "It's Rock Paper Scissors...", "Quit"); } @Override @@ -97,7 +97,7 @@ public String handle(String str) { @Override public String quit () { - return "I bet you're happy you're leaving"; + return "You probably lost to the computer lol "; } private void spin() { From 6702641962794658fb61778447582d906d099b4c Mon Sep 17 00:00:00 2001 From: Andy James Date: Sun, 20 May 2018 15:02:15 -0700 Subject: [PATCH 08/30] Restructured. Added API utils and more general dice roller. --- .idea/compiler.xml | 10 ++ ...jackson_core_jackson_annotations_2_9_0.xml | 13 ++ ...terxml_jackson_core_jackson_core_2_9_4.xml | 13 ++ ...ml_jackson_core_jackson_databind_2_9_4.xml | 13 ++ .../Maven__commons_lang_commons_lang_2_6.xml | 13 ++ .idea/libraries/Maven__junit_junit_4_12.xml | 13 ++ .../Maven__org_hamcrest_hamcrest_core_1_3.xml | 13 ++ .idea/misc.xml | 7 + Minigames.iml | 34 ++-- Test/org/vashonsd/Utils/UtilsTests.java | 20 --- pom.xml | 55 ++++++ src/main/java/org/vashonsd/Utils/API.java | 68 +++++++ src/main/resources/demo.txt | 3 + src/org/vashonsd/Games/AO/AngelicaGame.java | 13 -- src/org/vashonsd/Games/BR/BeckettGame.java | 13 -- src/org/vashonsd/Games/EO/EmmeGame.java | 13 -- src/org/vashonsd/Games/HA/HuthaifaGame.java | 13 -- src/org/vashonsd/Games/MinigameFactory.java | 64 ------- src/org/vashonsd/Games/NA/NabilGame.java | 13 -- src/org/vashonsd/Games/NE/NoahGame.java | 13 -- src/org/vashonsd/Games/RI/RobertGame.java | 13 -- src/org/vashonsd/Games/SP/SamGame.java | 13 -- src/org/vashonsd/Games/SR/SeanGame.java | 12 -- src/org/vashonsd/Games/TwentyQuestions.java | 52 ------ src/org/vashonsd/Main.java | 40 ----- src/org/vashonsd/Utils/Cards/Card.java | 166 ------------------ src/org/vashonsd/Utils/Cards/Deck.java | 90 ---------- src/org/vashonsd/Utils/DiceGame.java | 45 ----- src/org/vashonsd/Utils/Minigame.java | 62 ------- src/org/vashonsd/Utils/Placeholder.java | 33 ---- src/org/vashonsd/Utils/Utils.java | 29 --- src/test/java/org/vashonsd/Utils/APITest.java | 41 +++++ .../java/org/vashonsd/Utils/UtilsTest.java | 93 ++++++++++ src/test/testresources/demo.txt | 3 + 34 files changed, 377 insertions(+), 732 deletions(-) create mode 100644 .idea/libraries/Maven__com_fasterxml_jackson_core_jackson_annotations_2_9_0.xml create mode 100644 .idea/libraries/Maven__com_fasterxml_jackson_core_jackson_core_2_9_4.xml create mode 100644 .idea/libraries/Maven__com_fasterxml_jackson_core_jackson_databind_2_9_4.xml create mode 100644 .idea/libraries/Maven__commons_lang_commons_lang_2_6.xml create mode 100644 .idea/libraries/Maven__junit_junit_4_12.xml create mode 100644 .idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml delete mode 100644 Test/org/vashonsd/Utils/UtilsTests.java create mode 100644 pom.xml create mode 100644 src/main/java/org/vashonsd/Utils/API.java create mode 100644 src/main/resources/demo.txt delete mode 100644 src/org/vashonsd/Games/AO/AngelicaGame.java delete mode 100644 src/org/vashonsd/Games/BR/BeckettGame.java delete mode 100644 src/org/vashonsd/Games/EO/EmmeGame.java delete mode 100644 src/org/vashonsd/Games/HA/HuthaifaGame.java delete mode 100644 src/org/vashonsd/Games/MinigameFactory.java delete mode 100644 src/org/vashonsd/Games/NA/NabilGame.java delete mode 100644 src/org/vashonsd/Games/NE/NoahGame.java delete mode 100644 src/org/vashonsd/Games/RI/RobertGame.java delete mode 100644 src/org/vashonsd/Games/SP/SamGame.java delete mode 100644 src/org/vashonsd/Games/SR/SeanGame.java delete mode 100644 src/org/vashonsd/Games/TwentyQuestions.java delete mode 100644 src/org/vashonsd/Main.java delete mode 100644 src/org/vashonsd/Utils/Cards/Card.java delete mode 100644 src/org/vashonsd/Utils/Cards/Deck.java delete mode 100644 src/org/vashonsd/Utils/DiceGame.java delete mode 100644 src/org/vashonsd/Utils/Minigame.java delete mode 100644 src/org/vashonsd/Utils/Placeholder.java delete mode 100644 src/org/vashonsd/Utils/Utils.java create mode 100644 src/test/java/org/vashonsd/Utils/APITest.java create mode 100644 src/test/java/org/vashonsd/Utils/UtilsTest.java create mode 100644 src/test/testresources/demo.txt diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 96cc43e..841091f 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -17,6 +17,16 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_annotations_2_9_0.xml b/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_annotations_2_9_0.xml new file mode 100644 index 0000000..06441f4 --- /dev/null +++ b/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_annotations_2_9_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_core_2_9_4.xml b/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_core_2_9_4.xml new file mode 100644 index 0000000..5de0355 --- /dev/null +++ b/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_core_2_9_4.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_databind_2_9_4.xml b/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_databind_2_9_4.xml new file mode 100644 index 0000000..398994e --- /dev/null +++ b/.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_databind_2_9_4.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__commons_lang_commons_lang_2_6.xml b/.idea/libraries/Maven__commons_lang_commons_lang_2_6.xml new file mode 100644 index 0000000..2ec8376 --- /dev/null +++ b/.idea/libraries/Maven__commons_lang_commons_lang_2_6.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__junit_junit_4_12.xml b/.idea/libraries/Maven__junit_junit_4_12.xml new file mode 100644 index 0000000..d411041 --- /dev/null +++ b/.idea/libraries/Maven__junit_junit_4_12.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml new file mode 100644 index 0000000..f58bbc1 --- /dev/null +++ b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index e29b6f0..456b7b9 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,6 +3,13 @@ + + + diff --git a/Minigames.iml b/Minigames.iml index 6372453..e616cd8 100644 --- a/Minigames.iml +++ b/Minigames.iml @@ -1,23 +1,27 @@ - - - + + + + - + + + + + - - - - - - - - - - - + + + + + + + + + + \ No newline at end of file diff --git a/Test/org/vashonsd/Utils/UtilsTests.java b/Test/org/vashonsd/Utils/UtilsTests.java deleted file mode 100644 index 4e52362..0000000 --- a/Test/org/vashonsd/Utils/UtilsTests.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.vashonsd.Utils; - -import org.junit.Assert; -import org.junit.Test; - -/** - * Created by andy on 5/2/18. - */ -public class UtilsTests { - @Test - public void testIsInteger(){ - Assert.assertTrue(Utils.IsInteger("3")); - Assert.assertFalse(Utils.IsInteger("th4n")); - } - - @Test - public void testRollDie() { - Assert.assertTrue(true); - } -} diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..a9ab991 --- /dev/null +++ b/pom.xml @@ -0,0 +1,55 @@ + + + 4.0.0 + + + groupId + Minigames + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + + junit + junit + 4.12 + test + + + + com.fasterxml.jackson.core + jackson-databind + 2.9.4 + + + + + org.apache.httpcomponents + httpclient + 4.5.5 + + + + + commons-lang + commons-lang + 2.6 + + + + \ No newline at end of file diff --git a/src/main/java/org/vashonsd/Utils/API.java b/src/main/java/org/vashonsd/Utils/API.java new file mode 100644 index 0000000..252193b --- /dev/null +++ b/src/main/java/org/vashonsd/Utils/API.java @@ -0,0 +1,68 @@ +package org.vashonsd.Utils; + +import com.fasterxml.jackson.core.io.JsonStringEncoder; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.DeserializationConfig; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.lang.StringEscapeUtils; +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; + +import java.io.IOException; +import java.io.StringWriter; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by andy on 5/20/18. + */ +public class API { + + private static CloseableHttpClient httpclient; + + private static ObjectMapper mapper; + + /** + * This is a simple utility for taking a String in JSON format and returning it as a Map of String : Object. + * + * @param json The JSON object, represented as a url-escape String, to be parsed. + * @return a Map + */ + public static Map parseJSONToMap(String json) throws IOException { + mapper = new ObjectMapper(); + mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT); + mapper.enable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT); + mapper.enable(DeserializationFeature.WRAP_EXCEPTIONS); + TypeReference> typeRef + = new TypeReference>() {}; + return mapper.readValue(json, typeRef); + } + + public static String callAPI(String uri) throws IOException { + httpclient = HttpClients.createDefault(); + HttpGet httpGet = new HttpGet(uri); + CloseableHttpResponse response1 = httpclient.execute(httpGet); + HttpEntity entity1 = response1.getEntity(); + String result; + if(!(response1.getStatusLine().getStatusCode() == 200)) { + throw new IOException(response1.getStatusLine().getReasonPhrase()); + } else { + result = EntityUtils.toString(entity1); + } + return result; + } + + public static Map mapFromAPICall(String uri) throws IOException { + String resp = callAPI(uri); + ObjectMapper mapper = new ObjectMapper(); + StringWriter writer = new StringWriter(); + mapper.writeValue(writer, resp); + String jsonified = writer.toString(); + return parseJSONToMap(jsonified); + } +} diff --git a/src/main/resources/demo.txt b/src/main/resources/demo.txt new file mode 100644 index 0000000..06ce0b1 --- /dev/null +++ b/src/main/resources/demo.txt @@ -0,0 +1,3 @@ +apples +mangoes +pens diff --git a/src/org/vashonsd/Games/AO/AngelicaGame.java b/src/org/vashonsd/Games/AO/AngelicaGame.java deleted file mode 100644 index a9084b4..0000000 --- a/src/org/vashonsd/Games/AO/AngelicaGame.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.vashonsd.Games.AO; - -import org.vashonsd.Utils.Placeholder; - -/** - * Created by andy on 5/2/18. - */ -public class AngelicaGame extends Placeholder { - - public AngelicaGame() { - super("Angelica"); - } -} diff --git a/src/org/vashonsd/Games/BR/BeckettGame.java b/src/org/vashonsd/Games/BR/BeckettGame.java deleted file mode 100644 index c1ac84b..0000000 --- a/src/org/vashonsd/Games/BR/BeckettGame.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.vashonsd.Games.BR; - -import org.vashonsd.Utils.Placeholder; - -/** - * Created by andy on 5/2/18. - */ -public class BeckettGame extends Placeholder { - - public BeckettGame() { - super("Beckett"); - } -} diff --git a/src/org/vashonsd/Games/EO/EmmeGame.java b/src/org/vashonsd/Games/EO/EmmeGame.java deleted file mode 100644 index f14bb1c..0000000 --- a/src/org/vashonsd/Games/EO/EmmeGame.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.vashonsd.Games.EO; - -import org.vashonsd.Utils.Placeholder; - -/** - * Created by andy on 5/2/18. - */ -public class EmmeGame extends Placeholder { - - public EmmeGame() { - super("Emme"); - } -} diff --git a/src/org/vashonsd/Games/HA/HuthaifaGame.java b/src/org/vashonsd/Games/HA/HuthaifaGame.java deleted file mode 100644 index 5493131..0000000 --- a/src/org/vashonsd/Games/HA/HuthaifaGame.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.vashonsd.Games.HA; - -import org.vashonsd.Utils.Placeholder; - -/** - * Created by andy on 5/2/18. - */ -public class HuthaifaGame extends Placeholder { - - public HuthaifaGame() { - super("Huthaifa"); - } -} diff --git a/src/org/vashonsd/Games/MinigameFactory.java b/src/org/vashonsd/Games/MinigameFactory.java deleted file mode 100644 index e3dee18..0000000 --- a/src/org/vashonsd/Games/MinigameFactory.java +++ /dev/null @@ -1,64 +0,0 @@ -package org.vashonsd.Games; - -import org.vashonsd.Games.AO.AngelicaGame; -import org.vashonsd.Games.BR.BeckettGame; -import org.vashonsd.Games.EO.EmmeGame; -import org.vashonsd.Games.HA.HuthaifaGame; -import org.vashonsd.Games.NA.NabilGame; -import org.vashonsd.Games.NE.NoahGame; -import org.vashonsd.Games.RI.RobertGame; -import org.vashonsd.Games.SP.SamGame; -import org.vashonsd.Games.SR.SeanGame; -import org.vashonsd.Utils.DiceGame; -import org.vashonsd.Utils.Minigame; - -import java.util.HashMap; -import java.util.Map; - -/** - * Created by andy on 5/2/18. - */ -public class MinigameFactory { - - private static Map games; - - - static { - games = new HashMap(); - addGame(new TwentyQuestions()); - addGame(new AngelicaGame()); - addGame(new BeckettGame()); - addGame(new EmmeGame()); - addGame(new HuthaifaGame()); - addGame(new NoahGame()); - addGame(new RobertGame()); - addGame(new SamGame()); - addGame(new SeanGame()); - addGame(new NabilGame()); - addGame(new DiceGame()); - } - - public static void addGame(Minigame m) { - games.put(m.getName(), m); - } - - public static String listGames() { - String result = ""; - String spacer = ""; - for(Minigame m : games.values()) { - result += spacer + m.getName() + " - " + m.getDescription(); - spacer = "\n"; - } - return result; - } - - public static Minigame getGame(String s) { - return games.get(s); - } - - public static boolean hasGame(String s) { - return games.containsKey(s); - } - - -} diff --git a/src/org/vashonsd/Games/NA/NabilGame.java b/src/org/vashonsd/Games/NA/NabilGame.java deleted file mode 100644 index 6d80ecd..0000000 --- a/src/org/vashonsd/Games/NA/NabilGame.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.vashonsd.Games.NA; - -import org.vashonsd.Utils.Placeholder; - -/** - * Created by andy on 5/2/18. - */ -public class NabilGame extends Placeholder { - - public NabilGame() { - super("Nabil"); - } -} diff --git a/src/org/vashonsd/Games/NE/NoahGame.java b/src/org/vashonsd/Games/NE/NoahGame.java deleted file mode 100644 index a6d52a4..0000000 --- a/src/org/vashonsd/Games/NE/NoahGame.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.vashonsd.Games.NE; - -import org.vashonsd.Utils.Placeholder; - -/** - * Created by andy on 5/2/18. - */ -public class NoahGame extends Placeholder { - - public NoahGame() { - super("Noah"); - } -} diff --git a/src/org/vashonsd/Games/RI/RobertGame.java b/src/org/vashonsd/Games/RI/RobertGame.java deleted file mode 100644 index aa68a21..0000000 --- a/src/org/vashonsd/Games/RI/RobertGame.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.vashonsd.Games.RI; - -import org.vashonsd.Utils.Placeholder; - -/** - * Created by andy on 5/2/18. - */ -public class RobertGame extends Placeholder { - - public RobertGame() { - super("Robert"); - } -} diff --git a/src/org/vashonsd/Games/SP/SamGame.java b/src/org/vashonsd/Games/SP/SamGame.java deleted file mode 100644 index b406123..0000000 --- a/src/org/vashonsd/Games/SP/SamGame.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.vashonsd.Games.SP; - -import org.vashonsd.Utils.Placeholder; - -/** - * Created by andy on 5/2/18. - */ -public class SamGame extends Placeholder { - - public SamGame() { - super("Sam"); - } -} diff --git a/src/org/vashonsd/Games/SR/SeanGame.java b/src/org/vashonsd/Games/SR/SeanGame.java deleted file mode 100644 index 2c507fe..0000000 --- a/src/org/vashonsd/Games/SR/SeanGame.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.vashonsd.Games.SR; - -import org.vashonsd.Utils.Placeholder; - -/** - * Created by andy on 5/2/18. - */ -public class SeanGame extends Placeholder { - public SeanGame() { - super("Sean"); - } -} diff --git a/src/org/vashonsd/Games/TwentyQuestions.java b/src/org/vashonsd/Games/TwentyQuestions.java deleted file mode 100644 index a291ba0..0000000 --- a/src/org/vashonsd/Games/TwentyQuestions.java +++ /dev/null @@ -1,52 +0,0 @@ -package org.vashonsd.Games; - -import org.vashonsd.Utils.Minigame; -import org.vashonsd.Utils.Utils; - -import java.util.Random; - -/** - * Created by andy on 5/2/18. - */ -public class TwentyQuestions extends Minigame { - - private int targetNumber; - - public TwentyQuestions() { - super("20Q", "Guess a number between 0 and 1,000", "quit"); - } - - @Override - public String start() { - setUp(); - return("Okay, guess a number between 1 and 500."); - } - - private void setUp() { - Random rand = new Random(); - targetNumber = rand.nextInt(499) + 1; - } - - @Override - public String handle(String str) { - if(!Utils.IsInteger(str)) { - return "You must guess a number."; - } - int guess = Integer.parseInt(str); - if(!(guess >= 1 && guess <=500)) { - return "Your guess must be between 1 and 500."; - } else if(guess < targetNumber) { - return "too small"; - } else if(guess > targetNumber) { - return "too big"; - } else { - setUp(); - return "Congratulations! You guessed it!\nGuess another, or type " + quitWord + " to quit."; - } - } - - @Override - public String quit() { - return "Goodbye! Thanks for playing!"; - } -} diff --git a/src/org/vashonsd/Main.java b/src/org/vashonsd/Main.java deleted file mode 100644 index 84453f0..0000000 --- a/src/org/vashonsd/Main.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.vashonsd; - -import org.vashonsd.Utils.Minigame; -import org.vashonsd.Games.MinigameFactory; - -import java.util.Scanner; - -public class Main { - - private static final String quitWord = "quit"; - private static Minigame currentGame; - - public static void main(String[] args) { - Scanner in = new Scanner(System.in); - String userIn = ""; - while(userIn != quitWord) { - if (currentGame == null) { - System.out.println(MinigameFactory.listGames()); - System.out.println("Type the name of the game you would like to play: "); - userIn = in.nextLine(); - if(userIn.equalsIgnoreCase(quitWord)) break; - if(!MinigameFactory.hasGame(userIn)) { - System.out.println("Try again."); - } else { - currentGame = MinigameFactory.getGame(userIn); - System.out.println(currentGame.start()); - userIn = in.nextLine(); - } - } else if(userIn.equalsIgnoreCase(currentGame.getQuitWord())) { - System.out.println(currentGame.quit()); - currentGame = null; - } else { - System.out.println(currentGame.handle(userIn)); - userIn = in.nextLine(); - } - } - System.out.println("Goodbye!"); - in.close(); - } -} diff --git a/src/org/vashonsd/Utils/Cards/Card.java b/src/org/vashonsd/Utils/Cards/Card.java deleted file mode 100644 index 090f221..0000000 --- a/src/org/vashonsd/Utils/Cards/Card.java +++ /dev/null @@ -1,166 +0,0 @@ -package org.vashonsd.Utils.Cards; - -public class Card -{ - // These constants represent the possible suits and - // can be used to index into the suits array to get - // their string representation. - private static final int HEARTS = 0; - private static final int DIAMONDS = 1; - private static final int SPADES = 2; - private static final int CLUBS = 3; - - // These constants represent the ranks of the non-number - // cards, or cards above 10. To maintain the ordering after - // 2-10, the integer values are 11, 12, 13, and 14 and - // also allow us to index into the ranks array to get their - // String representation. - private static final int JACK = 11; - private static final int QUEEN = 12; - private static final int KING = 13; - private static final int ACE = 14; - - // Instance variables - - // This represents the rank of the card, the value from 2 to Ace. - private int rank; - - // This represents the suit of the card, one of hearts, diamonds, spades or clubs. - private int suit; - - // This represents the value of the card, which is 10 for face cards or 11 for an ace. - private int value; - - // This String array allows us to easily get the String value of a Card from its rank. - // There are two Xs in the front to provide padding so numbers have their String representation - // at the corresponding index. For example, the String for 2 is at index 2. - private String[] ranks = {"X", "X", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; - - // The String array allow us to easily get the String value of the Card for its suit. This - // is the same order as the suits above so we can index into this array. - private String[] suits = {"H", "D", "S", "C"}; - - /** - * This is the constructor to create a new Card. To create a new card - * we pass in its rank and its suit. - * - * @param r The rank of the card, as an int. - * @param s The suit of the card, as an int. - */ - public Card(int r, int s) - { - rank = r; - suit = s; - } - - // Getter Methods - - /** - * This returns the rank of the card as an integer. - * - * @return rank of card as an int. - */ - public int getRank() - { - return rank; - } - - /** - * This returns the suit of the card as an integer. - * - * @return suit of card as an int. - */ - public int getSuit() - { - return suit; - } - - /** - * This returns the value of the card as an integer. - * - * For facecards the value is 10, which is different - * than their rank underlying value. For aces the default - * value is 11. - * - * @return The value of the card as an int. - */ - public int getValue() - { - int value = rank; - if(rank > 10) - { - value = 10; - } - - if(rank == ACE) - { - value = 11; - } - - return value; - } - - /** - * This utility method converts from a rank integer to a String. - * - * @param r The rank. - * @return String version of rank. - */ - public String rankToString(int r) - { - return ranks[r]; - } - - /** - * This utility method converts from a suit integer to a String. - * - * @param s The suit. - * @return String version of suit. - */ - public String suitToString(int s) - { - return suits[s]; - } - - /** - * Return the String version of the suit. - * - * @return String version of suit. - */ - public String getSuitAsString() - { - return suitToString(suit); - } - - /** - * Return the String version of the rank. - * - * @return String version of the rank. - */ - public String getRankAsString() - { - return rankToString(rank); - } - - /** - * This returns the String representation of a card which - * will be two characters. For example, the two of hearts would - * return 2H. Face cards have a short string so the ace of - * spades would return AS. - * - * @return String representation of Card. - */ - public String toString() - { - // Get a string for rank - String rankString = ranks[rank]; - - // Get a string for the suit - String suitString = suits[suit]; - - // combine those - return rankString + suitString; - } - - -} \ No newline at end of file diff --git a/src/org/vashonsd/Utils/Cards/Deck.java b/src/org/vashonsd/Utils/Cards/Deck.java deleted file mode 100644 index 7fd7cfe..0000000 --- a/src/org/vashonsd/Utils/Cards/Deck.java +++ /dev/null @@ -1,90 +0,0 @@ -package org.vashonsd.Utils.Cards; - -import java.util.*; - -public class Deck -{ - private static final int HEARTS = 0; - private static final int DIAMONDS = 1; - private static final int SPADES = 2; - private static final int CLUBS = 3; - - private static final int JACK = 11; - private static final int QUEEN = 12; - private static final int KING = 13; - private static final int ACE = 14; - - // Instance variables - - // This stores the deck which is a list of the Card objects. - private ArrayList deck; - - /** - * This creates a Deck. A Deck starts as a list of 52 cards. - * We loop through each suit and rank and construct a card - * and add it to the deck. - */ - public Deck() - { - deck = new ArrayList(); - - for(int rank = 2; rank <= ACE; rank++) - { - for(int suit = HEARTS; suit <= CLUBS; suit++) - { - Card card = new Card(rank, suit); - deck.add(card); - } - } - } - - // Getter method - - /** - * This getter method returns the ArrayList of cards. - * @return ArrayList of the Cards. - */ - public ArrayList getCards() - { - return deck; - } - - /** - * This deals the first Card from the deck by removing it. - * @return The first Card in the deck. - */ - public Card deal() - { - return deck.remove(0); - } - - /** - * This prints out the current state of the deck. - */ - public void print() - { - for(Card card: deck) - { - System.out.println(card); - } - } - - /** - * This shuffles the deck by making 52 swaps of - * card positions. - */ - public void shuffle() - { - for(int i = 0; i < deck.size(); i++) - { - Random rand = new Random(); - int randomIndex = rand.nextInt(52); - Card x = deck.get(i); - Card y = deck.get(randomIndex); - - deck.set(i, y); - deck.set(randomIndex, x); - } - } - -} diff --git a/src/org/vashonsd/Utils/DiceGame.java b/src/org/vashonsd/Utils/DiceGame.java deleted file mode 100644 index 9cb68ea..0000000 --- a/src/org/vashonsd/Utils/DiceGame.java +++ /dev/null @@ -1,45 +0,0 @@ -package org.vashonsd.Utils; - -import java.util.ArrayList; -import java.util.List; - -/** - * Created by andy on 5/3/18. - */ -public class DiceGame extends Minigame { - List playerDice; - List computerDice; - int round = 1; - - public DiceGame() { - super("Dice Wars", "A battle of wits and luck", "quit"); - playerDice = new ArrayList(); - computerDice = new ArrayList(); - } - - @Override - public String start() { - setUp(); - return "Welcome to Dice Wars!"; - } - - private void setUp() { - for(int i=0; i<3; i++) { - playerDice.add(Utils.rollDie()); - computerDice.add(Utils.rollDie()); - } - } - - @Override - public String handle(String str) { - if(round == 1) { - return "Your dice are " + playerDice.toString(); - } - return null; - } - - @Override - public String quit() { - return "Thanks for playing!"; - } -} diff --git a/src/org/vashonsd/Utils/Minigame.java b/src/org/vashonsd/Utils/Minigame.java deleted file mode 100644 index 0c6ddbe..0000000 --- a/src/org/vashonsd/Utils/Minigame.java +++ /dev/null @@ -1,62 +0,0 @@ -package org.vashonsd.Utils; - -/** - * A Minigame can be run as a text-based game that interacts with the user. - * - * The mechanics of interacting with the user are left out of this code. - * It uses public methods to start, to finish, and, in between, respond - * to Strings with Strings. - */ -public abstract class Minigame { - protected String name; - protected String description; - protected String quitWord; - - public Minigame(String name, String description, String quitWord) { - this.name = name; - this.description = description; - this.quitWord = quitWord; - } - - public String getName() { - return this.name; - } - - public String getDescription() { - return this.description; - } - - public String getQuitWord() { - return this.quitWord; - } - - /** - * This method will be called when the game is started. - * - * This method can be used just to return a welcome message, - * or it could also be used to set up the starting state of a game. - * If the game will ever restarting, consider creating a setUp() - * method that can be reused. - * - * @return A String representing a greeting. - */ - public abstract String start(); - - /** - * This method will be called while the game is in play. - * - * handle() is the sole public method that should be called during - * game interactions. - * - * @param str The String representing input from the user. - * @return The response to the user. - */ - public abstract String handle(String str); - - /** - * This method will be called with the user signals a quit. - * - * @return A String representing an exit message - */ - public abstract String quit(); -} diff --git a/src/org/vashonsd/Utils/Placeholder.java b/src/org/vashonsd/Utils/Placeholder.java deleted file mode 100644 index e90c508..0000000 --- a/src/org/vashonsd/Utils/Placeholder.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.vashonsd.Utils; - -/** - * Created by andy on 5/2/18. - */ -public class Placeholder extends Minigame { - - - public Placeholder(String name) { - super(name, "A simple game in which you guess " + name + "'s name", "quit"); - } - - @Override - public String start() { - return "Hi! My name is " + name + "!" + - "\nAre you ready for a quiz?" + - "\nWhat's my name?"; - } - - @Override - public String handle(String str) { - if(str.equalsIgnoreCase(name)) { - return "Fantastic! Guess again, or type " + quitWord + " to quit!"; - } else { - return "Nope! Guess again!"; - } - } - - @Override - public String quit() { - return "Thanks for playing with me, " + name + "!"; - } -} diff --git a/src/org/vashonsd/Utils/Utils.java b/src/org/vashonsd/Utils/Utils.java deleted file mode 100644 index 11589b0..0000000 --- a/src/org/vashonsd/Utils/Utils.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.vashonsd.Utils; - - -import java.util.Random; - -public class Utils { - - /** - * Returns true if the given String is an integer, false otherwise. - * - * Note that this method still returns true if the String is negative or zero. - */ - public static boolean IsInteger(String s) { - if(s.isEmpty()) return false; - for(int i = 0; i < s.length(); i++) { - if(i == 0 && s.charAt(i) == '-') { - if(s.length() == 1) return false; - else continue; - } - if( !Character.isDigit(s.charAt(i)) ) return false; - } - return true; - } - - public static int rollDie() { - Random rand = new Random(); - return rand.nextInt(5)+1; - } -} diff --git a/src/test/java/org/vashonsd/Utils/APITest.java b/src/test/java/org/vashonsd/Utils/APITest.java new file mode 100644 index 0000000..a1b2520 --- /dev/null +++ b/src/test/java/org/vashonsd/Utils/APITest.java @@ -0,0 +1,41 @@ +package org.vashonsd.Utils; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by andy on 5/20/18. + */ +public class APITest { + + private String sampleJSON; + private String uri; + + @Before + public void setUp() throws Exception { + sampleJSON = "{\"key\": \"value\", \"hope\": \"Thing with feathers\"}"; + uri = "https://world.openfoodfacts.org/api/v0/product/737628064502.json"; + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void testParseJSONToMap() throws Exception { + System.out.println(API.parseJSONToMap(sampleJSON)); + } + + @Test + public void testCallAPI() throws Exception { + System.out.println(API.callAPI(uri)); + } + + +// @Test + public void testMapFromAPICall() throws Exception { + System.out.println(API.mapFromAPICall(uri)); + } +} \ No newline at end of file diff --git a/src/test/java/org/vashonsd/Utils/UtilsTest.java b/src/test/java/org/vashonsd/Utils/UtilsTest.java new file mode 100644 index 0000000..332d99e --- /dev/null +++ b/src/test/java/org/vashonsd/Utils/UtilsTest.java @@ -0,0 +1,93 @@ +package org.vashonsd.Utils; + +import junit.framework.TestCase; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.*; + +/** + * Created by andy on 5/20/18. + */ +public class UtilsTest { + Map rollFrequencies; + int dieMax; + + @Before + public void setUp() throws Exception { + rollFrequencies = new HashMap(); + dieMax = 12; + for(int i=0; i<1000; i++) { + int roll = Utils.rollDie(dieMax); + int val = rollFrequencies.containsKey(roll) ? rollFrequencies.get(roll) : 0; + rollFrequencies.put(roll, val+1); + } + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void testAppendToFile() throws Exception { + + } + + @Test + public void testIsInteger() throws Exception { + Assert.assertTrue(Utils.IsInteger("3")); + Assert.assertFalse(Utils.IsInteger("th4")); + } + + @Test + public void testRollDie() throws Exception { + System.out.println(rollFrequencies); + } + + @Test + public void testRollDieProducesOnlyLegalNumbers() throws Exception { + + //First we will check if any values outside the Set were rolled + Set legalValues = new HashSet(); + for(int i=1; i Date: Sun, 20 May 2018 15:41:34 -0700 Subject: [PATCH 09/30] The latest and cleanest version. --- .idea/workspace.xml | 897 ++++++++++++++++++ .../org/vashonsd/Games/AO/AngelicaGame.java | 13 + .../org/vashonsd/Games/BR/BeckettGame.java | 13 + .../java/org/vashonsd/Games/EO/EmmeGame.java | 13 + .../org/vashonsd/Games/HA/HuthaifaGame.java | 13 + .../org/vashonsd/Games/MinigameFactory.java | 66 ++ .../java/org/vashonsd/Games/NA/NabilGame.java | 13 + .../java/org/vashonsd/Games/NE/NoahGame.java | 13 + .../org/vashonsd/Games/RI/RobertGame.java | 13 + .../org/vashonsd/Games/RPS_games/Choice.java | 47 + .../vashonsd/Games/RPS_games/ChoiceType.java | 8 + .../org/vashonsd/Games/RPS_games/Paper.java | 11 + .../org/vashonsd/Games/RPS_games/Rock.java | 11 + .../Games/RPS_games/RockPaperPlus.java | 63 ++ .../vashonsd/Games/RPS_games/Scissors.java | 11 + .../java/org/vashonsd/Games/SP/SamGame.java | 13 + .../java/org/vashonsd/Games/SR/SeanGame.java | 12 + .../org/vashonsd/Games/TwentyQuestions.java | 52 + src/main/java/org/vashonsd/Main.java | 40 + .../java/org/vashonsd/Utils/Cards/Card.java | 166 ++++ .../java/org/vashonsd/Utils/Cards/Deck.java | 90 ++ .../java/org/vashonsd/Utils/DiceGame.java | 66 ++ .../java/org/vashonsd/Utils/Minigame.java | 62 ++ .../java/org/vashonsd/Utils/Placeholder.java | 33 + .../java/org/vashonsd/Utils/Randomizer.java | 85 ++ src/main/java/org/vashonsd/Utils/Utils.java | 70 ++ 26 files changed, 1894 insertions(+) create mode 100644 .idea/workspace.xml create mode 100644 src/main/java/org/vashonsd/Games/AO/AngelicaGame.java create mode 100644 src/main/java/org/vashonsd/Games/BR/BeckettGame.java create mode 100644 src/main/java/org/vashonsd/Games/EO/EmmeGame.java create mode 100644 src/main/java/org/vashonsd/Games/HA/HuthaifaGame.java create mode 100644 src/main/java/org/vashonsd/Games/MinigameFactory.java create mode 100644 src/main/java/org/vashonsd/Games/NA/NabilGame.java create mode 100644 src/main/java/org/vashonsd/Games/NE/NoahGame.java create mode 100644 src/main/java/org/vashonsd/Games/RI/RobertGame.java create mode 100644 src/main/java/org/vashonsd/Games/RPS_games/Choice.java create mode 100644 src/main/java/org/vashonsd/Games/RPS_games/ChoiceType.java create mode 100644 src/main/java/org/vashonsd/Games/RPS_games/Paper.java create mode 100644 src/main/java/org/vashonsd/Games/RPS_games/Rock.java create mode 100644 src/main/java/org/vashonsd/Games/RPS_games/RockPaperPlus.java create mode 100644 src/main/java/org/vashonsd/Games/RPS_games/Scissors.java create mode 100644 src/main/java/org/vashonsd/Games/SP/SamGame.java create mode 100644 src/main/java/org/vashonsd/Games/SR/SeanGame.java create mode 100644 src/main/java/org/vashonsd/Games/TwentyQuestions.java create mode 100644 src/main/java/org/vashonsd/Main.java create mode 100644 src/main/java/org/vashonsd/Utils/Cards/Card.java create mode 100644 src/main/java/org/vashonsd/Utils/Cards/Deck.java create mode 100644 src/main/java/org/vashonsd/Utils/DiceGame.java create mode 100644 src/main/java/org/vashonsd/Utils/Minigame.java create mode 100644 src/main/java/org/vashonsd/Utils/Placeholder.java create mode 100644 src/main/java/org/vashonsd/Utils/Randomizer.java create mode 100644 src/main/java/org/vashonsd/Utils/Utils.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..f286b55 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,897 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1525302511536 + + + 1525358157836 + + + 1525446474929 + + + 1525446784647 + + + 1526853736136 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No facets are configured + + + + + + + + + + + + + + + 1.8 + + + + + + + + Minigames + + + + + + + + Maven: junit:junit:4.12 + + + + + + + + \ No newline at end of file diff --git a/src/main/java/org/vashonsd/Games/AO/AngelicaGame.java b/src/main/java/org/vashonsd/Games/AO/AngelicaGame.java new file mode 100644 index 0000000..a9084b4 --- /dev/null +++ b/src/main/java/org/vashonsd/Games/AO/AngelicaGame.java @@ -0,0 +1,13 @@ +package org.vashonsd.Games.AO; + +import org.vashonsd.Utils.Placeholder; + +/** + * Created by andy on 5/2/18. + */ +public class AngelicaGame extends Placeholder { + + public AngelicaGame() { + super("Angelica"); + } +} diff --git a/src/main/java/org/vashonsd/Games/BR/BeckettGame.java b/src/main/java/org/vashonsd/Games/BR/BeckettGame.java new file mode 100644 index 0000000..c1ac84b --- /dev/null +++ b/src/main/java/org/vashonsd/Games/BR/BeckettGame.java @@ -0,0 +1,13 @@ +package org.vashonsd.Games.BR; + +import org.vashonsd.Utils.Placeholder; + +/** + * Created by andy on 5/2/18. + */ +public class BeckettGame extends Placeholder { + + public BeckettGame() { + super("Beckett"); + } +} diff --git a/src/main/java/org/vashonsd/Games/EO/EmmeGame.java b/src/main/java/org/vashonsd/Games/EO/EmmeGame.java new file mode 100644 index 0000000..f14bb1c --- /dev/null +++ b/src/main/java/org/vashonsd/Games/EO/EmmeGame.java @@ -0,0 +1,13 @@ +package org.vashonsd.Games.EO; + +import org.vashonsd.Utils.Placeholder; + +/** + * Created by andy on 5/2/18. + */ +public class EmmeGame extends Placeholder { + + public EmmeGame() { + super("Emme"); + } +} diff --git a/src/main/java/org/vashonsd/Games/HA/HuthaifaGame.java b/src/main/java/org/vashonsd/Games/HA/HuthaifaGame.java new file mode 100644 index 0000000..5493131 --- /dev/null +++ b/src/main/java/org/vashonsd/Games/HA/HuthaifaGame.java @@ -0,0 +1,13 @@ +package org.vashonsd.Games.HA; + +import org.vashonsd.Utils.Placeholder; + +/** + * Created by andy on 5/2/18. + */ +public class HuthaifaGame extends Placeholder { + + public HuthaifaGame() { + super("Huthaifa"); + } +} diff --git a/src/main/java/org/vashonsd/Games/MinigameFactory.java b/src/main/java/org/vashonsd/Games/MinigameFactory.java new file mode 100644 index 0000000..618c164 --- /dev/null +++ b/src/main/java/org/vashonsd/Games/MinigameFactory.java @@ -0,0 +1,66 @@ +package org.vashonsd.Games; + +import org.vashonsd.Games.AO.AngelicaGame; +import org.vashonsd.Games.BR.BeckettGame; +import org.vashonsd.Games.EO.EmmeGame; +import org.vashonsd.Games.HA.HuthaifaGame; +import org.vashonsd.Games.NA.NabilGame; +import org.vashonsd.Games.NE.NoahGame; +import org.vashonsd.Games.RI.RobertGame; +import org.vashonsd.Games.RPS_games.RockPaperPlus; +import org.vashonsd.Games.SP.SamGame; +import org.vashonsd.Games.SR.SeanGame; +import org.vashonsd.Utils.DiceGame; +import org.vashonsd.Utils.Minigame; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by andy on 5/2/18. + */ +public class MinigameFactory { + + private static Map games; + + + static { + games = new HashMap(); + addGame(new TwentyQuestions()); + addGame(new AngelicaGame()); + addGame(new BeckettGame()); + addGame(new EmmeGame()); + addGame(new HuthaifaGame()); + addGame(new NoahGame()); + addGame(new RobertGame()); + addGame(new SamGame()); + addGame(new SeanGame()); + addGame(new NabilGame()); + addGame(new DiceGame()); + addGame(new RockPaperPlus()); + } + + public static void addGame(Minigame m) { + games.put(m.getName(), m); + } + + public static String listGames() { + String result = ""; + String spacer = ""; + for(Minigame m : games.values()) { + result += spacer + m.getName() + " - " + m.getDescription(); + spacer = "\n"; + } + return result; + } + + public static Minigame getGame(String s) { + return games.get(s); + } + + public static boolean hasGame(String s) { + return games.containsKey(s); + } + + +} diff --git a/src/main/java/org/vashonsd/Games/NA/NabilGame.java b/src/main/java/org/vashonsd/Games/NA/NabilGame.java new file mode 100644 index 0000000..6d80ecd --- /dev/null +++ b/src/main/java/org/vashonsd/Games/NA/NabilGame.java @@ -0,0 +1,13 @@ +package org.vashonsd.Games.NA; + +import org.vashonsd.Utils.Placeholder; + +/** + * Created by andy on 5/2/18. + */ +public class NabilGame extends Placeholder { + + public NabilGame() { + super("Nabil"); + } +} diff --git a/src/main/java/org/vashonsd/Games/NE/NoahGame.java b/src/main/java/org/vashonsd/Games/NE/NoahGame.java new file mode 100644 index 0000000..a6d52a4 --- /dev/null +++ b/src/main/java/org/vashonsd/Games/NE/NoahGame.java @@ -0,0 +1,13 @@ +package org.vashonsd.Games.NE; + +import org.vashonsd.Utils.Placeholder; + +/** + * Created by andy on 5/2/18. + */ +public class NoahGame extends Placeholder { + + public NoahGame() { + super("Noah"); + } +} diff --git a/src/main/java/org/vashonsd/Games/RI/RobertGame.java b/src/main/java/org/vashonsd/Games/RI/RobertGame.java new file mode 100644 index 0000000..aa68a21 --- /dev/null +++ b/src/main/java/org/vashonsd/Games/RI/RobertGame.java @@ -0,0 +1,13 @@ +package org.vashonsd.Games.RI; + +import org.vashonsd.Utils.Placeholder; + +/** + * Created by andy on 5/2/18. + */ +public class RobertGame extends Placeholder { + + public RobertGame() { + super("Robert"); + } +} diff --git a/src/main/java/org/vashonsd/Games/RPS_games/Choice.java b/src/main/java/org/vashonsd/Games/RPS_games/Choice.java new file mode 100644 index 0000000..50293eb --- /dev/null +++ b/src/main/java/org/vashonsd/Games/RPS_games/Choice.java @@ -0,0 +1,47 @@ +package org.vashonsd.Games.RPS_games; + +/** + * Created by andy on 5/10/18. + */ +public class Choice { + + + private final int LOSE = -1; + private final int TIE = 0; + private final int WIN = 1; + + private String name; + private ChoiceType type; + + private ChoiceType winCase; + private ChoiceType loseCase; + + public Choice(String name, ChoiceType type, ChoiceType winCase, ChoiceType loseCase) { + this.name = name; + this.type = type; + this.winCase = winCase; + this.loseCase = loseCase; + } + + public ChoiceType getType() { + return type; + } + + public void setType(ChoiceType type) { + this.type = type; + } + + public String getName() { + return name; + } + + public int evaluate(Choice c) { + if(c.getType() == winCase) { + return WIN; + } else if (c.getType() == loseCase) { + return LOSE; + } else { + return TIE; + } + } +} diff --git a/src/main/java/org/vashonsd/Games/RPS_games/ChoiceType.java b/src/main/java/org/vashonsd/Games/RPS_games/ChoiceType.java new file mode 100644 index 0000000..67fd35d --- /dev/null +++ b/src/main/java/org/vashonsd/Games/RPS_games/ChoiceType.java @@ -0,0 +1,8 @@ +package org.vashonsd.Games.RPS_games; + +/** + * Created by andy on 5/10/18. + */ +public enum ChoiceType { + ROCK, PAPER, SCISSORS +} diff --git a/src/main/java/org/vashonsd/Games/RPS_games/Paper.java b/src/main/java/org/vashonsd/Games/RPS_games/Paper.java new file mode 100644 index 0000000..2e23762 --- /dev/null +++ b/src/main/java/org/vashonsd/Games/RPS_games/Paper.java @@ -0,0 +1,11 @@ +package org.vashonsd.Games.RPS_games; + +/** + * Created by andy on 5/10/18. + */ +public class Paper extends Choice { + + public Paper() { + super("Paper", ChoiceType.PAPER, ChoiceType.ROCK, ChoiceType.SCISSORS); + } +} diff --git a/src/main/java/org/vashonsd/Games/RPS_games/Rock.java b/src/main/java/org/vashonsd/Games/RPS_games/Rock.java new file mode 100644 index 0000000..65750e7 --- /dev/null +++ b/src/main/java/org/vashonsd/Games/RPS_games/Rock.java @@ -0,0 +1,11 @@ +package org.vashonsd.Games.RPS_games; + +/** + * Created by andy on 5/10/18. + */ +public class Rock extends Choice { + + public Rock() { + super("Rock", ChoiceType.ROCK, ChoiceType.SCISSORS, ChoiceType.PAPER); + } +} diff --git a/src/main/java/org/vashonsd/Games/RPS_games/RockPaperPlus.java b/src/main/java/org/vashonsd/Games/RPS_games/RockPaperPlus.java new file mode 100644 index 0000000..79840d7 --- /dev/null +++ b/src/main/java/org/vashonsd/Games/RPS_games/RockPaperPlus.java @@ -0,0 +1,63 @@ +package org.vashonsd.Games.RPS_games; + +import org.vashonsd.Utils.Minigame; + +import java.util.Random; + +/** + * Created by andy on 5/10/18. + */ +public class RockPaperPlus extends Minigame { + Choice userChoice; + Choice computerChoice; + + public RockPaperPlus() { + super("RPS", "A basic rock, paper, scissors game.", "quit"); + setComputerChoice(); + } + + @Override + public String start() { + return "Welcome to Rock, Paper, Scissors! Type your choice."; + } + + @Override + public String handle(String str) { + if(str.equalsIgnoreCase("rock")) { + userChoice = new Rock(); + } else if (str.equalsIgnoreCase("paper")) { + userChoice = new Paper(); + } else if (str.equalsIgnoreCase("scissors")) { + userChoice = new Scissors(); + } else { + return "You need to choose either rock, paper, or scissors."; + } + int result = userChoice.evaluate(computerChoice); + String rep = "The computer chose " + computerChoice.getName() + ". "; + switch (result) { + case -1: rep += "You lose!"; + break; + case 1: rep += "You win!"; + break; + default: rep += "Tie!"; + } + return rep; + } + + private void setComputerChoice() { + Random r = new Random(); + int choice = r.nextInt(2); + switch (choice) { + case 0: computerChoice = new Rock(); + break; + case 1: computerChoice = new Paper(); + break; + default: computerChoice = new Scissors(); + } + } + + @Override + public String quit() { + return null; + } +} diff --git a/src/main/java/org/vashonsd/Games/RPS_games/Scissors.java b/src/main/java/org/vashonsd/Games/RPS_games/Scissors.java new file mode 100644 index 0000000..d4de0f9 --- /dev/null +++ b/src/main/java/org/vashonsd/Games/RPS_games/Scissors.java @@ -0,0 +1,11 @@ +package org.vashonsd.Games.RPS_games; + +/** + * Created by andy on 5/10/18. + */ +public class Scissors extends Choice { + + public Scissors() { + super("Scissors", ChoiceType.SCISSORS, ChoiceType.PAPER, ChoiceType.ROCK); + } +} diff --git a/src/main/java/org/vashonsd/Games/SP/SamGame.java b/src/main/java/org/vashonsd/Games/SP/SamGame.java new file mode 100644 index 0000000..b406123 --- /dev/null +++ b/src/main/java/org/vashonsd/Games/SP/SamGame.java @@ -0,0 +1,13 @@ +package org.vashonsd.Games.SP; + +import org.vashonsd.Utils.Placeholder; + +/** + * Created by andy on 5/2/18. + */ +public class SamGame extends Placeholder { + + public SamGame() { + super("Sam"); + } +} diff --git a/src/main/java/org/vashonsd/Games/SR/SeanGame.java b/src/main/java/org/vashonsd/Games/SR/SeanGame.java new file mode 100644 index 0000000..2c507fe --- /dev/null +++ b/src/main/java/org/vashonsd/Games/SR/SeanGame.java @@ -0,0 +1,12 @@ +package org.vashonsd.Games.SR; + +import org.vashonsd.Utils.Placeholder; + +/** + * Created by andy on 5/2/18. + */ +public class SeanGame extends Placeholder { + public SeanGame() { + super("Sean"); + } +} diff --git a/src/main/java/org/vashonsd/Games/TwentyQuestions.java b/src/main/java/org/vashonsd/Games/TwentyQuestions.java new file mode 100644 index 0000000..a291ba0 --- /dev/null +++ b/src/main/java/org/vashonsd/Games/TwentyQuestions.java @@ -0,0 +1,52 @@ +package org.vashonsd.Games; + +import org.vashonsd.Utils.Minigame; +import org.vashonsd.Utils.Utils; + +import java.util.Random; + +/** + * Created by andy on 5/2/18. + */ +public class TwentyQuestions extends Minigame { + + private int targetNumber; + + public TwentyQuestions() { + super("20Q", "Guess a number between 0 and 1,000", "quit"); + } + + @Override + public String start() { + setUp(); + return("Okay, guess a number between 1 and 500."); + } + + private void setUp() { + Random rand = new Random(); + targetNumber = rand.nextInt(499) + 1; + } + + @Override + public String handle(String str) { + if(!Utils.IsInteger(str)) { + return "You must guess a number."; + } + int guess = Integer.parseInt(str); + if(!(guess >= 1 && guess <=500)) { + return "Your guess must be between 1 and 500."; + } else if(guess < targetNumber) { + return "too small"; + } else if(guess > targetNumber) { + return "too big"; + } else { + setUp(); + return "Congratulations! You guessed it!\nGuess another, or type " + quitWord + " to quit."; + } + } + + @Override + public String quit() { + return "Goodbye! Thanks for playing!"; + } +} diff --git a/src/main/java/org/vashonsd/Main.java b/src/main/java/org/vashonsd/Main.java new file mode 100644 index 0000000..84453f0 --- /dev/null +++ b/src/main/java/org/vashonsd/Main.java @@ -0,0 +1,40 @@ +package org.vashonsd; + +import org.vashonsd.Utils.Minigame; +import org.vashonsd.Games.MinigameFactory; + +import java.util.Scanner; + +public class Main { + + private static final String quitWord = "quit"; + private static Minigame currentGame; + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + String userIn = ""; + while(userIn != quitWord) { + if (currentGame == null) { + System.out.println(MinigameFactory.listGames()); + System.out.println("Type the name of the game you would like to play: "); + userIn = in.nextLine(); + if(userIn.equalsIgnoreCase(quitWord)) break; + if(!MinigameFactory.hasGame(userIn)) { + System.out.println("Try again."); + } else { + currentGame = MinigameFactory.getGame(userIn); + System.out.println(currentGame.start()); + userIn = in.nextLine(); + } + } else if(userIn.equalsIgnoreCase(currentGame.getQuitWord())) { + System.out.println(currentGame.quit()); + currentGame = null; + } else { + System.out.println(currentGame.handle(userIn)); + userIn = in.nextLine(); + } + } + System.out.println("Goodbye!"); + in.close(); + } +} diff --git a/src/main/java/org/vashonsd/Utils/Cards/Card.java b/src/main/java/org/vashonsd/Utils/Cards/Card.java new file mode 100644 index 0000000..090f221 --- /dev/null +++ b/src/main/java/org/vashonsd/Utils/Cards/Card.java @@ -0,0 +1,166 @@ +package org.vashonsd.Utils.Cards; + +public class Card +{ + // These constants represent the possible suits and + // can be used to index into the suits array to get + // their string representation. + private static final int HEARTS = 0; + private static final int DIAMONDS = 1; + private static final int SPADES = 2; + private static final int CLUBS = 3; + + // These constants represent the ranks of the non-number + // cards, or cards above 10. To maintain the ordering after + // 2-10, the integer values are 11, 12, 13, and 14 and + // also allow us to index into the ranks array to get their + // String representation. + private static final int JACK = 11; + private static final int QUEEN = 12; + private static final int KING = 13; + private static final int ACE = 14; + + // Instance variables + + // This represents the rank of the card, the value from 2 to Ace. + private int rank; + + // This represents the suit of the card, one of hearts, diamonds, spades or clubs. + private int suit; + + // This represents the value of the card, which is 10 for face cards or 11 for an ace. + private int value; + + // This String array allows us to easily get the String value of a Card from its rank. + // There are two Xs in the front to provide padding so numbers have their String representation + // at the corresponding index. For example, the String for 2 is at index 2. + private String[] ranks = {"X", "X", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; + + // The String array allow us to easily get the String value of the Card for its suit. This + // is the same order as the suits above so we can index into this array. + private String[] suits = {"H", "D", "S", "C"}; + + /** + * This is the constructor to create a new Card. To create a new card + * we pass in its rank and its suit. + * + * @param r The rank of the card, as an int. + * @param s The suit of the card, as an int. + */ + public Card(int r, int s) + { + rank = r; + suit = s; + } + + // Getter Methods + + /** + * This returns the rank of the card as an integer. + * + * @return rank of card as an int. + */ + public int getRank() + { + return rank; + } + + /** + * This returns the suit of the card as an integer. + * + * @return suit of card as an int. + */ + public int getSuit() + { + return suit; + } + + /** + * This returns the value of the card as an integer. + * + * For facecards the value is 10, which is different + * than their rank underlying value. For aces the default + * value is 11. + * + * @return The value of the card as an int. + */ + public int getValue() + { + int value = rank; + if(rank > 10) + { + value = 10; + } + + if(rank == ACE) + { + value = 11; + } + + return value; + } + + /** + * This utility method converts from a rank integer to a String. + * + * @param r The rank. + * @return String version of rank. + */ + public String rankToString(int r) + { + return ranks[r]; + } + + /** + * This utility method converts from a suit integer to a String. + * + * @param s The suit. + * @return String version of suit. + */ + public String suitToString(int s) + { + return suits[s]; + } + + /** + * Return the String version of the suit. + * + * @return String version of suit. + */ + public String getSuitAsString() + { + return suitToString(suit); + } + + /** + * Return the String version of the rank. + * + * @return String version of the rank. + */ + public String getRankAsString() + { + return rankToString(rank); + } + + /** + * This returns the String representation of a card which + * will be two characters. For example, the two of hearts would + * return 2H. Face cards have a short string so the ace of + * spades would return AS. + * + * @return String representation of Card. + */ + public String toString() + { + // Get a string for rank + String rankString = ranks[rank]; + + // Get a string for the suit + String suitString = suits[suit]; + + // combine those + return rankString + suitString; + } + + +} \ No newline at end of file diff --git a/src/main/java/org/vashonsd/Utils/Cards/Deck.java b/src/main/java/org/vashonsd/Utils/Cards/Deck.java new file mode 100644 index 0000000..7fd7cfe --- /dev/null +++ b/src/main/java/org/vashonsd/Utils/Cards/Deck.java @@ -0,0 +1,90 @@ +package org.vashonsd.Utils.Cards; + +import java.util.*; + +public class Deck +{ + private static final int HEARTS = 0; + private static final int DIAMONDS = 1; + private static final int SPADES = 2; + private static final int CLUBS = 3; + + private static final int JACK = 11; + private static final int QUEEN = 12; + private static final int KING = 13; + private static final int ACE = 14; + + // Instance variables + + // This stores the deck which is a list of the Card objects. + private ArrayList deck; + + /** + * This creates a Deck. A Deck starts as a list of 52 cards. + * We loop through each suit and rank and construct a card + * and add it to the deck. + */ + public Deck() + { + deck = new ArrayList(); + + for(int rank = 2; rank <= ACE; rank++) + { + for(int suit = HEARTS; suit <= CLUBS; suit++) + { + Card card = new Card(rank, suit); + deck.add(card); + } + } + } + + // Getter method + + /** + * This getter method returns the ArrayList of cards. + * @return ArrayList of the Cards. + */ + public ArrayList getCards() + { + return deck; + } + + /** + * This deals the first Card from the deck by removing it. + * @return The first Card in the deck. + */ + public Card deal() + { + return deck.remove(0); + } + + /** + * This prints out the current state of the deck. + */ + public void print() + { + for(Card card: deck) + { + System.out.println(card); + } + } + + /** + * This shuffles the deck by making 52 swaps of + * card positions. + */ + public void shuffle() + { + for(int i = 0; i < deck.size(); i++) + { + Random rand = new Random(); + int randomIndex = rand.nextInt(52); + Card x = deck.get(i); + Card y = deck.get(randomIndex); + + deck.set(i, y); + deck.set(randomIndex, x); + } + } + +} diff --git a/src/main/java/org/vashonsd/Utils/DiceGame.java b/src/main/java/org/vashonsd/Utils/DiceGame.java new file mode 100644 index 0000000..50d46f5 --- /dev/null +++ b/src/main/java/org/vashonsd/Utils/DiceGame.java @@ -0,0 +1,66 @@ +package org.vashonsd.Utils; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by andy on 5/3/18. + */ +public class DiceGame extends Minigame { + List playerDice; + List computerDice; + private int numDice; + int round; + private int playerScore; + private int computerScore; + + public DiceGame() { + super("Dice Wars", "A battle of wits and luck", "quit"); + playerDice = new ArrayList(); + computerDice = new ArrayList(); + } + + @Override + public String start() { + setUp(); + return "Welcome to Dice Wars!" + + "\nYour dice are: " + playerDice.toString() + +"Which die would you like to use in the first round?"; + } + + private void setUp() { + numDice = 3; + playerScore = computerScore = 0; + round = 0; + for(int i=0; i=1 && playerChoice <= playerDice.size())) { + return "You must choose a number from 1 to " + playerDice.size(); + } + + + + int compDie = computerDice.remove(Randomizer.nextInt(computerDice.size())); + //if(playerDie > compDie) { + + //} + return null; + } + + @Override + public String quit() { + return "Thanks for playing!"; + } +} diff --git a/src/main/java/org/vashonsd/Utils/Minigame.java b/src/main/java/org/vashonsd/Utils/Minigame.java new file mode 100644 index 0000000..0c6ddbe --- /dev/null +++ b/src/main/java/org/vashonsd/Utils/Minigame.java @@ -0,0 +1,62 @@ +package org.vashonsd.Utils; + +/** + * A Minigame can be run as a text-based game that interacts with the user. + * + * The mechanics of interacting with the user are left out of this code. + * It uses public methods to start, to finish, and, in between, respond + * to Strings with Strings. + */ +public abstract class Minigame { + protected String name; + protected String description; + protected String quitWord; + + public Minigame(String name, String description, String quitWord) { + this.name = name; + this.description = description; + this.quitWord = quitWord; + } + + public String getName() { + return this.name; + } + + public String getDescription() { + return this.description; + } + + public String getQuitWord() { + return this.quitWord; + } + + /** + * This method will be called when the game is started. + * + * This method can be used just to return a welcome message, + * or it could also be used to set up the starting state of a game. + * If the game will ever restarting, consider creating a setUp() + * method that can be reused. + * + * @return A String representing a greeting. + */ + public abstract String start(); + + /** + * This method will be called while the game is in play. + * + * handle() is the sole public method that should be called during + * game interactions. + * + * @param str The String representing input from the user. + * @return The response to the user. + */ + public abstract String handle(String str); + + /** + * This method will be called with the user signals a quit. + * + * @return A String representing an exit message + */ + public abstract String quit(); +} diff --git a/src/main/java/org/vashonsd/Utils/Placeholder.java b/src/main/java/org/vashonsd/Utils/Placeholder.java new file mode 100644 index 0000000..e90c508 --- /dev/null +++ b/src/main/java/org/vashonsd/Utils/Placeholder.java @@ -0,0 +1,33 @@ +package org.vashonsd.Utils; + +/** + * Created by andy on 5/2/18. + */ +public class Placeholder extends Minigame { + + + public Placeholder(String name) { + super(name, "A simple game in which you guess " + name + "'s name", "quit"); + } + + @Override + public String start() { + return "Hi! My name is " + name + "!" + + "\nAre you ready for a quiz?" + + "\nWhat's my name?"; + } + + @Override + public String handle(String str) { + if(str.equalsIgnoreCase(name)) { + return "Fantastic! Guess again, or type " + quitWord + " to quit!"; + } else { + return "Nope! Guess again!"; + } + } + + @Override + public String quit() { + return "Thanks for playing with me, " + name + "!"; + } +} diff --git a/src/main/java/org/vashonsd/Utils/Randomizer.java b/src/main/java/org/vashonsd/Utils/Randomizer.java new file mode 100644 index 0000000..3d106d9 --- /dev/null +++ b/src/main/java/org/vashonsd/Utils/Randomizer.java @@ -0,0 +1,85 @@ +package org.vashonsd.Utils; + +import java.util.*; + +public class Randomizer{ + + public static Random theInstance = null; + + public Randomizer(){ + + } + + public static Random getInstance(){ + if(theInstance == null){ + theInstance = new Random(); + } + return theInstance; + } + + /** + * Return a random boolean value. + * @return True or false value simulating a coin flip. + */ + public static boolean nextBoolean(){ + return Randomizer.getInstance().nextBoolean(); + } + + /** + * This method simulates a weighted coin flip which will return + * true with the probability passed as a parameter. + * + * @param probability The probability that the method returns true, a value between 0 to 1 inclusive. + * @return True or false value simulating a weighted coin flip. + */ + public static boolean nextBoolean(double probability){ + return Randomizer.nextDouble() < probability; + } + + /** + * This method returns a random integer. + * @return A random integer. + */ + public static int nextInt(){ + return Randomizer.getInstance().nextInt(); + } + + /** + * This method returns a random integer between 0 and n, exclusive. + * @param n The maximum value for the range. + * @return A random integer between 0 and n, exclusive. + */ + public static int nextInt(int n){ + return Randomizer.getInstance().nextInt(n); + } + + /** + * Return a number between min and max, inclusive. + * @param min The minimum integer value of the range, inclusive. + * @param max The maximum integer value in the range, inclusive. + * @return A random integer between min and max. + */ + public static int nextInt(int min, int max){ + return min + Randomizer.nextInt(max - min + 1); + } + + /** + * Return a random double between 0 and 1. + * @return A random double between 0 and 1. + */ + public static double nextDouble(){ + return Randomizer.getInstance().nextDouble(); + } + + /** + * Return a random double between min and max. + * @param min The minimum double value in the range. + * @param max The maximum double value in the rang. + * @return A random double between min and max. + */ + public static double nextDouble(double min, double max){ + return min + (max - min) * Randomizer.nextDouble(); + } + + +} diff --git a/src/main/java/org/vashonsd/Utils/Utils.java b/src/main/java/org/vashonsd/Utils/Utils.java new file mode 100644 index 0000000..3ccea17 --- /dev/null +++ b/src/main/java/org/vashonsd/Utils/Utils.java @@ -0,0 +1,70 @@ +package org.vashonsd.Utils; + + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.Random; + +public class Utils { + + static final String pathPrefix = "src/main/resources/"; + + /** + * Returns true if the given String is an integer, false otherwise. + * + * Note that this method still returns true if the String is negative or zero. + */ + public static boolean IsInteger(String s) { + if(s.isEmpty()) return false; + for(int i = 0; i < s.length(); i++) { + if(i == 0 && s.charAt(i) == '-') { + if(s.length() == 1) return false; + else continue; + } + if( !Character.isDigit(s.charAt(i)) ) return false; + } + return true; + } + + /** + * Returns a (pseudo)random die roll from 1 to the maximum number provided. + * @param max + * @return + */ + public static int rollDie(int max) { + Random rand = new Random(); + return rand.nextInt(max)+1; + } + + /** + * This method will return all the lines from the given file as an array of strings. + * + * It is up to the user what to do with that array. + * + * @param filename The name of the file. Please make it unique. + * @return All the lines of the file, in the form of an array of Strings. + */ + public static List readFromFile(String filename) throws IOException { + + List lines; + Path path = Paths.get(pathPrefix + filename); + lines = Files.readAllLines(path, StandardCharsets.UTF_8); + return lines; + } + + public static void writeToFile(List strings, String filename) throws IOException { + Path path = Paths.get(pathPrefix + filename); + Files.write(path, strings, StandardCharsets.UTF_8); + } + + public static void appendToFile(List strings, String filename) throws IOException { + Path path = Paths.get(pathPrefix + filename); + List previousLines = readFromFile(filename); + previousLines.addAll(strings); + writeToFile(previousLines, filename); + } +} From 774713fca0250c05866ed9742e9f222845524e98 Mon Sep 17 00:00:00 2001 From: Andy James Date: Sun, 20 May 2018 16:14:32 -0700 Subject: [PATCH 10/30] Added minigames from Noah, Beckett, Angelica, Sam, Sean, and Robert. --- .idea/workspace.xml | 437 +++++++++--------- .../org/vashonsd/Games/AO/AngelicaGame.java | 49 +- .../org/vashonsd/Games/BR/BeckettGame.java | 63 ++- .../java/org/vashonsd/Games/NE/Choice.java | 66 +++ .../org/vashonsd/Games/NE/ChoiceType.java | 5 + .../java/org/vashonsd/Games/NE/NoahGame.java | 129 +++++- .../java/org/vashonsd/Games/NE/Paper.java | 11 + src/main/java/org/vashonsd/Games/NE/Rock.java | 11 + .../java/org/vashonsd/Games/NE/Scissors.java | 11 + .../org/vashonsd/Games/RI/RobertGame.java | 53 ++- .../java/org/vashonsd/Games/SP/SamGame.java | 68 ++- .../java/org/vashonsd/Games/SR/SeanGame.java | 200 +++++++- src/main/java/org/vashonsd/Utils/Utils.java | 37 ++ .../java/org/vashonsd/Utils/UtilsTest.java | 6 + 14 files changed, 896 insertions(+), 250 deletions(-) create mode 100755 src/main/java/org/vashonsd/Games/NE/Choice.java create mode 100755 src/main/java/org/vashonsd/Games/NE/ChoiceType.java mode change 100644 => 100755 src/main/java/org/vashonsd/Games/NE/NoahGame.java create mode 100755 src/main/java/org/vashonsd/Games/NE/Paper.java create mode 100755 src/main/java/org/vashonsd/Games/NE/Rock.java create mode 100755 src/main/java/org/vashonsd/Games/NE/Scissors.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f286b55..a736a28 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,34 +1,20 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + @@ -52,12 +38,17 @@ - - + + - - + + + + + + + @@ -66,18 +57,38 @@ - - + + + + + - - + + - - - + + + + + + + + + + + + + + + + + + + + @@ -90,6 +101,7 @@ @@ -146,13 +158,19 @@ @@ -207,138 +225,6 @@