2016-01-31 88 views
3

我正在为我的School项目制作猜谜游戏,但我对Android Studio和Java颇为陌生。Android Studio - Array,ArrayList,List - JAVA

目前我的代码看起来是这样的:

public class MainActivity extends AppCompatActivity { 

    public int score = 0; 
    int random = random(); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Button correct = (Button) findViewById(R.id.correct); 
     Button other = (Button) findViewById(R.id.other); 
     Button newGame = (Button) findViewById(R.id.newGame); 
     TextView words = (TextView) findViewById(R.id.words); 


    } 

    public Integer random(){ 
     int random = (int)(Math.random() * 5); 
     return random; 
    } 

    private String list[] = 
      {"Dog", "Cat", "Mouse", "Elephant", "Rat", "Parrot"}; 

    public void clickedButton(View view) { 
     TextView words = (TextView) findViewById(R.id.words); 
     if (view.getId()== R.id.newGame) 
     { 
      words.setText(list[random]); 
      score = 0; 
      Log.i("score", "score = " + score); 
     } 
     if (view.getId() == R.id.correct) 
     { 
      // set maybe new array so new textview does not give the same value 
      words.setText(list[random]); 
      score = score +1; 
      Log.i("score", "score = " + score); 
     } 
     if (view.getId() == R.id.other) 
     { 
      // set maybe new array so new textview does not give the same value 
      words.setText(list[random]); 
      Log.i("score", "score = " + score); 
     } 

    } 

}

的想法很简单。我有我的阵列(目前)6个字。所以当我启动游戏时,它给了我一个随机的单词,我必须向其他人描述。如果他们猜对了,我按正确,我会得到另一个词,如果不是,我可以通过,并得到另一个词......呃,我看到的问题是,我有机会再次获得相同的单词。所以我认为应该有办法解决这个问题。

我想添加像if语句一样,如果索引1或2或3然后给另一个单词,但如果我有100个单词,这将是猴子的工作来做到这一点。

所以我认为应该有一个机会,我可以如何从类似的温度删除单词。数组,但没有内置的方法来完成它。

我读的是有那些arrayLists和列表,但不是更容易使用数组?也许一些提示如何?

+0

为工作选择合适的工具很重要。 'Array'是固定长度的,而'ArrayList'允许你扩展它并且(低效地)从中间删除条目。一个'LinkedList'可以有效地让你删除任何条目,同时可以根据需要增长和缩小。 – hexafraction

+1

就我个人而言,我会将数组混合起来*(使用随机交换位置)*,然后只保留一个“索引”来指明您所在的单词,因此您不必跟踪数组中已经经过了哪些单词。 –

+0

@RileyCarney你的意思是 - 我的随机数是3,然后我将[3]和[1]与地点互换。而对于下一个随机我只是添加变量x,这将是1,在每次“正确”或“通过”被按下后增加? – osiic21

回答

4

你可以先创建一个ArrayList(因为它更灵活):

List<String> list; 

然后初始化构造函数中的对象和洗牌的ArrayList象下面这样:

list = new ArrayList<String>(); 
list.add("Dog"); 
list.add("Cat"); 
... 
//or alternatively list.addAll(Arrays.asList(normalArray)); if you have your data in array 

Collections.shuffle(list,new Random(System.nanoTime()); //randomly shuffles your list 

然后你就可以保留最后一个项目的指数跟踪您阅读:

int index = 0; 

而且每当你读一个项目只是增加index

words.setText(list.get(index++)); 

这将做诡计

+0

我觉得很奇怪,如果我有100个单词,我将不得不拥有100x list.add是不是我? – osiic21

+0

@ osiic21你也可以这样做:list.addall(Arrays.asList(normalArray));它取决于你 – Pooya

+0

而不是做'list.add(“ex”);'你可以做一些诸如'list = new ArrayList (Arrays.asList({“你说的是”,“我说不”), “你说为什么”)));'ie'list = new ArrayList (Arrays.asList(anyArray))' –

1

尽管有多种方法可以解决您的问题,但我个人喜欢将数组混合(随机交换位置),然后只保留index,表示您在数组中的单词,因此您没有跟踪其中的单词。

我写了一个代码,一个类似的方案,下面一副扑克牌上做随机洗牌,它会通过int times量,但您完全可以将其删除,并有一个静态的循环值,如果你想:

// ... 

// Random Shuffle 
// Tip: Use this method to shuffle, may not be the fastest (not noticeable however), but it makes your array totally randomized. 
public static void totalRandom(String[] array, int times) 
{ 
    // Random Initialize 
    Random randomGenerator = new Random(); 

    // Variables 
    int first = 0; 
    int second = 0; 

    // Swap 
    for (int i = 0; i < times; i++) 
    { 
     // Generates number from 0 to array length 
     first = randomGenerator.nextInt(array.length); 
     second = randomGenerator.nextInt(array.length); 
     String word = array[second]; 
     array[second] = array[first]; 
     array[first] = word; 
    } 
} 

// ... 

然后在之后你的类,你会想要写的线沿线的东西...

//... 
int index = 0; 

// ... 
// Code for user input 
// ... 

if (userInput.equals(array[index])) 
{ 
    // do stuff 
    index++; 
} 
// ... 
1

您可以缓存您的最后一个单词。添加一个字符串成员变量。

... 
public int score = 0; 
public String cache; // new 

而不是使用

words.setText(list[random]); 

你可以调用请求新词的方法。你还这个词保存到缓存:

... 
cache = getRandomWord(); 
words.setText(cache); 
... 

你的新方法是这样的:

private String getRandomWord(){ 
    int random = random(); 
    String temp = list[random]; 
    if(temp.equals(cache)){ 
     temp = getRandomWord();  
    } 
    return temp; 

} 

只是把这个方法在你clickButton方法。它需要一个随机单词。这个单词是否与前一个单词相同,该方法会再次调用自己来选择另一个单词。如果不是,它会返回新的单词。

有没有必要洗牌ArrayLists。

+1

但缓存将只保存1个字,所以有机会,我会得到:狗,猫,狗。我对吗? – osiic21

+0

当用户只传递一次该单词时,这将起作用。如果他再次传递下一个单词会发生什么? – Pooya

+0

是的,你的链条是可能的。 – skymedium

0

我认为一个更简单的方法是从复制列表中弹出每个显示的文本。

//when start a new game. 
ArrayList<String> currentGameList = YourList.clone(); 

//when need next word. 
String displayText = currentGameList.remove(randomIntInRestSize); //after called remove, you will get the object at list(index), and the object will be removed from the list. 

每场比赛开始后,您只需从原始单词列表中克隆一个新列表并播放列表。