Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
lecture_code
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Tyra.Eide
lecture_code
Commits
e890d6f1
Commit
e890d6f1
authored
1 year ago
by
Martin Vatshelle
Browse files
Options
Downloads
Patches
Plain Diff
Code to be used in lecture
parent
36a56d87
Branches
main
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/lecture6_interface/Pokemon.java
+81
-0
81 additions, 0 deletions
src/main/java/lecture6_interface/Pokemon.java
with
81 additions
and
0 deletions
src/main/java/lecture6_interface/Pokemon.java
0 → 100644
+
81
−
0
View file @
e890d6f1
package
lecture6_interface
;
public
class
Pokemon
{
String
name
;
int
healthPoints
;
int
strength
;
// Constructor
Pokemon
(
String
name
,
int
strength
){
this
.
name
=
name
;
this
.
healthPoints
=
100
;
this
.
strength
=
strength
;
}
/**
* Get name of the pokémon
* @return name of pokémon
*/
String
getName
()
{
return
name
;
}
/**
* Get strength of the pokémon
* @return strength of pokémon
*/
int
getStrength
()
{
return
strength
;
}
/**
* Get current health points of pokémon
* @return current HP of pokémon
*/
int
getCurrentHP
()
{
return
healthPoints
;
}
/**
* Check if the pokémon is alive.
* A pokemon is alive if current HP is higher than 0
* @return true if current HP > 0, false if not
*/
boolean
isAlive
()
{
return
healthPoints
>
0
;
}
/**
* Damage the pokémon. This method reduces the number of
* health points the pokémon has by <code>damageTaken</code>.
* If <code>damageTaken</code> is higher than the number of current
* health points then set current HP to 0.
*
* @param damageTaken
*/
void
damage
(
int
damageTaken
)
{
healthPoints
-=
damageTaken
;
if
(
healthPoints
<
0
)
healthPoints
=
0
;
}
/**
* Attack another pokémon. The method conducts an attack by <code>this</code>
* on <code>target</code>. Calculate the damage using the pokémons strength
* and a random element. Reduce <code>target</code>s health.
*
* If <code>target</code> has 0 HP then print that it was defeated.
*
* @param target pokémon that is being attacked
*/
void
attack
(
Pokemon
target
)
{
int
dmg
=
Math
.
max
(
this
.
strength
-
target
.
strength
,
1
);
target
.
damage
(
dmg
);
}
@Override
public
String
toString
()
{
return
name
+
" has "
+
healthPoints
+
" left."
;
}
}
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