views:

52

answers:

3

This particular variant of Nim involves: Two players alternately take marbles from a pile. In each move, a player chooses how many marbles to take. The player must take at least one but at most half of the marbles. Then the other player takes a turn. The player who takes the last marbles loses. I need to: - Write a program in which a human plays vs a computer - Generate a random int between 10 and 100 to denote initial size of the pile - Generate a random int between 0 and 1 to decide whether the computer or the human takes the first turn - Generate a random int between 0 and 1 to decide whether the computer plays "smart" or "stupid" (calculating moves or choosing randomly)

The problem is that we have to use three classes: Pile, Player, and Game. I have no clue how to get all three classes to "talk" to each other and how to split up what goes where. I am completely lost on how to start this.

+1  A: 

Nominally, I would expect Game to contain one instance of Pile and two instances of Player. Game would then call the methods of each instance as necessary.

Pile would minimally contain an int to store the number of marbles in the pile, a method which would generate the initial number and a method for taking marbles.

mootinator
+2  A: 

Start by defining your classes.

You have Pile. What can you do to a Pile? What questions can you ask it? ex: "Pile, how many marbles remain?" Write these questions and actions down - they will become public methods in Pile.

You have a Player. What can a Player do? One thing a player can do is "take a turn." What else might a player do? Write all these things down - they will become public methods in Player.

You have a Game. What does a game do? probably something like "play". Write these things down, they will be public methods in Game.

Now you have 3 classes full of useful methods. Code them up. Once you've gotten that far, post back.

Tony Ennis
"What questions can you ask" seems like a good way to conceptualize this part of object design.
philosodad
A: 

If what you mean is: "I don't know how to have a class call the methods of another class" this is sort of a common problem when you first start to learn to program. So I'm going to make the possibly unwarranted assumption that you are very new to programming and OO oriented programming in particular.

Try starting by writing a class called 'Pile' with a private variable of type Integer. Call this variable "marbles" and initialize it to some number, like 50.

Write a public method for that class called 'how_many' that returns the number of marbles, and another public method called 'take_marbles' that is called with an integer and reduces the size of the pile.

Create a class called 'Game'. Game has a attribute of type 'Pile', created when a new Game is created, and it has methods that allow it to call the 'how_many' and 'take_marbles' methods of it's Pile.

Then build a little test program that creates a new Game and has it randomly reduce the number of marbles in the pile until that number reaches zero. Every time it removes some marbles, it should output the number of marbles left.

I think once you do that, you'll have an idea of what to do next.

philosodad