I feel like this is a noobish question but I'm getting back into java so I'm a little stumped
I have a Player class that contains a public attack method() but for some reason when I try to call the method in the main class where I have created an instance of the Player class java says it can't find the method? I'm hoping someone can tell me what's wrong
I have tried rebuilding the class to make sure it was up to date... I don't see any other errors with other methods in the class.. so I'm not sure what the issue is??
Exception in thread "main" java.lang.NoSuchMethodException: org.example.Player.attack()
at java.base/java.lang.Class.getMethod(Class.java:2227)
at org.example.Main.main(Main.java:31)
here is my player class
package org.example;
import java.util.Random;
public class Player {
public String name;
public static int health;
public int def;
public int atk;
private String[] domains = {"hauntings","findings","solutions","busters","slayers","defense","protection","powers","scary"};
public Player(String name, int health, int atk, int def) {
this.name = name;
this.health = health;
this.atk = atk;
this.def = def;
}
// player will make an http request for a random site trying to find anything related to ghosts
// if the response is 200 we assume the player has learned something new about ghosts and increased their knowledge
// this in turn increases player atk power
public void loseHealth(int amount){
this.health = this.health-amount;
System.out.println(this.name+" now has "+this.health+" health");
}
public void searchWebForAnswers () {
Random random = new Random();
int randomIndex = random.nextInt(this.domains.length);
String randomUrl = domains[randomIndex];
// if this website exists we will increase atk by 1
if( randomUrl == "defense"){
this.atk +=1;
}
}
public void attack(Ghost ghost){
Random rand = new Random();
double attackPlusRoll = Math.floor(Math.random() *(4-1 +this.atk) + 1);
if(this.atk > ghost.def && ghost.getIsTransparent() == false){
ghost.setGhostHealth(this.atk-ghost.def);
}else{
System.out.println("the attack does no damage: player_atk:"+this.atk+"ghost_defense:"+ghost.def);
}
}
public int getHealth(){
return this.health;
}
public void setPlayerHealth(int val){
this.health = val;
}
}
here is my main class
package org.example;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Random;
public class Main {
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
String[] playerMethods = {"attack", "searchWebForAnswers"};
String[] ghostMethods = {"learnToSpeak", "sayBoo", "haunt","becomeTransparent","attack"};
System.out.println("BEGIN THE HAUNT!");
/*
player will go first, then ghost
a random action will be taken by whoever goes
the turns continue until either the ghost is exorcised or the player dies
*/
Player player = new Player("Luther", 10, 1, 3);
Ghost ghost = new Ghost(20, 0, 3);
while (player.health > 0 && ghost.health > 0) {
Random random = new Random();
// a random method is chosen each turn
// this does not include methods like getHealth or methods used by the game to determine state
System.out.println("player is taking their turn");
int randomMethodIndex = random.nextInt(playerMethods.length);
String randomMethodName = playerMethods[randomMethodIndex];
Method method = Player.class.getMethod(randomMethodName);
if( randomMethodName == "attack"){
player.attack(ghost);
}else {
method.invoke(player);
}
System.out.println("ghost is taking their turn");
int randomMethodIndex2 = random.nextInt(playerMethods.length);
String randomMethodName2 = ghostMethods[randomMethodIndex];
Method method2 = Ghost.class.getMethod(randomMethodName2);
if( randomMethodName2 == "ghostAttack"){
ghost.ghostAttack(player);
}else {
method2.invoke(ghost);
}
}
if(player.getHealth() == 0){
System.out.println("player has died");
System.out.println(player.health);
}else{
System.out.println("congratulations you've exorcised the ghost");
System.out.println("player "+player.health);
System.out.println("ghost "+ghost.health);
}
}
};
here is a quick dump using a python script of all the files present in the local project
\simple-api\ghostgame\.idea\.gitignore
\simple-api\ghostgame\.idea\compiler.xml
\simple-api\ghostgame\.idea\dbnavigator.xml
\simple-api\ghostgame\.idea\encodings.xml
\simple-api\ghostgame\.idea\jarRepositories.xml
\simple-api\ghostgame\.idea\misc.xml
\simple-api\ghostgame\.idea\workspace.xml
\simple-api\ghostgame\.idea
\simple-api\ghostgame\pom.xml
\simple-api\ghostgame\src\main\java\org\example\GameActions.java
\simple-api\ghostgame\src\main\java\org\example\Ghost.java
\simple-api\ghostgame\src\main\java\org\example\Main.java
\simple-api\ghostgame\src\main\java\org\example\Player.java
\simple-api\ghostgame\src\main\java\org\example
\simple-api\ghostgame\src\main\java\org
\simple-api\ghostgame\src\main\java
\simple-api\ghostgame\src\main\resources
\simple-api\ghostgame\src\main
\simple-api\ghostgame\src\test\java
\simple-api\ghostgame\src\test
\simple-api\ghostgame\src
\simple-api\ghostgame\target\classes\org\example\GameActions.class
\simple-api\ghostgame\target\classes\org\example\Ghost.class
\simple-api\ghostgame\target\classes\org\example\Main.class
\simple-api\ghostgame\target\classes\org\example\Player.class
\simple-api\ghostgame\target\classes\org\example
\simple-api\ghostgame\target\classes\org
\simple-api\ghostgame\target\classes
\simple-api\ghostgame\target\generated-sources\annotations
\simple-api\ghostgame\target\generated-sources
\simple-api\ghostgame\target