Results 1 to 5 of 5
  1. #1
    jimmyboy17 is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default Java soccer game help

    Hi all, I have been self learning Java for a few weeks and have a problem with a task from my book. I am really stuck as to what to do, I am trying to learn Java but it is hard. If anyone could help in any way it would be greatly appreciated.

    My code is below.

    Here it the task outline:
    The task is to create a basic soccer game in JCreator.

    The team info is entered by the user. Users in turn decide whether to pass the ball or shoot for goal. A pass has a 50% success of being successful. A defender has a 10% chance of scoring a goal, midfielder 30% and striker 50% chance. When a team gets the ball it always starts with a defender. If the defender makes a successful pass it goes to the midfielder. If the midfielder makes a successful pass it goes to a striker.

    If the pass is unsuccessful, the ball goes to the defender of the other team and the other user then chooses from the menu.

    The player that currently has the ball is represented by an integer. Both the score and player that currently has the ball should be set to 0 by default.

    The menu is:
    Java Code:
    <--- Options --->
    1. Pass
    2. Shoot
    3. Restart Game
    4. Quit
    What would you like to do?

    This is what I need help doing:
    A method will be required to increase the teamís score by 1, and a method is also required to reset the teamís score. A method will be required to move the ball forward to another player, as well as one to indicate the team has lost the ball (setting the attribute of which player has the ball to 0).


    The output should look like this:
    Java Code:
    User One: Where is your team from? A
    User One: What is their name? B
    User Two: Where is your team from? C
    User Two: What is their name? D
    
    C D has the first move!
    
    <--- Current Game Status --->
    0 minutes have elapsed
    A B has 0 goals.
    C D has 0 goals. Their Defender has the ball.
    
    <--- Options --->
    1. Pass
    2. Shoot
    3. Restart Game
    4. Quit
    What would you like to do? 1
    
    The ball is passed successfully!
    
    <--- Current Game Status --->
    3 minutes have elapsed
    A B has 0 goals.
    C D has 0 goals. Their Midfielder has the ball.
    
    <--- Options --->
    1. Pass
    2. Shoot
    3. Restart Game
    4. Quit
    What would you like to do? 1
    
    The ball is passed successfully!
    
    <--- Current Game Status --->
    6 minutes have elapsed
    A B has 0 goals.
    C D has 0 goals. Their Striker has the ball.
    
    ...
    ...
    ...
    REPEATS UNTIL THE 90 MINUTES IS UP
    This is my code so far:
    Java Code:
    import java.util.*;
     
    public class Team {
        
        public static void main(String[] args) {
        
        Random generator = new Random();
        String p1TeamLocation;
        String p2TeamLocation;
        String p1TeamName;
        String p2TeamName;
        int p1TeamScore = 0;
        int p2TeamScore = 0;	
        String p1Position;	
        String p2Position;	
        int randomNumber; 			
    			
    	// Team info
    		Scanner scan = new Scanner(System.in);
    		
    		System.out.print("User One: Where is your team from? ");
    		p1TeamLocation = scan.next();	
    		
    		System.out.print("User One: What is their name? ");
    		p1TeamName = scan.next();	
    			
    	    System.out.print("User Two: Where is your team from?");
    	    p2TeamLocation=scan.next();
    		
    		System.out.print("User Two: What is their name? ");
    		p2TeamName = scan.next();
    		
    		
    		randomNumber = generator.nextInt(2);
    		
    		if (randomNumber == 0)
    		{
    			System.out.println(p1TeamLocation + " " + p1TeamName + " has the first move!");
    		}
    		else
    		{
    			System.out.println(p2TeamLocation + " " + p2TeamName + " has the first move!" );
    		}
    
    		System.out.println();
    		System.out.println("<--- Current Game Status --->");
    		
    		
    		System.out.print(p1TeamLocation + " " + p1TeamName + " has " + p1TeamScore + " goals.");
    		if (randomNumber == 0)
    		{
    			System.out.println(" Their Defender has the ball.");
    		} else { System.out.println();
    		}
    
    		System.out.print(p2TeamLocation + " " + p2TeamName+ " has " + p2TeamScore + " goals.");
    		if (randomNumber == 1)
    		{
    			System.out.println(" Their Defender has the ball.");
    		}
    		
        }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    First off, why are you clumping everything in main? Why not try and make a class for a team which has variables for score, location, name, and methods for pass, shoot, etc. Then in main you can prompt for locations and create team objects with the user defined info. From there you can print a menu with choices and use switch cases to do the correct thing.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    @sunde: The OP is dated 05-05-10...

    @elanspark: Hi, welcome to the forum!

    You shouldn't really post randomly in someone else's thread - there is an Introductions forum that might be a more appropriate place to start your own thread.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Ouch, I usually realize this stuff, Im sorry :(

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Not your fault. I often forget to glance at the "Views" count which often indicates the return of the undead. (I have no idea why the view count would increase the way it does - there must be a lot of people being led by google to some very random forum threads. Or maybe its a side effect of some periodic action taken by the server.)

Similar Threads

  1. help Java game
    By rawan in forum JCreator
    Replies: 1
    Last Post: 12-25-2009, 07:43 PM
  2. Java 2d game
    By Mr.Beans in forum Java 2D
    Replies: 0
    Last Post: 08-05-2009, 10:27 PM
  3. java game
    By mayhewj7 in forum New To Java
    Replies: 1
    Last Post: 04-10-2009, 08:01 AM
  4. Soccer Game
    By Ayegbeni in forum Java 2D
    Replies: 9
    Last Post: 10-17-2008, 08:40 AM
  5. Help with my game in java
    By lenny in forum New To Java
    Replies: 1
    Last Post: 07-23-2007, 05:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •