DEV Community

Cover image for Magic 8 Ball
Meg
Meg

Posted on • Updated on

Magic 8 Ball

Magic 8 Ball originally was a class project from my most recent Summer course, however the professor required that we not post that code anywhere, so I’ve been refactoring it so it doesn’t match what we originally did, while staying true to the Magic 8 Ball idea.

I started out by searching for the possible options for a real Magic 8 Ball, cleared out the array and added them in. From there I updated the class private variables to remove unneeded variables, and set the default constructor to be empty.

I’ve been working on sticking to the DRY method, “Don’t Repeat Yourself”, so I moved the “checking” code (which evaluates the input by capitalizing and comparing it) into its own function. I also updated the main driver code so that the four other functions call each other, removing the need for a while loop. After the first shake is completed, the three other functions bounce from one to another while the user wants to continue shaking the Magic 8 Ball. Once the user inputs N, the driver code ends.

Wrapping up editing the small project by updating my Class UML, I realized that although I haven’t been coding in C++ since moving on from my most recent college Computer Science course, it really is like riding a bike and comes right back to me when I hop back into it. All in all, I had fun with the original project and enjoyed refactoring this very small game so I hope you enjoy!

Play it: https://replit.com/@Meg-Div/Magic-8-Ball#main.cpp

The Code: https://github.com/Meg-Div/DigitalCraftsProjects/tree/main/Magic8Ball

Top comments (0)