Advertisement
Aspirior

Klasse Game

Mar 5th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1. package ch.cleverbytes.job;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Random;
  5.  
  6. public class Game
  7. {
  8.     private ArrayList<String> code;
  9.     private ArrayList<ArrayList<String>> history = new ArrayList<>();
  10.     public static final int NUMBER_OF_COLOR = 6;
  11.     public static final int NUMBER_OF_PINS = 4;
  12.     public static final String BLACK_PIN = "O";
  13.     public static final String WHITE_PIN = "o";
  14.  
  15.     private Random rand = new Random();
  16.     private ArrayList<String> colors = new ArrayList<>();
  17.  
  18.     public Game()
  19.     {
  20.         createCode();
  21.     }
  22.  
  23.     /**
  24.      *
  25.      * @param code L�sung des Mastermind Spiels.
  26.      */
  27.     public Game(ArrayList<String> code)
  28.     {
  29.         this.code = code;
  30.     }
  31.  
  32.     /**
  33.      *
  34.      * @param codeGuessed Geratener Farbcode
  35.      * @return Ob der geratene Farbcode mit der L�sung �bereinspricht.
  36.      */
  37.     public boolean userGuesses(ArrayList<String> codeGuessed)
  38.     {
  39.         boolean allCorrect = true;
  40.         history.add(codeGuessed);
  41.         for (String s : codeGuessed)
  42.         {
  43.             int myIndex = codeGuessed.indexOf(s);
  44.  
  45.             if (s != code.get(myIndex))
  46.             {
  47.                 allCorrect = false;
  48.             }
  49.         }
  50.         return allCorrect;
  51.     }
  52.  
  53.     /**
  54.      *
  55.      * @return Gibt die L�sung zur�ck
  56.      */
  57.     public ArrayList<String> getCode()
  58.     {
  59.         return code;
  60.     }
  61.  
  62.     /**
  63.      *
  64.      * @return Gibt alle geratenen Farbcodes und ihre R�ckmeldungen zur�ck.
  65.      */
  66.     public ArrayList<Guess> getGuesses()
  67.     {
  68.         ArrayList<Guess> Guesses = new ArrayList<>();
  69.         for(ArrayList<String> guess : history)
  70.         {
  71.             Guesses.add(new Guess(guess, getFeedback(guess)));
  72.         }
  73.         return Guesses;
  74.     }
  75.  
  76.     private ArrayList<String> getFeedback(ArrayList<String> codeGuessed)
  77.     {
  78.         ArrayList<String> feedback = new ArrayList<>();
  79.         for (String s : codeGuessed)
  80.         {
  81.             int myIndex = codeGuessed.indexOf(s);
  82.  
  83.             if (s == code.get(myIndex))
  84.             {
  85.                 feedback.add(BLACK_PIN);
  86.             } else if (code.contains(s))
  87.             {
  88.                 feedback.add(WHITE_PIN);
  89.             }
  90.         }
  91.         return feedback;
  92.     }
  93.  
  94.     private void createCode()
  95.     {
  96.         colors.add("red");
  97.         colors.add("green");
  98.         colors.add("orange");
  99.         colors.add("purple");
  100.         colors.add("yellow");
  101.  
  102.         for (int i = 1; i <= NUMBER_OF_PINS; i++)
  103.         {
  104.             code.add(colors.get(rand.nextInt(colors.size() - 1)));
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement