Turn Based Game run by Commands
Hello everyone. I'm creating a small darts game, it has two players who take turns throwing darts. It's done by entering commands. So far, I've created a menu system with some classes to help. Here is my code.
Code :
public class Darts{
int userInput;
public static void main(String[] args) {
menu();
}
public static void menu (){
int userInput;
TextIO.putln("Darts!");
TextIO.putln();
TextIO.putln();
TextIO.putln("Please select an Option :");
TextIO.putln("1 - to Play the Game");
TextIO.putln("2 - for Instructions");
TextIO.putln("3 - to Exit the Game");
userInput = TextIO.getlnInt();
switch (userInput){
case 1 : playGame();
case 2 : instructions();
case 3 : endGame();
default : NotA();
}
}
public static void playGame() {
char anyChar;
Turn currentTurn = new Turn();
currentTurn.thrown[0] = new Throw();
TextIO.putln("What number are you aiming at?");
currentTurn.thrown[0].number = TextIO.getlnInt();
TextIO.putln("Enter 2 for a Double, 3 for a Treble, otherwise enter 1:");
currentTurn.thrown[0].multiplier = TextIO.getlnInt();
currentTurn.thrown[1] = new Throw();
TextIO.putln("What number are you aiming at?");
currentTurn.thrown[1].number = TextIO.getlnInt();
TextIO.putln("Enter 2 for a Double, 3 for a Treble, otherwise enter 1:");
currentTurn.thrown[1].multiplier = TextIO.getlnInt();
currentTurn.thrown[2] = new Throw();
TextIO.putln("What number are you aiming at?");
currentTurn.thrown[2].number = TextIO.getlnInt();
TextIO.putln("Enter 2 for a Double, 3 for a Treble, otherwise enter 1:");
currentTurn.thrown[2].multiplier = TextIO.getlnInt();
TextIO.put("Your score now is: ");
TextIO.putln(currentTurn.total - (currentTurn.thrown[0].number + currentTurn.thrown[1].number + currentTurn.thrown[2].number));
TextIO.put("Game in Progress. Press any key to continue: ");
anyChar = TextIO.getAnyChar();
menu();
}
public static void instructions() {
char anyChar;
TextIO.putln();
TextIO.putln("Commands");
TextIO.putln();
TextIO.putln("You have three darts, when prompted you need to enter your command.");
TextIO.putln("Your command will be a number followed by a number.");
TextIO.putln("The numbers will be 1, 2 and 3.");
TextIO.putln("");
TextIO.putln("They represent Single, Double or Triple");
TextIO.putln("E.g. 20 followed by 2 represents Double twenty.");
TextIO.putln();
TextIO.put("Press any key to continue: ");
anyChar = TextIO.getAnyChar();
menu();
}
public static void endGame() {
System.exit(0);
}
public static void NotA() {
menu();
}
}
Here are my other classes:
Code :
public class Turn {
Throw[] thrown = new Throw[3]; // three darts thrown per turn
int total = 501;
}
Code :
public class Throw {
int number;
int multiplier; // 3 for a treble, 2 for a double, 1 for anything else
}
Basically at this moment in time, I'm just asking how to implement the multiplier from my throw class to show Triples, Doubles and a single. This is because at the moment so far, it's only noting the single number I put down but not the multipliers.
Thanks
Re: Turn Based Game run by Commands
Did you leave off the import statements?
Where is the TextIO class defined?
Quote:
how to implement the multiplier from my throw class
I see a variable named: multiplier in the Throw class. What do you mean by "implement the multiplier"?
Are there different values you want to assign to that variable depending on .... something?
What method should make the decision on what value to assign it?
Re: Turn Based Game run by Commands
Ok, the multiplier, should have the numbers 1,2 and 3. The multiplier multiply's the number entered by 1, 2 or 3.
In playGame in my Darts class, when I enter a number, it records the number fine. But what I want to do is use the multiplier to times the number I type by 1,2 or 3 depending on what I type after "Enter 2 for a Double, 3 for a Treble, otherwise enter 1:".
Sorry If I'm not very clear, I'm quite a begginer
Re: Turn Based Game run by Commands
Quote:
I want to do is use the multiplier to times the number
Are you talking about an arithmetic expression:
value = number * multiplier;
Re: Turn Based Game run by Commands
Yes, I want to get an expression like that to make my number multiplied by the multiplier to come up with a final value.
Re: Turn Based Game run by Commands
If you have the values in the two variables, then you should be able to write the expression and compute the result. I don't understand what your problem is now.