java - 随机化字符串数组并显示所有值

标签 java android

我正在制作纸牌游戏,我已经到了洗牌时间。

我必须洗几张牌(之前从用户那里选择,所以它们的数量并不总是相同),然后一张一张地显示给用户。

由于我仍在开发游戏逻辑,因此我通过更改按钮文本来显示卡牌名称。

但是当我尝试获取卡片的名称并将它们设置为按钮的文本时,我遇到了困难。

会发生什么是我得到一个空白按钮或只是“Masons”或“Villager”字符串。事实上,如果我检查日志,我会看到所有其他卡片(字符)都显示为“空”。

这就是我试图实现目标的方式(是的,我是新手):

这是头:

int demoniac;
int guard;
int masons;
int medium;
int mythomaniac;
int owl;
int villager;
int werehamster;
int all;
int i;
int t;
String[] characters = new String[24];
Button randomButton;

我添加所有卡片(字符)的方法:

public void addAll(){
for(i = 0; i < all; i++){
    add(demoniac, "Demoniac");
    add(guard, "Guard");
    add(medium, "Medium");
    add(mythomaniac, "Mythomaniac");
    add(owl, "Owl");
    add(werehamster, "Werehamster");
    add(villager, "Villager");
    add(masons, "Masons");
   }

}

我添加管理各类卡牌(人物)的方法:

public int add(int character, String name){
    if(character != 0 && name == "Villager"){
        for(t = 0; t < character; t++){
            i+=t;
            characters[i] = name;}
    }
    else if(character == 2 && name == "Masons"){
        characters[i] = name;
        i++;
        characters[i] = name;
        Toast.makeText(randomSelection.this, "works", Toast.LENGTH_SHORT).show();
    }else if(character != 0){
        characters[i] = name;
    }
    return i;
}

随机化:

 public void randomize(){
    Collections.shuffle(Arrays.asList(characters));
    for (int s = 1; s < characters.length; s++)
    {
        System.out.println(characters[s]);
    }

}

每次用户点击按钮时显示不同卡片(字符)的方法:

public void show(View view){
    for (int s = 1; s < characters.length; s++)
    {
        randomButton.setText(characters[s]);
    }
}

编辑:

我注意到我做过的no sense for loop 顺便说一下你应该知道 虽然大多数角色只有 1 个同类(恶魔、守卫等)有 2 个泥瓦匠和 5 到 12 个村民,所以我们需要找回> 这些int并向Array添加尽可能多的String我们从那些ints.

示例:如果我得到 6 个 Villager,我必须将 String "Villager"添加到 中 6 次字符串数组

然后我将 s 值设置为 1 因为我必须显示第一个 String ([0]) 一旦 Activity 开始,所以在 OnCreate() 方法上。

也许我错了,如果是这样,请您纠正我!

最佳答案

Getting a blank button or just with "Masons" or "Villager" String

那是因为您只用列表的最后一个元素设置按钮的文本。要么是 null,要么是 “Masons”(看不出它怎么可能是 “Villager”)。

for (int s = 1; s < characters.length; s++)
{
    randomButton.setText(characters[s]);
}

If I check the log I see that all the others cards(characters) get displayed as "null"

您只设置数组的位置 0。例如,您没有初始化位置,因此这些 int 值默认为 0。

int demoniac;
int guard;
int all;

然后

for(i = 0; i < all; i++){
    add(demoniac, "Demoniac");
    add(guard, "Guard");

真的,不应该进入那个循环,因为 all 等于 0。

此外

集合是零索引的,所以这不会打印元素 0。您需要设置 int s = 0;

for (int s = 1; s < characters.length; s++)

我不清楚 add(int character, String name) 方法返回什么,但如果您解释一下,我会更新这个答案。

我相信这段代码可以实现您想要实现的大部分目标

// Where the characters are stored
private ArrayList<String> characters;

public void initDeck() {
    if (characters == null)
        characters = new ArrayList<String>();
    // Extract the numbers if you actually need them, otherwise, they just are constants
    addCharacter("Demoniac", 1, characters);
    addCharacter("Guard", 1, characters);
    addCharacter("Medium", 1, characters);
    addCharacter("Mythomaniac", 1, characters);
    addCharacter("Owl", 1, characters);
    addCharacter("Werehamster", 1, characters);
    addCharacter("Villager", 5, characters);
    addCharacter("Masons", 1, characters);
}

public void addCharacter(String name, int amount, ArrayList<String> cards) {
    if (amount < 0) {
        throw new IllegalArgumentException("Must add a non-negative number of characters for " + name);
    }

    // Don't use '==' for Strings
    if (name.equals("Villager")) {
        if (amount != 5 || amount != 12) {
            throw new IllegalArgumentException("There can only be 5 or 12 " + name);
        }
    }

    for (int i = 0; i < amount; i++) {
        cards.add(name);
    }
}

public int searchCharacters(String character, ArrayList<String> cards) {
    return cards.indexOf(character);
}

public Map<String, Integer> getAllCharacterPositions() {
    Map<String, Integer> allPositions = new LinkedHashMap<String, Integer>();
    for (int i = 0; i < characters.size(); i++) {
        allPositions.put(characters.get(i), i);
    }
    return allPositions;
}

void run() {
    // initialize the characters
    initDeck();

    // shuffle them
    Collections.shuffle(characters);

    // print them all out
    for (int i = 0; i < characters.size(); i++) {
        System.out.printf("%d: %s\n", i, characters.get(i));
    }

    // Find the position of a character
    System.out.println();
    String findCharacter = "Owl";
    // Option 1 -- always linear search lookup
    System.out.printf("%d: %s\n", searchCharacters(findCharacter, characters), findCharacter);
    // Option 2 -- one-time linear scan, constant lookup
    Map<String, Integer> positions = getAllCharacterPositions();
    System.out.printf("%d: %s\n", positions.get(findCharacter), findCharacter);

    // Get a random character
    System.out.println();
    Random rand = new Random(System.currentTimeMillis());
    int randPos = rand.nextInt(characters.size());
    System.out.printf("%d: %s\n", randPos, characters.get(randPos));

    // randomButton.setText(characters.get(randPos));
}

关于java - 随机化字符串数组并显示所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35072359/

相关文章:

java - 我应该如何存储用户凭据,以便用户不必再次登录?

android - 网络连接关闭时,适用于 Android 的 Picasso 库是否处理图像加载?

java - Android Retrofit 2 发布请求响应以及响应类名称

java - 使子类在java中可观察

java - cvc-complex-type.2.4.c : The matching wildcard is strict, 但找不到元素 'tx:annotation- driven' 的声明

java - 如何让数组的大小自动增长和收缩?

android - cURL 不遵守 TLS v1

android - 如何获取屏幕分辨率包括软键盘的大小?

java - 在 JSP 页面中使用 request.setAttribute

Netbeans 中 Java EE 的 Javadoc 未显示