I am trying to make my first Java program using classes and objects and so I picked a simple game problem to model. (1) Spin wheel (2) Move player 6 spaces (for example) (3) Display all the "spaces" with the player shown on the correct space. Done! (This is somewhat like some homework I have seen on this forum).
I want to learn to use objects so I set up a class called Space, which can be occupied or vacant. I made an array of 20 spaces. I set up another class called Player, which can have a "position" , for example player[1].position =6. So the player has landed on space 6. The main method will spin the wheel to generate a number.
My theory question is: how do I let the space object "know" that the player object contains the value "6". Or perhaps some other information found only in the player object, such as last_position (where the player was earlier, or the color of the piece, etc.).
Below is code for the Space class. Below that is an error. I'm a newbie so I hope this make sense to you all. The main() class spins the wheel and then I want "the action" to occur in my objects, which I believe is the whole point of OOP.
One other thing. I use Eclipse and so I think I am required to create three files. The main file for the Game class and then two more files for Space and Player. Does that seem correct? To have a separate file for each class? Just checking. Thanks.
public class Space { public int player_on; // occupied by which player int bonus_value; int zz; public void update_space (int id) { this.player_on =id; // so maybe put player "1" on space 6 zz= player[1].last_position; // this is causing the error } public void zero_space () { this.player_on =0 ; } }
-----------------------------
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
player cannot be resolved to a variable
at Space.update_space(Space.java:17)
at gameopoly.main(gameopoly.java:81)
0