Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Turn Based Game run by Commands

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.


    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:
    public class Turn {
     
    	Throw[] thrown = new Throw[3];  // three darts thrown per turn
        int total = 501;
     
    }
    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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,199
    Thanks
    49
    Thanked 2,173 Times in 2,146 Posts

    Default Re: Turn Based Game run by Commands

    Did you leave off the import statements?
    Where is the TextIO class defined?
    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?

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,199
    Thanks
    49
    Thanked 2,173 Times in 2,146 Posts

    Default Re: Turn Based Game run by Commands

    I want to do is use the multiplier to times the number
    Are you talking about an arithmetic expression:
    value = number * multiplier;

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,199
    Thanks
    49
    Thanked 2,173 Times in 2,146 Posts

    Default 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.

Similar Threads

  1. Executing commands throws an InterruptedException even with waitFor()
    By LilaH in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 20th, 2011, 03:57 AM
  2. Replies: 1
    Last Post: June 7th, 2011, 11:53 AM
  3. simple java console program, need help recalling commands
    By zero0000000 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 22nd, 2010, 05:47 AM
  4. token-based processing
    By amandat in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 21st, 2010, 12:05 AM
  5. Executing Linux Commands with Java GUI
    By linuxrockers in forum AWT / Java Swing
    Replies: 2
    Last Post: February 15th, 2010, 09:57 PM