[英]Randomize String Array and display all of the values
我正在玩纸牌游戏,而我已经到了洗牌时间。
我必须随机整理几张卡片(之前是从用户那里选择的,因此它们的金额不一定总是相同的),然后将它们一张一张地显示给用户。
当我仍在开发游戏逻辑时,我正在通过更改按钮文本来显示纸牌的名称。
但是,当我尝试获取卡的名称并将其设置为按钮的文本时,我陷入了困境。
发生的事情是我得到一个空白按钮,或者仅输入“ Masons”或“ Villager” String。 实际上,如果我查看日志,就会看到所有其他卡(字符)都显示为“空”。
这就是我试图实现目标的方式(是的,我是新手):
这是头:
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]);
}
}
编辑:
我注意到了没有意义 for
loop
我已经做了,由你应该知道 ,尽管大多数字符是他们的那种1路(邪恶的,门卫等。) 有2个泥瓦匠和5〜12个村民 ,所以我们需要找回这些int
S 和添加尽可能多的String
到S Array
一样多,我们需要从这些告诉int
秒。
示例:如果我得到6个Villager
,则必须将String
“ Villager ”添加6次到String Array
。
然后,将s
值设置为1, 因为一旦 Activity
开始 , 就必须显示第一个 String
( [0]
),因此使用OnCreate()方法。
也许我错了,如果是的话,请您指正我!
获取空白按钮或仅使用“泥瓦匠”或“别墅”字符串
那是因为您仅将Button的文本设置为列表的最后一个元素。 这是null
或"Masons"
(看不到它可能是"Villager"
)。
for (int s = 1; s < characters.length; s++)
{
randomButton.setText(characters[s]);
}
如果我查看日志,就会看到所有其他卡(字符)显示为“空”
您只需将数组的位置设置为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;
设置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));
}
鉴于阵列已经被改组,请看第一张卡:
public void show(View view){
randomButton.setText(characters[0]);
}
如果您想浏览该牌组,建议您将混洗后的列表放入Queue
,在这里您可以查看下一张卡片( peek
)或拿下一张卡片( poll
):
private static Queue<string> buildNewShuffledDeck(String[] characters){
List<String> shuffledCharacterList = new ArrayList<String>(characters);
Collections.shuffle(shuffledCharacterList);
Queue<string> deck = new ArrayDeque(shuffledCharacterList);
return deck;
}
public void show(View view){
String nextCard = deck.peek();
if (nextCard != null)
randomButton.setText(nextCard);
else
//deck is empty...
}
然后从甲板上拿出,说随机按钮上单击:
String nextCard = deck.poll();
关于数组的一般建议:停止使用它们,而转而使用其他更有用和可互换的数据类型。
然后下一步建议,创建一个代表Card
的类,并停止使用Strings
,您当前拥有的字符串只是card的一个属性。
您只是显示添加的最后一个字符名称,用此替换
public void show(View view){
Random r = new Random(System.currentTimeMillis());
randomButton.setText(characters[r.nexInt(characters.length)])
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.