Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class MasterMind {
- int[] masterCode = new int[5];
- int[] codeGuess = new int[5];
- int masterCodeLen;
- int guess;
- boolean hasWon = false;
- public MasterMind(){
- masterCode = new int[5];
- for(int i = 0; i<4; i++)
- masterCode[i] = (int)(Math.random()*4)+1;
- masterCodeLen = 4;
- guess = 0;
- hasWon = false;
- playGame();
- }
- public MasterMind(String level){
- masterCode = new int[5];
- if(level == "intermediate")
- masterCodeLen = 4;
- else{
- masterCodeLen = 5;
- }
- for(int i = 0; i<masterCodeLen; i++)
- masterCode[i] = (int)(Math.random()*7)+1;
- guess = 0;
- hasWon = false;
- playGame();
- }
- public int rightSpot(){
- int sum = 0;
- for(int i = 0; i < masterCodeLen; i++){
- if(codeGuess[i] == masterCode[i])
- sum+=1;
- }
- return sum;
- }
- public int wrongSpot(){
- int sum = 0;
- for(int i = 0; i < masterCodeLen; i++){
- if(codeGuess[i] != masterCode[i])
- sum+=1;
- }
- return sum;
- }
- public void playerGuess(){
- Scanner kbInput = new Scanner(System.in);
- System.out.println("Enter a new guess.");
- for(int i = 0; i < masterCodeLen; i++)
- codeGuess[i] = kbInput.nextInt();
- guess++;
- }
- public boolean guessChecker(){
- while(guess < 8)
- return true;
- return false;
- }
- public boolean hasWon(){
- int sum = 0;
- for(int i = 0; i < masterCodeLen; i++){
- if(codeGuess[i] == masterCode[i])
- sum+=1;
- }
- if(sum == masterCodeLen)
- return true;
- return false;
- }
- public String toString(){
- if(hasWon() == false)
- return "Sorry, you have lost. The computer's code was "+masterCode+".";
- return "You've won! It took you "+guess+" guesses.";
- }
- public void playGame(){
- while(guessChecker() == true){
- playerGuess();
- hasWon();
- }
- }
- }
Add Comment
Please, Sign In to add comment