Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

polymorphic computer player: an abstract class with an abstract method  RSS feed

 
D Anthony
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, i have to build this nim game with two players v the computer and part of the requirements is to have an a polymorphic computeplayer: an abstract class which could have an abstract move() method overridden in two ways by a naive and smart player class.

The naiveComputerPlayer move method simply updates a randomly chosen plie with a random but legal amount.

The SmartComputerPlayer move method operates a strategy based on the nim calculation.

I have implemented this abstract class and x2 concrect class but am stuck in terms of working out the random and strategy move, any advice would be welcome. Please see below.



Thanks
 
Campbell Ritchie
Marshal
Posts: 54882
155
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

The overriding looks all right, but I don't know about the strategies. You will have to write them down carefully, then you will understand what to do.
Your indentation is inconsistent; you can hide errors from yourself by poor indentation.
Don't use multiple Random objects (see line 15). Make that a field or something, because you usually only need one instance of Random. I think naivePlayer is a bad name for a Random object.
 
D Anthony
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you and thanks for the reply, could you suggest an example of how i could approach this strategy. Thanks
 
Stephan van Hulst
Saloon Keeper
Posts: 7473
134
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That doesn't look right. You're asking players to make decisions based on no information (Naive player) or only one previous game state (Smart player).

Instead, when you ask them for a move, you should let them know the current state of the game. Seeing as how all state is passed to the decision making method, and the player has no other state, you can just make an interface out of this:

A naive strategy could then look like this:

For a smart player, on every turn you need to take exactly so much items that the left over amount of items is a multiple of the maximum amount of items to take per turn, plus 1. That way, in the end your opponent has to take the last item in the last turn.
 
D Anthony
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply, i see your logic. Are you saying i should use an interface instead of an abstract class. Part of the assignment requirement is to also implement a swing GUI, where i intend to put the game logic could this interface you mentioned be that? Bear with me, i learnt Java a long time ago and haven't really used it since-
Thanks
 
Stephan van Hulst
Saloon Keeper
Posts: 7473
134
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, in this case the word interface has nothing to do with "user interface". In this case, interface refers to a contract that every strategy implementation has to fulfill, so that they can be used by other classes that don't know about the concrete type of these strategies.

I find that abstract classes very rarely are useful. I usually only use them when I want to give default implementations for interface methods, which doesn't really happen all that often.
 
D Anthony
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thank you for this implementation, very helpful, i will now implement the game strategy and the other classes.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!