1

我正在开发一个代码破解游戏,即 android 中的 Bulls and Cows。问题是,在我的主课中,我应用了一个逻辑来检查数字。这个逻辑属于循环,但是当我运行应用程序时,它在进入循环后冻结。我厌倦了在互联网上搜索答案。如果您能以某种方式提供帮助,我将不胜感激。如果我的代码对你来说很蹩脚,我很遗憾,因为我是 android 编程的初学者。以下是我的主类的代码。:-

包 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;
  }

 }

以下是同一页面的 XML 编码:-

    <?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>
4

2 回答 2

5

如果不阅读您的所有代码,我似乎不太可能

else if(bulls == 4)

将永远评估为真,因为您每次迭代都会重置公牛

int bulls = 0;

你只有四次尝试:

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

自从

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

是您唯一的终止条件,您将永远循环。

于 2012-05-03T03:06:30.733 回答
0

如果您可以将代码范围缩小到导致问题的区域,那将有所帮助。但我可能猜到有错误的循环是

while(n > 0){
    n/= 10;
}

n 可能永远不会达到 0。你能单步执行你的代码并准确找出哪一部分失败了吗?

于 2012-05-03T03:03:21.453 回答