如何从youtube中删除此Anagram中的随机单词

时间:2017-07-17 02:08:10

标签: java android

我按照https://www.youtube.com/watch?v=qo6cdULtWik Anagram游戏中的步骤进行操作。但我想删除随机的单词,所以在我从字典中回答所有问题后,它将结束,以便问题不会重复

package com.example.child.fragments;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.plattysoft.leonids.ParticleSystem;
import com.plattysoft.leonids.modifiers.ScaleModifier;

import com.example.child.sidenavigation.R;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;


/**
 * Created by Child on 7/4/2017.
 */

public class Anagram extends Activity implements View.OnClickListener {
    TextView tv_info,tv_word;
    EditText et_guess;
    Button b_check;

    Random r;

    String currentWord;

    String[] dictionary = {
            "one",
            "two",
            "three",
            "four",
            "five",
            "six",
            "seven",
            "eight",
            "nine",
            "ten"
    };


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


        tv_info = (TextView)findViewById(R.id.tv_info);
        tv_word = (TextView)findViewById(R.id.tv_word);
        et_guess = (EditText)findViewById(R.id.et_guess);
        b_check = (Button)findViewById(R.id.b_check);
        b_check.setOnClickListener(this);
        r = new Random();

        newGame();

    }
    private String shuffleWord(String word) {
        List<String> letters = Arrays.asList(word.split(""));
        Collections.shuffle(letters);
        String shuffled = "";
        for(String letter:letters) {
            shuffled += letter;
        }
        return shuffled;
    }
    private void newGame() {
        //get random word from the dictionary
        currentWord = dictionary[r.nextInt(dictionary.length)];

        //shoow the shuffleword
        tv_word.setText(shuffleWord(currentWord));

        // clear the text
        et_guess.setText("");
        b_check.setEnabled(true);
    }

    @Override
    public void onClick(View v) {
        if(et_guess.getText().toString().equalsIgnoreCase(currentWord)) {
            tv_info.setText("Awesome!");
            b_check.setEnabled(false);
            newGame();

            new ParticleSystem(this, 10, R.drawable.star, 3000)
                    .setSpeedByComponentsRange(-0.1f, 0.1f, -0.1f, 0.02f)
                    .setAcceleration(0.000003f, 90)
                    .setInitialRotationRange(0, 360)
                    .setRotationSpeed(120)
                    .setFadeOut(2000)
                    .addModifier(new ScaleModifier(0f, 1.5f, 0, 1500))
                    .oneShot(v, 10);


        } else {
            tv_info.setText("Try Again");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,你想在猜到之后删除这个词,然后当它们全部完成时,结束游戏?

从阵列中删除猜测的单词(Hard Way)

最简单的方法是使用for循环并将值设置为不可能的值,例如null""

for(int i = 0; i < dictionary.length; i++) {
    if(dictionary[i].equalsIgnoreCase(WordToRemove)) {
        dictionary[i] = "";
    }
}

然后,当您生成下一个猜测时,您需要确保该单词仍然有效:

String currentWord = "";
while(!currentWord.equals("")) {
    currentWord = dictionary[r.nextInt(dictionary.length)];
}

现在这不是实现最终目标的最佳方式,但这是通过您的设置实现目标的一种方式。

从阵列中删除猜测的单词(Easy Way)

如果我们使用ArrayList<String>代替String[],那么我们已经完成了很多这方面的工作。要创建项目并将其添加到ArrayList

ArrayList<String> dictionary = new ArrayList<>();
dictionary.add("One");
dictionary.add("Two");
dictionary.add("Three");
dictionary.add("Four");

要从ArrayList

中删除
dictionary.remove(guessedString);

更容易吧?让我们看看你的代码会是什么样子:

ArrayList<String> dictionary = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    dictionary.add("one");
    dictionary.add("two");
    dictionary.add("three");
    dictionary.add("four");
    ...
}

然后从数组中删除:

@Override
public void onClick(View v) {
    String guess = et_guess.getText().toString().toLowerCase();
    if(guess.equals(currentWord)) {
        dictionary.remove(guess);
    }
    ...
}

请注意,我也改变了你的比较检查。这是为了确保我们总能在字典中找到这个单词。字典必须仅包含小写字母才能以此方式工作,因为.remove()将区分大小写。

最后一部分是选择新词或决定游戏结束:

private void newGame() {
    // Check game state
    if(dictionary.size() < 1) {
        // Do gameover stuff here
        return;
    }

    //get random word from the dictionary
    currentWord = dictionary.get(r.nextInt(dictionary.size()));

    ...
}

代码的...部分引用了我为了简洁和清晰而省略的旧代码