Answer to Question #262495 in Java | JSP | JSF for masedi moncho

Question #262495

Create a program of water sort puzzle playing the game

1
Expert's answer
2021-11-07T15:54:46-0500

MAIN IDEA:

In order to check if a game is completed one needs to check if all the colours in a bottle are the same. A generic version of this method should be added to the MyArrayList class called:

public boolean checkUniform()•The method should return true if all the filled entries are identical.•Make sure you have a accessor for the instance variable called:•public int getSize()

Make the following Additions to the StackAsMyArrayList class

•As you know you may only use a few actions of a stack: Push and Pop but there is another common one called Peek. Peek returns the value of the top element without removing it.•You need to create a Peek method:•public E peek() [Make sure you understand the difference between Peek and Pop]•We are going to add 2 non-typical stack methods [just to make this cool game work!]public int getStackSize()  which calls the getSize() method of the MyArrayList class•public boolean checkStackUniform() which calls the checkUniform() method of the MyArrayList class

HINT: The toString() of the stack class calls the toString() of the MyArrayList class

Create a test class containing a main() method - Call it Watersort

•The test program first should create single bottle (StackAsArrayList).•Create objects of the character glass called red, green and blue.•Use the following type of static variables:

static Character red= new Character('r');

•Put ink in the bottles (Push character objects onto the stack)•Test the getStackSize() and CheckStackUniform() methods thoroughly.

•You can either use and array or a linked list. In this case the direct access of an array is much easier to use. So you need an array of 5 bottles. [Call the array bottles]. We are using the simple built-in array of Java NOT our own MyArrayList class. [We want direct access without using accessors and mutators.]

You can add ink to your bottles - take care not to spill ink!•Write a ShowAll() method which displays the content of all the bottles•Fill the bottles with ink to make sure your ShowAll works well!•Make use of the toString() method in the stack class - remember that because your bottles are in an array you can use a for-loop

IMPORTANT NOTE: We are not going to start with 2 empty bottles, but rather with 8 empty slots spread over the 5 bottles as explained in the intro video.

Start with 5 empty bottles. Use a random number generator to fill bottles one slot at a time with the colours while keeping 8 slots empty - there is a total of 5*4 = 20 slots - so if we will 12 slots, 8 are free.

•Advantage: Easy to create a puzzle with a good mix•Disadvantage: With more colours and bottles and only 2 open bottles for more advanced versions of the game, the result might not be solvable.

Start with three sorted bottles. In the strategy the idea is to load three bottles with uniform colour and then move ink around for a number of moves until the bottles are mixed up.

•Advantage: Result is always solvable since the bottles are created in a reversed-game strategy.•Disadvantage: It is hard to develop an algorithm which will reach the bottoms of the bottles. We tried this by moving on item from every bottle in rounds. But it still took more than 100 moves to obtain a good mix in the bottles.

Start with three sorted bottles. In the strategy the idea is to load three bottles with uniform colour and then move ink around for a number of moves until the bottles are mixed up.

•Advantage: Result is always solvable since the bottles are created in a reversed-game strategy.•Disadvantage: It is hard to develop an algorithm which will reach the bottoms of the bottles. We tried this by moving on item from every bottle in rounds. But it still took more than 100 moves to obtain a good mix in the bottles.

•You only need to submit one of the two strategies. Your program should have output of 5 scrambled bottles.

•To start off: create a method called

public static boolean solved( StackAsMyArrayList bottles[])

Which checks if a the game is solved.

•When you have the basic game working, you can try implement finer rules such as that one that if there is enough space in the target bottle and the source bottle has 2 adjacent similar colour spots, both will be poured.

public class StackAsMyArrayList<E> 
{   
        MyArrayList<E> theStack;
    public StackAsMyArrayList()
    {  theStack = new MyArrayList<E>();       
    }
        
    public void push(E newElement) //insert at end of array!
    {  
                   if (!theStack.checkSpace())
                   throw new IndexOutOfBoundsException
                    ("Stack out of bounds");
                   theStack.add(theStack.getSize(),newElement);
    }
        
        public E pop() //remove end of array
    {  
                E temp = null;
                
                boolean isDone = false;
                if (theStack.getSize() > 0)
                        temp=theStack.remove(theStack.getSize()-1);
                return temp; // temp will be null in special case of empty list
    }
    
        public String toString()
        {
                return theStack.toString();
        }
   
}//end class
 
public class MyArrayList<E> 
{
  private int size; // Number of elements in the list
  private E[] data;
  private int MAXELEMENTS = 100;
  /** Create an empty list */
  public MyArrayList() {
           data = (E[])new Object[MAXELEMENTS];// cannot create array of generics
       size = 0; // Number of elements in the list
  }
  
  public int getMAXELEMENTS(){
          return MAXELEMENTS;
  }
          
   
  
  public boolean checkSpace()
  {
          if (size+1<MAXELEMENTS)
                  return true;
          else
                  return false;
  }
  
  public void add(int index, E e) {   
    // Ensure the index is in the right range
    if (index < 0 || index > size)
      throw new IndexOutOfBoundsException
        ("Index: " + index + ", Size: " + size); 
    // Move the elements to the right after the specified index
    for (int i = size - 1; i >= index; i--)
      data[i + 1] = data[i];
    // Insert new element to data[index]
    data[index] = e;
    // Increase size by 1
    size++;
  }

  public boolean contains(Object e) {
    for (int i = 0; i < size; i++)
      if (e.equals(data[i])) return true;
    return false;
  }

  public E get(int index) {
    if (index < 0 || index >= size)
      throw new IndexOutOfBoundsException
        ("Index: " + index + ", Size: " + size);
    return data[index];
  }
  
  public E remove(int index) {
        if (index < 0 || index >= size)
      throw new IndexOutOfBoundsException
        ("Index: " + index + ", Size: " + size);
    E e = data[index];
    // Shift data to the left
    for (int j = index; j < size - 1; j++)
      data[j] = data[j + 1];
    data[size - 1] = null; // This element is now null
    // Decrement size
    size--;
    return e;
  }
  
  public void clear()
  {
     size = 0;
  }
 
  public MyArrayList<E> merge(MyArrayList<E> param)
  {
          int i=0; //counter in calling array
          int j=0; // counter in param array
          int k=0; // counter in return array
          MyArrayList<E> returnArray = new MyArrayList();
          
          if (this.getSize() ==0) // same as if (size==0)
                  return param;
          if (param.getSize()==0)
                  return this;
          if ((this.getSize()+ param.getSize()) > MAXELEMENTS)
                   throw new IndexOutOfBoundsException
        ("Combined list out of bounds");
                
          // traverse both list until one list is completely done
          while (i<this.getSize() && j<param.getSize())
          {
                  // Compare single value from each list and copy smallest into result
                  if (((Comparable)data[i]).compareTo(param.data[j]) <0)
                  {
                        returnArray.data[k]= this.data[i];
                        k++;
                        i++;    
                  }
                  else
                  {
                        returnArray.data[k]=param.data[j];
                        k++;
                        j++;
                  }
          }
          
          // copy remainder of the array
          if (i < this.getSize())
          {
                  for (i=i;i<getSize();i++) //for starts at current position
                  {
                        returnArray.data[k]= this.data[i];
                        k++;
                  }
          }
          if (j < param.getSize())
          {
                  for (j=j;j<param.getSize();j++)
                  {
                        returnArray.data[k]=param.data[j];
                        k++;
                  }
          }
          returnArray.size = k; // set size of return array
          return returnArray;
  }               
                  
          
  public String toString() {
    String result="[";
    for (int i = 0; i < size; i++) {
      result+= data[i];
      if (i < size - 1) result+=", ";
    }
    return result.toString() + "]";
  }

  
  public int getSize() {
    return size;
  }
  
 public boolean sortList() {
    E hold;
        for (int i = 0; i < size-1; i++)
         {
           for (int j = 0; j<size-1; j++)
            {    
             if(((Comparable)data[j]).compareTo(data[j+1])>0)
              {
               hold= data[j+1];
               data[j+1]=data[j];
               data[j]=hold;
              }       
           }
     } 
         return true;           
  }


 
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS