所以我有這樣的任務,我必須做一個骰子滾,然後搜索特定組合,如果它被軋。我有一個重寫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]
問題是什麼? – SamTebbs33
(1)當你重寫'equals'時,總是重寫'hashCode'。 (2)如果你不想重複,爲什麼不用'Set'而不是'List'? – RealSkeptic
爲什麼要爲每個組合創建一個字符串數組?它似乎太複雜。我只是將骰子存儲在一個集合中,就像RealSkeptic所說的那樣,然後檢查這個集合是否包含特定的骰子(使用您的自定義哈希碼和等於方法) – vefthym