user1010101
user1010101

Reputation: 1658

Object Oriented Approach for a simple game

enter image description here

I am working on building a simplified version of this game. The problem states that you can have a computer that is smart or stupid but i have decided that to exclude that feature for now and decided to only go with a computer that picks a random amount of objects. I posted a question earlier and worked on it. (https://softwareengineering.stackexchange.com/questions/244680/object-oriented-design-of-a-small-java-game/244683#244683)

So initially i only had designed one class. But now i have designed 3 classes as stated by the problem. I have a pile class , a player class and a game class (also my main class).

I have so far coded the Pile and Player class. I started coding the Game class aswell however i am stuck now as i dont know how to make the game class interact with the Player and Pile class. I basically determine who's turn it is first and then rotate turns till the pile of objects is complete and declare a winner...I don't know how to make the different classes interact with each other . SO i would highly appreciate if someone can guide me further and help me finish this simple game.

I sincerely apologize if i have asked a bad question. This is my first such program i am doing in java dealing with multiple classes so it is a little confusing. I do not want anyone write code but even mentioning or telling me i should make so and so methods etc will be great !

HERE IS THE CODE I HAVE SO FAR.

PILE CLASS

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;
        }
    }

}

PLAYER CLASS

package Nim;

import java.util.Scanner;
import java.util.Random;

public class Player {

    public static final int HUMAN = 0;
    public static final int COMPUTER = 1;
    private int type;

    public Player(int theType) {

        type = theType;

    }

    Scanner in = new Scanner(System.in);

    // n is number of marbles left in the pile

    public int makeMove(int n) {

        int max = n / 2;
        int grab;

        if (type == HUMAN) {

            System.out.println("There are " + n
                    + " marbles in total. However you can only"
                    + "grab no more than " + max + " marbles");
            System.out.println("Please Enter the number of marbles to grab: ");
            grab = in.nextInt();

            while (grab > max || grab < 0) {

                System.out.println("You have entered a illelgal value. Please enter a legal value: ");
                grab = in.nextInt();

            }

            return grab;

        }

        else {
            Random rand = new Random();

            grab = (rand.nextInt(n / 2 - 1) + 1);

            System.out.println("Computer has grabbed: " + grab + " marbles");

            return grab;

        }
    }

    public void updateTurn(int n) {

        type = n;

    }

}

GAME CLASS (in progress)

package Nim;

import java.util.Scanner;
import java.util.Random;

public class Game {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        Random rand = new Random();

        System.out.println(welcome());

        Pile marbles = new Pile();
        Player human = new Player(Player.HUMAN);
        Player computer = new Player(Player.COMPUTER);
        marbles.setPile();

        System.out.println("There are total: " + marbles.getPile()
                + " marbles in the Pile.");
        System.out.println("Time for a toin coss to see who goes first!");

        System.out.println("Please Select heads(0) or Tails(1): ");
        int choice = in.nextInt();

        int tossResult = rand.nextInt(2);

        boolean playerTurn = false;
        boolean computerTurn = false;

        if (choice == tossResult) {
            System.out.println("You have won the Coin Toss! You will go first!");

            human.updateTurn(0);
            playerTurn = true;

        }

        else {
            System.out.println("Computer has won the coin toss! Computer will go first");

            computer.updateTurn(1);
            computerTurn = true;
        }

        while (marbles.getPile() > 0 && marbles.hasStick()) {

            while (playerTurn) {

                int removeMarbles = human.makeMove(marbles.getPile());

                marbles.reducePile(removeMarbles);
                computerTurn = true;
                playerTurn = false;
            }

            while (computerTurn) {
                int removeMarbles = computer.makeMove(marbles.getPile());
                marbles.reducePile(removeMarbles);

                playerTurn = true;
                computerTurn = false;

            }

        }

    }

    private static String welcome() {

        return "Welcome to the Game of Nim";

    }

}

Upvotes: 1

Views: 3156

Answers (1)

Anubian Noob
Anubian Noob

Reputation: 13596

So I'm going to take a step back and look at your class diagram.

You have three classes: Pile, Player, and Game. Pile can represent the pile at the center. Game can be the class with the main method that manages everything. And Player? Well, since you're having one Player class for all three (or two) possible states (Human, Dumb, and Smart) it should be pretty easy to implement.

Let's start with Game. It need to contain two instances of Player (human and computer), and an instance of Pile. It also needs to have a loop that goes until the game is over. So let's start to come up with a basic implementation:

Starting with Pile:

private int size;

public Pile(int newSize) {
    size = newSize;
}

public boolean takeMarbles(int amount) {
    size -= amount;
    if (size < 1)
        return false;
    return true;
}

The constructor is pretty self explanatory. The takeMarbles method is returning false if the player lost, and true if they're still in the game.

Now on to Player:

public static final int HUMAN = 0;
public static final int COMPUTER = 1;

private int type;

public Player(int type) {
    this.type = type;
}

Those two static fields may seem a little out of place, but it will come together in a second. On to the Game class:

Pile pile = new Pile(size);
Player human = new Player(Player.HUMAN);
Player computer = new Player(Player.COMPUTER);

Now we have a pile with a certain size, and two players that differ by type. This is how we're using the classes.

Now we need to get the main game loop working. Basically, just add an update() method to Player that depends on it's type (ie prompts for input if player, randomly decides if computer). The update method should return the amount of marbles to take. Then create a loop in the main method in Game. The loop will call update of the player and the computer repeatedly until someone loses (takeMarbles returns false). Then it will break out of the loop and print the output.

This is the basic idea, good luck and feel free to ask more questions!

Upvotes: 1

Related Questions