javascript中的记忆游戏

时间:2015-11-17 05:37:35

标签: java arrays loops memory

我正在尝试为Java中的作业制作记忆游戏。我被卡住了,所以我需要一些帮助。所以,我已经制作了一个菜单,您可以选择游戏难度(简单,中等和难度)。各种游戏模式为玩家提供不同数量的尝试。我被卡住了,因为我不知道如何让try'计数循环,因为用户猜错了,尝试次数会降低。此外,我不知道如何翻转卡片来显示某个字母(目前还没有代码)。现在,我将游戏设置为'[ - ]'来模仿正在翻转的卡片。如果你能给我一些关于算法的提示来计算尝试次数,那就太棒了!

import java.util.Scanner;

public class MemoryGame {

public static void main(String[] args) {
    Scanner in = new Scanner (System.in);
    System.out.print("Welcome to Single Memory Game!\n");
    System.out.println("Please choose a diffictulty:");
    System.out.println("1 - Easy\n2 - Medium\n3 - Hard\n0 - Exit ");

    System.out.println(">> ");
    int Option = in.nextInt();

    if (Option == 1)
    {
        Scanner cardGuess = new Scanner (System.in);
        int Guesses = 20;
        System.out.println("You have "+ Guesses + " guesses remaining!");
        System.out.println("Please select card one: ");
        int cardOne = cardGuess.nextInt();
        System.out.println("Please select card two: ");
        int cardTwo = cardGuess.nextInt();
    }
    else if (Option == 2)
    {
        Scanner cardGuess = new Scanner (System.in);
        int Guesses = 10;
        System.out.println("You have " + Guesses + " guesses remaining!");
        System.out.println("Please select card one: ");
        int cardOne = cardGuess.nextInt();
        System.out.println("Please select card two: ");
        int cardTwo = cardGuess.nextInt();
    }
    else if (Option == 3)
    {
        Scanner cardGuess = new Scanner (System.in);
        int Guesses = 5;
        System.out.println("You have " + Guesses + " guesses remaining!");
        System.out.println("Please select card one: ");
        int cardOne = cardGuess.nextInt();
        System.out.println("Please select card two: ");
        int cardTwo = cardGuess.nextInt();
    }
    else 
    {
        System.out.println("Goodbye!");
    }
    int[] randomNumbers; 
       randomNumbers = new int[11]; //All 19 cards 

       for(int index = 0; index < 11; index++) //Makes 9 indexes in the array
       {
           randomNumbers[index] = (int)(Math.random()*10); //Gives random values to index       
       }
       for(int index = 1; index < 11; index++)
       {
           randomNumbers[index] += randomNumbers[index-1];
       }

       for(int index = 0; index < 11; index++)
       {
           System.out.printf("[%3d] = [--]\n",index,randomNumbers[index]);
       }
}
}

1 个答案:

答案 0 :(得分:0)

我建议您使用的是ArrayList(易于处理)并随机播放(摆脱随机内容)。

ArrayList<Character> list = new ArrayList();

for(int i = 0; i < numOfCards; ++i)
 list.add((char) (i + 'a'));

System.out.println("Before shuffle : " + list);

Collections.shuffle(list);

System.out.println("After shuffle : " + list);

通过这种方式,您可以每次随机播放整个arrayList,并且每次都可以获得不同的数组。

Before shuffle : [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]
After shuffle : [z, m, s, t, j, o, v, b, u, q, f, n, x, w, k, c, e, y, i, d, r, h, g, l, p, a]