Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Assignment Specifications: Problem: Magical Flask A flask is a narrow - neck glass container used in a laboratory to hold chemical samples. Visualize we are

Assignment Specifications:
Problem: Magical Flask
A flask is a narrow-neck glass container used in a laboratory to hold chemical samples. Visualize we are in an imaginary laboratory with chemicals in liquid form that never mix.
You have flasks that can contain 0 to 4 units of chemicals. Each chemical is represented by a pair of identical letters, such as AA, BB, CC, and so forth. When you have a unit of chemical AA in a flask and pour another chemical BB on top of it, they do not mix. Chemical BB stays on top of chemical AA.
||
||
|BB|
|AA|
+--+
When you pour from a flask that has some unmixed chemicals to another flask, only the top most chemical flows to the next flask. In this imaginary laboratory, you can assume that you either have 3 chemicals and 4 flasks or 6 chemicals and 8 flasks. In either case, there will always be 3 units of each chemical, all distributed randomly in the available flasks. Do not use the literal value of 3 to represent units in your program, define a global constant instead. When a flask has only 3 units of the same chemical, it seals automatically and cant be used anymore. The flask cannot seal if there is a different chemical in the flask. Here is an example of a sealed flask:
+--+
|AA|
|AA|
|AA|
+--+
Your goal is to make a game where you start with chemical units distributed randomly across the flasks. The goal of the game is to do the least number of pouring in order to get all the chemicals in sealed flasks.
Part 1: Implementation
You have several flasks, each with a capacity limit of 4 units. Alongside, you possess a collection of chemicals, where each chemical type is available in quantities of 3 units. The file chemicals.txt is read and chemicals are queued into a bounded queue. From that bounded queue, the chemicals are distributed into flasks. Each flask should be implemented as a bounded stack. The first line of the file indicates the number of flasks and the number of chemicals respectively, separated by a space. After the first line, the text file contains two types of lines:
1: Name of the chemical. For example AA or BB or CC. Each chemical should be enqueued into a bounded queue.
2: F. This means to dequeue x chemicals from the queue, and add them to flask y one at a time.
Here is a sample text file to demonstrate.
32BB
AA
BB
BB
AA
2F1
AA
AA
4F3
For your assignment, use this text file for 4 flasks 3 chemicals and this text file for 8 flasks 6 chemicals.
You must use a bounded queue of size 4 to store the chemical once you have read it from the file. You must check if the queue is full when adding chemicals. If it is full and the text file tells you to add another chemical, discard that chemical and move on to the next line. For the input file given above your bounded queue should look like this:
Bounded queue after each line
[BB]
[BB, AA]
[BB, AA, BB]
[BB, AA, BB, BB]
[BB, AA, BB, BB](Bounded queue is full, so AA is discarded)
[BB, BB](Dequeue 2 chemicals and add to flask 1)
[BB, BB, AA, AA]
[](Dequeue 4 chemicals and add to flask 3)
After reading the file, your flasks should now each contain 0-4 mixed chemicals and should look like this:
|||||AA|
|||||AA|
|AA||||BB|
|BB||||BB|
+--++--++--+
123
Take 2 inputs from the user. The user can enter an int from 1 to the number of flasks for the source and destination flasks, or can enter 'exit' for either at any time to terminate the program. If the user does not enter an int from 1 to the number of flasks or exit, print the error message Invalid input. Try again. If the source flask is empty or sealed, print the error message Cannot pour from that flask. Try again. If your destination flask is full or sealed, print the message Cannot pour into that flask. Try again. Lastly, if the user tries to pour into the same flask theyre pouring out of, print the error message Cannot pour into the same flask. Try again. If all inputs are correct, then the topmost chemical from the source flask will be removed from the source flask, and added to the top of the destination flask. The flask seals after it contains only three of the same chemicals. Flask 1 below should seal, flask 2 should not. Flask two will seal once the top chemical (BB) is removed.
+--+|BB|
|AA||AA|
|AA||AA|
|AA||AA|
+--++--+
12
After taking input, move the chemical as specified by the user, then display your flasks. The flasks should be displayed with at most 4 in one line. If there are more than 4 flasks, the remaining should be displayed in the next row of flasks. There should be 2 spaces between each flask, and the number of the flask should be underneath the second dash of the flask. Should you need 2+ rows to display all the flasks, the rows should be separated by 1 line of space. This is what your entire display with 8 flasks should look like.
Magical Flask Game
Select source flask:
Select destination flask:
||||||||

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Carry out an interview and review its success.

Answered: 1 week ago