Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Lab2.02/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
3 changes: 3 additions & 0 deletions Lab2.02/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Lab2.02/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Lab2.02/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Lab2.02/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions Lab2.02/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>Ironhack.schl</groupId>
<artifactId>Lab2.02</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
10 changes: 10 additions & 0 deletions Lab2.02/src/main/java/Ironhack/schl/ConcretePlayer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package Ironhack.schl;

public class ConcretePlayer extends Player {

public ConcretePlayer(int health, int attackPower, int lives) {
super(health, attackPower, lives);
}

// Implement any abstract methods from the Player class here
}
19 changes: 19 additions & 0 deletions Lab2.02/src/main/java/Ironhack/schl/Elf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package Ironhack.schl;

public class Elf extends Player{
private int speed;

public Elf(int health, int strength, int lives, int speed) {
super(health, strength, lives);
this.speed = speed;
}

public int getSpeed() {
return speed;
}

public void setSpeed(int speed) {
this.speed = speed;
}

}
25 changes: 25 additions & 0 deletions Lab2.02/src/main/java/Ironhack/schl/JavaKeywordsChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package Ironhack.schl;

import java.util.Arrays;
import java.util.List;

public class JavaKeywordsChecker {

private static final List<String> JAVA_KEYWORDS = Arrays.asList(
"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const",
"continue", "default", "do", "double", "else", "enum", "extends", "final", "finally", "float",
"for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native",
"new", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super",
"switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while"
);

public boolean containsJavaKeywords(String input) {
String[] words = input.split("\\s+");
for (String word : words) {
if (JAVA_KEYWORDS.contains(word)) {
return true;
}
}
return false;
}
}
18 changes: 18 additions & 0 deletions Lab2.02/src/main/java/Ironhack/schl/OddIntegers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package Ironhack.schl;
public class OddIntegers {

public int[] getOddIntegers(int n) {
if (n < 0) {
return new int[0];
}

int count = n / 2 + (n % 2 == 1 ? 1 : 0);
int[] oddIntegers = new int[count];

for (int i = 0; i < count; i++) {
oddIntegers[i] = 2 * i + 1;
}

return oddIntegers;
}
}
67 changes: 67 additions & 0 deletions Lab2.02/src/main/java/Ironhack/schl/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package Ironhack.schl;

public abstract class Player {
private int health;
private int strength;
private int lives;

//Constructor

public Player(int health, int strength, int lives) {
this.health = health;
this.strength = strength;
this.lives = lives;
}

// Getters and setters

public int getHealth() {
return health;
}

public int getStrength() {
return strength;
}

public int getLives() {
return lives;
}

public void setStrength(int strength) {
this.strength = strength;
}

public void setHealth(int health) {
this.health = health;
}

public void setLives(int lives) {
this.lives = lives;
}

public void decrementLive() {
lives--;
if (lives > 0) {
restoreHealth();
System.out.println("You have " + lives + " lives left");
} else {
System.out.println("This character died.");
}
}

public void checkHealth() {
if (health <= 0) {
decrementLive();
}
}

public void restoreHealth() {
this.health = 100;

}

public void attack(Player playerToAttack) {
playerToAttack.setHealth(playerToAttack.getHealth() - strength);
}

}
23 changes: 23 additions & 0 deletions Lab2.02/src/main/java/Ironhack/schl/Warrior.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package Ironhack.schl;

public class Warrior extends Player {
private int force;

public Warrior(int health, int strength, int lives, int force) {
super(health, strength, lives);
this.force = force;
}

public int getForce() {
return force;
}

public void setForce(int force) {
this.force = force;
}

public Elf convertToElf() {

return new Elf(getHealth(), getStrength(), getLives(), 10);
}
}
24 changes: 24 additions & 0 deletions Lab2.02/src/main/java/Ironhack/schl/Wizard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package Ironhack.schl;

public class Wizard extends Player{
private String spell;

public Wizard(int health, int strength, int lives, String spell) {
super(health, strength, lives);
this.spell = spell;
}

public void setSpell(String spell) {
this.spell = spell;

}

public String getSpell() {
return spell;
}

public Elf convertToElf() {

return new Elf(getHealth(), getStrength(), getLives(), 10);
}
}
6 changes: 6 additions & 0 deletions Lab2.02/src/test/java/ElfTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class ElfTest {
// No tests to address
}
29 changes: 29 additions & 0 deletions Lab2.02/src/test/java/OddIntegersTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

import Ironhack.schl.OddIntegers;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class OddIntegersTest {

@Test
public void testOddIntegers_nIs10() {
OddIntegers oddIntegers = new OddIntegers();
int[] expected = {1, 3, 5, 7, 9};
assertArrayEquals(expected, oddIntegers.getOddIntegers(10));
}

@Test
public void testOddIntegers_nIs0() {
OddIntegers oddIntegers = new OddIntegers();
int[] expected = {};
assertArrayEquals(expected, oddIntegers.getOddIntegers(0));
}

@Test
public void testOddIntegers_nIsNegative() {
OddIntegers oddIntegers = new OddIntegers();
int[] expected = {};
assertArrayEquals(expected, oddIntegers.getOddIntegers(-5));
}

}
40 changes: 40 additions & 0 deletions Lab2.02/src/test/java/PlayerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Ironhack.schl.ConcretePlayer;
import Ironhack.schl.Player;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class PlayerTest {

@Test
public void testDecrementLive() {
Player player = new ConcretePlayer(100, 20, 3);
player.decrementLive();
assertEquals(2, player.getLives());
assertEquals(100, player.getHealth());

player.decrementLive();
assertEquals(1, player.getLives());
assertEquals(100, player.getHealth());

player.decrementLive();
assertEquals(0, player.getLives());
System.out.println("This character died.");
}

@Test
public void testAttack() {
ConcretePlayer player1 = new ConcretePlayer(100, 20, 3);
ConcretePlayer player2 = new ConcretePlayer(80, 15, 2);

player1.attack(player2);
assertEquals(60, player2.getHealth());
}

@Test
public void testCheckHealth() {
ConcretePlayer player = new ConcretePlayer(0, 20, 3);
player.checkHealth();
assertEquals(2, player.getLives());
assertEquals(100, player.getHealth());
}
}
Loading