[英]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.