Go to Top

Wednesday 21 December 2011

Utilities 26 : The nice old cups game

Recently, I had made a nice cups game. Most of you would be familiar with the old cups game, where a ball is kept inside one of the three cups, and while the cups are interchanged amongst themselves, you have to look out for the position of the ball inside that particular cup.

I had posted that game within a package, if you don't remember it, click here. I am posting the game individually now. Enjoy.

Java Program :

/*
 *      _____
 *     |     |              
 *     |     |
 *     |     |
 */

import java.io.*;
import java.util.Random;
class cups
{
 static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 static String feed="",err="",garb,s1,s2,s="      _____       _____       _____\n     |     |     |     |     |     |\n     |     |     |     |     |     |\n     |     |     |     |     |     |";
 static Random r=new Random();
 static int c,pos,time=900,score=0,lvl=1,iter=2,ded=0,ff=0,choice;
 static void main()throws IOException
 {
    control();
}    
 static void control()throws IOException
 {
     String choice=null;
    System.out.print("\fCups : The Game");
        System.out.print("\n\n1: New Game\n2: View score\n3: Visual demo\n4: Help\n5: Feedback\n6: Report an error\n7: Exit");
        System.out.print("\n\nEnter your choice : ");
        choice=br.readLine();
        if(choice.equals(""))
        choice=" ";
        switch(choice.charAt(0))
        {
            case '1':
            cont();
            break;
            case '2':
            details();
            control();
            break;
            case '3':
            viddem();
            break;
            case '4':
            help();
            break;
            case '5':
            feedback();
            break;
            case '6':
            error();
            break;
            case '7':
            exit();
            break;
            case 'q':
            case 'Q':
            viewer();
            break;
            default:
            System.out.print("\nInput error (Invalid input). Press enter to continue : ");
            garb=br.readLine();
            control();
}
}
    static void cont()throws IOException
    {
        String choice=" ";
        if(lvl!=1)
        {
        System.out.print("\fPress enter to continue or 'm' to initiate in-game menu : ");
        choice=choice+br.readLine();
    }
        if(choice.equals(" "))
        {
            gamerand();
            randmove();
            input();
        }
        else
        {
             System.out.print("\fIn-Game Menu\n\n1: Continue\n2: View score\n3: Restart\n4: Exit");
            System.out.print("\n\nEnter your choice : ");
            choice=br.readLine();
            switch(choice.charAt(0))
            {
                case '1':
                gamerand();
                randmove();
                input();
                break;
                case '2':
                details();
                cont();
                break;
                case '3':
                reset();
                break;
                case '4':
                exit();
                break;
                default:
                System.out.print("\nInput error (Invalid input). Press enter to continue : ");
                garb=br.readLine();
                cont();
            }
        }
    }
 static void gamerand()throws IOException
 {
     System.out.print("\fStage : "+lvl+"\n\n");
     c=r.nextInt(4);
     if(c==0)
     gamerand();
     else
     balldisp();
    }
    static void balldisp()throws IOException
    {
        if(c==1)
        {
            System.out.print("      _____       _____       _____\n     |     |     |     |     |     |\n     |  O  |     |     |     |     |\n     |     |     |     |     |     |");
            pos=1;
         }
        if(c==2)
        {
            System.out.print("      _____       _____       _____\n     |     |     |     |     |     |\n     |     |     |  O  |     |     |\n     |     |     |     |     |     |");
            pos=2;
         }
        if(c==3)
        {
            System.out.print("      _____       _____       _____\n     |     |     |     |     |     |\n     |     |     |     |     |  O  |\n     |     |     |     |     |     |");
            pos=3;
         }
        sleep(2000);
        System.out.print("\f");
    }
    static void randmove()throws IOException
        {      
        System.out.print(s);
        for(int i=1;i<iter;i++)
        {
            int c=r.nextInt(7);
            switch(c)
            {
                case 1:
                if(pos==1)
                pos=2;
                else if(pos==2)
                pos=1;
                move12();
                break;
                case 2:
                if(pos==2)
                pos=3;
                else  if(pos==3)
                pos=2;
                move23();
                break;
                case 3:
                if(pos==1)
                pos=3;
                else  if(pos==3)
                pos=1;
                move13();
                break;
                case 4:
                if(pos==1)
                pos=2;
                else  if(pos==2)
                pos=1;
                move21();
                break;
                case 5:
                if(pos==2)
                pos=3;
                else  if(pos==3)
                pos=2;
                move32();
                break;
                case 6:
                if(pos==1)
                pos=3;
                else  if(pos==3)
                pos=1;
                move31();
                break;
                default:
                i=i-1;
            }  
        }
    }
    static void input()throws IOException
    {
        int c=0;
        System.out.print("\n\n\nWhere is the ball ? : ");
        try
        {
            c=Integer.parseInt(br.readLine());
       }
        catch(Exception e)
        {
            ff=ff+1;
            System.out.print("\nLevel forfeited (Forfeits "+ff+" of 3) . Press enter to restart : ");
            garb=br.readLine();
            if(ff==3)
            {
                System.out.print("\nForfeit limit reached. Game over. Press enter to view score : ");
                garb=br.readLine();
                details();
                control();
            }
            cont();
        }
        if(c!=1235)
        {
        if(c==pos)
        {
        System.out.print("\nCorrect Answer");
        score=score+10;
    }
    if(c!=pos)
    System.out.print("\nWrong answer. The ball is in cup "+pos);
    System.out.print("\n\nPress enter to continue : ");
    garb=br.readLine();}
    level();
}  
static void level()throws IOException
{
  lvl=lvl+1;
  if(lvl<5)
  time=time-200;
  if(lvl==5)
  time=time-120;
  iter=iter+1;
  cont();
 }
 static void move12()
 {
     sleep(time);
     s1="\f                  _____       _____\n                 |     |     |     |\n                 |     |     |     |\n                 |     |     |     |\n      _____\n     |     |\n     |     |\n     |     |";
     System.out.print(s1);  
     sleep(time);
     s1="\f                  _____       _____\n                 |     |     |     |\n                 |     |     |     |\n                 |     |     |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |";
     System.out.print(s1);
     sleep(time);
     s1="\f      _____                   _____\n     |     |                 |     |\n     |     |                 |     |\n     |     |                 |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |";
     System.out.print(s1);
     sleep(time);
     s1="\f      _____       _____       _____\n     |     |     |     |     |     |\n     |  1  |     |  2  |     |  3  |\n     |     |     |     |     |     |";
  System.out.print(s1);
    }
    static void move23()
 {
        sleep(time);
        s1="\f      _____                   _____\n     |     |                 |     |\n     |     |                 |     |\n     |     |                 |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____                   _____\n     |     |                 |     |\n     |     |                 |     |\n     |     |                 |     |\n                              _____\n                             |     |\n                             |     |\n                             |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____       _____\n     |     |     |     |\n     |     |     |     |\n     |     |     |     |\n                              _____\n                             |     |\n                             |     |\n                             |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____       _____       _____\n     |     |     |     |     |     |\n     |  1  |     |  2  |     |  3  |\n     |     |     |     |     |     |";
        System.out.print(s1);
    }
    static void move13()
    {
        sleep(time);
        s1="\f                  _____\n                 |     |\n                 |     |\n                 |     |\n      _____\n     |     |\n     |     |\n     |     |\n                              _____\n                             |     |\n                             |     |\n                             |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f                  _____\n                 |     |\n                 |     |\n                 |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f                  _____\n                 |     |\n                 |     |\n                 |     |\n                              _____\n                             |     |\n                             |     |\n                             |     |\n      _____\n     |     |\n     |     |\n     |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____       _____       _____\n     |     |     |     |     |     |\n     |  1  |     |  2  |     |  3  |\n     |     |     |     |     |     |";
        System.out.print(s1);
    }
    static void move21()
    {
        sleep(time);
        s1="\f      _____                   _____\n     |     |                 |     |\n     |     |                 |     |\n     |     |                 |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____                   _____\n     |     |                 |     |\n     |     |                 |     |\n     |     |                 |     |\n      _____\n     |     |\n     |     |\n     |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f                  _____       _____\n                 |     |     |     |\n                 |     |     |     |\n                 |     |     |     |\n      _____\n     |     |\n     |     |\n     |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____       _____       _____\n     |     |     |     |     |     |\n     |  1  |     |  2  |     |  3  |\n     |     |     |     |     |     |";
        System.out.print(s1);
    }
    static void move32()
    {
        sleep(time);
         s1="\f      _____       _____\n     |     |     |     |\n     |     |     |     |\n     |     |     |     |\n                              _____\n                             |     |\n                             |     |\n                             |     |\n";
        System.out.print(s1);
        sleep(time);
         s1="\f      _____       _____\n     |     |     |     |\n     |     |     |     |\n     |     |     |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |\n";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____                   _____\n     |     |                 |     |\n     |     |                 |     |\n     |     |                 |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____       _____       _____\n     |     |     |     |     |     |\n     |  1  |     |  2  |     |  3  |\n     |     |     |     |     |     |";
        System.out.print(s1);
    }
    static void move31()
    {
        sleep(time);
        s1="\f                  _____\n                 |     |\n                 |     |\n                 |     |\n                              _____\n                             |     |\n                             |     |\n                             |     |\n      _____\n     |     |\n     |     |\n     |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f                  _____\n                 |     |\n                 |     |\n                 |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f                  _____\n                 |     |\n                 |     |\n                 |     |\n      _____\n     |     |\n     |     |\n     |     |\n                              _____\n                             |     |\n                             |     |\n                             |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____       _____       _____\n     |     |     |     |     |     |\n     |  1  |     |  2  |     |  3  |\n     |     |     |     |     |     |";
        System.out.print(s1);
    }
        static void sleep(int c)
  {
      try{Thread.sleep(c);}
      catch(Exception e){}
    }
    static void details()throws IOException
    {
        double fps=1/time;
        System.out.print("\fYour are in level : "+lvl+"\nYour current score is : "+score+"\nFPS : "+fps+"\nNo. of shuffles : "+(iter-1));
        System.out.print("\n\nPress enter to continue : ");
        garb=br.readLine();
    }
    static void exit()
    {
        System.out.print("\nThanks for trying out Cups : The Game");
        System.exit(0);
    }
    static void reset()throws IOException
    {
        time=950;
        lvl=1;
        ded=0;
        iter=2;
        score=0;
        System.out.print("\fGame reset. Press enter to continue : ");
        garb=br.readLine();
        control();
    }
    static void error()throws IOException
    {
        System.out.print("\fPlease type the error : ");
        err=err+br.readLine();
        System.out.print("\nThank you. Your report has been sent for rectification.");
        err=err+" | ";
        System.out.print("\n\nPress enter to continue : ");
        garb=br.readLine();
        control();
    }
    static void feedback()throws IOException
    {
        System.out.print("\fPlease enter your feedback : ");
        feed=feed+br.readLine();
        System.out.print("\nThank you for your feedback.");
        feed=feed+" | ";
        System.out.print("\n\nPress enter to continue : ");
        garb=br.readLine();
        control();
    }
    static void viewer()throws IOException
    {
        System.out.print("\fReports : ");
        if(err!="")
        System.out.print(err);
        else
        System.out.print("No reports submitted yet");
        System.out.print("\n\nFeedbacks : ");
        if(feed!="")
        System.out.print(feed);
        else
        System.out.print("No feedbacks submitted yet");
        System.out.print("\n\nPress enter to continue : ");
        garb=br.readLine();
        if(garb.equalsIgnoreCase("r"))
        {    
            err="";
            viewer();
        }
        if(garb.equalsIgnoreCase("f"))
        {    
            feed="";
            viewer();
        }
        control();
    }
    static void viddem()throws IOException
    {
        System.out.print("\fWelcome to the video demo\n\nThis demo will teach you how to master the game step-by-step\n\nPress enter to continue : ");
        garb=br.readLine();
        System.out.print("\fStep 1 : Look at the initial arrangement carefully\n\n");
        System.out.print("      _____       _____       _____\n     |     |     |     |     |     |\n     |     |     |  O  |     |     |\n     |     |     |     |     |     |\n");
        sleep(4000);
        for(int i=1;i<3;i++)
        {
            System.out.print("                    |\n");
            sleep(500);
        }
        sleep(500);
        System.out.print("                     -");
        sleep(500);
        for(int i=1;i<5;i++)
        {
           System.out.print("-");
           sleep(500);
        }
        sleep(50);
        System.out.print("> See, the ball is at position 'two'");
        System.out.print("\n\nNext step : ");
        garb=br.readLine();
        System.out.print("\fStep 2 : Keep your eyes onto the ball as the cups move\n");
        s1="\n\n      _____       _____       _____\n     |     |     |     |     |     |\n     |     |     |  O  |     |     |\n     |     |     |     |     |     |";
        System.out.print(s1);
        sleep(4000);
        System.out.print("\fStep 2 : Keep your eyes onto the ball as the cups move\n");
        s1="\n\n      _____                   _____\n     |     |                 |     |\n     |     |                 |     |\n     |     |                 |     |\n                  _____\n                 |     |\n                 |  O  |\n                 |     |";
        System.out.print(s1);
        sleep(900);
        System.out.print("\fStep 2 : Keep your eyes onto the ball as the cups move\n");
        s1="\n\n      _____                   _____\n     |     |                 |     |\n     |     |                 |     |\n     |     |                 |     |\n                              _____\n                             |     |\n                             |  O  |\n                             |     |";
        System.out.print(s1);
        sleep(900);
        System.out.print("\fStep 2 : Keep your eyes onto the ball as the cups move\n");
        s1="\n\n      _____       _____\n     |     |     |     |\n     |     |     |     |\n     |     |     |     |\n                              _____\n                             |     |\n                             |  O  |\n                             |     |";
        System.out.print(s1);
        sleep(900);
        System.out.print("\fStep 2 : Keep your eyes onto the ball as the cups move\n");
        s1="\n\n      _____       _____       _____\n     |     |     |     |     |     |\n     |     |     |     |     |  O  |\n     |     |     |     |     |     |";
        System.out.print(s1);
        System.out.print("\n\nNext step : ");
        garb=br.readLine();
        System.out.print("\fStep 3 : Now you know where the ball is hiding ? Hit the right key and press enter\n\n");
        System.out.print(s);
        System.out.print("\n\n\nWhere is the ball ? : ");
        sleep(5000);
        System.out.print("3");
        sleep(900);
        System.out.print("  >CLICK< ");
        sleep(900);
        System.out.print("\n\nCorrect Answer");
        sleep(900);
        System.out.print("\n\nYipeee !");
        sleep(900);
        System.out.print("\n\nNext step : ");
        garb=br.readLine();
        System.out.print("\fNow you are all ready to begin. Press enter to return to main menu : ");
        garb=br.readLine();
        control();
    }
    static void help()throws IOException
    {
        System.out.print("\fWelcome to the help guide for Cups : The Game");
        System.out.print("\n\nThe game has virtually unlimited levels and its up to you when you forfeit.");
        System.out.print("\n\nA 2 second clip will show you the position of the ball hidden in the cups.\nThen the cups will be randomly shuffled.\n");
        System.out.print("\nYou'll have to keep track of the ball and when the shuffling is over,\nenter your response by entering the cups numbers 1,2,3 respectively for each cup.");
        System.out.print("\nTry to enter the correct answer, and get rewarded with points.");
        System.out.print("\n\nAs you cross levels, the game become tougher, with more speed of shuffling the cups,\nand more number of shuffles.");
        System.out.print("\n\nGood luck and try to beat them all");
        System.out.print("\n\nPress enter to continue : ");
        garb=br.readLine();
        control();
    }
}
         

1 comment:

ShareThis