0

I am implementing RISK/Conquer game in JAVA. During the start-up phase of the game, I have to assign countries to each and every player. Just like distributing of cards from deck one by one to each player in the round-robin fashion.

The relation between Country and Player :

One Country has One Player (One to One)

One Player has Many Country (One to Many)

So far, this is my code:

Player.java

public class Player {

    public String name;
    public int totalArmies;
    public List<Country> assignedCountries;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public int getTotalArmies() {
        return totalArmies;
    }

    public void setTotalArmies(int totalArmies) {
        this.totalArmies = totalArmies;
    }

    public List<Country> getAssignedCountries() {
        return assignedCountries;
    }

    public void setAssignedCountries(List<Country> assignedCountries) {
        this.assignedCountries = assignedCountries;
    }


    public Player(String name) {
        super();
        this.name = name;
    }

startup.java

 public class startup {
        HashMap<Country, List<Country>> graphMap = new HashMap<>();
    List<Country> neigNodesList = new ArrayList<>();
    List<Country> neigNodesList1 = new ArrayList<>();
        static List<Country> listofCountrytoAssignPlayers = new ArrayList<>();
        static List<Player> player = new ArrayList<>();
    public  void startup() {

            Country neig1 = new Country("ABC", 13, 14, "Asia");
            neigNodesList.add(neig1);
            Country neig2 = new Country("XYZ", 13, 14, "Asia");
            neigNodesList.add(neig2);

            Country country1 = new Country("MNP", 10, 11, "NorthAmerica");
            Country country2 = new Country("QWERTY", 10, 11, "NorthAmerica");
            Country country3 = new Country("IJK", 10, 11, "NorthAmerica");



            graphMap.put(country1, neigNodesList);
            graphMap.put(country2, neigNodesList1);
            graphMap.put(country3, neigNodesList);


            Iterator it = graphMap.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry pair = (Map.Entry) it.next();
                Country keyCountry = (Country) pair.getKey();
                listofCountrytoAssignPlayers.add(keyCountry);
                List<Country> neiCountryList = (List<Country>) pair.getValue();
                System.out.println("Country -->" + keyCountry.getCountryName());
                System.out.print("------Neigh List--------");
                System.out.println(graphMap.size());

                for (Country county : neiCountryList) {
                    System.out.println(county.getCountryName());
                }
                System.out.println("-----------");


            }// avoids a ConcurrentModificationException
        }

        public static void main(String args[]) {
            startup s1= new startup();
            s1.startup();
            System.out.println("Enter number of players:  ");
            Scanner scanner = new Scanner(System.in);
            int totalPlayers = scanner.nextInt();
            List<Player> playerList = new ArrayList<Player>();
            int i = 0;
            while (i < totalPlayers) {
                playerList.add(new Player("player" + i));
                i++;
            }

            Collections.shuffle(listofCountrytoAssignPlayers);
            for(Player player:playerList){
                List<Country> countryforaPlayer = new ArrayList<>();
                 for(int k=0;k<playerList.size();k++)
                 {
                    countryforaPlayer.add(listofCountrytoAssignPlayers.get(k));
                 }
                 //player.setAssignedCountries(countryforaPlayer);
                 playerList.add(player);
            }
        }

    }

I am getting the following error:

Enter number of players:  
1
Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
    at java.util.ArrayList$Itr.next(ArrayList.java:851)
    at startupphase.startup.main(startup.java:86)

Process finished with exit code 1

What I am not getting is how to implement it, every algorithm/logic think of and try to implement it I am not getting the desired result.

shahakshay94
  • 145
  • 1
  • 2
  • 13

1 Answers1

1

Because you are modifying the collection (playerList) in the same loop, you are getting this ConcurrentModificationException.

Please go through this SO link. You need to use

ListIterator

if you want to add any element in the same loop.

ManishKr
  • 211
  • 2
  • 9