Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CS1PC20 Spring Coursework
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Oscar Hernandez Herrera
CS1PC20 Spring Coursework
Commits
a87507dd
Commit
a87507dd
authored
1 year ago
by
Oscar Hernandez Herrera
Browse files
Options
Downloads
Patches
Plain Diff
Add new file
parent
24096964
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Adventure Game/opponent.cpp
+55
-0
55 additions, 0 deletions
Adventure Game/opponent.cpp
with
55 additions
and
0 deletions
Adventure Game/opponent.cpp
0 → 100644
+
55
−
0
View file @
a87507dd
#include
"Opponent.h"
// Opponent class implementation
Opponent
::
Opponent
(
std
::
string
name
,
int
health
)
:
name
(
name
),
health
(
health
)
{}
std
::
string
Opponent
::
getName
()
{
return
name
;
}
int
Opponent
::
getHealth
()
{
return
health
;
}
void
Opponent
::
setHealth
(
int
newHealth
)
{
health
=
newHealth
;
}
void
Opponent
::
takeDamage
(
int
damage
)
{
health
-=
damage
;
// Ensure health doesn't go below 0
if
(
health
<
0
)
{
health
=
0
;
}
}
// OpponentList class implementation
class
OpponentList
{
private:
std
::
vector
<
Opponent
>
opponents
;
public:
// Constructor not shown for brevity
// Method to add an opponent to the list
void
addOpponent
(
const
Opponent
&
opponent
)
{
opponents
.
push_back
(
opponent
);
}
// Method to remove an opponent from the list by name
void
removeOpponent
(
const
std
::
string
&
opponentName
)
{
for
(
auto
it
=
opponents
.
begin
();
it
!=
opponents
.
end
();
++
it
)
{
if
(
it
->
getName
()
==
opponentName
)
{
opponents
.
erase
(
it
);
return
;
}
}
}
};
// Explanation:
// This code demonstrates two classes: Opponent and OpponentList.
// The Opponent class represents individual opponents in the game and provides methods to interact with them.
// The OpponentList class extends the behavior of Opponent through composition by managing a collection of Opponent objects.
// OpponentList encapsulates a vector of Opponent objects and provides methods to add and remove opponents from the list.
// By using composition, OpponentList achieves modularity and separation of concerns, adhering to the principles of object-oriented design.
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment