Results 1 to 5 of 5
Thread: Java soccer game help
- 05-06-2010, 04:17 AM #1
Member
- Join Date
- May 2010
- Posts
- 1
- Rep Power
- 0
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:
This is my code so far: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
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."); } }
- 01-30-2011, 12:39 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 01-30-2011, 12:43 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
@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.
- 01-30-2011, 12:45 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Ouch, I usually realize this stuff, Im sorry :(
- 01-30-2011, 12:49 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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
-
help Java game
By rawan in forum JCreatorReplies: 1Last Post: 12-25-2009, 06:43 PM -
Java 2d game
By Mr.Beans in forum Java 2DReplies: 0Last Post: 08-05-2009, 09:27 PM -
java game
By mayhewj7 in forum New To JavaReplies: 1Last Post: 04-10-2009, 07:01 AM -
Soccer Game
By Ayegbeni in forum Java 2DReplies: 9Last Post: 10-17-2008, 07:40 AM -
Help with my game in java
By lenny in forum New To JavaReplies: 1Last Post: 07-23-2007, 04:40 PM


LinkBack URL
About LinkBacks

Bookmarks