簡體   English   中英

我想用java做一個猜詞游戲。 我想在數組中添加更多單詞

[英]I want to make a word guess game using java. I want to add more words in array

它基本上是一個猜詞游戲。 我想在每次猜測后添加更多單詞並增加分數。 我還想添加一個30秒的計時器,玩家必須在時限內回答。 正如您目前所看到的,我已將字母表及其正確答案存儲在一個數組中。 我願意添加一套完整的單詞並比較每個單詞的答案。 我怎樣才能做到這一點?

package com.example.anggarisky.wordmatters;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

private int presCounter = 0;
private int maxPresCounter = 4;
private String[] keys = {"R", "I", "B", "D", "X"};
private String textAnswer = "BIRD";
TextView textScreen, textQuestion, textTitle;
Animation smallbigforth;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    smallbigforth = AnimationUtils.loadAnimation(this, R.anim.smallbigforth);

    keys = shuffleArray(keys);

    for (String key : keys) {
        addView(((LinearLayout) findViewById(R.id.layoutParent)), key, ((EditText) 
findViewById(R.id.editText)));
    }

    maxPresCounter = 4;
}


private String[] shuffleArray(String[] ar) {
    Random rnd = new Random();
    for (int i = ar.length - 1; i > 0; i--) {
        int index = rnd.nextInt(i + 1);
        String a = ar[index];
        ar[index] = ar[i];
        ar[i] = a;
    }
    return ar;
}


private void addView(LinearLayout viewParent, final String text, final EditText editText) {
    LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
    );

    linearLayoutParams.rightMargin = 30;

    final TextView textView = new TextView(this);

    textView.setLayoutParams(linearLayoutParams);
    textView.setBackground(this.getResources().getDrawable(R.drawable.bgpink));
    textView.setTextColor(this.getResources().getColor(R.color.colorPurple));
    textView.setGravity(Gravity.CENTER);
    textView.setText(text);
    textView.setClickable(true);
    textView.setFocusable(true);
    textView.setTextSize(32);

    Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/FredokaOneRegular.ttf");

    textQuestion = (TextView) findViewById(R.id.textQuestion);
    textScreen = (TextView) findViewById(R.id.textScreen);
    textTitle = (TextView) findViewById(R.id.textTitle);

    textQuestion.setTypeface(typeface);
    textScreen.setTypeface(typeface);
    textTitle.setTypeface(typeface);
    editText.setTypeface(typeface);
    textView.setTypeface(typeface);

    textView.setOnClickListener(new View.OnClickListener() {

        @SuppressLint("SetTextI18n")
        @Override
        public void onClick(View v) {
            if(presCounter < maxPresCounter) {
                if (presCounter == 0)
                    editText.setText("");

                editText.setText(editText.getText().toString() + text);
                textView.startAnimation(smallbigforth);
                textView.animate().alpha(0).setDuration(300);
                presCounter++;

                if (presCounter == maxPresCounter)
                    doValidate();
            }
        }
    });


    viewParent.addView(textView);


}


    private void doValidate() {
    presCounter = 0;

    EditText editText = findViewById(R.id.editText);
    LinearLayout linearLayout = findViewById(R.id.layoutParent);

    if(editText.getText().toString().equals(textAnswer)) {
        Toast.makeText(MainActivity.this, "Correct", Toast.LENGTH_SHORT).show();

        Intent a = new Intent(MainActivity.this,BossAct.class);
        startActivity(a);

        editText.setText("");
    } else {
        Toast.makeText(MainActivity.this, "Wrong", Toast.LENGTH_SHORT).show();
        editText.setText("");
    }

    keys = shuffleArray(keys);
    linearLayout.removeAllViews();
    for (String key : keys) {
        addView(linearLayout, key, editText);
    }

}

}

對於timer我會在下面將其注冊為 LifecycleObserver,以便計時器可以在最小化時自動暫停等。您需要調用.Subscribe(); 當您希望計時器啟動時,在timerObservable()

您需要將此添加到您的實現RxJava

AtomicLong elapsedTime = new AtomicLong();
AtomicBoolean resumed = new AtomicBoolean();
AtomicBoolean stopped = new AtomicBoolean();

public final int MAX_SECONDS = 30;

/**
 * Sets up the Atomic values and returns the String value of the timer
 *
 * @return Flowable<String>
 */
@Override
public Flowable<String> timerObservable() {
    resumed.set(true);
    stopped.set(false);
    return Flowable.interval(0, 1, TimeUnit.SECONDS)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .takeWhile(seconds -> !stopped.get())
            .filter(aLong -> resumed.get())
            .map(aLong -> MAX_SECONDS - elapsedTime.addAndGet(1))
            .map(seconds -> String.format(Locale.getDefault(), "%01d:%02d", elapsedTime.get() / 60, elapsedTime.get() % 60));
}

/**
 * Pauses the Flowable Atomic Timer
 */
public void pauseTimer() {
    resumed.set(false);
}

/**
 * Resumes the Flowable Atomic Timer
 */
public void resumeTimer() {
    resumed.compareAndSet(false, true);
}

/**
 * Stops the Flowable Atomic Timer
 */
public void stopTimer() {
    stopped.compareAndSet(true, false);
}

/**
 * returns elapsedTime
 */
public long getElapsedTime() {
    return elapsedTime.get();
}

/**
 * updates the elapsedTime
 */
public void updateElapsedTime(Long newTime) {
    elapsedTime.set(newTime == null ? 0 : newTime);
}

/**
 * Observes the lifecycle of the Activity and manages the Flowable Atomic Timer
 */
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void appInResumeState() {
    resumeTimer();
}

/**
 * Observes the lifecycle of the Activity and manages the Flowable Atomic Timer
 */
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void appInPauseState() {

    pauseTimer();
}

/**
 * Observes the lifecycle of the Activity and manages the Flowable Atomic Timer
 */
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void appOnStopCalled() {
    stopTimer();
}

“我願意添加一套完整的單詞並比較每個單詞的答案。我怎樣才能做到這一點?”

你的意思是如果你有一個詞“咬”,你可能的答案是“咬”和“咬”?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM