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++ F…



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;
    }
}
  • 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;

}
  • 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.

Print Share Comment Cite Upload Translate
APA
Tristan Elliott | Sciencx (2024-03-28T22:44:32+00:00) » Game setup in C++. add() and remove() methods. Retrieved from https://www.scien.cx/2021/09/02/game-setup-in-c-add-and-remove-methods/.
MLA
" » Game setup in C++. add() and remove() methods." Tristan Elliott | Sciencx - Thursday September 2, 2021, https://www.scien.cx/2021/09/02/game-setup-in-c-add-and-remove-methods/
HARVARD
Tristan Elliott | Sciencx Thursday September 2, 2021 » Game setup in C++. add() and remove() methods., viewed 2024-03-28T22:44:32+00:00,<https://www.scien.cx/2021/09/02/game-setup-in-c-add-and-remove-methods/>
VANCOUVER
Tristan Elliott | Sciencx - » Game setup in C++. add() and remove() methods. [Internet]. [Accessed 2024-03-28T22:44:32+00:00]. Available from: https://www.scien.cx/2021/09/02/game-setup-in-c-add-and-remove-methods/
CHICAGO
" » Game setup in C++. add() and remove() methods." Tristan Elliott | Sciencx - Accessed 2024-03-28T22:44:32+00:00. https://www.scien.cx/2021/09/02/game-setup-in-c-add-and-remove-methods/
IEEE
" » Game setup in C++. add() and remove() methods." Tristan Elliott | Sciencx [Online]. Available: https://www.scien.cx/2021/09/02/game-setup-in-c-add-and-remove-methods/. [Accessed: 2024-03-28T22:44:32+00:00]
rf:citation
» Game setup in C++. add() and remove() methods | Tristan Elliott | Sciencx | https://www.scien.cx/2021/09/02/game-setup-in-c-add-and-remove-methods/ | 2024-03-28T22:44:32+00:00
https://github.com/addpipe/simple-recorderjs-demo