Guest User

MasterMind_boardGame

a guest
Nov 6th, 2021
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.10 KB | None | 0 0
  1. package extra;
  2.  
  3. import java.util.Scanner;
  4. import java.io.*;
  5. import java.util.Random;
  6.  
  7. /*
  8.  *Author: Donghui Yu
  9.  *use programming knowledge and skills leaned from school to implement this chessboard game
  10.  */
  11.  
  12. public class MasterMind_boardGame{
  13.    
  14.     public static String[] codeEntered(String codeEntered_string) {//method 1
  15.         String[] codeEntered_arrayRaw = new String[codeEntered_string.length()];//create unprocessed array
  16.         int numBlank = 0;//create counter
  17.         for(int i = 0; i < codeEntered_string.length();i++) {
  18.             if(codeEntered_string.charAt(i) != ' ') {//when no blanks
  19.                 codeEntered_arrayRaw[i] = "" + codeEntered_string.charAt(i);//assign char variable to array
  20.             }else {
  21.                 numBlank++;//count the number of blanks
  22.                 codeEntered_arrayRaw[i] = "";
  23.             }
  24.         }
  25.         String[] codeEntered_arrayFine = new String[codeEntered_string.length()-numBlank];//create new array that is going to be returned
  26.         int counterCodeEntered = 0;//create the counter for codeEntered_arrayFine
  27.         for (int i = 0; i < codeEntered_string.length(); i++) {
  28.             if (!(codeEntered_arrayRaw[i].equals(""))) {
  29.                 codeEntered_arrayFine[counterCodeEntered] = codeEntered_arrayRaw[i];
  30.                 counterCodeEntered ++;
  31.             }
  32.         }
  33.         return(codeEntered_arrayFine);//return
  34.     }
  35.    
  36.    
  37.    
  38.     public static String[] shuffle(String[] array) {//method 2--use switch to shuffle the array
  39.         Random randomGeneratorInMethod = new Random();
  40.         for(int i = 0; i < array.length; i++) {//randomly switch the array for 24 times in order to make the array as random as possible
  41.             int temptIndex = randomGeneratorInMethod.nextInt(array.length);
  42.             String tempt = array[i];
  43.             array[i] = array[temptIndex];
  44.             array[temptIndex] = tempt; //switching the variables in array
  45.         }
  46.         return array;//return
  47.        
  48.     }
  49.    
  50.    
  51.    
  52.     public static String[] compareArray(String[] arrayPlayer, String[] arrayCode) {//method 3
  53.         String[] arrayResult = new String[4];//create a new array which stores the comparing result of 'ø' or 'o'
  54.         for(int i = 0; i < arrayPlayer.length; i++) {
  55.             if (arrayPlayer[i].equals(arrayCode[i])) {
  56.                 arrayResult[i] = "ø";//both color(type) and order is correct
  57.             } else {
  58.                 boolean colorRightOrderWrong = false; //set boolean value: the color is correct but the order is wrong
  59.                 for(int k = 0; k < arrayResult.length; k++) {
  60.                     if (arrayPlayer[i].equals(arrayCode[k])) {
  61.                         colorRightOrderWrong = true;
  62.                         arrayResult[i] = "o";//correct color but wrong order
  63.                     }
  64.                 }
  65.                 if (! colorRightOrderWrong) {//when both color and order are wrong
  66.                     arrayResult[i] = "";//assign "nothing"(nothing will display on the screen)
  67.                 }
  68.             }
  69.         }
  70.         return arrayResult;//return
  71.     }
  72.    
  73.    
  74.    
  75.     public static void main(String[] args) throws Exception {//main method
  76.        
  77.        
  78.         Scanner scanNormal = new Scanner(System.in);//create a normal scanner to receive input from user
  79.         PrintWriter writerCodePeg = new PrintWriter("codePegs.txt");//create new print writer
  80.         File fileCodePeg = new File("codePegs.txt");//create file
  81.         PrintWriter writerRecording = new PrintWriter("record.txt");//create; used to record win and lose of a single player
  82.        
  83.         for(int i = 0; i < 6; i++) {//store 24 code pegs into codePeg.txt
  84.             writerCodePeg.println("r");
  85.             writerCodePeg.println("y");
  86.             writerCodePeg.println("b");
  87.             writerCodePeg.println("g");
  88.             writerCodePeg.println("v");
  89.             writerCodePeg.println("i");
  90.         }
  91.         writerCodePeg.close();//save the file
  92.        
  93.         //print rules
  94.         System.out.println("RULES: in this game, there are a hidden code and you are supposed to decode by inputing 4 code pegs");
  95.         System.out.println("you have 8-12 turns to decode depending on the difficulty you choose");
  96.         System.out.println("the hidden code consists of 4 different code pegs; ");
  97.         System.out.println("there are totally 6 different code pegs, each type has 4 code pegs");
  98.         System.out.println("r for red, y for yellow, b for blue, g for green, v for violet, i for indigo");
  99.         System.out.println("the console will print 'ø' for both order and color is correct ");
  100.         System.out.println("and output 'o' for correct color but wrong order");
  101.         System.out.println("System will not print anything if you enter the wrong color");
  102.         System.out.println("however, player, the result is shuffled!");
  103.         System.out.println("believe yourself and try your best :) ");
  104.         System.out.println("");
  105.         System.out.println("");
  106.         System.out.println("");
  107.        
  108.        
  109.         System.out.println("WELCOME to MASTERMIND!!!  what is your name, player?");
  110.         String name = scanNormal.nextLine();
  111.         System.out.println("Feel HAPPY! to play this game, "+name);
  112.        
  113.        
  114.        
  115.         boolean boolMoreRounds = true;//set a boolean variable to make the big loop work;
  116.         while (boolMoreRounds) {//the big loop will iterate if the player wants to play more
  117.             Scanner scanFile = new Scanner(fileCodePeg);//create scanner of file
  118.             String[] codePegArray = new String[24];//there should only 24 elements in the code peg array
  119.             for(int i = 0; i < 24; i++) {//assign values to array
  120.                 codePegArray[i] = scanFile.next();
  121.             }//to get the array of all code pegs
  122.             scanFile.close();//close the scanFile
  123.            
  124.            
  125.             System.out.println("which difficulty level you want to play, "+name+"? enter 1 for easy(12 turns to decode) and enter 2 for hard(8 turns to decode)");
  126.             int difficulty = scanNormal.nextInt();//ask for difficulty
  127.             scanNormal.nextLine();//to convenient the following input
  128.             int turns = 12;//set and initialize how many turns the player is  going to play
  129.            
  130.             if ((difficulty != 1)&&(difficulty != 2)) {
  131.                 System.out.println("Sorry, the console cannot identify what you entered, due to the choice you choose, you have 12 turns to decode");
  132.                 turns = 12;
  133.             }else if(difficulty == 1){
  134.                 System.out.println("Thanks for your wise decision, you now have 12 turns to decode");
  135.                 turns = 12;
  136.             }else if(difficulty == 2) {
  137.                 System.out.println("Thanks for your wise decision, you now have 8 turns to decode");
  138.                 turns = 8;
  139.             }
  140.             System.out.println("");
  141.             System.out.println("");
  142.            
  143.            
  144.             String[] codeArray = new String[4];//create the blank array which will store the code
  145.             String[] codeShuffleArray = shuffle(codePegArray);//shuffle to create the code
  146.             for(int i = 0; i < 4; i++) {//assign values to code array
  147.                 codeArray[i] = codeShuffleArray[i];
  148.             }
  149.        
  150.            
  151.            
  152.             boolean winLose = false;//create and initialize; winLose will be true when the player win this round, otherwise, false
  153.             for(int i = 0; i < turns; i++) {//start the real game
  154.                 System.out.println("turn " + (i+1));
  155.                 System.out.println("enter your code with 4 code pegs");
  156.                 String codeEnteredString = scanNormal.nextLine();//prompt the user to enter their codes
  157.                 String[] codeEnteredArray = codeEntered(codeEnteredString);//convert input to array
  158.                 boolean formatCorrect = false;//set and initialize the boolean
  159.                 if(codeEnteredArray.length == 4) {
  160.                     formatCorrect = true;//when the user enter 4 code pegs, formatCorrect is true; if not, false
  161.                 }
  162.                 while(formatCorrect == false) {//to make sure the user enters in the correct format
  163.                     System.out.println("please enter your code with 4 code pegs");
  164.                     codeEnteredString = scanNormal.nextLine();//prompt the user to enter their codes
  165.                     codeEnteredArray = codeEntered(codeEnteredString);//convert input(string) to array
  166.                     if(codeEnteredArray.length == 4) {
  167.                         formatCorrect = true;//to end the loop
  168.                     }else {
  169.                         formatCorrect = false;
  170.                     }
  171.                 }
  172.                
  173.                
  174.                 System.out.print("                     result: ");
  175.                 String[] resultArray = compareArray(codeEnteredArray , codeArray);//compare the input from player and the code
  176.                 String[] resultShuffleArray = shuffle(resultArray);//shuffle the result
  177.                 for(int a = 0; a < 4; a++) {
  178.                     System.out.print(resultShuffleArray[a]);//print the result
  179.                 }
  180.                 System.out.println("");
  181.                 if ((resultArray[0].equals("ø"))&&(resultArray[1].equals("ø"))&&(resultArray[2].equals("ø"))&&(resultArray[3].equals("ø"))) {//when decoding successfully
  182.                     winLose = true;
  183.                     i = turns;//to end the loop
  184.                 } else {
  185.                     winLose = false;
  186.                 }
  187.             }
  188.            
  189.            
  190.            
  191.            
  192.             if(winLose) {//when winning
  193.                 System.out.println("Congratulation, WIN!!!");
  194.                 writerRecording.println("win");//recording the winning of player
  195.             }else if (! winLose) {//when losing
  196.                 System.out.println("Unluckily, you lose");
  197.                 System.out.println("the code is " + codeArray[0]+codeArray[1]+codeArray[2]+codeArray[3]);
  198.                 writerRecording.println("lose");//recording the losing of player
  199.             }
  200.             System.out.println("do you want another game round? enter 1 for yes and 2 for no");
  201.             int willOfPlayer = scanNormal.nextInt();
  202.             if(willOfPlayer == 1) {
  203.                 boolMoreRounds = true;//the player wants to play more
  204.             }else if(willOfPlayer == 2) {
  205.                 boolMoreRounds = false;//the player wants to stop
  206.             }else {
  207.                 System.out.println("the console cannot identify what you entered, the game round is now over");
  208.                 boolMoreRounds = false;//cannot identify what the player wants, the program automatically end the game
  209.             }
  210.            
  211.            
  212.        
  213.         }
  214.         writerRecording.close();//save win and lose
  215.         scanNormal.close();//close scanner
  216.     }
  217.  
  218. }
  219.  
Advertisement
Add Comment
Please, Sign In to add comment