我想在数组中添加更多的单词,并在每次成功猜测后增加分数.怎么做



这基本上是一个猜字游戏。我想添加更多的单词,并在每次猜测后增加分数。我还想添加一个30秒的计时器,玩家必须在时间限制内回答。正如你所看到的,目前我已经在一个数组中存储了字母表及其正确答案。我愿意添加一套完整的单词,并对每个单词进行比较和回答。我怎样才能做到这一点?有人能帮我吗?

private int presCounter = 0;
private int score = 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 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);
}
}

如有任何额外建议,我们将不胜感激!

只需创建一个新方法,即可在数组中清除和添加新字母。每次完成时调用该方法。

我认为你应该在这里提供更多的代码。这个应用程序看起来很简单,但你的方法是什么?你想让你的应用程序做什么?你为什么不创建一个类来存储你的问题集?

我不确定您对Kotlin有多熟悉,但应该直接将其转换为Java。

有很多方法可以做你想做的事情。一个简单的方法是遵循以下步骤:

1-你有一系列单词(答案(吗例如:

private val words = listOf(
"BIRD",
"CAT",
"HOUSE" ...
)

2-有一个函数,生成X个随机字母,你可以添加到你的单词中。EX:这个>getRandomString(3(将返回一个由3个随机字母Ex组成的字符串:";BCG";

fun getRandomString(length: Int) : String {
val allowedChars = ('A'..'Z')
return (1..length)
.map { allowedChars.random() }
.joinToString("")
}

3-有一个函数,从单词列表(答案(中返回特定位置的单词,使事情变得更容易。例如:

fun getWordFromList(position: Int): String {
return words[position]
}

4-现在你可以做如下操作:获得X个额外的字母,将其添加/连接到你想要的任何单词,然后你可以将该字符串转换为非数组,并使用shuffleArray函数,你已经必须对其进行shuffle了。例如:

//Get 3 extra letters as a String
val extraLetters = getRandomString(3)
//Concatenate extra letters to your word (In this case position 0 is BIRD)
val wordAndExtraLetters = getWordFromList(0).plus(extraLetters)

//Convert to Array
val newArray = wordAndExtraLetters.toCharArray()
//Shuffle
val shuffledWordAndExtraLetters = shuffleArray(newArray)

4-现在你可以得到用户的回复,与单词列表中的单词进行比较,如果正确的话,可以提高分数:

//GET USER RESPONSE...
if(response == getWordFromList(0))
score ++

对不起,我没有时间测试这个。这种逻辑肯定还有待改进。希望它能帮你一点

下面是一个例子。

public static void main(String[] args)
{
String[] answers = {
"BIRD",
"HAMSTER",
"BEAR",
};
//How many random extra letters to put in the answer.
int extraLetters = 2;
Random rand = new Random();
for(int i=0; i < answers.length; ++i) {
//Convert string of answer to a list of characters.
List<Character> randLetters = new ArrayList<Character>(answers[i].length() + extraLetters);
for(int n=0; n<answers[i].length(); ++n) {
randLetters.add(answers[i].charAt(n));
}
//Generate N random letters for each answer.
for(int l=0; l < extraLetters; ++l) {
final char letter = (char)(65 + rand.nextInt(25));
randLetters.add(letter);
}
//Shuffle the list.
Collections.shuffle(randLetters);
//DEBUG
System.out.println("ANSWER: " + answers[i] + " SHUFFLE LETTERS: " + randLetters);
//Output: (Formatted the clarity)
//ANSWER: BIRD     SHUFFLE LETTERS: [Y, D, R, I, W, B]
//ANSWER: HAMSTER  SHUFFLE LETTERS: [Y, M, A, M, E, T, H, R, S]
//ANSWER: BEAR     SHUFFLE LETTERS: [E, S, B, O, A, R]
}
}

最新更新