Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys,

I'm having a problem with my java game I am making. It is a darts game, but to "throw" a dart, you must enter a command. I've managed to create a way to "throw" 3 darts, with the final score of the 3 darts deducted from 501. This is done in the "playGame" part of my Darts class.

I want to be able to make a turn system from what I have made already, and a way too make it for 2 players. Thanks!

Here is my code:

Java
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();
		TextIO.put("You scored: ");
		TextIO.putln(currentTurn.thrown[0].number * currentTurn.thrown[0].multiplier);
		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();
		TextIO.put("You scored: ");
		TextIO.putln(currentTurn.thrown[1].number * currentTurn.thrown[1].multiplier);
		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("You scored: ");
		TextIO.putln(currentTurn.thrown[2].number * currentTurn.thrown[2].multiplier);
		TextIO.put("Your score now is: ");
		TextIO.putln(currentTurn.sTotal - ((currentTurn.thrown[0].number * currentTurn.thrown[0].multiplier) + (currentTurn.thrown[1].number * currentTurn.thrown[1].multiplier) + (currentTurn.thrown[2].number * currentTurn.thrown[2].multiplier)));
		
		int nTotal = currentTurn.sTotal - ((currentTurn.thrown[0].number * currentTurn.thrown[0].multiplier) + (currentTurn.thrown[1].number * currentTurn.thrown[1].multiplier) + (currentTurn.thrown[2].number * currentTurn.thrown[2].multiplier));

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


That was my main menu, with the system of taking the first turn. Here are the other classes I use:
Java
public class Turn {
	
	Throw[] thrown = new Throw[3];  // three darts thrown per turn
	int sTotal = 501;    
}


Java
public class Throw {
	
	  int number;
	  int multiplier; // 3 for a treble, 2 for a double, 1 for anything else

}
Posted
Updated 11-Aug-11 8:57am
v2
Comments
Dr.Walt Fair, PE 11-Aug-11 15:57pm    
I'm going to assume you are not asking someone to write or design your program for you, so what have you tried so far and where are you getting stuck?
Elliot Harrison 11-Aug-11 16:01pm    
All I need to know at the moment is how to create an unlimited amount of turns, from the code in "playGame", with the score carrying on from then. I generally can't think what to do to create it

1 solution

You need to think about how a darts match runs in the real world:
The playGame() function runs until one player gets down to zero. Each player takes it in turns to throw, and has a total of 3 throws. After each throw the score is checked to see if the game has ended. Repeat.
So playGame() should look something like:
while not finished
    for darts = 1 to 3
        score = player1.throw()
        if score == 0
            finished = true
            break;
    if finished
        break;
// repeat the above for player 2

// repeat the entire process
 
Share this answer
 
Comments
Elliot Harrison 12-Aug-11 9:47am    
And I'd be able to make this from "playGame" with the code I already have?
Richard MacCutchan 12-Aug-11 12:39pm    
Possibly, but I was rather trying to get you to think about the structure of your application first. Then when you have the structure and the various classes clear, you can start putting them all together to make the game. That's what programming is all about.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900