views:

127

answers:

4

Please can anyone help me

public class KalaGameState implements Cloneable
{
// your code goes here
public KalaGameState(int startingStones) throws InvalidStartingStonesException
{
// your code goes here
}
public int getTurn()
{
// your code goes here
}
public int getKala(int playerNum) throws IllegalPlayerNumException
{
// your code goes here
}
public int getNumStones(int sidePitNum) throws IllegalSidePitNumException
{
// your code goes here
}
public int getNumStones(int playerNum, int sidePitNum) throws IllegalPlayerNumException,
IllegalSidePitNumException
{
// your code goes here
}
public int getScore(int playerNum) throws IllegalPlayerNumException
{
// your code goes here
}
private int getSidePitArrayIndex(int sidePitNum) throws IllegalSidePitNumException
{
// your code goes here
}
public boolean gameOver()
{
// your code goes here
}
public void makeMove(int sidePitNum) throws IllegalSidePitNumException, IllegalMoveException
{
// your code goes here
}
}


public class PlayKala
{
public static void main(String[] args)
{
KalaGame g = new KalaGame(new RandomPlayer(),
new KeyboardPlayer());
g.play();
}
} 

The RandomPLayer and KeyboardPLayer are both classes that extend KalaPlayer

A: 

It looks like your teacher wants you to use only constructors to set initial state and then rely on the defined methods to make changes to that state.

Remember: a class method is free to change the privately scoped members of the class. So the following is completely legitimate (I don't know the rules of Kala, so I've provided a generic example):

public class MyClass {
    private int number;

    public void doSomeLogic() {
         //...
         this.number += 1;
         //...
    }
}
JavadocMD
i understand that but during game play another class takes input from player and based on that the state of teh game changes. How can i pass that input value to the KalaGameState Class
fari
It seems to me like the method makeMove(...) is where most of the work is going to be done. You've shown us classes KalaGameState and PlayKala, PlayKala references KalaGame. Are you programming KalaGame or is it provided?
JavadocMD
it looks like it all should happen in your `makeMove()` function
fuzzy lollipop
i have to write kalaGame..heres what the teacher said about the classThe KalaGame class allows each player to move andcalls methods to print the corresponding output to the console window.
fari
Ah, okay then. So KalaGame should handle taking input from the user and deciding what to do with it. KalaGame should have a reference to the KalaGameState and use KalaGameState's methods (like makeMove()) to alter the state. KalaGame will need to be able to decide (by checking KalaGameState) when the game is over, whose turn it is, etc.
JavadocMD
A: 

If I understand your question correctly, it looks like your teacher wants you to design this class so that the state of the game cannot be changed from outside the class. Meaning, anything that has access to the class cannot change its state and can only create a new instance.

If that is the case you can only use the constructor to set values.

If I'm not understanding your question correctly, maybe you can explain your issue a bit more.

Vivin Paliath
The teacher has said i cannot add any public methods or variables. but how am i able to maintain the stae if i cannot set anything in it after the initial state has been set
fari
Has the teacher said that you cannot modify the signature of the constructor, or add more constructors?What properties do think you need to set?
Vivin Paliath
i cannot modify it because he is going to use it so it can interface with what the other students are making
fari
A: 

It sounds like the teacher wants you to create private variables within the class to maintain whatever game "state" that you need to. (How you do this is of course up to you.)

Keep in mind that the code you add in the public methods (the "//your code goes here" lines) can access and change the private variables that you add to the class.

J Higs
i need to be able to pass some values to the private variables from instance of this class. how do i pass them
fari
A: 

One of the principles of object oriented design is encapsulation. Classes can (should?) be designed to contain some state (local variables) and all of the logic needed to maintain and manipulate that state. A lack of public setters is a key to the 'maintain' part of that description.

As an example, think of an ArrayList. If users could directly access the guts of an ArrayList to add something to an internal array, they might forget to update the internal variable that keeps track of size. Think about how often you refer to size() when working with ArrayLists. If you couldn't trust size(), working with lists becomes much harder. By hiding both the internal data structures and the size variable, the ArrayList class forces you to use method calls instead. Those methods are then written to enforce the relationship between the internal data and size.

So, in your case, you have a constructor to initialize your state, a lot of getters to view the state, and one method to change your state: makeMove(). makeMove() should make all of the changes to local variables that are necessary to represent a move. Remember, inside methods of a class you are free to access and write to private class members as you see fit - you won't need setters.

Addendum:

To answer some of the questions you've left in comments to your original question:

You explicitly cannot explicitly call private methods on an instance of an object. They can only be called by other methods of the class or the class's constructor.

Regarding Cloneable: The Object class (from which all other classes inherit) contains a method 'Object clone()'. This means that all classes have a clone() method. clone() essentially creates and returns a copy (by default a shallow copy) of the object. Implementing Cloneable is important because if clone() is called on an object of a class that does not implement Cloneable, a CloneNotSupportedException will be thrown. Essentially, implementing the Cloneable interface turns on the clone() method. This is a brief description, and not entirely correct. Google if you want more info.

Greg Howell