I'm designing a type of Monopoly game to practice my Java skills.
I want there to be a "Space" class to represent a space on the game board. Something like this.
public class Space{
private Player [] occupants;
private String name;
public Space(String name){
this.name = name;
}
}
I am coming from a python background, so if I were doing it that way, I would probably initialize each space with some sort of "action" function, unique to each path. So Something like this.
class Space():
def __init__(self, name):
self.name = name
self.action = None
GO_SPACE = Space()
def GO_ACTION():
## do some processing
GO_SPACE.action = GO_ACTION
Essentially assigning functions at run time to the Space object. I can't remember if I got the syntax just right, but hopefully it is clear what I am trying to do. Each space has its own unique function to process the actions when a player lands on it.
I am still open to changing the architectural direction of things, but how would I do something like this in Java? I admit, my knowledge of Java is a bit rusty, but my first thought was to try and assign a private member variable a function somehow... Is this approach way off? I can't remember if Java has functions as objects or anything like it.
I don't see why you need this to be dynamic. Why not something like this:
public abstract class Space {
// name etc.
public abstract void action();
}
public class GoSpace extends Space {
public void action() {
// collect 200 dollars
}
}
etc.