This is a simple Football Simulator for Java beginners that is seen in a console.
1. Create main integer and boolean variables. The scoring will be based off of touchdowns, field goals, and turnovers. There will also be two teams, which are simply called the home and away teams.
public class Simulator { public static void main(String[] args) { int touchDown = 7; int fieldGoal = 3; int homeScore = 0; int awayScore = 0; boolean homeBall = true; boolean awayBall = false; boolean gameInProgress = false;
2. Now we need to create a coin toss to see which team should go first.
3. Now for the game loop. The computer will randomly decide whether the team will score a TD(40% chance), field goal(20% chance), or a turnover(40% chance). Once one of those events happen the other team gets possession. So on and so forth...
while (gameInProgress){ while (homeBall){ double homeChance = Math.random(); if (homeChance < 0.4){ System.out.print("Home turnover!"); System.out.println( " Home: " + homeScore + " Away: " + awayScore ); homeBall = false; awayBall = true; } else if (0.4 <= homeChance){ if (homeChance < 0.8){ homeScore = homeScore + fieldGoal; System.out.print("Home field goal!"); System.out.println( " Home: " + homeScore + " Away: " + awayScore ); homeBall = false; awayBall = true; }else{ homeScore = homeScore + touchDown; System.out.print("Home touch down!"); System.out.println( " Home: " + homeScore + " Away: " + awayScore ); homeBall = false; awayBall = true; } } } while (awayBall){ double awayChance = Math.random(); if (awayChance < 0.4){ System.out.print("Away turnover!"); System.out.println( " Home: " + homeScore + " Away: " + awayScore ); homeBall = true; awayBall = false; } else if (0.4 <= awayChance){ if (awayChance < 0.8){ awayScore = awayScore + fieldGoal; System.out.print("Away field goal!"); System.out.println( " Home: " + homeScore + " Away: " + awayScore ); homeBall = true; awayBall = false; }else{ awayScore = awayScore + touchDown; System.out.print("Away touch down!"); System.out.println( " Home: " + homeScore + " Away: " + awayScore ); homeBall = true; awayBall = false; } } }
4. Lastly we need to end the game. For simplicity, we'll just have the game stop when a team reaches 50 points or higher and then display the final score.
5. Now your program should be finished. Run it and the console should read something like...
The home team won the coin toss!
Home turnover! Home: 0 Away: 0
Away touch down! Home: 0 Away: 7
Home field goal! Home: 3 Away: 7
Away touch down! Home: 3 Away: 14
Home touch down! Home: 10 Away: 14
Away touch down! Home: 10 Away: 21
Home field goal! Home: 13 Away: 21
Away field goal! Home: 13 Away: 24
Home touch down! Home: 20 Away: 24
Away field goal! Home: 20 Away: 27
Home field goal! Home: 23 Away: 27
Away touch down! Home: 23 Away: 34
Home touch down! Home: 30 Away: 34
Away turnover! Home: 30 Away: 34
Home field goal! Home: 33 Away: 34
Away turnover! Home: 33 Away: 34
Home field goal! Home: 36 Away: 34
Away field goal! Home: 36 Away: 37
Home field goal! Home: 39 Away: 37
Away field goal! Home: 39 Away: 40
Home touch down! Home: 46 Away: 40
Away field goal! Home: 46 Away: 43
Home field goal! Home: 49 Away: 43
Away turnover! Home: 49 Away: 43
Home field goal! Home: 52 Away: 43
Away field goal! Home: 52 Away: 46
Final Score: Home 52, Away 46
Pretty simple, but good practice for learning loops and other basic stuff. From here you could add things like interceptions, timeouts, a timer, and other events that might happen in a football game to make it more realistic. Hope you enjoyed!