Comp 116 Programming Assignment 06:
Asciimation

Due 11:59pm Wednesday, November 7 2018

This is an individual assignment. You must complete this lab on your own, although you may discuss lab concepts with other students. Please keep the Academic Integrity Policy in mind---do not show your code to anyone outside of the professors and ninjas, and do not look at anyone else's code for this lab. If you need help, please post a question on Piazza, or contact the professor.

The main goal of this lab is to gain practice using and implementing abstract data types (ADTs). To that end, you will complete a Singly Linked List with Tail implementation of the List ADT. You will then use these to create and play animated movies using a format called ASCIImation. Concepts you will be familiar with after this lab include:

Note: A significant portion of the credit for this lab is assigned to the correctness of your linked list. For that reason, be sure to develop your linked list and test all the methods thoroughly before working on the main application.

Introduction

ASCII, ASCII art, ASCII animation

ASCII is an abbreviation for the American Standard Code for Information Interchange. In brief, the ASCII table picks a numeric representation for each of a set of characters; for instance, the letter 'A' is given the code 65, 'B' is 66, and '^' is 94. Text is stored in a computer as a sequence of numbers and then, when displayed, is translated from these numbers into visual representations of characters by tables like the ASCII table. Until recently, most text stored on computers was encoded in ASCII.

ASCII art is a term for the practice of using text to create images.

  31
                 -===                    `"',
                ""o o                    O O|)   Going somewhere,
                _\ -/_                  _\o/ _        Solo?
               /| || |\                /|\ / |\
              //| || |\\              //| |  |\\
             || | || | \\            // | |  | ||
             || |/  \|  \\        -==#  | |  | ||
             |! |====|   ')         '\  |====| /#
             =] |/|| |                  | || | ''
              I ( )( )                  | || |
                |-||-|                  | || |
                | || |                  | || |
________________[_][__\________________/__)(_)_____________________

The use of printed text to form pictures is older than the ASCII standard. The term “ASCII art” gained popularity as the practice found its way onto computerized devices which used the ASCII table to standardize text representation. ASCIImation is the practice of using a repeated sequence of these textual images to create an animation using ASCII art. For this lab, you will be writing an ASCIImation player.

Program Requirements

First, download the .zip file that contains the starter code for the fifth programming assignment. It contains the code the files you will have to complete for this assignment. Unzip the folder it contains in your CLionProjects folder and turn it into a CLion Project using the Import Project from Sources feature of CLion. If CLion automatically opens in your previous project, you may need to close that project (File->Close) Project to get back to the CLion welcome screen.

Singly Linked List with Tail

The first (and most important) part of this assignment is to implement the SLLwT class (Singly Linked List with Tail); the declaration of that class appears in SLLwT.h. You will implement all of the methods in SLLwT.cpp.

Note: For this lab, you will not be penalized for memory leaks, but your code must be completely free of memory errors (e.g. using uninitialized pointers or pointers to memory you have deleted). And of course, you should do your best to ensure that as much of the dynamic memory you used is eventually reclaimed.

Your implementation of the SLLwT class must meet the following complexity requirements:

The pair Class

The pair class is part of the C++ standard template library (STL). It is defined in the the utility library and acts as a simple container of two values. We write pair<T1,T2> to create an object of two values. The first value has type T1; the second has type T2. For isntance, a pair<int,string> is an object with a field first of type int and a field second of type string.

The pair class knows how to make copies of itself by assignment; that is, you can use = assignment with a pair just like you would with an int. Consider the following code:

pair<int,string> p1(4, "apple"); // create an int*string pair
pair<int,string> p2 = p1;        // copy the values from p1 into p2
p1.first = 5;                    // change p1's integer to 5
cout << p2.first << endl;        // prints 4, since p2's int is a different variable
pair<int,string>* ptr1 = new pair<int,string>(8,"orange"); // dynamically allocate a pair
pair<int,string>* ptr2 = ptr1;   // this copies the *pointer*, not the pair
ptr1->first = 10;                // change the dynamically-allocated object's integer to 10
cout << ptr2->first << endl;     // prints 10, since both pointers point to the same pair object

In the above, p1 and p2 are statically-allocated objects. Although none of the statically allocated objects we have used so far have been copyable (other than string objects), pair objects can be copied. Note how copying a pair object is different from copying a pair pointer. In this lab, you won’t need any pair pointers, so you don’t really need to worry about that case.

Programming ASCIImation

The second part of your lab is to implement the asciimation program, which will read ASCIImation videos from a file and play them. Your program should first ask the user for the name of the animation file. Then, it should ask if the user wants to play the movie in reverse. Then, your program should load the appropriate movie file and play it, either as normal or in reverse. If the file name entered by the user is invalid, the program should display an error message and terminate.

For instance, this command plays the smiley video:

$ ./asciimation
animation file: smiley.asciimation
reverse list? no

You can play the movie in reverse simply by reversing the contents of your list before playing the video. Reversing the list should require $O(n)$ time, and you are allowed to destroy the original list in the process of creating the reversed list.

The ASCIImation Format

The file asciimation.zip contains several ASCIImation files with the file extension .asciimation. These files contain the text representing different scenes in the animation. Copy these files in the cmake-build-debug folder of your project.

Our animations will run at 15 frames per second; that is, your program will need to display a frame, wait for \(\frac{1}{15}\) of a second, and then clear the screen and display the next frame.

One way to accomplish this is to use the function

this_thread::sleep_for(chrono::microseconds(usec));
where usec is the number of microseconds to sleep. To use this, you need to include the libraries thread and chrono. For instance, this_thread::sleep_for(chrono::microseconds(1000000/15)) will sleep for about $(\frac{1}{15})$ seconds. After sleeping, you can clear the screen using system("clear"); (which will do the same as typing the command "clear" in the terminal). Note that this will only work in an actual terminal window, I will show you in lab how to run the program properly.

The .asciimation files themselves contain groups of 14 lines each. The first line of each group indicates a frame count while the remaining lines are the contents of the frame. (That is, each frame is 13 lines tall.) The frame count exists to reduce the size of the file, since we often want frames which do not change for a second or more. For instance, the following .asciimation file would display a smiley face for two seconds (30 frames). The smiley would then close its eyes for the next second (15 frames).

30
    ~~~~~~~~~~~~~~
   /              \
  /   ~_~    ~_~   \
 /    / \    / \    \
|    | * |  | * |    |
|     \_/    \_/     |
|                    |
|         ~~         |
|    \          /    |
 \    \________/    /
  \                /
   \              /
    --------------
15
    ~~~~~~~~~~~~~~
   /              \
  /   ~~~    ~~~   \
 /                  \
|    -===-  -===-    |
|                    |
|                    |
|         ~~         |
|    \          /    |
 \    \________/    /
  \                /
   \              /
    --------------

When you read the file, you should read it into a list of pairs. The number of frames in the ASCIImation is not indicated in the file, so you should read the file until you reach the end-of-file marker.

Read the comments about the two functions in asciimationFunctions.h. Your implementation should follow the behavior dictated in these comments exactly.

The function playMovie should run in $O(n)$ time, but is allowed to destroy the list containing the movie while playing it. If you want an extra challenge, use the iterator idea discussed in class to implement playMovie in $O(n)$ time in such a way that it does not destroy the list it is given as input.

Commenting and Coding Style Requirements
For this and future labs, I will assign minor penalties for poor commenting and coding style. Here are some style tips for this lab:
Submit

Submit a .zip file containing the entire project folder for the programming assignment using the submission link on onCourse.

Acknowledgement

This lab write up is based off of the ASCIImation lab developped by the Computer Science Department of Swarthmore College.