Nils Anton wrote:Hello!
As a school exercise I've created a Nim game. The exercise is supposed to teach me about abstract classes, inheritance, and interfaces. Therefore I decided to have an abstract Player-class that the human and computer player inherit methods and instance variables from.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
However, while I see a HumanPlayer, I don't see any ComputerPlayer class. What are the differences between them?
Try to explain it to us IN ENGLISH, not in "Java-speak".
Note that this is NOT the game itself, simply the "playing area"; and many games - including Chess, Go, Connect4/5, Mastermind, etc - have one. It's also the thing that you're likely to want to display (however you do that).
Also: DON'T include underscores ('_') in your variable names. It's regarded as "Python-esque", and generally frowned on in the Java community. You can find proper naming conventions here; and I advise you to memorise them as quickly as possible.
Overall, this code strongly hints at someone who has experience writing programs in another language. I doubt this is your first rodeo, right Cowboy?
In a program this small, mixing concerns is not much of a big deal. In larger programs, however, you should think about separation of concerns, particularly the concerns of doing calculations vs input/display.
Using inheritance and polymorphism to simplify the decision to prompt or not prompt for input works in this case but experienced hands will detect a slight smell there and will keep a wary eye on it. Mixed responsibilities tend to make for code that is more difficult to maintain.
As for using interfaces vs abstract classes, my rule of thumb is to use an interface when you know there will be a definite need to have an unknown number of different implementations. Also, when the idea represented is along or crosses application layer boundaries (View, Control, Data, Service, Model, etc.), prefer interfaces as they have the least amount of coupling.
2 seconds is an awfully long time to simulate a computer trying to make a decision. Are you trying to make the computer look stupid or what? ;)
Nils Anton wrote:... I recently started studying programming at school, so I feel I don't quite get what:
In a program this small, mixing concerns is not much of a big deal. In larger programs, however, you should think about separation of concerns, particularly the concerns of doing calculations vs input/display.
Yet I appreciate your attempt to explain it to me : ).
Thank you both for taking time out of your Friday to help me! : ).
Fred Kleinschmidt wrote:Note that a constructor should never call any public or protected methods that are not specified as final.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
That's fine. Just remember this: Your methods should do one thing and only one thing well. If it's calculating things, avoid mixing in input and/or display. If it's display, avoid mixing in lots of calculations and/or input. Those things that you want to mix in, that's what return values and parameters are for. So, main calls a method to get user input, then passes the input on the method that does calculations, then passes the results to another method that displays them.
Note that a constructor should never call any public or protected methods that are not specified as "final". If any subclass of your Player class happens to override that non-final method you will encounter problems that are very difficult to debug.
Nils Anton wrote:The computer randomly takes a number of sticks - while following the above rules...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Hmmm. Really?
NIM is one of the oldest computer games there is, and the reason is that it has a very simple winning strategy.
In fact, I remember going to an exhibition at Kensington Olympia when I was about 7 (which would have been 1964) and getting 30 seconds of fame on national TV because I worked out how to "beat the computer" at a NIM game with either 13 or 15 matchsticks on the bottom row.
So you might want to look up the way to win rather than just having the computer take sticks at random.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |