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
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");
}
}
84 changes: 52 additions & 32 deletions src/main/java/org/example/models/PlayerStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,42 @@
import org.example.enums.*;
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;
private int health = 100;
private int score = 0;
private int lives = 3;
private int health;
private WeaponModel weaponInHand;
private double positionX;
private double positionY;

public boolean shouldAttackOpponent(PlayerStatus opponent) {
if (opponent.getWeaponInHand() == weaponInHand) {
return winProbability(opponent, health, score);
public PlayerStatus(IServices service) {
this.service = service;
}

public boolean shouldAttackOpponent(PlayerStatus opponent, PlayerStatus self) {
if (opponent.getWeaponInHand() == self.getWeaponInHand()) {
return service.myWinProbability(opponent, self);
} else {
var distance = getDistance(opponent, positionX, positionY);
return winDuel(opponent, distance, weaponInHand);
var distance = service.getDistance(opponent, self);
return service.opponentWins(opponent, distance, weaponInHand);
}
}

public WeaponModel getWeaponInHand() {
return weaponInHand;
}

public void setWeaponInHand(WeaponModel weapon, Weapons weaponType) {
weaponInHand = new WeaponModel(FIST);
if (!canBuyWeapon(weapon, score)) {
weaponInHand.setName(weaponType);
public boolean setWeaponInHand(WeaponModel weapon) {
weaponInHand = new WeaponModel(Weapons.FIST);
if (service.canBuyWeapon(weapon, score)) {
weaponInHand = weapon;
this.score -= weapon.getCost();
return true;
}
return false;
}

public String getGameName() {
Expand Down Expand Up @@ -64,20 +69,24 @@ 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;
if (this.health < 0) {
this.health += health;
if (this.health <= 0) {
lives--;
this.health = 100;
}
Expand All @@ -90,27 +99,38 @@ public double getPositionX() {
return positionX;
}

public void movePlayer(double newPosX, double newPosY) {
positionX += newPosX;
positionY += newPosY;
public void setPositionX(double posX) {
positionX = posX;
}

public double getPositionY() {
return positionY;
}

public void setPositionY(double positionY) {
this.positionY = positionY;
}

public void movePlayer(double newPosX, double newPosY) {
positionX += newPosX;
positionY += newPosY;
}

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);
if (score < 0) {
setScore(0);
}
setHealth(-25);
} else {
score += artifactCode;
}
Expand Down
32 changes: 20 additions & 12 deletions src/main/java/org/example/models/WeaponModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
import org.example.enums.*;

public class WeaponModel {
private String name;
private Weapons weaponName;
private int cost;
private int combatValue;

public WeaponModel(Weapons weapon) {
this.name = weapon.toString();
setCostAndValue(weapon);
this.weaponName = weapon;
setCostAndValue();
}

private void setCostAndValue(Weapons name) {
switch (name) {
private void setCostAndValue() {
switch (weaponName) {
case FIST:
this.cost = 0;
this.combatValue = 1;
Expand All @@ -38,21 +38,29 @@ public int getCombatValue() {
}

public void setCombatValue(double distance) {
if (distance > 1000) {
this.combatValue = 4;
if (weaponName == Weapons.SNIPER) {
if (distance > 1000) {
combatValue = 4;
} else {
combatValue = 3;
}
} else if (weaponName == Weapons.KALASHNIKOV && distance <= 1000) {
combatValue = 4;
} else {
combatValue = 3;
}
this.combatValue = 3;

}

public int getCost() {
return cost;
}

public String getName() {
return name;
public Weapons getWeaponName() {
return weaponName;
}

public void setName(Weapons weapon) {
this.name = weapon.toString();
public void setWeaponName(Weapons weapon) {
this.weaponName = weapon;
}
}
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, PlayerStatus self);

double getDistance(PlayerStatus opponent, PlayerStatus self);

boolean opponentWins(PlayerStatus opponent, double distance, WeaponModel myWeapon);
}
67 changes: 26 additions & 41 deletions src/main/java/org/example/services/Services.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,82 +2,67 @@

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;

for (int i = 1; i <= artifactCode / 2; i++) {
if (artifactCode % i == 0) {
sum += i;
}
}
if (sum == artifactCode) {
isPerfect = true;
}
return isPerfect;

return artifactCode != 0 && sum == artifactCode;
}

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) {
var opponentWinProb = (3 * opponent.getHealth() + opponent.getScore() / 1000) / 4;
var myWinProb = (3 * health + score / 1000) / 4;
@Override
public boolean myWinProbability(PlayerStatus opponent, PlayerStatus self) {
double opponentWinProb = (3 * opponent.getHealth() + (double) opponent.getScore() / 1000) / 4;
double myWinProb = (3 * self.getHealth() + self.getScore() / (double) 1000) / 4;

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

public static double getDistance(PlayerStatus opponent, double posX, double posY) {
double ac = Math.abs(opponent.getPositionX() - posX);
double cb = Math.abs(opponent.getPositionY() - posY);
@Override
public double getDistance(PlayerStatus opponent, PlayerStatus self) {
double ac = Math.abs(opponent.getPositionX() - self.getPositionX());
double cb = Math.abs(opponent.getPositionY() - self.getPositionY());
return Math.hypot(ac, cb);
}

public static boolean winDuel(PlayerStatus opponent, double distance, WeaponModel myWeapon) {
@Override
public boolean opponentWins(PlayerStatus opponent, double distance, WeaponModel myWeapon) {
opponent.getWeaponInHand().setCombatValue(distance);
myWeapon.setCombatValue(distance);

Expand Down
Loading