Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
5 changes: 4 additions & 1 deletion src/main/java/org/example/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@


import org.example.models.*;
import org.example.services.*;

public class Main {
public static void main(String[] args) {

PlayerStatus player = new PlayerStatus();

IServices service = new Services();
PlayerStatus player = new PlayerStatus(service);
player.initPlayer("Jane Doe");
}
}
45 changes: 27 additions & 18 deletions src/main/java/org/example/models/PlayerStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import org.example.services.*;

import static org.example.enums.Weapons.*;
import static org.example.services.Services.*;

public class PlayerStatus {
private static final String GAME_NAME = "UNREAL";
private final IServices service;
private String nickname;
private int score;
private int lives;
Expand All @@ -16,12 +16,16 @@ public class PlayerStatus {
private double positionX;
private double positionY;

public PlayerStatus(IServices service) {
this.service = service;
}

public boolean shouldAttackOpponent(PlayerStatus opponent) {
if (opponent.getWeaponInHand() == weaponInHand) {
return winProbability(opponent, health, score);
return service.myWinProbability(opponent, health, score);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor this to use as parameters (this, opponent) instead.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

solved

} else {
var distance = getDistance(opponent, positionX, positionY);
return winDuel(opponent, distance, weaponInHand);
var distance = service.getDistance(opponent, positionX, positionY);
Comment thread
razvanexu marked this conversation as resolved.
Outdated
return service.winDuel(opponent, distance, weaponInHand);
}
}

Expand All @@ -31,8 +35,9 @@ public WeaponModel getWeaponInHand() {

public void setWeaponInHand(WeaponModel weapon, Weapons weaponType) {
Comment thread
razvanexu marked this conversation as resolved.
Outdated
weaponInHand = new WeaponModel(FIST);
if (!canBuyWeapon(weapon, score)) {
if (service.canBuyWeapon(weapon, score)) {
weaponInHand.setName(weaponType);
this.score -= weapon.getCost();
}
}

Expand Down Expand Up @@ -64,19 +69,23 @@ public int getScore() {
}

public void setScore(int score) {
this.score = Services.updateScoreService(weaponInHand, score);
this.score = score;
}

public int getLives() {
return lives;
}

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

public int getHealth() {
return health;
}

public void setHealth(int health) {
this.health -= health;
this.health += health;
if (this.health < 0) {
lives--;
this.health = 100;
Expand All @@ -100,17 +109,17 @@ public double getPositionY() {
}

public void findArtifactCode(int artifactCode) {
if (isPerfectNumber(artifactCode)) {
score += 5000;
lives++;
health = 100;
} else if (isPrime(artifactCode)) {
score += 1000;
lives += 2;
health += 25;
} else if (isTrap(artifactCode)) {
score -= 3000;
health -= 25;
if (service.isPerfectNumber(artifactCode)) {
setScore(5000);
setLives(1);
setHealth(100);
} else if (service.isPrime(artifactCode)) {
setScore(1000);
setLives(2);
setHealth(25);
} else if (service.isTrap(artifactCode)) {
setScore(-3000);
setHealth(-25);
} else {
score += artifactCode;
}
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/org/example/services/IServices.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.example.services;

import org.example.models.*;

public interface IServices {
boolean canBuyWeapon(WeaponModel weapon, int playerScore);

boolean isPerfectNumber(int artifactCode);

boolean isPrime(int artifact);

boolean isTrap(int artifact);

default boolean isSumDivBy3(int artifact) {
int sum = 0;
int num = artifact;

while (num != 0) {
sum += num % 10;
num /= 10;
}

return sum % 3 == 0;
}

boolean myWinProbability(PlayerStatus opponent, int health, int score);

double getDistance(PlayerStatus opponent, double posX, double posY);

boolean winDuel(PlayerStatus opponent, double distance, WeaponModel myWeapon);
}
57 changes: 24 additions & 33 deletions src/main/java/org/example/services/Services.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@

import org.example.models.*;

public class Services {
public class Services implements IServices {

private Services() {
public Services() {
//empty constructor
}

public static int updateScoreService(WeaponModel weapon, int playerScore) {
return playerScore - weapon.getCost();
}

public static boolean canBuyWeapon(WeaponModel weapon, int playerScore) {
@Override
public boolean canBuyWeapon(WeaponModel weapon, int playerScore) {

return playerScore >= weapon.getCost();
}

public static boolean isPerfectNumber(int artifactCode) {
@Override
public boolean isPerfectNumber(int artifactCode) {
long sum = 0;
boolean isPerfect = false;

Expand All @@ -25,59 +24,51 @@ public static boolean isPerfectNumber(int artifactCode) {
sum += i;
}
}
if (sum == artifactCode) {

if (artifactCode == 0) {
return isPerfect;
} else if (sum == artifactCode) {
isPerfect = true;
}
Comment thread
razvanexu marked this conversation as resolved.
Outdated
return isPerfect;
}

public static boolean isPrime(int artifact) {
@Override
public boolean isPrime(int artifact) {
if (artifact <= 1) {
return false;
}

for (int i = 2; i < Math.sqrt(artifact); i++) {
for (int i = 2; i < artifact; i++) {
if (artifact % i == 0) {
return false;
}
}
return true;
}

public static boolean isTrap(int artifact) {
return isSumDivBy3(artifact) && isEven(artifact);
}

private static boolean isSumDivBy3(int artifact) {
int sum = 0;
int num = artifact;

while (num != 0) {
sum += num % 10;
num /= 10;
}

return sum % 3 == 0;
}

private static boolean isEven(int artifact) {
return artifact % 2 == 0;
@Override
public boolean isTrap(int artifact) {
return isSumDivBy3(artifact) && artifact % 2 == 0;
}

public static boolean winProbability(PlayerStatus opponent, int health, int score) {
@Override
public boolean myWinProbability(PlayerStatus opponent, int health, int score) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor this to use PlayerStatus opponent1, PlayerStatus opponent2 as parameters instead, to be more clear.

Copy link
Copy Markdown
Owner Author

@razvanexu razvanexu Jun 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

solved in 535b2dd

var opponentWinProb = (3 * opponent.getHealth() + opponent.getScore() / 1000) / 4;
var myWinProb = (3 * health + score / 1000) / 4;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create another method that calculates win prob. taking a PlayerStatus player as parameter and use it here instead.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

solved


return myWinProb > opponentWinProb;
return myWinProb >= opponentWinProb;
}

public static double getDistance(PlayerStatus opponent, double posX, double posY) {
@Override
public double getDistance(PlayerStatus opponent, double posX, double posY) {
Comment thread
razvanexu marked this conversation as resolved.
Outdated
double ac = Math.abs(opponent.getPositionX() - posX);
double cb = Math.abs(opponent.getPositionY() - posY);
return Math.hypot(ac, cb);
}

public static boolean winDuel(PlayerStatus opponent, double distance, WeaponModel myWeapon) {
@Override
public boolean winDuel(PlayerStatus opponent, double distance, WeaponModel myWeapon) {
Comment thread
razvanexu marked this conversation as resolved.
Outdated
opponent.getWeaponInHand().setCombatValue(distance);
myWeapon.setCombatValue(distance);

Expand Down
Loading