Two semesters of C++

05-11-2024

At the end of this week I'll have taken the final exam for CS10B, the second semester of C++ at my college. I found this class significantly harder than the first, but the projects were challenging and taught me a lot about algorithms and the language. Here's some of my learnings + a walkthrough of my favorite project.

Some C++ Basics

C++ is a statically typed and compiled language, meaning code is converted to machine code by a compiler. Faster execution than interpreted languages.

  • C++ supports OOP concepts like classes, objects, inheritance, polymorphism, and encapsulation.

  • Basic Syntax:

    • some data types include int, float, double, char, and bool
    • C++ uses the standard control flow structures like if, else, switch, for, while, and do-while
    • Functions are usually declared before the main() function or in separate files
  • Low-level memory manipulation through pointers, and manual memory management (new and delete). Super controlled and efficient if you know how to use it

  • Templates, use them for "generic" programming and reuse by allowing functions and classes to operate with any data type

  • Error Handling with exceptions (try, catch, throw)

  • Multi-Paradigm Support (procedural, functional, and object-oriented programming paradigms)

Fav project: Conway's Game of Life

Conway's Game of Life is a classic simulation that shows how simple rules can lead to complex outcomes. It's a grid-based game where each cell is either "alive" or "dead," and the state of the grid evolves over time. The rules are pretty straightforward: cells live, die, or come to life depending on how crowded or empty their surroundings are. What's cool is how these simple interactions can create all sorts of unexpected patterns.

  1. boolMatrix.h: The header file defining the boolMatrix class, which represents a grid of boolean values.
  2. boolMatrix.cpp: The implementation file containing the methods for manipulating and accessing the boolean matrix.
  3. client.cpp: The main driver file that initializes the matrix, runs the Game of Life simulation, and displays statistics.

How I made it

Designing the boolMatrix Class

The boolMatrix class is the backbone of the project. It represents the game board as a 2D array where each cell is either "alive" (true) or "dead" (false). Some of the key parts of this class are:

  • Constructor (boolMatrix()): This initializes the entire grid to false, so every cell starts out dead. It sets up a blank slate for the simulation to begin.
  • Getters and Setters (get, set): These functions let you access and modify specific cells in the matrix safely. They also have checks to make sure you're not trying to access a cell that's out of bounds.
  • Counting Functions (rowCount, colCount, totalCount): These help count the number of "alive" cells in a specific row, column, or the whole grid. This is useful for getting a snapshot of how the game is progressing.
  • neighborCount Method: This function counts how many live neighbors a particular cell has. This is a big deal because the Game of Life rules depend entirely on how many neighbors each cell has.
  • print Method: This provides a visual of the matrix, showing live cells as * and dead ones as spaces. It makes it easy to see how patterns evolve over time.

Game Logic

The client.cpp file handles the main game logic and user interaction:

  • initialize Function: Reads the initial setup from a file (lifedata.txt) and sets specific cells to "alive." This way, you can start with any pattern and see how it plays out, making it more flexible to test different scenarios.

  • playGameOfLife Function: This runs the simulation for a certain number of generations. It uses a temporary matrix to calculate the next state without affecting the current one. The rules are simple:

    • A live cell stays alive if it has two or three live neighbors; otherwise, it dies.
    • A dead cell becomes alive only if it has exactly three live neighbors.

    By using a temporary matrix, you avoid messing up the calculations by changing the board while you're still working on it.

  • displayStatistics Function: This gives you a summary after the simulation is done—like how many cells are alive or dead in certain rows or columns. It helps to quickly understand what happened after a few generations.

How to Run it

Create a file named lifedata.txt in the same directory. List the initial "alive" cells as row and column pairs, like this:

1 2
3 4
5 6

Then run. It'll ask for the number of generations you want to simulate.