I was inspired by this Reddit /r/dailyprogrammer challenge:
Contrary to popular belief, the tetromino pieces you are given in a game of Tetris are not randomly selected. Instead, all seven pieces are placed into a "bag." A piece is randomly removed from the bag and presented to the player until the bag is empty. When the bag is empty, it is refilled and the process is repeated for any additional pieces that are needed.
The pieces are as follows:
O
,I
,S
,Z
,L
,J
,T
I was just wanting some general feedback on the code regarding readability, structure, and efficiency.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
/**
* Created on 6/23/2016.
*
* Inspired by r/dailyprogrammer
* https://www.reddit.com/r/dailyprogrammer/comments/3ofsyb/20151012_challenge_236_easy_random_bag_system/
*/
public class RandomBag {
/**
* This is the main method. The main acquires input from the user
* then generates a combination of pieces in a particular order.
* @param args Unused.
*/
public static void main(String args[]){
Scanner scanner = new Scanner(System.in); // Used to obtain user input.
loop : while (true) {
System.out.print(">>> ");
String input = scanner.nextLine().replace(" ", "");
// Checks for special cases.
switch (input) {
case "exit" : break loop;
case "" : continue loop;
}
// The generator class will return a string of its results.
System.out.println(new Generator(Integer.parseInt(input)));
}
}
/**
* Created on 6/23/2016.
* Used to create a bag that generates a set amount of pieces in a combination
* that will never have the same piece displayed 3 times in a row. This is
* accomplished by placing all pieces in a list and removing them one by one.
* Once the list has been depleted the list is renewed with the same pieces.
*/
private static class Generator{
private int pieceNumber; // Number of pieces that will be generated.
private String generation = ""; // Used to store the piece generation.
private final String[] pieces = {"O", "I", "J", "L", "S", "Z", "T"}; // Pieces used in the generation.
/**
* Constructor used to store the number of pieces that need to be
* generated and execute the piece generation method.
* @param pieceNumber Used to determine how long the generation should be.
*/
Generator(int pieceNumber){
this.pieceNumber = pieceNumber; // Initializes number of pieces.
generatePieces(); // Generates piece combination.
}
/**
* Used to generate the pieces in the specific order. Manipulates class instanced variables.
*/
private void generatePieces(){
ArrayList<String> temp = new ArrayList<>(Arrays.asList(pieces)); // Used to store all pieces.
while(generation.length() <= pieceNumber){ // Generates string until it is the specified size.
if(temp.isEmpty()) // Checks if list is empty.
temp = new ArrayList<>(Arrays.asList(pieces)); // Replaces the pieces in the list.
Collections.shuffle(temp); // Shuffles the list
generation += temp.get(0); // Places the first element of the list onto the string.
temp.remove(0); // Removes the element that was added from the list.
}
}
/**
* Returns the generation constructed by the generatePieces method.
* @return The string generated.
*/
@Override
public String toString(){
return generation; // Returns piece generation once generated.
}
}
}