Facebook
From Tan Jun Qi, 4 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 60
  1. /**
  2.  *
  3.  * @author junqi
  4.  */
  5.  
  6. public class Hangman {
  7.     // instant variable or attributes of the class
  8.     private String secretword;
  9.     int correct=0 ;
  10.     int wrong= 0 ;
  11.     int total= 0 ;
  12.     char [] newDisguise;
  13.        
  14.        
  15.    
  16.    
  17.     public void disguisedword()
  18.           {
  19.              
  20.              char [] Disguseses = new char [secretword.length()];
  21.              // create a char array with the length of secretword
  22.              for(int i = 0;i<secretword.length();i++)
  23.              {
  24.                  Disguseses[i] = '?';
  25.                  // assigning '?' into all the array locations
  26.              }
  27.             System.out.print("The Disguised word is <<");
  28.              for(int i = 0;i<secretword.length();i++)
  29.              {
  30.               System.out.print(Disguseses[i]);
  31.               //printing out the disguise word all values in the array.
  32.              }
  33.              System.out.print(">>");
  34.              System.out.println("");
  35.              newDisguise = Disguseses; // assigning this local char array variable to instant char array variable
  36.           }
  37.    
  38.    
  39.     public void SecretWord(String Secretword)
  40.     {
  41.         this.secretword = Secretword; // assigning value to attribute secretword
  42.        
  43.     }
  44.    
  45.    
  46.     public boolean makeGuess(String guess)
  47.     {
  48.        
  49.         boolean complete = false; // set to false
  50.         int found = 0; // if its not completed
  51.         int cfound = 0; // correct guess
  52.        
  53.         for(int i = 0;i<secretword.length();i++)
  54.         {
  55.             if(guess.charAt(0) == secretword.charAt(i))
  56.             { // if user guess is equals to secretword replace attribute char array location with guess
  57.                 newDisguise[i]=guess.charAt(0);
  58.                 cfound = 1; //
  59.             }
  60.         }
  61.        
  62.         System.out.println("");
  63.        
  64.         if(cfound==1)
  65.         { // if its correct total will increment by 1 and print out the result statement how many guess
  66.           // and how many wrong
  67.             total++;
  68.             System.out.printf("Correct, Guesses made %d with %d wrong\n",total,wrong);
  69.            
  70.         }
  71.         else
  72.         { // if cfound = 0 means its a wrong guess
  73.             wrong++; // wrong guesss + 1
  74.             total++;// total guess +1
  75.             System.out.printf("Incorrect, Guesses made %d with %d wrong\n",total,wrong);
  76.         }
  77.         System.out.println("");
  78.         System.out.print("The Disguised word is <<");
  79.         for(int i =0;i<secretword.length();i++)
  80.         {
  81.             System.out.print(newDisguise[i]); // printing the updated disguise word
  82.         }
  83.         System.out.print(">>");
  84.         System.out.println("");
  85.         System.out.println("");
  86.        
  87.         for(int i = 0;i<secretword.length();i++)
  88.         {
  89.             if(newDisguise[i]=='?')
  90.             { // checking if the disguise word is completed or not
  91.                 found = 1; // if not completed
  92.                 break;
  93.             }
  94.             else
  95.             {
  96.              found = 2;   // if completed
  97.             }
  98.         }
  99.         if(found==2)
  100.         {
  101.            
  102.             complete = true; // return true if completed
  103.            
  104.         }
  105.  
  106.        return complete; // return false and true
  107.     }
  108.    
  109.     public boolean inputval(String guess)
  110.     { // to check if the user enter more than 1 character
  111.         boolean value = false;
  112.         if(guess.length()==1)
  113.         {// checking the length of the user input
  114.             value = true;
  115.         }
  116.         return value;
  117.     }
  118.    
  119.     public boolean checkInt(String guess)
  120.     {
  121.         // catching if the user enter a digit
  122.         try
  123.         {
  124.             Integer.parseInt(guess); // if user input can be converted into int
  125.             return true; // means user entered a number
  126.         }
  127.         catch(NumberFormatException e)
  128.                 {
  129.                     return false; // not a digit.
  130.                 }
  131.     }
  132.    
  133.    
  134. }
  135.  
  136.  
  137.  
  138.  
  139. /**
  140.  *
  141.  * @author junqi
  142.  */
  143. public class HangmanTest {
  144.     public static void main (String [] args)
  145.     {
  146.     Scanner read = new Scanner(System.in);
  147.     Hangman round1 = new Hangman();
  148.     Hangman round2 = new Hangman();
  149.     String choice;
  150.     String ch;
  151.     String secretword;
  152.     int breaker=0; // used a loop condition
  153.     StudentInfo();
  154.     System.out.println("");
  155.    
  156.    
  157.     while(breaker!=-1) // whole game will loop uptil breaker is = -1
  158.     {
  159.     System.out.println("--------Lets play a round of Hangman---------");
  160.     System.out.println("Would you like to key in the secretword?(yes or no)");
  161.     choice = read.nextLine(); // give user a choice to enter keyword
  162.     if (choice.equalsIgnoreCase("yes"))
  163.     { // if user enter yes
  164.         System.out.println("Please enter the secretWord");
  165.         secretword = read.nextLine().toLowerCase(); // read the user secret word
  166.         round1.SecretWord(secretword); // user secretword will be assigned to object 1 secret word
  167.     }
  168.     else
  169.     {
  170.     round1.SecretWord("secret"); // default keyword if the user enters no
  171.     }
  172.    
  173.     round1.disguisedword(); // display the disguised word
  174.     System.out.println("Please enter your guess here");
  175.     ch = read.nextLine().toLowerCase(); // prompting for user guess
  176.     boolean real = round1.inputval(ch);//check how many character input
  177.     boolean checkint = round1.checkInt(ch); //check if it is a number
  178.    
  179.    
  180.     if (real==false || checkint==true)// checking if either the input has more than 1 char or number.
  181.     {
  182.         while(round1.inputval(ch)==false || round1.checkInt(ch)==true)
  183.         { // if user enters more than 1 char or enters a digit
  184.             if(round1.inputval(ch)==false)
  185.             { // if user enters more than 1 char prompt again
  186.                 System.out.println("Please enter single Alphabet only");
  187.                 ch = read.nextLine().toLowerCase();
  188.             }
  189.             else if(round1.checkInt(ch)==true)
  190.             { //if user enters a digit prompt again
  191.                 System.out.println("Please enter alphabet only");
  192.                 ch = read.nextLine().toLowerCase();
  193.             }
  194.         } // will check until the one character and its a alphabet is entered
  195.     }
  196.  
  197.     while (round1.makeGuess(ch)==false) // will loop until disguised word is completed and return a true
  198.     { // system checking the user input against the secretword
  199.         System.out.println("Please enter your guess here");
  200.         ch = read.nextLine().toLowerCase(); // keep prompting until true is returned
  201.         real = round1.inputval(ch);// check is it more than one char
  202.         checkint = round1.checkInt(ch);//check is it an int
  203.        
  204.         if (real==false || checkint==true)// checking if either the input has more than 1 char or number.
  205.     {
  206.         while(round1.inputval(ch)==false || round1.checkInt(ch)==true)
  207.         {
  208.             if(round1.inputval(ch)==false)
  209.             {
  210.                 System.out.println("Please enter single Alphabet only");
  211.                 ch = read.nextLine().toLowerCase();
  212.             }
  213.             else if(round1.checkInt(ch)==true)
  214.             {
  215.                 System.out.println("Please enter an alphabet only");
  216.                 ch = read.nextLine().toLowerCase();
  217.             }
  218.         }
  219.     }
  220.     }
  221.    
  222.     // prompt the user if they want to to continue
  223.     System.out.println("Would you like to stop the program?(press -1 to stop)");
  224.     breaker = read.nextInt();//read their choice
  225.     read.nextLine();
  226.     if(breaker ==-1)
  227.     {
  228.      break;  
  229.     }
  230.     // Its the same as the above program
  231.     System.out.println("------Lets play another round of Hangman--------");
  232.     System.out.println("Would you like to key in the secretword?(yes or no)");
  233.     choice = read.nextLine();
  234.     if (choice.equalsIgnoreCase("yes"))
  235.     {
  236.         System.out.println("Please enter the secretWord");
  237.         secretword = read.nextLine().toLowerCase();
  238.         round2.SecretWord(secretword);
  239.     }
  240.     else
  241.     {
  242.     round2.SecretWord("zouk");
  243.     }
  244.    
  245.     round2.disguisedword();
  246.     ch = read.nextLine().toLowerCase();
  247.     real = round2.inputval(ch);
  248.     checkint = round1.inputval(ch);
  249.    
  250.     if (real==false || checkint==true)// checking if either the input has more than 1 char or number.
  251.     {
  252.         while(round1.inputval(ch)==false || round1.checkInt(ch)==true)
  253.         {
  254.             if(round1.inputval(ch)==false)
  255.             {
  256.                 System.out.println("Please enter single Alphabet only");
  257.                 ch = read.nextLine().toLowerCase();
  258.             }
  259.             else if(round1.checkInt(ch)==true)
  260.             {
  261.                 System.out.println("Please enter an alphabet only");
  262.                 ch = read.nextLine().toLowerCase();
  263.             }
  264.         }
  265.     }
  266.     while (round2.makeGuess(ch)==false)
  267.     {
  268.         System.out.println("Please enter your guess here");
  269.         ch = read.nextLine().toLowerCase();
  270.         real = round2.inputval(ch);
  271.         checkint = round1.inputval(ch);
  272.         if (real==false || checkint==true)// checking if either the input has more than 1 char or number.
  273.      {
  274.         while(round1.inputval(ch)==false || round1.checkInt(ch)==true)
  275.         {
  276.             if(round1.inputval(ch)==false)
  277.             {
  278.                 System.out.println("Please enter single Alphabet only");
  279.                 ch = read.nextLine().toLowerCase();
  280.             }
  281.             else if(round1.checkInt(ch)==true)
  282.             {
  283.                 System.out.println("Please enter an alphabet only");
  284.                 ch = read.nextLine().toLowerCase();
  285.             }
  286.         }
  287.       }
  288.     }
  289.    
  290.     System.out.println("Would you like to continue?(press-1 to stop)");
  291.     breaker = read.nextInt();
  292.     read.nextLine();
  293.    
  294.     }
  295.     System.out.println("--------Thank you for playing----------");// if user enters -1
  296. }
  297.    
  298.     public static void StudentInfo()
  299.     {
  300.         System.out.println("My Name is: Tan Jun Qi");
  301.         System.out.println("Murdoch StudentID: 33872846");
  302.         System.out.println("Mode of enrollment: External");
  303.         System.out.println("Tutor name: Aaron Yeo");
  304.         System.out.println("Workshop: Friday 4.15pm ");
  305.     }
  306.    
  307. }
  308.  
  309.