DEV Community

Cover image for Game setup in C++. add() and remove() methods
Tristan Elliott
Tristan Elliott

Posted on

Game setup in C++. add() and remove() methods

Introduction

  • This series is going to be dedicated to first understanding the basics of C++ syntax and then evolve into learning data structures and algorithms in C++. For this series I will be reading Data Structures and algorithms in C++ Fourth edition by Adam Drozek. The book is very expensive on Amazon by this is the internet, so I am sure you can find a cheaper PDF version.

quick note

  • I will be continuing on from my last post where we built a GameEntry and a Score class. In this section will are going to implement the add() and remove() methods of the Score class.

add(e) method

  • The implementation of the add method is as follows
void Scores::add(const GameEntry& e) {
    int newScore = e.getScore();
    if (numEntries == maxEntries) {
        if (newScore <= entries[numEntries - 1].getScore()) {
            return;
        }
    }
    else {
        numEntries++;

        int i = numEntries - 2; // the second to last element
        while (i >= 0 && newScore > entries[i].getScore()) {
            entries[i + 1] = entries[i];
            i--;
        }
        entries[i + 1] = e;
    }
}
Enter fullscreen mode Exit fullscreen mode
  • The basic logic of the code above follows these steps:

1) : Check to see if the array is already full.

2) : If the array is full, then check whether the score at the last entry in the array(which is as entries[maxEntries -1]) is at least as large as the newScore.

3) : If the newScore is not larger than the last element's score, then we return immediately since newScore is not high enough to replace any of the existing high scores.

4) : If the array is not full yet, we know that newScore will be added to the array so increment the numEntries value.

5) : Identify all entries whose scores are smaller than newScore's score and shift them one entry to the right to avoid overwriting existing array entries

6) : The loop continuously until we encounter an entry whose score is not smaller than newScore's score or we fall off the frontend of the array.

remove(e) method

  • The implementation of the remove method is as follows:
GameEntry Scores::remove(int i) throw(std::out_of_range){
    if (i < 0 || i > numEntries) {// valid index check
        throw "there is a error";
    }
    GameEntry e = entries[i];
    for (int j = i+1; j < numEntries; j++) {
        entries[j - 1] = entries[i]; //moves everything over;
    }
    numEntries--;
    return e;

}
Enter fullscreen mode Exit fullscreen mode
  • The basic logic of the code above follows these steps:

1) : Check to see if the index i is outside the boundary at the entries array.

2) : If true throw an exception

3) : Otherwise the entries array is updated to remove the object at index i

4) : All objects previously stored at indices higher than i are "shifted left" to fill in for the removed object.

  • With those two methods implemented, we finally have our array class all setup and ready to use.

Conclusion

  • Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.

Top comments (0)