0

enter image description here

This is the problem i am dealing with. I have to make a simple game of NIM. I am learning java using a book so far i have only coded programs that deal with 2 classes. This program would have about 4 classes i guess including the main class. My problem is i am having a difficult time designing classes how they will interact with each other. I really want to think and use a object oriented approach. So the first thing i did was design the Pile CLASS as it seemed the easiest and made the most sense to me in terms of what methods go in it.

Here is what i have got down for the Pile Class so far.

package Nim;

import java.util.Random;

public class Pile {

    private int initialSize;


    public Pile(){

    }

    Random rand = new Random();

    public void setPile(){

        initialSize = (rand.nextInt(100-10)+10);
    }


    public void reducePile(int x){

        initialSize = initialSize - x;
    }


    public int getPile(){

        return initialSize;
    }


    public boolean hasStick(){

        if(initialSize>0){

            return true;
        }

        else
        {
            return false;
        }
    }

}

Now i need help in designing the Player Class. By that i mean i am not asking for anyone to write code for me as that defeats the purpose of learning i was just wondering how would i design the player class and what would go on it. My guess is that the player class would contain method for choosing move for computer and also receiving the move human user makes. Lastly i am guessing in the Game class i am guessing the turns would be handeled. I am really lost right now so i was wondering if someone can help me think through this problem it would be great. Starting with the player class would be appreciated.

I know there are some solutions for this problem online but i refuse to look at because i want to develop my own approach to such problems and i am confident if i can get through this problem i can solve other problems.

I apologize if this question is a bit poor but in specific i need help in designing the Player class.

1 Answer 1

2

A player:

  • Stupid, Smart, or Human

  • Player is an interface and Stupid, Smart, or Human are concrete classes OR

  • Player is an Abstract Class and Stupid, Smart, or Human are concrete classes

The interface or abstract class Player would contain everything a Player should do. The concretes would implement Player behavior and extend it specific to Stupid, Smart, or Human.

Human would need to prompt for input and Stupid, Smart, or Human activities per the problem description.

This is a possible beginning to organize the obvious hierarchy and to help you separate in your own mind what each part of Player will do.

An example of how the Java code could look when starting out using an interface and abstract method:

public interface Player
{
    abstract void grabMarbles();
    abstract int howManyMarbles();

}

public abstract class GamePlayer implements Player
{

     // Give definitions to all Player classes
     // Add more as abstract
}

public class Stupid extends GamePlayer { // What is Stupid gonna do? }

public class Smart extends GamePlayer { // What is Smart gonna do? }

public class Human extends GamePlayer { // What is Human gonna do? }
5
  • I was wondering what is the purpose of interface? isn't it okay just to make abstract and concrete classes? Jun 11, 2014 at 17:00
  • Also up to this point in the book we have not covered abstract or interface class so thats why i was asking. Jun 11, 2014 at 17:01
  • Interface is a nice way to advertise products sold in a store but still requiring a visit to see the actual object. A Java interface can be a literal interface (keyword) or an abstract class; anything which you intend to use as a model for how your objects behave. It is ok to just use abstract and concrete classes. You could even use inheritance whereby you create a player class and then extend it in other classes to improve the definition for Stupid, Smart, and Human.
    – Mushy
    Jun 11, 2014 at 17:27
  • Thank you for the wonderful explanation. I shall code my player class as such and update you. Jun 11, 2014 at 17:29
  • Inheritance is best avoided for numerous reasons: it makes subclasses dependent on implementation details, changes to the superclass can break subclasses unless you really know what you're doing, and you can only inherit from one class at a time. You should avoid it whenever possible and mark your classes final; use composition or an Interface instead. If you're forced into using inheritance, mark any methods that aren't meant to be overridden as final.
    – Doval
    Jun 11, 2014 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.