0

I'm trying to make Monopoly/Eurbuisness game in Java, but I got stuck with kinda simple problem... So, I have basic Field class

public class Field {
    protected int fieldPosition;

    Field(){    }
}

then I extend this class to subclasses like Start, Property, Jail, Chance, etc.

Currently I'm working on Property class

public class Property extends Field{
    private String propertyGroup;
    private String propertyOwner;
    private String propertyName;
    private float propertyPrice;
    private boolean allowBuilding;
    private int numberOfHouses;
    private int numberOfHotels;
    private float basicValue;
    private float houseValue;

    final private int maxNumberOfHouses = 4;
    final private int getMaxNumberOfHotels = 1;

    public Property(int fieldPosition, String propertyGroup, String propertyName, float propertyPrice, float basicValue, float houseValue){
        this.fieldPosition = fieldPosition;
        this.propertyGroup = propertyGroup;
        propertyOwner = "none";
        this.propertyName = propertyName;
        this.propertyPrice = propertyPrice;
        allowBuilding = false;
        this.basicValue = basicValue;
        this.houseValue = houseValue;
        numberOfHotels = 0;
        numberOfHotels = 0;
    }

    /*
    Setters
     */
    public void setAllowBuilding(boolean allowBuilding) {
        this.allowBuilding = allowBuilding;
    }

    public void setPropertyOwner(String propertyOwner) {
        this.propertyOwner = propertyOwner;
    }

    /*
    Getters
     */

    public String getPropertyGroup() { return propertyGroup; }

    public String getPropertyOwner() {
        return propertyOwner;
    }

    public String getPropertyName() {
        return propertyName;
    }

    public float getPropertyPrice() {
        return propertyPrice;
    }

    public boolean isAllowBuilding() {
        return allowBuilding;
    }

    public int getNumberOfHouses() {
        return numberOfHouses;
    }

    public int getNumberOfHotels() {
        return numberOfHotels;
    }

    public int getMaxNumberOfHouses() {
        return maxNumberOfHouses;
    }

    public float getBasicValue() {
        return basicValue;
    }

    public float getHouseValue() {
        return houseValue;
    }



    /*
         Add and remove house and hotel methods
         */
    public void addHouse(){
            this.numberOfHouses = getNumberOfHouses() + 1;
            this.propertyPrice = getPropertyPrice() + 100;
    }

    public void removeHouse(){
            this.numberOfHouses = getNumberOfHouses() - 1;
            this.propertyPrice = getPropertyPrice() - 100;
    }

    public void addHotel(){
            this.numberOfHotels = getNumberOfHotels() + 1;
            this.propertyPrice = getPropertyPrice() + 400;
    }

    public void removeHotel(){
            this.numberOfHotels = getNumberOfHotels() - 1;
            this.propertyPrice = getPropertyPrice() - 400;
    }

    @Override
    public String toString() {
        return propertyName + getNumberOfHouses();
    }

Next class is Board, it should create basic table of all fields (using polymorphysm, some of them will be Properties, some will be something else)

    public class Board {
    public Board() {
        Field board[] = new Property[40];

        board[0] = new Start();
        board[1] = new Property(1,"Katowice","Mariacka", 600, 100, 175);
        board[2] = new Property(2, "Katowice","Stawowa", 750, 125, 215);
        board[3] = new Property(3, "Katowice","Korfantego", 1000, 150, 250);
    }
}

Then in Game class I got method buyProperty

  public void buyProperty(Player player, Property property){
    if(player.getPlayerCash() >= property.getPropertyPrice() &&
            (property.getPropertyOwner().equals("none"))){
        player.addProperty(property);
        property.setPropertyOwner(player.getPlayerName());
        player.removeCash(property.getPropertyPrice());
    }
}

Finnaly, Im trying to test it but unfortunatelly, it doesnt work

public static void main(String[] args) {
    Board board = new Board();
    Game game = new Game();
    Player johny = new Player("Johny");

    game.buyProperty(johny, board[1]);

}

java: array required, but game.components.Board found

and it points into last line I posted

any ideas, suggestions?

0

2 Answers 2

1

You should be more carefull with your variable names. In your main method, you have a variable named board - but it's an instance of the Board class you created earlier. It is not an array, therefore you cannot use the [ ] to access elements in it.

I assume you want to access the board array inside the board class. But before you can do that, you need to change a few things:

First of all, board only exists as a local variable in the constructor, i.e. once your constructor is executed the array is out of scope and will be deleted automatically. What you need to do instead is make it a field in the Board class:

public class Board {

    Field board[] = new Property[40];

    public Board() {

        board[0] = new Start();
        board[1] = new Property(1,"Katowice","Mariacka", 600, 100, 175);
        board[2] = new Property(2, "Katowice","Stawowa", 750, 125, 215);
        board[3] = new Property(3, "Katowice","Korfantego", 1000, 150, 250);
    }
}

Now at least you can use board outside of the constructor.

That, however is still not enough since board is going to be private by default. So you need a getter method for it:

public Field[] getBoard(){
    return board;
}

Then you can do something like:

game.buyProperty(johny, Board.getBoard()[1]);

Also, you have a little type error in the line:

Field board[] = new Property[40];

You first declare board as an array of the type Field, then initialize it as an array of the type Property. That works because Property is a subclass of Field but now you can only put objects of the type Property in the array. Assuming Start is not a subclass of Field, the line board[0] = new Start(); will cause problems. So I would suggest you simply write:

Field board[] = new Field[40];

It also makes more sense that way.

Sign up to request clarification or add additional context in comments.

Comments

0

This is a Board class containing Field board[]

Board board = new Board();

So yes, the following line won't work because board is a Board class, not the Property[] Array inside the Board class.

game.buyProperty(johny, board[1]);

I suppose what you want to achieve is

game.buyProperty(johny, board.getBoard()[1]);

Edit: thanks for the correction from Gumbo.

You should consider move Field board[] out of constructor to Board class, and then provide a public method to access Field board[]:

public class Board {
    // Move board  
    Field board[] = new Property[40];

    // Provide public method
    public Field getBoard(){
        return board[];
    }
}

5 Comments

That won't work since board is not a public field of the Board class. In fact, it's scope is limited to the constructor, i.e. it is deleted once the execution of the constructor is finished.
So I made it public. I'v also added board.board[1] in main method. Now I got "Wrong 2nd argument type", it requires Property and found Field... so I have to rewrite whole buyProperty method?
@AGasior Well, that works but kind of violates the principle of encapsulation. Read more here, I don't want to copy the explanation into a comment
@AGasior Gumbo's approach is much better.
Yeah I got to it too. I made what Gumbo suggested. Unfortunatelly, I still got: "Wrong 2nd argument type", found ...Field, requires ...Property. So I guess solution is to rewerite buyProperty method I posted above? I can simply change 2nd parameter of this method to Field but then I cannot access Property class methods I use inside this method... Property extends Field, so I cant use methods from Property on Field objects. I could move those methods to Field Class, but idk if thats right, because getPropertyPrice() for example is specific just for Property, not for all Fields

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.