Пример #1
0
 // -----Creating random code for user to guess--------
 public static void answerGenerator() { // it's public because Main is using it
   answerKey.clear();
   answer.clear();
   for (int x = 0; x < Params.pegNumbertoGuess; x++) {
     int temp = randomIntGenerator();
     Peg answerPeg = PegCreator.pegConstructor(PegCreator.numberToPegColor(temp));
     answerKey.add(answerPeg);
     if (answer.get(answerPeg.getPegName()) == null) {
       ArrayList<Integer> colorPositions = new ArrayList<>(Params.pegNumbertoGuess);
       colorPositions.add(x);
       answer.put(answerPeg.getPegName(), colorPositions);
     } else {
       answer.get(answerPeg.getPegName()).add(x);
     }
   }
 }
Пример #2
0
 // ------Did the user guess the correct code?---- From graphics or console, just send in an
 // arraylist of Pegs, and this method will check for you
 public static ArrayList inputCheck(ArrayList<Peg> userInput) {
   // Tad PROBLEM is that this method uses the pegname or the peg color's name as the Hashmap's key
   // If we have time, I would like to fix this so that it uses Peg value as the key
   ArrayList<Peg> pegAnswer = new ArrayList<Peg>(Params.pegNumbertoGuess);
   ArrayList<Integer> checker = new ArrayList<>(Params.pegNumbertoGuess);
   HashMap<String, ArrayList<Integer>> answercopy = answercopier();
   //	ArrayList<Integer> countOfEachPeg=new ArrayList<>(Params.pegNumbertoGuess);
   for (int x = 0; x < Params.pegNumbertoGuess; x++) {
     Peg test1 = userInput.get(x);
     checker = answercopy.get(test1.getPegName());
     if (checker == null || checker.size() <= 0) { // if color doesn't exist in the answer
       pegAnswer.add(x, null);
     } else {
       boolean checkedFlag = false;
       for (int i = 0; i < checker.size(); i++) {
         if (((Integer) x).equals(checker.get(i))) { // same color and position
           pegAnswer.add(x, new BlackPeg());
           checkedFlag = true;
           answercopy.get(test1.getPegName()).remove(i); // remove the index already found
           break;
           //
         }
       }
       if (checkedFlag == false) {
         pegAnswer.add(x, new WhitePeg());
       }
     }
   }
   for (int x = 0; x < pegAnswer.size(); x++) {
     if (pegAnswer.get(x) instanceof WhitePeg) {
       if ((answercopy.get(userInput.get(x).getPegName()).size() <= 0)) {
         pegAnswer.set(x, null);
       }
     }
   }
   return pegAnswer;
 }