0

I'm in a bit of a pickle and need some help. What I'm doing is some thing like:

 boolean[] turn1 = {false, false, false, false, false, false};
 boolean[] turn2 = {false, false, false, false, false, false};

I'm trying to make something like if I could make a pair from turn1 and turn2 true, how could I make it so that you could only choose those that are false? I am sorry beforehand if this seems a bit vague but I will answer any question if asked.

edit:ok ill try to specify. I'm trying to make matching pairs. I suppose it could be anything as long as they have the same word. I'm making a memory game. What I want is that when I choose a matching pair, they could never be selected again. So I figured that in order to do that is to make the matching pair true so I could limit myself from choosing those that are false.

4

1 回答 1

0

在处理此类问题时,有时考虑一下在没有计算机的情况下如何在现实生活中进行处理会有所帮助。如果我和孩子玩记忆游戏,我们不会使用单独的纸来跟踪选择和匹配的卡片的真假标记。我们只需将牌从桌子上移开。

因此,不要使用大小固定的数组,而是使用ArrayList,它可以让您访问像数组一样的元素,但也可以像列表一样删除它们。

您的初始设置是这样的:

ArrayList<String> turn1 = new ArrayList<String>();
turn1.add("Orange");
turn1.add("Apple");
turn1.add("Banana");
turn1.add("Pear");

同样对于turn2. 找到匹配项后,从每个列表中删除项目:

turn1.remove(selection1);
turn2.remove(selection2);

然后你可以简单地继续,直到你的列表为空(字面意思是,turn1.isEmpty()))。

于 2012-05-03T07:30:37.773 回答