In: Computer Science
Top-down design: Design a program called TeamGame to simulate a
simple game of drafting players to teams. Rules of the game: •
There are two teams: Team A and Team B. • There are 50 players,
with numbers from 1 to 50. • Each team will get 10 of the players.
The program should randomly select 10 players for each team. To
accomplish this, you may select each player randomly, or you may
shuffle the list of players before making teams. Note that
selecting a player does two things: Adds a player to a team, and
removes that player from the list of available players. • The team
with the minimum sum of player numbers wins the game.
Your program should display: • a list of the players on each
team
• the remaining undrafted players (30 players should remain) • the
name of the winning team
Use java, Write the full code for the TeamGame program. Develop
appropriate methods for your subproblems.
/*********************************************TestGame.java********************************/
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class TestGame {
// Two list for teamA and teamB
static ArrayList<Integer> teamA;
static ArrayList<Integer> teamB;
public static void main(String[] args) {
// List of players
ArrayList<Integer>
playerNumber = new ArrayList<>();
teamA = new
ArrayList<>();
teamB = new
ArrayList<>();
// fill the list
for (int i = 1; i <= 50; i++)
{
playerNumber.add(i);
}
// calling makeTeam method
makeTeam(playerNumber);
System.out.println("Team A = " +
teamA);
System.out.println("Team B = " +
teamB);
System.out.println("Remaining
players: " + playerNumber);
// select the winner
selectWin();
}
/*
* Make two teams from a list of players
*/
private static void makeTeam(ArrayList<Integer>
playerNumber) {
Collections.shuffle(playerNumber);
Random random = new Random();
for (int i = 0; i < 10; i++) {
int number =
playerNumber.get(random.nextInt(playerNumber.size()));
teamA.add(number);
playerNumber.remove(new Integer(number));
}
for (int i = 0; i < 10; i++)
{
int number =
playerNumber.get(random.nextInt(playerNumber.size()));
teamB.add(number);
playerNumber.remove(new Integer(number));
}
}
// calculate the score and select the winner
private static void selectWin() {
int totalScoreA = 0, totalScoreB = 0;
for (Integer integer : teamA) {
totalScoreA
+= integer;
}
for (Integer integer : teamB) {
totalScoreB
+= integer;
}
if (totalScoreA > totalScoreB) {
System.out.println("Team A Wins and Score of Team A = " +
totalScoreA + ", Team B = " + totalScoreB);
} else {
System.out.println("Team B wins and Score of Team B = " +
totalScoreB + " Team A = " + totalScoreA);
}
}
}
/********************************output*******************************/
Team A = [44, 11, 30, 36, 15, 23, 39, 49, 19, 13]
Team B = [8, 37, 14, 26, 3, 6, 18, 43, 41, 50]
Remaining players: [47, 32, 17, 31, 35, 20, 21, 45, 34, 27, 24, 5,
7, 2, 42, 28, 33, 38, 16, 40, 9, 25, 29, 10, 4, 1, 22, 46, 48,
12]
Team A Wins and Score of Team A = 279, Team B = 246
Please let me know if you have any doubt or modify the answer, Thanks and do not forget to Thumbs Up :)