Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Arrays;
- import java.util.Random;
- public class SentenceScramble {
- // the main method declares a random and sets an integer to a random value from 0 to 2
- //6 different arrays are declared and assigned sentences with pairs of two that are identical
- //a println statement gives the user instructions on how to play
- //it uses the random integer to determine which sentence is used
- //it sends one sentence to method scrambleArray and then sends the
- //scrambled sentence, original sentence, and corresponding word to method acceptUser
- public static void main(String[] args) {
- Random rand = new Random();
- int determineArr = rand.nextInt(3);
- //declare sentences
- String[] theTrueFirst = {"no ", "one ", "Cares ", "About ", "your ", "feeLings ", "except ", "your ", "moM ", "."};
- String[] theTrueSecond = {"there ", "iS ", "a ", "lot ", "to ", "do ", "In ", "The ", "world ", "."};
- String[] theTrueThird = {"life ", "maY ", "havE ", "no ", "purpose, ", "but ", "neither ", "doeS ","death ", "."};
- String[] theFirst = {"no ", "one ", "Cares ", "About ", "your ", "feeLings ", "except ", "your ", "moM ", "."};
- String[] theSecond = {"there ", "iS ", "a ", "lot ", "to ", "do ", "In ", "The ", "world ", "."};
- String[] theThird = {"life ", "maY ", "havE ", "no ", "purpose, ", "but ", "neither ", "doeS ","death ", "."};
- //give instructions to user
- System.out.println("If you want to see your sentence, input \"LOOK\" \n"
- + "If you want to switch words in a sentnce, input \"SWAP int int\"\n"
- + "If you want to guess the word, input \"GUESS\" and then input your word\n"
- + "Enter \"quit\" if you want to stop\n\n");
- //uses random int to determine which sentence to use and scrambles it
- //then sends it to method acceptUser
- if (determineArr == 0) {
- theFirst = scrambleArray(theFirst);
- acceptUser(theFirst, theTrueFirst, "CALM");
- }else if(determineArr == 1) {
- theSecond = scrambleArray(theSecond);
- acceptUser(theSecond, theTrueSecond, "SIT");
- }else {
- theThird = scrambleArray(theThird);
- acceptUser(theThird, theTrueThird, "YES");
- }
- }
- // scrambles the sentence by randomly swappng array index values 100 times
- public static String[] scrambleArray (String[] newArr) {
- for(int i = 0; i < 100; i++) {
- Random sequence = new Random();
- int swapOne = sequence.nextInt(9);
- int swapTwo = sequence.nextInt(9);
- String tempSavedValue = newArr[swapOne];
- newArr[swapOne] = newArr[swapTwo];
- newArr[swapTwo] = tempSavedValue;
- }
- return newArr;
- }
- //accepts the scrambled sentence, original sentence, and the word the user is trying to find
- //accepts user input for the action the user wants to take so long s they don't enter quit
- public static void acceptUser(String[] sentence, String[] trueSentence, String word) {
- Scanner user = new Scanner (System.in);
- //primes while loop
- String userAction = "nothing";
- while(!userAction.equals("quit")) {
- //accepts user input and sets it to uppercase
- userAction = user.nextLine();
- userAction = userAction.toUpperCase();
- //if the user inputs look, it prints the sentence
- if(userAction.equals("LOOK")) {
- for(int i = 0; i < sentence.length; i++) {
- System.out.print(sentence[i]);
- }
- System.out.println("\n");
- //if the user inputs guess, it checks to see if the word is correct
- //it either tells the user they won and stops the loop or tells the user they're wrong
- }else if (userAction.equals("GUESS")) {
- String userGuess = user.nextLine();
- userGuess = userGuess.toUpperCase();
- if(userGuess.equals(word)) {
- System.out.println("Congrats, you won!");
- userAction = "quit";
- }else {
- System.out.println("Nope, try again.\n");
- }
- //if the user didn't type look or guess, it breaks apart the input into substrings
- //substrings are checked to see if they are valid input
- //if they are correct for swap, it swaps the ints and checks if the new sentence is in order
- }else {
- if (userAction.length() == 8) {
- //breaks into substrings
- String modAction = userAction.substring(0, 4);
- String firstSpace = userAction.substring(4, 5);
- String firstSwap = userAction.substring(5, 6);
- String secondSpace = userAction.substring(6, 7);
- String secondSwap = userAction.substring(7, 8);
- // checks to see if ints are valid by sending substrings to checkSwap method
- //if they aren't, it tells the user and asks for new input
- Boolean errorCheck = true;
- errorCheck = checkSwap(firstSwap, secondSwap);
- if (errorCheck == false) {
- System.out.println("Error: invalid input\n");
- }else {
- //checks if their are spaces between the ints
- if(!firstSpace.equals(" ") || !secondSpace.equals(" ") || !firstSpace.equals(" ") && !secondSpace.equals(" ")) {
- System.out.println("Error: invalid input\n");
- }else {
- //checks if input equals swap
- if (modAction.equals("SWAP")) {
- int intFirstSwap = remade(firstSwap) - 1;
- int intSecondSwap = remade(secondSwap) - 1;
- String firstValue = sentence[intFirstSwap];
- sentence[intFirstSwap] = sentence[intSecondSwap];
- sentence[intSecondSwap]= firstValue;
- //checks if new sentence is in order
- Boolean check = false;
- if(Arrays.equals(sentence, trueSentence)) {
- check = true;
- }
- if(check.equals(true)) {
- System.out.println("Congrats! Your Sentence is in order.\n");
- for(int i = 0; i < sentence.length; i++) {
- System.out.print(sentence[i]);
- }
- System.out.println("\n");
- }else {
- System.out.println("Not quite, swap again.\n");
- }
- }else {
- System.out.println("Error: invalid input\n");
- }
- }
- }
- } else {
- System.out.println("Error: invalid input\n");
- }
- }
- }
- }
- //checks if the ints input as SWAP int int are between 1 and 9
- public static Boolean checkSwap (String firstInt, String secondInt) {
- if ( !firstInt.equals("1") && !firstInt.equals("2") && !firstInt.equals("3") && !firstInt.equals("4") && !firstInt.equals("5") &&
- !firstInt.equals("6") && !firstInt.equals("7") && !firstInt.equals("8") && !firstInt.equals("9")) {
- return false;
- } else if ( !secondInt.equals("1") && !secondInt.equals("2") && !secondInt.equals("3") && !secondInt.equals("4") && !secondInt.equals("5") &&
- !secondInt.equals("6") && !secondInt.equals("7") && !secondInt.equals("8") && !secondInt.equals("9")) {
- return false;
- } else {
- return true;
- }
- }
- //turns strings into ints
- public static int remade(String str) {
- int change;
- Scanner look = new Scanner(str);
- change = look.nextInt();
- look.close();
- return change;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement