Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Björn Norén
9,569 PointsQuestion about abstract, extends & polymorphism
Hello there! I'm trying to build a simple game in Java called Nim, where you are supposed to pick sticks from a pile, you can't pick more than half the pile and and the person who picks the last stick loses.
My problem is that I don't know how to do this in proper object oriented programming. I want it to be something like:
- my superClass(Nim) to be abstract
- User & Computer to extend from Nim.
- Game to handle the game-logic and have the 'void main()'.
But I don't know how I should instantiate the User & Computer objects in the Game class. Either my thinking is wrong in all classes or I don't know the best to intantiate User & Computer in the Game class. I feel like if there were a map over the correlation between the classes it would look like:
> User
/ \
Nim >> Game
\ /
> Computer
Does this make sense?
Here's some example code:
abstract public class Nim {
//Fields
private int pile = 10;
//Getters
public int GetPile(){
return pile;
}
//Setters
public void SetPile(int num){
pile = num;
}
public boolean CheckPile(){
boolean ok;
if ( pile >= 1 )
ok = true;
else
ok = false;
return ok;
}
}
// User
public class User extends Nim {
public void UserRemoveFromPile(){
if ( CheckPile() ){
int x = GetPile();
System.out.println( "User tar bort från hög");
x--;
SetPile(x);
System.out.println( "Antalet stickor kvar: " + x );
}
else
System.out.println( "Det är bara en sticka kvar");
}
}
// Computer
public class Computer extends Nim {
public int GenerateRandomNumber(){
int min = 1;
int max = GetPile()/2;
int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1);
return randomNum;
}
public void ComputerRemoveFromPile( int y){
if ( CheckPile () ){
int x = GetPile();
System.out.println( "Computer tar bort " + y + " från hög");
x = x - y;
SetPile(x);
System.out.println( "Antalet stickor kvar: " + x );
}
else
System.out.println( "Det är bara en sticka kvar");
}
}
// Game
public class Prompter {
Upp2Test2 u = new User();
Upp2Test2 c = new Computer();
public void Play(){
while ( u.GetPile() > 1 ){
u.UserRemoveFromPile();
c.ComputerRemoveFromPile(c.GenerateRandomNumber());
}
}
public static void main(String[] args) {
Prompter p = new Prompter();
p.Play();
}
}
Any help is apreciated. Thanks!
2 Answers
Florian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 PointsWhy do you want your Nim class to be abstract? You are not defining any abstract methods in it. Anyway, composition would still work if you pass in a concrete subclass of the abstract Nim class like this one:
public class NimGame extends Nim {
...
}
Then in a game class you could pass it in like this:
Nim nimGame = new NimGame();
Computer computer = new Computer(nimGame);
Florian
Florian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 PointsHi Björn,
I would not make User and Computer inherit from Nim. You want to ask yourself: Is a user a Nim? Is a computer a Nim? I would say it's not.
You can use composition and pass in the Nim object to the User and Computer constructor. Example:
public class Computer {
Nim nim;
public Computer(Nim nim) {
this.nim = nim;
}
}
Then you can call nim.checkPile() or nim.getPile() on the nim field.
Kind Regards, Florian
Björn Norén
9,569 PointsHi, thanks for taking your time to answer!
A question though, does this work even if the superclass(Nim) is abstract? Or is it a way to go around it?
Björn
Björn Norén
9,569 PointsBjörn Norén
9,569 PointsIt works! Thank you very much! I did think of User & Computer as Nim-objects but you are right, they are not :)
Thanks again!