/**
*    @Author: Jayesh R Prajapati
*    @Title: Cows and Bulls Game
**/

package packageCowBull;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.*;
import borland.jbcl.layout.*;

public class CowBull extends Applet {
/* The layout class is from borland ..
  not available in sun jdk*/
  XYLayout xYLayout1 = new XYLayout();

  boolean isStandalone = false;
/*  Components*/
  TextField txtGuess = new TextField();
  Label label1   = new Label();
  Label lblTried = new Label();
  Label lblTr1   = new Label();
  Label lblTr2   = new Label();
  Label lblTry3  = new Label();
  Label lblTry4  = new Label();
  Label lblTry5  = new Label();
  Label lblPrompt1 = new Label();
  Label lblPrompt2 = new Label();
  Label lblPrompt3 = new Label();
  Label lblPrompt4 = new Label();
  Label lblPrompt5 = new Label();
 
 

  Label lblshowResults = new Label();
  Label lblResult = new Label();
  Button butGiveUp  = new Button();

/* Variables */
  int iGeneratedNum =0;
  int iGenerateArry[];
  int iguessArry[];
  int iFlagArry[];
  int iNumOfBulls = 0;
  int iNumOfCows  = 0;
  int iNumOfTries = 0;

  Button butStart = new Button();
  Label lblTry6 = new Label();
  Label lblTry7 = new Label();
  Label lblTry8 = new Label();
  Label lblTry9 = new Label();
  Label lblTry10 = new Label();
  Label lblPrompt6 = new Label();
  Label lblPrompt7 = new Label();
  Label lblPrompt8 = new Label();
  Label lblPrompt9 = new Label();
  Label lblPrompt10 = new Label();
  Label lblTitle = new Label();

  //Get a parameter value
  public String getParameter(String key, String def) {
    return isStandalone ? System.getProperty(key, def) :
      (getParameter(key) != null ? getParameter(key) : def);
  }

  //Construct the applet
  public CowBull() {
  }

  //Initialize the applet
  public void init() {
    try { jbInit(); } catch (Exception e) { e.printStackTrace(); }
  }

  //Component initialization
  private void jbInit() throws Exception{
    lblPrompt3.setForeground(Color.red);
    lblPrompt3.setBackground(Color.white);
    lblPrompt3.setAlignment(1);
    lblPrompt4.setForeground(Color.red);
    lblPrompt4.setBackground(Color.white);
    lblPrompt4.setAlignment(1);
    lblPrompt5.setForeground(Color.red);
    lblPrompt5.setBackground(Color.white);
    lblPrompt5.setAlignment(1);
    lblPrompt1.setForeground(Color.red);
    lblPrompt1.setBackground(Color.white);
    lblPrompt1.setAlignment(1);
    lblPrompt2.setForeground(Color.red);
    lblPrompt2.setBackground(Color.white);
    lblPrompt2.setAlignment(1);
    lblTry6.setBackground(Color.white);
    lblTry6.setAlignment(1);
    lblTry7.setBackground(Color.white);
    lblTry7.setAlignment(1);
    lblTry8.setBackground(Color.white);
    lblTry8.setAlignment(1);
    lblTry9.setBackground(Color.white);
    lblTry9.setAlignment(1);
    lblTry10.setBackground(Color.white);
    lblTry10.setAlignment(1);
    lblPrompt6.setForeground(Color.red);
    lblPrompt6.setBackground(Color.white);
    lblPrompt6.setAlignment(1);
    lblPrompt7.setBackground(Color.white);
    lblPrompt7.setAlignment(1);
    lblPrompt7.setForeground(Color.red);
    lblPrompt8.setBackground(Color.white);
    lblPrompt8.setAlignment(1);
    lblPrompt8.setForeground(Color.red);
    lblPrompt10.setForeground(Color.red);
    lblPrompt10.setBackground(Color.white);
    lblPrompt10.setAlignment(1);
    lblTitle.setText("COWS And BULLS");
    lblTitle.setForeground(Color.green);
    lblTitle.setFont(new Font("Dialog", 1, 14));
    lblTitle.setBackground(Color.black);
    lblTitle.setAlignment(1);
    this.setBackground(Color.lightGray);
    xYLayout1.setWidth(337);
    xYLayout1.setHeight(395);
    txtGuess.addActionListener(new CowBull_txtGuess_actionAdapter(this));
    label1.setBackground(Color.lightGray);
    label1.setForeground(Color.blue);
    label1.setFont(new Font("Dialog", 1, 12));
    label1.setAlignment(1);
    label1.setText("Your Guess:");
    lblTried.setForeground(Color.red);
    lblTried.setFont(new Font("Dialog", 1, 12));
    lblTried.setText("You Tried:       |        Prompts :");
    lblTried.setBackground(Color.black);
    lblTried.setAlignment(1);
    lblTr1.setBackground(Color.white);
    lblTr1.setAlignment(1);
    lblTr2.setBackground(Color.white);
    lblTr2.setAlignment(1);
    lblTry3.setBackground(Color.white);
    lblTry3.setAlignment(1);
    lblTry4.setBackground(Color.white);
    lblTry4.setAlignment(1);
    lblTry5.setBackground(Color.white);
    lblTry5.setAlignment(1);
    lblshowResults.setBackground(Color.white);
    lblshowResults.setAlignment(1);
    lblshowResults.setForeground(Color.blue);
    lblshowResults.setFont(new Font("Dialog", 1, 12));
    lblResult.setBackground(Color.black);
    lblResult.setForeground(Color.white);
    lblResult.setFont(new Font("Dialog", 1, 12));
    lblResult.setAlignment(1);
    lblResult.setText("Result:");
    butGiveUp.setBackground(Color.black);
    butGiveUp.setForeground(Color.red);
    butGiveUp.setFont(new Font("Dialog", 1, 12));
    butGiveUp.setLabel("GiveUp :-(");
    butGiveUp.addActionListener(new CowBull_butGiveUp_actionAdapter(this));
    butStart.setBackground(Color.black);
    butStart.setForeground(Color.green);
    butStart.setFont(new Font("Dialog", 1, 12));
    butStart.setLabel("Start");
    lblPrompt9.setBackground(Color.white);
    lblPrompt9.setForeground(Color.red);
    lblPrompt9.setAlignment(1);
    butStart.addActionListener(new CowBull_butStart_actionAdapter(this));
    this.setLayout(xYLayout1);
    this.add(txtGuess, new XYConstraints(160, 34, 83, 24));
    this.add(label1, new XYConstraints(79, 35, 78, 22));
    this.add(lblTried, new XYConstraints(59, 71, 209, 21));
    this.add(lblTr1, new XYConstraints(69, 94, 81, 21));
    this.add(lblTr2, new XYConstraints(69, 117, 81, 21));
    this.add(lblTry3, new XYConstraints(69, 140, 81, 21));
    this.add(lblTry4, new XYConstraints(69, 163, 81, 21));
    this.add(lblTry5, new XYConstraints(69, 186, 81, 21));
    this.add(lblshowResults, new XYConstraints(57, 359, 274, -1));
    this.add(lblResult, new XYConstraints(3, 359, 54, -1));
    this.add(butGiveUp, new XYConstraints(164, 328, -1, 25));
    this.add(butStart, new XYConstraints(87, 328, 71, 24));
    this.add(lblPrompt3, new XYConstraints(172, 140, 81, 21));
    this.add(lblPrompt4, new XYConstraints(172, 163, 81, 21));
    this.add(lblPrompt5, new XYConstraints(172, 186, 81, 21));
    this.add(lblPrompt1, new XYConstraints(172, 94, 81, 21));
    this.add(lblPrompt2, new XYConstraints(172, 117, 81, 21));
    this.add(lblTry6, new XYConstraints(69, 210, 81, 21));
    this.add(lblTry7, new XYConstraints(69, 233, 81, 21));
    this.add(lblTry8, new XYConstraints(69, 256, 81, 21));
    this.add(lblTry9, new XYConstraints(69, 279, 81, 21));
    this.add(lblTry10, new XYConstraints(69, 302, 81, 21));
    this.add(lblPrompt6, new XYConstraints(172, 210, 81, 21));
    this.add(lblPrompt7, new XYConstraints(172, 233, 81, 21));
    this.add(lblPrompt8, new XYConstraints(172, 256, 81, 21));
    this.add(lblPrompt9, new XYConstraints(172, 279, 81, 21));
    this.add(lblPrompt10, new XYConstraints(172, 302, 81, 21));
    this.add(lblTitle, new XYConstraints(3, 2, 330, 25));

    /* Create objects and initialize them  */
    iguessArry = new int[4];
    iGenerateArry = new int[4];
    iFlagArry    = new int[11];
   }

  //Get Applet information
  public String getAppletInfo() {
    return "Applet Information";
  }

  //Get parameter info
  public String[][] getParameterInfo() {
    return null;
  }

  void txtGuess_actionPerformed(ActionEvent e) {

/* if the game was won and than strated again without
   pressing the start button */
    if(iNumOfBulls == 4){
        startGame();
    }

/*  increase the number of tries and get the input */
    String strGetGuess = txtGuess.getText();
    String strPrompt = "";

    int NumberFormatError = 0;
    try{
    Integer.parseInt(strGetGuess);
    }catch(Exception ioe){ NumberFormatError = 1; }

/* check if the number entered is a four digit number */
    if(strGetGuess.length() <4 ||
       strGetGuess.length() >4 ||
       NumberFormatError == 1 ){
      lblshowResults.setText("PLEASE ENTER ALL 4 DIGITS");
   }else{

     lblshowResults.setText("");
 /* increase the number of tries */
    iNumOfTries++;

    filliguessArry(strGetGuess);

 /* check the number of cows */
    iNumOfCows  = checkForCows();

 /* check the number of Bulls */
    iNumOfBulls = checkForBulls();

 /* form the prompt string */
    strPrompt =  iNumOfCows + "Cows:" + iNumOfBulls + "Bulls";

    if(iNumOfTries == 1){
      lblTr1.setText(strGetGuess);
      lblPrompt1.setText(strPrompt);
    }
    if(iNumOfTries == 2){
      lblTr2.setText(strGetGuess);
      lblPrompt2.setText(strPrompt);
    }
    if(iNumOfTries == 3){
      lblTry3.setText(strGetGuess);
      lblPrompt3.setText(strPrompt);
    }
    if(iNumOfTries == 4){
      lblTry4.setText(strGetGuess);
      lblPrompt4.setText(strPrompt);
    }
    if(iNumOfTries == 5){
      lblTry5.setText(strGetGuess);
      lblPrompt5.setText(strPrompt);
    }
    if(iNumOfTries == 6){
      lblTry6.setText(strGetGuess);
      lblPrompt6.setText(strPrompt);
    }
    if(iNumOfTries == 7){
      lblTry7.setText(strGetGuess);
      lblPrompt7.setText(strPrompt);
    }
    if(iNumOfTries == 8){
      lblTry8.setText(strGetGuess);
      lblPrompt8.setText(strPrompt);
    }
    if(iNumOfTries == 9){
      lblTry9.setText(strGetGuess);
      lblPrompt9.setText(strPrompt);
    }
    if(iNumOfTries == 10){
      lblTry10.setText(strGetGuess);
      lblPrompt10.setText(strPrompt);
    }

    if( iNumOfBulls == 4){
    lblshowResults.setText("CONGRAJULATIONS on winning in "+iNumOfTries+" tries" );
    }

   if(iNumOfTries == 10 && iNumOfBulls != 4){
     lblshowResults.setText("The Number Generated was :" + iGeneratedNum );
     startGame();
     }

   }//else for 4< >4 and numberformat error
   txtGuess.setText("");
 }//txtGuess_actionPerformed..

  /**
  *     @name :checkForCows
  *     @param: void
  *     @return: int gives the number of cows in the guess
  *     @description: gives the number of cows for guess
  */
  public int checkForCows(){

  /* reset the flags to 0 */
    resetFlags();

  /* set the flags to 1*/
 for(int i=0;i    int itempindex = 0;
    itempindex = iguessArry[i];
    iFlagArry[itempindex] = 1;
   }//iguessArry

 for(int i=0;i    for(int j=0;j      if(iFlagArry[i] == 1){
        if(i ==  iGenerateArry[j]){
         iNumOfCows++;
        }//if(i ==  iGenerateArry[j])
      }//if(iFlagArry[i] == 1)
    }//j
  }//i

   System.out.println("COWS:" + iNumOfCows);
   return iNumOfCows;

  }//checkForCows()

  /**
  *     @name :checkForBulls
  *     @param: void
  *     @return: int gives the number of bulls in the guess
  *     @description: gives the number of bulls for guess
  */
 public int checkForBulls(){
  /* reset the count to bulls to zero */
   iNumOfBulls = 0;

  for(int i=0;i      if(iguessArry[i] == iGenerateArry[i]){
      iNumOfBulls++;
      }
   }//for(i)

   System.out.println("Bulls:"+iNumOfBulls );
   return iNumOfBulls;
 }// checkForBulls()

  /**
   *    @name :resetFlags()
   *    @param : void
   *    @return : void
   *    @description: This is a functions which just sets the flags
   *    to zero. this is required to find only the DISTINCT digits
   *    in the guessed array for cows
  */
  public void  resetFlags(){
/* also set the count of the bulls to zero*/
    iNumOfCows = 0;

    for (int i =0; i       iFlagArry[i] = 0;
    }
  }//resetFlag()
 

  /**
   *  @name:filliguessArry
   *  @param : String : the string inputed by the user
   *  @return: void
   *  @description: this is a function which fills the
   *  array of the integers.
  */
  public void filliguessArry(String strGetGuess){
    System.out.println(strGetGuess);

    int iNum = 0;
    try{
     iNum = Integer.parseInt(strGetGuess);
    }catch(Exception ioe){
      System.out.println("You got exception");
      lblshowResults.setText("PLEASE ENTER ALL DIGITS");
   }

   /* fill the integer array */
    iguessArry[0] = (int)iNum /1000;
    iguessArry[1] = (((iNum /10)/10) % 10);
    iguessArry[2] = ((iNum /10) % 10);
    iguessArry[3] = iNum % 10;

  }//filliguessArry()

  void butStart_actionPerformed(ActionEvent e) {
  startGame();
  }

  public void startGame(){

  /* initialise the num  of tries to zero */
    iNumOfTries = 0;
/* generate  a random number */
    generateNumber();

/* set the labels to blank */
  lblTr1.setText("");
  lblTr2.setText("");
  lblTry3.setText("");
  lblTry4.setText("");
  lblTry5.setText("");
  lblTry6.setText("");
  lblTry7.setText("");
  lblTry8.setText("");
  lblTry9.setText("");
  lblTry10.setText("");

  lblPrompt1.setText("");
  lblPrompt2.setText("");
  lblPrompt3.setText("");
  lblPrompt4.setText("");
  lblPrompt5.setText("");
  lblPrompt6.setText("");
  lblPrompt7.setText("");
  lblPrompt8.setText("");
  lblPrompt9.setText("");
  lblPrompt10.setText("");
  }//startGame()

/**
   @name : ganareatNumber()
   @param: void
   @return: void
   @description: This is a function which generates
   the random number and fills tha array
*/
   public void generateNumber(){
   int iNum = 0;
   /* generate a 4 didgit number between 1000 and 9999 */
   while( iNum < 1000){
   iNum  = (int)(Math.random()* 9999);
   }

   System.out.println("Number :" + iNum);
   iGeneratedNum = iNum;

  /* fill the integer array */
    iGenerateArry[0] = (int)iNum /1000;
    iGenerateArry[1] = (((iNum /10)/10) % 10);
    iGenerateArry[2] = ((iNum /10) % 10);
    iGenerateArry[3] = iNum % 10;

   System.out.println("Generated Num:"+iGenerateArry[0]+iGenerateArry[1]+iGenerateArry[2]+iGenerateArry[3]);

  }//generateNumber()

  void butGiveUp_actionPerformed(ActionEvent e) {
     lblshowResults.setText("The Number Generated was :" + iGeneratedNum );
     startGame();
  }//butGiveUp_actionPerformed(ActionEvent e)

}//class CowBull

class CowBull_txtGuess_actionAdapter implements java.awt.event.ActionListener{
  CowBull adaptee;

  CowBull_txtGuess_actionAdapter(CowBull adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.txtGuess_actionPerformed(e);
  }
}

class CowBull_butStart_actionAdapter implements java.awt.event.ActionListener {
  CowBull adaptee;

  CowBull_butStart_actionAdapter(CowBull adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.butStart_actionPerformed(e);
  }
}

class CowBull_butGiveUp_actionAdapter implements java.awt.event.ActionListener {
  CowBull adaptee;

  CowBull_butGiveUp_actionAdapter(CowBull adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.butGiveUp_actionPerformed(e);
  }
}
back

For the list of Free applets go to HOME page.