KeeganT

Ch10Ex5 mastermindGame

Mar 26th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. package ch10ex5;
  2. import java.util.Random;
  3. import java.util.ArrayList;
  4. import java.util.stream.Collectors;
  5.  
  6. public class mastermindGame
  7. {
  8.     int pegs, colors;
  9.     boolean win=false;
  10.     int correctPegs=0, correctNums=0;
  11.     mastermindGame(int pegs, int colors)
  12.     {
  13.         this.pegs=pegs;
  14.         this.colors=colors;
  15.     }
  16.     Random rand=new Random();
  17.     ArrayList<Integer> nums=new ArrayList();
  18.     public void generate()
  19.     {
  20.         for(int c=0;c<this.pegs;c++)nums.add(rand.nextInt(colors)+1);
  21.     }
  22.    
  23.     public void checkEach(int current, int count)
  24.     {
  25.         if(count==1)correctPegs=0;
  26.         if(nums.get(count-1)==current)correctPegs++;
  27.         ArrayList uniqueNums=(ArrayList)nums.stream().distinct().collect(Collectors.toList());
  28.         if(uniqueNums.indexOf(current)!=-1)correctNums++;
  29.     }
  30.    
  31.     public void game()
  32.     {
  33.         win=false;
  34.         System.out.println("You have guessed "+correctPegs+" correct peg(s) and "+correctNums+" correct color(s).");
  35.         if(correctPegs==this.pegs)
  36.         {
  37.             System.out.println("You Win!");
  38.             win=true;
  39.         }
  40.     }
  41. }
Add Comment
Please, Sign In to add comment