2015-06-12 143 views
0

所以我有這樣的任務,我必須做一個骰子滾,然後搜索特定組合,如果它被軋。我有一個重寫equals方法,它檢查組合並且它正常工作。來自class Dice的每個對象都有其自己的字符串數組,其中包含有關滾動的組合的信息。例如兩個滾動骰子(2, 4)的組合物上第五輥滿分5捲起,以便它的陣列具有:[.., .., .., .., 5]再從class Dice每個對象被存儲在其中,從另一方面放入一個HashMap並排骰子」串陣列的List<Dice> 。 我的努力是,我無法理解如何遍歷骰子列表,並檢查每個組合是否滾動多次,並將有關在哪個捲上的信息放入第一個,然後刪除重複。通過迭代列表尋找重複

例如假設組合(4, 1)一直滾到第一,然後在第4卷...它的字符串數組應該是這樣:[1, .., .., 4, ..],相反,HashMap中的印刷顯示2個元素(4, 1)組合和他們的自己的數組:

[1, .., .., .., ..][.., .., .., 4, ..]

我希望你能理解我的掙扎。

public class Dice { 
    private int firstDice; 
    private int secondDice; 
    public String[] rollArray; 
    public int roll; 
    public int duplicate = 1; 

    /** 
    * Constructor for the class Dice. 
    * @param first first dice 
    * @param second second dice 
    */ 
    public Dice(int first, int second) { 
    firstDice = first; 
    secondDice = second; 
    } 

    @Override 
    public String toString() { 
    return "(" + firstDice + ", " + secondDice + ")"; 
    } 

    /** 
    * Method equals used for comparing two objects from the class Dice. 
    * @param obj object from dice class 
    * @return returns true/false if conditions are matched. 
    */ 
    public boolean equals(Dice obj) { 
    return (obj.firstDice == firstDice && obj.secondDice == secondDice); 
    } 
} 



import java.util.Arrays; 
import java.util.HashMap; 
import java.util.Iterator; 
import java.util.LinkedList; 
import java.util.List; 
import java.util.Map; 
import java.util.Random; 
import java.util.Set; 

/** 
* Created by leo on 6/10/15. Class which contains all methods that realize the rolling of two dices 
* and storing the information about them in a hash map. 
*/ 
public class DiceRoller { 

    public List<Dice> diceList = new LinkedList<>(); 
    public List<String> rollingList = new LinkedList<>(); 

    /** 
    * A method which rolls two dices a number of times with random values. 
    * 
    * @param numberOfRolls number of rolls 
    */ 
    public void rollDice(int numberOfRolls) { 
    Random rand = new Random(); 
    for (int i = 0; i < numberOfRolls; i++) { 
     diceList.add(i, new Dice(rand.nextInt(7 - 1) + 1, rand.nextInt(7 - 1) + 1)); 
     diceList.get(i).rollArray = new String[numberOfRolls]; 
     diceList.get(i).roll = i + 1; 
     diceList.get(i).rollArray[i] = diceList.get(i).roll + ""; 
     rollingList.add("" + (i + 1)); 
     checkDuplicateDice(diceList, diceList.get(i)); 
    } 
    } 


    private void checkDuplicateDice(List<Dice> listOfDice, Dice tempDice) { 
    /* 
    * for (int i = 0; i < listOfDice.size(); i++) { for (int j = i + 1; j < listOfDice.size(); j++) 
    * { if (listOfDice.get(i).toString().equals(listOfDice.get(j).toString())) { 
    * listOfDice.get(i).duplicate++; } } } for (int i = 0; i < listOfDice.size(); i++) { 
    * System.out.println(listOfDice.get(i).toString() + listOfDice.get(i).duplicate); } 
    */ 
    Iterator<Dice> iter = listOfDice.iterator(); 
    while (iter.hasNext()) { 
     Dice elem = iter.next(); 
     if (elem.toString().equals(tempDice.toString())) { 
     elem.duplicate++; 
     } 
     System.out.println(elem.toString() + elem.duplicate); 
    } 
    } 

    /** 
    * A method which checks if the combination entered is rolled. 
    * 
    * @param first first dice 
    * @param second second dice 
    */ 

    public void checkCombination(int first, int second) { 
    Dice checker = new Dice(first, second); 
    int index = 1; 
    boolean flag = false; 
    for (Dice diceObject : diceList) { 
     diceObject.rollArray = new String[diceList.toArray().length]; 
     diceObject.rollArray[index - 1] = index + ""; 
     for (int i = 0; i < diceList.size(); i++) { 
     if (diceObject.rollArray[i] == null) { 
      diceObject.rollArray[i] = ".."; 
     } 
     } 

     if (diceObject.equals(checker)) { 
     System.out.println("Combination: (" + first + ", " + second + ") rolled on roll No: " 
      + index); 
     flag = true; 
     } 
     index++; 
    } 
    if (!flag) { 
     System.out.println("Combination not rolled."); 
    } 
    } 

    /** 
    * A method which stores the data of the dice and each dice'. 
    */ 
    public void hashMapThingy() { 
    System.out.print("Roll: "); 
    for (int i = 0; i < rollingList.size(); i++) { 
     System.out.print((i + 1) + " "); 
    } 

    System.out.print("\n"); 
    System.out.println("Comb:"); 
    HashMap<Dice, String[]> hm = new HashMap<>(); 
    for (Dice diceObject : diceList) { 
     hm.put(diceObject, diceObject.rollArray); 
    } 

    Set<Map.Entry<Dice, String[]>> set = hm.entrySet(); 
    for (Map.Entry<Dice, String[]> me : set) { 
     System.out.println(me.getKey() + " " + Arrays.toString(printArray(me.getValue()))); 
    } 
    } 

    /** 
    * Printer method. 
    * 
    * @param array array that contains the roll number 
    * @return returns the array string 
    */ 
    public String[] printArray(String[] array) { 
    return array; 
    } 
} 


public class Test { 
    /** 
    * Main function. 
    * 
    * @param args arguments 
    */ 
    public static void main(String[] args) { 
    int number = 5; 
    DiceRoller diceRoller = new DiceRoller(); 
    diceRoller.rollDice(number); 
// Dice.fillDiceList(); 

// Dice.printListDices(); 
    diceRoller.checkCombination(3, 2); 
    diceRoller.checkCombination(1, 3); 
    diceRoller.checkCombination(6, 3); 
    diceRoller.hashMapThingy(); 
    } 
} 

而且當前控制檯輸出:

(5, 1)2 
(5, 1)2 
(1, 1)2 
(5, 1)3 
(1, 1)2 
(5, 1)2 
(5, 1)3 
(1, 1)2 
(5, 1)2 
(1, 5)2 
(5, 1)3 
(1, 1)2 
(5, 1)2 
(1, 5)2 
(4, 4)2 
Combination not rolled. 
Combination not rolled. 
Combination not rolled. 
Roll: 1 2 3 4 5 
Comb: 
(1, 1) [.., 2, .., .., ..] 
(1, 5) [.., .., .., 4, ..] 
(5, 1) [1, .., .., .., ..] 
(5, 1) [.., .., 3, .., ..] 
(4, 4) [.., .., .., .., 5] 
+0

問題是什麼? – SamTebbs33

+1

(1)當你重寫'equals'時,總是重寫'hashCode'。 (2)如果你不想重複,爲什麼不用'Set'而不是'List'? – RealSkeptic

+0

爲什麼要爲每個組合創建一個字符串數組?它似乎太複雜。我只是將骰子存儲在一個集合中,就像RealSkeptic所說的那樣,然後檢查這個集合是否包含特定的骰子(使用您的自定義哈希碼和等於方法) – vefthym

回答

0

我想如果我能追隨Sujit切塔尼亞的邏輯,我想爲什麼不把方法從類骰子返回一個對象答案,而不是無效的,然後使用該對象安全地刪除它,沒有例外..我修改了一下,它變得非常好。

private Dice checkDuplicateDice(List<Dice> listOfDice, Dice tempDice) { 

boolean duplicate = false; 
for (Dice elem : listOfDice) { 
    if (elem.roll != tempDice.roll && elem.toString().equals(tempDice.toString())) { 
    elem.rollArray[tempDice.roll - 1] = tempDice.roll + ""; 
    duplicate = true; 
    } 
} 
if (duplicate) { 
    return tempDice; 
} 
return null; 

}

rollDice方法我插入一個新的循環for後的第一個:

for (int j = 0; j < diceList.size(); j++) { 
    if (checkDuplicateDice(diceList, diceList.get(j)) != null) { 
    diceList.remove(j); 
    } 
} 

我還修改了checkCombination方法不覆蓋陣列。我添加了一個新的全局變量listSize,它在所有骰子已經滾動後立即取diceList.size(),以便在骰子被移除後它不會改變。我使用在迴路中的listSize爲了正確標記字符串數組不包含的值的那些元素(是null

public void checkCombination(int first, int second) { 
Dice checker = new Dice(first, second); 
int index = 1; 
boolean flag = false; 
for (Dice diceObject : diceList) { 

    for (int i = 0; i < listSize; i++) { 
    if (diceObject.rollArray[i] == null) { 
     diceObject.rollArray[i] = ".."; 
    } 
    } 

    if (diceObject.equals(checker)) { 
    System.out.println("Combination: (" + first + ", " + second + ") rolled on roll No: " 
     + index); 
    flag = true; 
    } 
    index++; 
} 
if (!flag) { 
    System.out.println("Combination not rolled."); 
} 

}

然後輸出被如下:

Roll: 1 2 3 4 5 
Comb: 
(2, 1) [.., .., 3, .., ..] 
(3, 6) [.., 2, .., 4, 5] 
(6, 5) [1, .., .., .., ..] 
0

問題是與你的checkDuplicateDice方法

private void checkDuplicateDice(List<Dice> listOfDice, Dice tempDice) { 
    boolean duplicate = false; 
    for (Dice elem : listOfDice) { 
     if (elem.roll != tempDice.roll && elem.toString().equals(tempDice.toString())) { 
      elem.duplicate++; 
      elem.rollArray[tempDice.roll-1] = tempDice.roll + ""; 
      duplicate = true; 
     } 
    } 
    if(duplicate) 
     listOfDice.remove(tempDice.roll -1); 

}

和上面一樣,你需要更新你的rollArray並將它送回這樣rollArray會被更新。

這樣做,這樣是不是最好的方式,但由於上述變化會得到你想要

+0

我不知道,但沒有幫助。 –

+0

這是因爲在checkCombination方法中數組正在重新初始化。在checkCombination方法之前嘗試使用hashMapThingy –

+0

好的。它的工作是啊!謝謝。現在..找出如何移除骰子,而不會拋出任何討厭的例外。 –