Home > Blockchain >  Working on Program Called DieToss that Searches for Duplicate Tosses. How can I limit where parenthe
Working on Program Called DieToss that Searches for Duplicate Tosses. How can I limit where parenthe

Time:03-14

Hello StackoverFlow Community

I am working on a DieToss program intended to print random sequence numbers given an inputted length and the randomized toss needs to be between 1 and 6. The goal is to put parentheses around the longest or first string of duplicates.

The issue here is that it continues to put parentheses around all duplicate strings of integers and I have been unsuccessful in my efforts.

Below is the program to look for duplicates in a given array of integers.

public class DieToss {
    public static void main(String[] args) {
        boolean restartQ = true;
        
         while(restartQ){
            int numDie = 0;
            
            Scanner in = new Scanner(System.in);
            
            boolean ehandle = true;
            
            System.out.println("Tell me how many dice you want me to throw?");
            
            System.out.println("Be sure that it is 10 or greater but only up to 30");
            
            
            while (ehandle) {
                try {
                  numDie = in.nextInt();
                  if(numDie <= 30 && numDie >= 10){
                      ehandle = false;
                  }
                  
                  else{
                      System.out.println("Please Re-enter. It needs to be greater than or equal to 10 but only up to 30.");
                      System.out.println("Tell me how many dice you want me to throw?");
                      continue;
                  }
                } 
                catch (Exception e) {
                  System.out.println("Please enter integers only");
                  
                  System.out.println("Tell me how many dice you want me to throw?");
            
                  System.out.println("Be sure that it is 10 or greater but only up to 30");
                  in.next();
                }
            }
            
            int[] dieToss = new int[numDie];
            Random random = new Random();
            
    
            for (int i = 0; i < dieToss.length; i  ) {
                dieToss[i] = 1   random.nextInt(6);
            }
    
            //String dieArr = (Arrays.toString(dieToss));
            
            //System.out.print(Arrays.toString(dieToss));
            
            boolean parenOpen = false;
            
            
            for (int i = 0; i < dieToss.length; i  ){
                if (i < dieToss.length - 1)
                {
                    if (dieToss[i] == dieToss[i   1] && !parenOpen )
                    {
                        System.out.print("(");
                        parenOpen = true;
                    }
                    System.out.print(dieToss[i]   " ");
                    if (dieToss[i] != dieToss[i   1] && parenOpen)
                    {
                        System.out.print(")");
                        parenOpen = false;
                    }
    
                }
    
            }
            System.out.println(dieToss[dieToss.length - 1]);
            System.out.println(" ");
            
            System.out.println("Any duplicates that I have found are displayed above in parentheses.");
            if (parenOpen)
            {
                System.out.print(")");
            }
            
            System.out.println("Would you like to reroll? Maybe test your luck?");
            System.out.println(" ");
            System.out.println("Enter any key if you would like to. Press n to exit.");
            
            Scanner in2 = new Scanner(System.in);
            
            String reRoll = in2.nextLine();
            
            if(reRoll.equals("n") || reRoll.equals("N")){
                restartQ = false;
            }
            else{
                continue;
            }
        }
    }
}

How can I modify this block of code to limit itself to the longest string of duplicates?

Any help is greatly appreciated.

CodePudding user response:

Introduction

I modified your code and ran 63 tests. Here are the results of the 63rd test.

How many dice do you want me to throw?
Type a number between 10 and 30
15
The 15 die rolls are: [2, 3, 4, 4, 6, 6, 5, 4, 6, 3, 3, 5, 4, 1, 4]
The first, longest duplicate run is: [2, 3, (4, 4), 6, 6, 5, 4, 6, 3, 3, 5, 4, 1, 4]
Would you like to reroll (y or n)?
y
How many dice do you want me to throw?
Type a number between 10 and 30
15
The 15 die rolls are: [3, 1, 6, 4, 3, 5, 4, 6, 1, 1, 1, 5, 2, 6, 5]
The first, longest duplicate run is: [3, 1, 6, 4, 3, 5, 4, 6, (1, 1, 1), 5, 2, 6, 5]
Would you like to reroll (y or n)?
y
How many dice do you want me to throw?
Type a number between 10 and 30
15
The 15 die rolls are: [1, 2, 5, 1, 2, 6, 4, 1, 4, 4, 6, 4, 5, 1, 1]
The first, longest duplicate run is: [1, 2, 5, 1, 2, 6, 4, 1, (4, 4), 6, 4, 5, 1, 1]
Would you like to reroll (y or n)?
n

Explanation

The first thing I did was break your monolithic code up into methods I wound up creating nine methods, not including the main method. By splitting your code up into methods, I was able to test each small part individually. As I said, I ran 63 tests before I was satisfied with the code.

Java method names are verb-noun combinations.

The findLargestDuplicates method requires some explanation. The method returns a pair of indexes, a start index and an end index. If there are no duplicate dice rolls the method returns an end index of zero.

The findLargestDuplicates method finds the start index of each and every run of duplicate dice rolls. Only the largest run of duplicate dice rolls is kept and returned. Because the dice roll array is checked from the first index through the last, the first, longest run of duplicate dice rolls is returned.

I returned an int array of two index values because a Java method returns one value.

Code

Here's the complete runnable code.

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class DieToss {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        new DieToss().performDieToss(scanner);
        scanner.close();
    }
    
    public void performDieToss(Scanner scanner) {
        Random random = new Random();
        
        do {
            int numberOfTosses = getNumberOfTosses(scanner);
            int[] dieToss = createRandomArray(random, numberOfTosses);
            int[] indexes = findLargestDuplicates(dieToss);
            
            String prefix = "The "   numberOfTosses   " die rolls are: ";
            System.out.println(generateOutput(prefix, dieToss));
            
            if (indexes[1] == 0) {
                System.out.println("There were no duplicate dice rolls");
            } else {
                prefix = "The first, longest duplicate run is: ";
                System.out.println(generateOutput(prefix, dieToss, indexes));
            }
        } while (continueRolling(scanner));
    }
    
    private int getNumberOfTosses(Scanner scanner) {
        int numberOfTosses = 0;
        int minimum = 10;
        int maximum = 30;
        String text = "Type a number between "   minimum   " and "   maximum;
        
        System.out.println("How many dice do you want me to throw?");
        System.out.println(text);
        
        do {
            numberOfTosses = valueOf(scanner.nextLine().trim());
            if (!isNumberInRange(numberOfTosses, minimum, maximum)) {
                System.out.println(text);
            }
        } while (!isNumberInRange(numberOfTosses, minimum, maximum));
        
        return numberOfTosses;
    }
    
    private boolean isNumberInRange(int number, int minimum, int maximum) {
        return number >= minimum && number <= maximum;
    }
    
    private int valueOf(String line) {
        try {
            return Integer.valueOf(line);
        } catch (NumberFormatException e) {
            return 0;
        }
    }
    
    private int[] createRandomArray(Random random, int numberOfTosses) {
         int[] dieToss = new int[numberOfTosses];
         
         for (int i = 0; i < dieToss.length; i  ) {
             dieToss[i] = 1   random.nextInt(6);
         }
         
         return dieToss;
    }
    
    private int[] findLargestDuplicates(int[] dieToss) {
        int startIndex = 0;
        int duplicateValue = 0;
        int duplicateCount = 0;
        int maximumStartIndex = 0;
        int maximumEndIndex = 0;
        int maximumDuplicateCount = 0;
        
        for (int index = 0; index < dieToss.length; index  ) {
            if (dieToss[index] == duplicateValue) {
                duplicateCount  ;
                if (index >= (dieToss.length - 1)) {
                    if (duplicateCount > maximumDuplicateCount) {
                        maximumStartIndex = startIndex;
                        maximumEndIndex = index;
                        maximumDuplicateCount = duplicateCount;
                    }
                }
            } else {
                if (index < (dieToss.length - 1)) {
                    if (duplicateCount > maximumDuplicateCount) {
                        maximumStartIndex = startIndex;
                        maximumEndIndex = index - 1;
                        maximumDuplicateCount = duplicateCount;
                    }
                } else {
                    if (duplicateCount > maximumDuplicateCount) {
                        maximumStartIndex = startIndex;
                        maximumEndIndex = index;
                        maximumDuplicateCount = duplicateCount;
                    }
                    
                    if (maximumEndIndex < maximumStartIndex) {
                        maximumEndIndex = index;
                    }
                }
                
                startIndex = index;
                duplicateValue = dieToss[index];
                duplicateCount = 1;
            }
        }
        
        int[] indexes = { maximumStartIndex, maximumEndIndex };
        return indexes;
    }
    
    private String generateOutput(String prefix, int[] dieToss) {
        return prefix   Arrays.toString(dieToss);
    }
    
    private String generateOutput(String prefix, int[] dieToss, int[] indexes) {
        String output = prefix   "[";
        for (int index = 0; index < dieToss.length; index  ) {
            if (indexes[0] == index) {
                output  = "(";
            }

            output  = dieToss[index];
            if (indexes[1] == index) {
                output  = ")";
            }

            if (index < (dieToss.length - 1)) {
                output  = ", ";
            }
        }
        output  = "]";
        
        return output;
    }
    
    private boolean continueRolling(Scanner scanner) {
        System.out.println("Would you like to reroll (y or n)?");
        String line = scanner.nextLine().trim();
        if (line.length() > 0) {
            char c = Character.toLowerCase(line.charAt(0));
            return c == 'y';
        } else {
            return false;
        }
    }

}
  • Related