A SIMPLE SCRAMBLE GAME IN JAVA

 NB: Save it as puzzle.java
package puzzle;

import java.util.ArrayList;
import java.util.Random;
import javax.swing.JOptionPane;



public class puzzle {

  public static void main(String args[])
{
      // Creation of an Arraylist
      ArrayList<Integer> buffer =  new ArrayList<Integer>();
String word;
/*
     List of the answers
     In case you wanna add your words and answers
     or you wanna practice mal-practice               
 I tried creating an array of string but there were big bed bugs there
         <<<<<<>>>>>>>            */

  String Ans1 =  "greed";
  String Ans2 = "maniac";
  String Ans3= "stimulus";
  String Ans4= "salsa";
  String Ans5="debug";
  String Ans6="crone";
  String Ans7="saliva";
  String Ans8="amaze";
  String Ans9="trash";
  String Ans10="scent";
  /*        <<<<<<<<< Feel free to add more words    >>>>>>>>>>    */
 
   // Declaration statements
  String name;
    int Tryagain,temp,num;
int got=0,i;
    Random rand=new Random();
           
           
    name=JOptionPane.showInputDialog(null,"input your name to get started","",JOptionPane.CLOSED_OPTION);       
         JOptionPane.showMessageDialog(null,"Rules of the Game \n 1. Unscramble words with small letters \n 2. You only have 4 trial to unscramble a word" ,"welcome \n"+ name,JOptionPane.PLAIN_MESSAGE);
        /** Restart is a label that makes it possible:
         *   to redo this loop without running the entire loop by using the continue function
         * to break out of this loop without the entire loop by using the break function
         */
         restart:
             /**
              * This loop is used for generating the random numbers
              * If the random number generated is not found in the arraylist buffer
              * the number is assigned to variable temp
              * variable temp now becomes my switch value
              * after each iteration, the random number is added to the arraylist
              * the 1+ at the front of the random object is to make sure that random does not generate 0 as a  number
              * if u add a scrambled word, remember to increase your random number possibilities by +1
              * num=1+rand.nextInt(11);
              */
             for (int p=0;p<100;p++){
            num=1+rand.nextInt(10);
               if (!buffer.contains(num)){
                   temp=num;
           buffer.add(num);
          
 /***************************************************************************************************/
//switch statements
 switch(temp){

 // The first case
 case 1:
   
    for (i=0;i<4;){
        i++;
        word=JOptionPane.showInputDialog(null,"E D R E G\n A strong desire for more \n (Write your answers in SMALL letters)","UNSCRAMBLE",JOptionPane.QUESTION_MESSAGE);
        if (word.equals(Ans1)){
            got++;
            JOptionPane.showMessageDialog(null, "correct",got+"\n \nquestion(s) answered correctly",JOptionPane.PLAIN_MESSAGE); continue restart;
            }
       
        else if(i==4){
            JOptionPane.showMessageDialog(null,"you have to start again","you have tried four times now", JOptionPane.HEIGHT);break restart;
            }
            else
                {JOptionPane.showMessageDialog(null,"Hint:\n word starts from G","try again" + i,JOptionPane.ERROR_MESSAGE );continue;}
        }break;

 // The second case
 case 2:
   
     for (i=0;i<4;){
         word=JOptionPane.showInputDialog(null,"A C I N M A\n A person who behaves in an extremely wild or stupid way.\n(Write your answers in SMALL letters)","UNSCRAMBLE",JOptionPane.QUESTION_MESSAGE);
            i++;
             if(word.equals(Ans2)){
                got++;
                JOptionPane.showMessageDialog(null, "correct",got+"\nquestion(s) answered correctly",JOptionPane.PLAIN_MESSAGE);continue restart;
                    }
           
                else if(i==4){
                    JOptionPane.showMessageDialog(null,"you have to start again","you have tried four times now", JOptionPane.ERROR_MESSAGE);break restart;
                    }
                    else
                        {JOptionPane.showMessageDialog(null,"Hint:\n word starts from M","try again" + i,JOptionPane.ERROR_MESSAGE );continue;}
     }break;
//The third case
 case 3:
   
     for (i=0;i<4;){
         word=JOptionPane.showInputDialog(null,"I U S M S T L U \n Something that produces a reaction.\n (Write your answers in SMALL letters)","UNSCRAMBLE  ",JOptionPane.QUESTION_MESSAGE);
            i++;
             if(word.equals(Ans3)){
                 got++;
                    JOptionPane.showMessageDialog(null, "correct",got+"\nquestion(s) answered correctly",JOptionPane.PLAIN_MESSAGE);continue restart;
                        }
           
                else if(i==4){
                    JOptionPane.showMessageDialog(null,"you have to start again","you have tried four times now", JOptionPane.ERROR_MESSAGE);break restart;
                    }
                    else
                        {JOptionPane.showMessageDialog(null,"Hint:\n word starts from S","try again" + i,JOptionPane.ERROR_MESSAGE );continue;}
     }break;
 //The fourth case
 case 4:
   
     for (i=0;i<4;){
         word=JOptionPane.showInputDialog(null,"L A S A S \n A type of Latin america dance.\n (Write your answers in SMALL letters)","UNSCRAMBLE  ",JOptionPane.QUESTION_MESSAGE);
            i++;
             if(word.equals(Ans4)){
                 got++;
                    JOptionPane.showMessageDialog(null, "correct",got+"\nquestion(s) answered correctly",JOptionPane.PLAIN_MESSAGE);continue restart;
                        }
                else if(i==4){
                    JOptionPane.showMessageDialog(null,"you have to start again","you have tried four times now", JOptionPane.ERROR_MESSAGE);break restart;
                    }
                    else
                        {JOptionPane.showMessageDialog(null,"Hint:\n word starts from S","try again" + i,JOptionPane.ERROR_MESSAGE );continue;}
     }break;
// The fifth case
 case 5:
   
     for (i=0;i<4;){
         word=JOptionPane.showInputDialog(null,"G D E U B \n The removal of  bugs in a program.\n (Write your answers in SMALL letters)","UNSCRAMBLE  ",JOptionPane.QUESTION_MESSAGE);
            i++;
             if(word.equals(Ans5)){
                 got++;
                    JOptionPane.showMessageDialog(null, "correct",got+"\nquestion(s) answered correctly",JOptionPane.PLAIN_MESSAGE);continue restart;
                        }
           
                else if(i==4){
                    JOptionPane.showMessageDialog(null,"you have to start again","you have tried four times now", JOptionPane.ERROR_MESSAGE);break restart;
                    }
                    else
                        {JOptionPane.showMessageDialog(null,"Hint:\n word starts from D","try again" + i,JOptionPane.ERROR_MESSAGE );continue;}
     }break;
// The sixth case
 case 6:
   
     for (i=0;i<4;){
         word=JOptionPane.showInputDialog(null,"R N E O C \n An ugly old woman.\n (Write your answers in SMALL letters)","UNSCRAMBLE  ",JOptionPane.QUESTION_MESSAGE);
            i++;
             if(word.equals(Ans6)){
                 got++;
                    JOptionPane.showMessageDialog(null, "correct",got+"\nquestion(s) answered correctly",JOptionPane.PLAIN_MESSAGE);continue restart;
                        }
           
                else if(i==4){
                    JOptionPane.showMessageDialog(null,"you have to start again","you have tried four times now", JOptionPane.ERROR_MESSAGE);break restart;
                    }
                    else
                        {JOptionPane.showMessageDialog(null,"Hint:\n word starts from C","try again" + i,JOptionPane.ERROR_MESSAGE );continue;}
     }break;
//The seventh case
 case 7:
   
     for (i=0;i<4;){
         word=JOptionPane.showInputDialog(null,"A S V I L A \n The liquid that is roduced in your mouth.\n (Write your answers in SMALL letters)","UNSCRAMBLE  ",JOptionPane.QUESTION_MESSAGE);
            i++;
             if(word.equals(Ans7)){
                 got++;
                    JOptionPane.showMessageDialog(null, "correct",got+"\nquestion(s) answered correctly",JOptionPane.PLAIN_MESSAGE);continue restart;
                        }
           
                else if(i==4){
                    JOptionPane.showMessageDialog(null,"you have to start again","you have tried four times now", JOptionPane.ERROR_MESSAGE);break restart;
                    }
                    else
                        {JOptionPane.showMessageDialog(null,"Hint:\n word starts from S","try again" + i,JOptionPane.ERROR_MESSAGE );continue;}
     }break;
//The eight case
 case 8:
   
     for (i=0;i<4;){
         word=JOptionPane.showInputDialog(null,"M Z A E A \n To susprise somebody.\n (Write your answers in SMALL letters)","UNSCRAMBLE  ",JOptionPane.QUESTION_MESSAGE);
            i++;
             if(word.equals(Ans8)){
                 got++;
                    JOptionPane.showMessageDialog(null, "correct",got+"\nquestion(s) answered correctly",JOptionPane.PLAIN_MESSAGE);continue restart;
                        }
           
                else if(i==4){
                    JOptionPane.showMessageDialog(null,"you have to start again","you have tried four times now", JOptionPane.ERROR_MESSAGE);break restart;
                    }
                    else
                        {JOptionPane.showMessageDialog(null,"Hint:\n word starts from A","try again" + i,JOptionPane.ERROR_MESSAGE );continue;}
     }break;
     //The ninth case

 case 9:
   
     for (i=0;i<4;){
         word=JOptionPane.showInputDialog(null,"H R S T A \n Things that you throw away because you no longer nedd them.\n (Write your answers in SMALL letters)","UNSCRAMBLE  ",JOptionPane.QUESTION_MESSAGE);
            i++;
             if(word.equals(Ans9)){
                 got++;
                    JOptionPane.showMessageDialog(null, "correct",got+"\nquestion(s) answered correctly",JOptionPane.PLAIN_MESSAGE);continue restart;
                        }
                else if(i==4){
                    JOptionPane.showMessageDialog(null,"you have to start again","you have tried four times now", JOptionPane.ERROR_MESSAGE);break restart;
                    }
                    else
                        {JOptionPane.showMessageDialog(null,"Hint:\n word starts from T","try again" + i,JOptionPane.ERROR_MESSAGE );continue;}
     }break;
//The tenth case

 case 10:
   
     for (i=0;i<4;){
         word=JOptionPane.showInputDialog(null,"E S N T C \n The pleasant smell that something has.\n (Write your answers in SMALL letters)","UNSCRAMBLE  ",JOptionPane.QUESTION_MESSAGE);
            i++;
             if(word.equals(Ans10)){
                 got++;
                    JOptionPane.showMessageDialog(null, "correct",got+"\nquestion(s) answered correctly",JOptionPane.PLAIN_MESSAGE);continue restart;
                        }
           
                else if(i==4){
                    JOptionPane.showMessageDialog(null,"you have to start again","you have tried four times now", JOptionPane.ERROR_MESSAGE);break restart;
                    }
                    else
                        {JOptionPane.showMessageDialog(null,"Hint:\n word starts from S","try again" + i,JOptionPane.ERROR_MESSAGE );continue;}
     }break;
                default:
                System.out.println("there is no need for this because random will never make a mistake");

 }}
        if (got==10){
        end();
        break restart;
            }}
 // end of switch

   
   
   
   
   
    /** A switch statement to accept the input to try again
         * if no ... it returns 1
     */
         Tryagain=JOptionPane.showConfirmDialog(null,"will you like to try again?","You got " + got +" question(s) correct",JOptionPane.YES_NO_OPTION);
    switch (Tryagain){
    case 1:
        JOptionPane.showMessageDialog(null," \n all rights reserved to proggrammer's forum (08176251802 )"," Thank you for using this program",JOptionPane.INFORMATION_MESSAGE);
        break;
         default:main(null);
            }
    /*********************************************************/
             }
// this is a method that is called upon when all questions have been answered correctly
private static void end() {
   
    JOptionPane.showMessageDialog(null,"You have answered all questions correctly","CONGRATULATION",JOptionPane.INFORMATION_MESSAGE);
    JOptionPane.showMessageDialog(null," \n all rights reserved to proggrammer's forum (08176251802 )"," Thank you for using this program",JOptionPane.INFORMATION_MESSAGE);
}
       
}
   
   
       











Previous PostOlder Post Home

0 comments:

Post a Comment