Question

In: Computer Science

Create a Java8 Program to play Bulgarian Solitaire: Use an Assertion Use Array and/or ArrayList Display...

Create a Java8 Program to play Bulgarian Solitaire:

Use an Assertion

Use Array and/or ArrayList

Display every turn/iteration of this game. Simple print statements with asterisks is fine.

Solutions

Expert Solution

// Working Code :-

public class BulgarianSolitaire {
  
public static void printArray(String header, int[] array, int size)
  {
    System.out.print(header);
    for(int i=0; i<size; i++)
      System.out.printf("%d ", array[i]);
    System.out.println();
  }
public static void sort(int[] arr, int cs){
    for(int i=0; i<cs; i++)
    {
      for(int j=i; j<cs; j++)
      {
        if(arr[j]<arr[i])
        {
          int temp = arr[i];
          arr[i] = arr[j];
          arr[j] = temp;
        }
      }
    }
  }
public static boolean checkSame(int[] a, int[] goal){
    
    for(int i=0; i<goal.length; i++)
    {
      if(a[i] != goal[i])
        return false;
    }
    
    return true;
}
  public static int removeZero(int[] A, int cs) {
    for(int i=0; i<cs; i++)
    {
      if(A[i]==0 && A[cs-1]!=0)
      {
        A[i] = A[cs-1];
        cs--;
      }
      if(A[cs-1]==0)
      {
        cs--;
        i--;
      }
    }
    return cs;
  }
  
  public static void main(String[] args){
    
    int[] goal = {1,2,3,4,5,6,7,8,9};
    int[] A = new int[45];
    int total = 45;
    int currentSize = 0;
    int counter=0;
    for(int i=0; total>0; i++)
    {
      int rn = (int) (Math.random()*total)+1;
      total -= rn;
      A[i] = rn;
      currentSize++;
    }
    printArray("Initial pile: ", A, currentSize);
    System.out.println("Initial # of piles: " + currentSize);
    
    //All set ! start the ame by picking one card from each pile
    while(true)
    {
      int newPile=0;
      for(int i=0; i<currentSize ; i++)
      { 
          //Now remove one card from the pile
        A[i] = A[i]-1; 
        newPile++;      
      }
      
      currentSize++;
      //build new pile from removed cards
      A[currentSize-1] = newPile; 
      
      //remove any zero-ed out piles and get size of trimmed array
      currentSize = removeZero(A,currentSize);
      
      printArray("Intermediate pile: ", A, currentSize);
      counter++; 
      //Counter keeps track of number of solitaire steps
      
      //condition to check if solitary config has reached
      if(currentSize==9)
      {
        sort(A,currentSize);
        boolean done = checkSame(A, goal);
        //needs to exit if achieved
        if(done) 
        {
          System.out.println("***Solitaire Config Reached!!***");
          break;
        }       
      }     
    }
    
    printArray("Final Array Sorted: ", A, currentSize); 
    System.out.println("Number of solitaire steps needed: " + counter);
  }
}

//Output :-


Related Solutions

I want the program to use 1- D array and display a table as output. Write...
I want the program to use 1- D array and display a table as output. Write a script to simulate the rolling of two dice. The script should use Math.random to roll the first die and again to roll the second die. The sum of the two values should then be calculated. [Note: Since each die can show an integer value from 1 to 6, the sum of the values will vary from 2 to 12, with 7 being the...
Create a C++ program that makes use of both concepts i.e. Array of structure and array...
Create a C++ program that makes use of both concepts i.e. Array of structure and array within the structure by using the following guidelines: 1. Create an Array of 5 Structures of Student Records 2. Each structure must contain: a. An array of Full Name of Student b. Registration Number in proper Format i.e 18-SE-24 c. An array of Marks of 3 subjects d. Display that information of all students in Ascending order using “Name” e. Search a particular student...
Pandas exercises: 1. Write a python program using Pandas to create and display a one-dimensional array-like...
Pandas exercises: 1. Write a python program using Pandas to create and display a one-dimensional array-like object containing an array of data. 2. Write a python program using Pandas to convert a Panda module Series to Python list and print it's type. (Hint: use ds.tolist() to convert the pandas series ds to a list) 3. Create a pandas dataframe called **my_df** with index as integer numbers between 0 to 10, first column (titled "rnd_int") as 10 integer random numbers between...
Create a program that calculates the average of 3 test scores. Make use of an array...
Create a program that calculates the average of 3 test scores. Make use of an array to store the integer scores. const int size = 3; int testScores[size]; Send this array to a function that actually calculates and returns the average. 1. Tell the user what the program does. 2. Prompt the user to enter the integer scores. ( Use a for loop to do this. ) 3. Create and implement a function with prototype: double average( int a[], int...
Using an ArrayList, create a program which does the following: If the user enters quit, the...
Using an ArrayList, create a program which does the following: If the user enters quit, the program ends. If the user enters add followed by a string, program adds the string only to your ArrayList. If the user enters print, the program prints out the ArrayList contents. If the user enters size, the program prints out size of ArrayList, If the user enters remove followed by string, it removes all strings. interface example: **** Welcome to my List Program ****...
Create a program in java with the following information: Design a program that uses an array...
Create a program in java with the following information: Design a program that uses an array with specified values to display the following: The lowest number in the array The highest number in the array The total of the numbers in the array The average of the numbers in the array Initialize an array with these specific 20 numbers: 26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51 example...
In C create an array of 4 integers. Assign a pointer to the array. Use the...
In C create an array of 4 integers. Assign a pointer to the array. Use the pointer to find the average value of the elements in the array and display the results on the screen.
This is c++ Create a program where you create, display, search, delete elements within a linked...
This is c++ Create a program where you create, display, search, delete elements within a linked list. Watch your function arguments. Pointer parameters are passed by reference to some functions and by value to others. Functions to make: copy : copies element in linked list destroy all: destroys all elements in linked list wherethisgoes:user  enters element and returns where the element is located insert sorted: inserts element create using linked lists with a head pointer and so forth
c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
Java Program Use for loop 1.) Write a program to display the multiplication table of a...
Java Program Use for loop 1.) Write a program to display the multiplication table of a given integer. Multiplier and number of terms (multiplicand) must be user's input. Sample output: Enter the Multiplier: 5 Enter the number of terms: 3 5x0=0 5x1=5 5x2=10 5x3=15 2 Create a program that will allow the user to input an integer and display the sum of squares from 1 to n. Example, the sum of squares for 10 is as follows: (do not use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT