Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7907795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:38:20+00:00 2026-06-03T11:38:20+00:00

I am developing a code breaking game i.e. Bulls and Cows in android. The

  • 0

I am developing a code breaking game i.e. Bulls and Cows in android. The problem is that in my main class I have applied a logic to check the numbers. This logic falls under a loop but when i run the application it freezes after entering into the loop. I’m tired of searching the answer on internet. I would be grateful to you people here if you could help out in some way. I regret if my code look lame to you as I’m a beginner in android programming.
The following is the code of my main class. :-

package com.bullsncows.bnc;

import java.util.Random;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

  public class Startingpoint extends Activity {
     EditText etn1, etn2, etn3, etn4;
     Button bsub;
     TextView errormsg,res;
     Random r = new Random();
     int num = 0;
     boolean guessed = false;
       int count =0;

  /** Called when the activity is first created. */
   @Override
     public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       requestWindowFeature(Window.FEATURE_NO_TITLE);
       getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
       setContentView(R.layout.main);
       initializevar();
       // making the computer select a random four digit number
       while(hasDupes(num= (r.nextInt(9000) + 1000)));
       // on clicking the submit button

       bsub.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            String n1 = etn1.getText().toString();
            String n2 = etn2.getText().toString();
            String n3 = etn3.getText().toString();
            String n4 = etn4.getText().toString();
            String cnum = num+"";
            if      (n1.length()==0||n2.length()==0||n3.length()==0||n4.length()==0) {
                errormsg.setText("Fields cannot be left blank");
                errormsg.setTextColor(Color.RED);
                errormsg.setGravity(Gravity.CENTER);
            } else if (n1.equals(n2) || n1.equals(n3) || n1.equals(n4)
                    || n2.equals(n3) || n2.equals(n4) || n3.equals(n4)) {
                errormsg.setText("Please enter distinct number");
                errormsg.setTextColor(Color.RED);
                errormsg.setGravity(Gravity.CENTER);
            }else{
                String guess = n1+n2+n3+n4;
                errormsg.setText("Correct "+ cnum + " "+ guess);
                errormsg.setTextColor(Color.GREEN);
                errormsg.setGravity(Gravity.CENTER);

            do{
                    int bulls = 0;
                    int cows = 0;
                    count++;
                    for(int i= 0;i < 4;i++){
                        if(guess.charAt(i) == cnum.charAt(i)){
                            bulls++;
                        }else if(cnum.contains(guess.charAt(i)+"")){
                            cows++;
                        }
                        else if(bulls == 4){
                            guessed = true;
                            break;
                        }else{
                            res.setText(cows+" Cows and "+bulls+" Bulls.");
                            res.setTextColor(Color.BLUE);
                            res.setGravity(Gravity.CENTER);
                        }
                    }
                }while(!guessed);
                errormsg.setText("You won after "+count+" guesses!");
                errormsg.setTextColor(Color.MAGENTA);
                errormsg.setGravity(Gravity.CENTER);
            }
        }
    });

}

private void initializevar() {
    // TODO Auto-generated method stub
    etn1 = (EditText) findViewById(R.id.etnum1);
    etn2 = (EditText) findViewById(R.id.etnum2);
    etn3 = (EditText) findViewById(R.id.etnum3);
    etn4 = (EditText) findViewById(R.id.etnum4);
    bsub = (Button) findViewById(R.id.bsubmit);
    errormsg = (TextView) findViewById(R.id.tverror);
    res = (TextView) findViewById(R.id.tvres);
}
public static boolean hasDupes(int n){
    boolean[] digs = new boolean[10];
    while(n > 0){
        if(digs[n%10]) return true;
        digs[n%10] = true;
        n/= 10;
    }
    return false;
  }

 }

The following is the XML coding for the same page :-

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Please select the numbers below" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/etnum1"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:maxLength="1" />

        <EditText
            android:id="@+id/etnum2"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:maxLength="1" />

        <EditText
            android:id="@+id/etnum3"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:maxLength="1" />

        <EditText
            android:id="@+id/etnum4"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:maxLength="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView android:id="@+id/tverror" 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"/>
        <TextView android:id="@+id/tvres" 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/bsubmit"
            android:layout_width="80dp"
            android:layout_height="40dp"
            android:layout_gravity="fill_vertical"
            android:text="Submit" />
    </LinearLayout>

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </TableLayout>

        </LinearLayout>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T11:38:22+00:00Added an answer on June 3, 2026 at 11:38 am

    without reading all of your code, i seems very unlikely

    else if(bulls == 4)
    

    will ever evalute to true, since you reset bulls each iteration

    int bulls = 0;
    

    and you only have four tries:

    for(int i= 0;i < 4;i++)
    

    Since

     else if(bulls == 4){
       guessed = true;
       break;
    

    is your only termination condition, you loop forever.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

have a really annoying time developing some code for a set of UserControls that
At the time of developing the code in c# winforms, i have a problem..
I have been developing some code that uses Data.Array to use multidimensional arrays, now
We have a software house developing code for us on a project, .NET Web
I have code running in an iPhone application I am developing. Basically, the code
I am developing an android app using code from a normal java application. In
I am developing R code that needs to run redundantly on two servers. Some
I'm developing some code snippet, and I experienced that Visual Studio doesn't automatically add
I'm developing some code that is simulating network equipment. I need to run several
I am developing a java code that signs documents using a certificate token. So

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.