Home > OS >  How to get values from a class to another class where the first class has an object from the second
How to get values from a class to another class where the first class has an object from the second

Time:11-20

I'm making this game in Java as a part of one of my CompSci course classes. It's an arena style game where you have to collect coins and kill monsters. Very basic.

I have an Arena class a Hero class and a Coin class where the Arena has a Hero object and list of Coin objects. I want to check if my hero is at the same place as a certain coin and I wanted to do this with some sort of hero.onCoin() method, but I can't since my Hero class doesn't have any instances of the Coin class.

I then tried to do something like:

private List<Coin> getCoins(){
    return coins;
}

So, I could have my coins list in the Hero class, but since I don't have the Arena Class instantiated in my Hero class, I can't. Any tips for a newbie?

Here are my 4 classes (Class Hero and Coin extend Element Class)

import com.googlecode.lanterna.*;
import com.googlecode.lanterna.graphics.TextGraphics;
import com.googlecode.lanterna.input.KeyStroke;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Arena {

    private final int width, height;
    private final Hero hero = new Hero(20, 10);
    private List<Wall> walls;
    private List<Coin> coins;

    public Arena(int width, int height){
        this.width = width;
        this.height = height;
        this.walls = createWalls();
        this.coins = createCoins();
    }

    public void processKey(KeyStroke key){
        System.out.println(key);
        switch (key.getKeyType()){
            case ArrowUp : moveHero(hero.moveUp()); break;
            case ArrowDown :  moveHero(hero.moveDown()); break;
            case ArrowLeft :  moveHero(hero.moveLeft()); break;
            case ArrowRight :  moveHero(hero.moveRight()); break;
        }
    }

    public void draw(TextGraphics graphics){
        graphics.setBackgroundColor(TextColor.Factory.fromString("#336699"));
        graphics.fillRectangle(new TerminalPosition(0, 0), new TerminalSize(width, height), ' ');

        for (Wall wall : walls){
            wall.draw(graphics, wall.color, wall.model, wall.bold);
        }

        for (Coin coin : coins){
            coin.draw(graphics, coin.color, coin.model, coin.bold);
        }

        hero.draw(graphics, hero.color, hero.model, hero.bold);
    }

    private boolean canHeroMove(Position position) {
        for (Wall wall : walls) {
            if (wall.getPosition().equals(position)) {
                return false;
            }
        }
        return true;
    }

    public void moveHero(Position position) {
        if (canHeroMove(position))
            hero.setPosition(position);
    }

    private List<Wall> createWalls(){
        List<Wall> walls = new ArrayList<>();

        for (int c = 0; c < width; c  ){
            walls.add(new Wall(c, 0));
            walls.add(new Wall(c, height - 1));
        }

        for (int r = 1; r < height -1; r  ){
            walls.add(new Wall(0, r));
            walls.add(new Wall(width - 1, r));
        }

        return walls;
    }

    private List<Coin> createCoins() {
        Random random = new Random();
        List<Coin> coins = new ArrayList<>();

        for (int i = 0; i < 5; i  ) {
            //Create new random coin
            int x = random.nextInt(width - 2)   1, y = random.nextInt(height - 2)   1;
            Position p = new Position(x, y);
            boolean new_coin = true;

            //If it's on player's position
            if(hero.getPosition().equals(p)){
                i--;
                continue;
            }
            //If it's on another coin's position
            for (Coin coin : coins){
                if(coin.getPosition().equals(p)){
                    i--;
                    new_coin = false;
                    break;
                }
            }
            //Add coin to coins
            if(new_coin){
                coins.add(new Coin(x, y));
            }

        }
        return coins;
    }

    private List<Coin> getCoins(){
        return coins;
    }

    private void retrieveCoins(){

    }
}
import com.googlecode.lanterna.SGR;
import com.googlecode.lanterna.TerminalPosition;
import com.googlecode.lanterna.TextColor;
import com.googlecode.lanterna.graphics.TextGraphics;

public class Element {

    protected Position position;

    public Element(int x, int y){
        position = new Position(x, y);
    }

    public void draw(TextGraphics graphics, String color, String model, boolean bold){
        graphics.setForegroundColor(TextColor.Factory.fromString(color));
        if(bold){
            graphics.enableModifiers(SGR.BOLD);
        }
        graphics.putString(new TerminalPosition(position.getX(), position.getY()), model);

    }

    public Position getPosition(){
        return position;
    }
}
public class Coin extends Element{

    public final String color = "#ffdb19", model = "O";
    public final boolean bold = true;

    public Coin(int x, int y){
        super(x, y);
    }


}
import java.util.List;

public class Hero extends Element{

    public final String color = "#ff0000", model = "X";
    public final boolean bold = true;

    public Hero(int x, int y){
        super(x, y);
    }

    public Position moveUp(){
        return new Position(position.getX(), position.getY() -1);
    }

    public Position moveDown(){
        return new Position(position.getX(), position.getY()   1);
    }

    public Position moveLeft(){
        return new Position(position.getX() -1, position.getY());
    }

    public Position moveRight(){
        return new Position(position.getX()   1, position.getY());
    }

    public void setPosition(Position new_position){
        position.setX(new_position.getX());
        position.setY(new_position.getY());
    }
}

CodePudding user response:

Put the logic in the Arena class:

public boolean heroOnCoin() {
    for (Coin coin: coins) {
        if (hero.getPosition().equals(coin.getPosition())) {
            return true;
        }
    }
    return false;
}
  •  Tags:  
  • java
  • Related