Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ch10ex5;
- import java.util.Random;
- import java.util.ArrayList;
- import java.util.stream.Collectors;
- public class mastermindGame
- {
- int pegs, colors;
- boolean win=false;
- int correctPegs=0, correctNums=0;
- mastermindGame(int pegs, int colors)
- {
- this.pegs=pegs;
- this.colors=colors;
- }
- Random rand=new Random();
- ArrayList<Integer> nums=new ArrayList();
- public void generate()
- {
- for(int c=0;c<this.pegs;c++)nums.add(rand.nextInt(colors)+1);
- }
- public void checkEach(int current, int count)
- {
- if(count==1)correctPegs=0;
- if(nums.get(count-1)==current)correctPegs++;
- ArrayList uniqueNums=(ArrayList)nums.stream().distinct().collect(Collectors.toList());
- if(uniqueNums.indexOf(current)!=-1)correctNums++;
- }
- public void game()
- {
- win=false;
- System.out.println("You have guessed "+correctPegs+" correct peg(s) and "+correctNums+" correct color(s).");
- if(correctPegs==this.pegs)
- {
- System.out.println("You Win!");
- win=true;
- }
- }
- }
Add Comment
Please, Sign In to add comment