我想用java做一个猜词游戏。我想在数组中添加更多单词
Posted
技术标签:
【中文标题】我想用java做一个猜词游戏。我想在数组中添加更多单词【英文标题】:I want to make a word guess game using java. I want to add more words in array 【发布时间】:2021-09-10 22:06:29 【问题描述】:它基本上是一个猜词游戏。我想在每次猜测后添加更多单词并提高分数。我还想添加一个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);
【问题讨论】:
请理解您的问题归结为“请帮助我解决我的复杂问题”。但我们不将此类请求视为有效问题(详见here)。 SO:最好是访问help center,了解这里的提问内容和方法。 【参考方案1】:对于timer
,我会在下面注册并将其注册为 LifecycleObserver,以便计时器在最小化等时自动暂停。当您希望计时器启动时,您需要在timerObservable()
上调用.Subscribe();
。
您需要将此添加到您的实施中 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();
“我愿意添加一套完整的单词并比较每个单词的答案。我怎样才能做到这一点?”
你的意思是如果你有“咬”这个词,你可能的答案是“咬”和“咬”?
【讨论】:
不,如果我有一个单词 RACS,那么正确的答案是什么。它的汽车以上是关于我想用java做一个猜词游戏。我想在数组中添加更多单词的主要内容,如果未能解决你的问题,请参考以下文章
java 我想在2秒时间里执行一个代码 过了2秒后就不执行 怎么做
我想从数组中 UITableviewCell 的文本字段中添加字符串