2017-10-05 4 views
0

JAVAでRISK/CONQUERのゲームを実装しています。ゲームのスタートアップ段階では、各プレイヤーに各国を割り当てる必要があります。デッキからカードを一人ずつラウンドロビン方式で各プレイヤーに配布するのと同じです。PlayerListのPlayerにCountry Listで国を1つずつ割り当てるには

国と選手の関係は:

一国がワンプレーヤー(マンツーマン)

一人のプレイヤーがこれまでのところ、これは私のコードの多くの国(多くの一つ)

がありましたあり:

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); 
      } 
     } 

    } 

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 

私は何を取得しておりませんと、それを実装する方法を、すべてのアルゴリズム/ロジックを考えると、それを実装しようとしています私は希望の結果を得ていません。

答えて

0

同じループ内でコレクション(playerList)を変更するため、このConcurrentModificationExceptionが発生しています。

これを実行してください。link あなたは同じループ内の任意の要素を追加したい場合は

ListIterator

を使用する必要があります。

関連する問題