Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ch.cleverbytes.job;
- import java.util.ArrayList;
- import java.util.Random;
- public class Game
- {
- private ArrayList<String> code;
- private ArrayList<ArrayList<String>> history = new ArrayList<>();
- public static final int NUMBER_OF_COLOR = 6;
- public static final int NUMBER_OF_PINS = 4;
- public static final String BLACK_PIN = "O";
- public static final String WHITE_PIN = "o";
- private Random rand = new Random();
- private ArrayList<String> colors = new ArrayList<>();
- public Game()
- {
- createCode();
- }
- /**
- *
- * @param code L�sung des Mastermind Spiels.
- */
- public Game(ArrayList<String> code)
- {
- this.code = code;
- }
- /**
- *
- * @param codeGuessed Geratener Farbcode
- * @return Ob der geratene Farbcode mit der L�sung �bereinspricht.
- */
- public boolean userGuesses(ArrayList<String> codeGuessed)
- {
- boolean allCorrect = true;
- history.add(codeGuessed);
- for (String s : codeGuessed)
- {
- int myIndex = codeGuessed.indexOf(s);
- if (s != code.get(myIndex))
- {
- allCorrect = false;
- }
- }
- return allCorrect;
- }
- /**
- *
- * @return Gibt die L�sung zur�ck
- */
- public ArrayList<String> getCode()
- {
- return code;
- }
- /**
- *
- * @return Gibt alle geratenen Farbcodes und ihre R�ckmeldungen zur�ck.
- */
- public ArrayList<Guess> getGuesses()
- {
- ArrayList<Guess> Guesses = new ArrayList<>();
- for(ArrayList<String> guess : history)
- {
- Guesses.add(new Guess(guess, getFeedback(guess)));
- }
- return Guesses;
- }
- private ArrayList<String> getFeedback(ArrayList<String> codeGuessed)
- {
- ArrayList<String> feedback = new ArrayList<>();
- for (String s : codeGuessed)
- {
- int myIndex = codeGuessed.indexOf(s);
- if (s == code.get(myIndex))
- {
- feedback.add(BLACK_PIN);
- } else if (code.contains(s))
- {
- feedback.add(WHITE_PIN);
- }
- }
- return feedback;
- }
- private void createCode()
- {
- colors.add("red");
- colors.add("green");
- colors.add("orange");
- colors.add("purple");
- colors.add("yellow");
- for (int i = 1; i <= NUMBER_OF_PINS; i++)
- {
- code.add(colors.get(rand.nextInt(colors.size() - 1)));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement