need help Create a Bluel project with a new class called R as described. esequen
ID: 3707641 • Letter: N
Question
need help
Create a Bluel project with a new class called R as described. esequencingGame. Write an app for the game ill start with the user typing in a word Make an string resequencing puzzle game. Your game wi hich is stored as a String. The computer will scramble the letters of that word Type a word: qwerty puzzle: twyqre ter will sk the user how many characters to reverse. if the player says 3,the computer will reverse the first 3 letters of the puzzle string. Example: ywtqre The computer will ask the user how many characters to reverse. If the player says 6, the computer will reverse the first 6 letters of the puzzle string Example: erqtwy The computer will ask the user how many characters to reverse. If the player says 4, the computer will reverse the first 4 letters of the puzzle string. Example: tqrewy The computer will ask the user how many characters to reverse. If the player says 5, the computer will reverse the first 5 letters of the puzzle string. Example: werqty The game will continue until the user has successfully put the letters back in order to form the original word, at which point a message will inform the player of winning.Explanation / Answer
import java.lang.*;
import java.io.*;
import java.util.*;
public class ResequencingGame{
public static void main(String []args){
// scanner for taking inputs...
Scanner scanner = new Scanner( System.in );
System.out.println("Type a word:");
String input = scanner.nextLine();
List<Character> characters = new ArrayList<Character>();
for(char c:input.toCharArray()){
characters.add(c);
}
StringBuilder output = new StringBuilder(input.length());
while(characters.size()!=0){
int randPicker = (int)(Math.random()*characters.size());
output.append(characters.remove(randPicker));
}
String puzzle = output.toString();
System.out.println("Puzzle: " + puzzle);
int puzzlelen = puzzle.length();
while(input != puzzle)
{
System.out.println("How many Char to reverse, please enter number lower than " + puzzlelen + " : ");
int cctr = scanner.nextInt(); // char count to reverse
// convert String to character array
// by using toCharArray
char[] chararray = puzzle.toCharArray();
int j = 0;
char temp;
for (int i = cctr-1; i>=cctr/2; i--)
{
temp = chararray[i];
chararray[i] = chararray[j];
chararray[j] = temp;
}
puzzle = new String(chararray);
System.out.println("Puzzle: " + puzzle);
}
System.out.println("you won...");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.