Java – Object Oriented Design for Small Games

javaobject-oriented-design

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.

Best Answer

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? }
Related Topic