Java基本内存匹配游戏
我试图编码一个4x4内存匹配游戏,有8个相同的数字对。所有正面朝下的卡片都标有星号。然后用户输入(1,1)和(1,2)作为猜测,如果正确,他们将保持正面朝上,游戏继续
我的问题是:我已经完成了大部分代码,但是我运行游戏的方法game我不知道如何设置它。如有任何建议,将不胜感激
我的代码:
//Welcome Prompt
System.out.println("Welcome to the memory matching game!");
System.out.println("Enter the card coordinate for each card when promted to.");
System.out.println("For Example, 11 = Card 1 and 12 = Card 2.\n\n");
board();
}
//print the board
public static void board() {
int[][] cards = new int[4][4];
boolean upDown[][] = new boolean[4][4];
cards = randomizer(); //Shuffle cards
System.out.println(" 1 2 3 4 ");
System.out.println("---+---------");
for (int i = 0; i < 4; i++) {
System.out.print(" " + (i + 1) + " | ");
for (int a = 0; a < 4; a++) {
System.out.print("* ");
upDown[i][a] = false;
}
System.out.println();
}
System.out.println();
game(upDown, cards); // calls the game
}
public static int[][] randomizer() {
int num[] = {1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8};
int cards[][] = new int[4][4];
Random random = new Random();
int temp, t;
for (int j = 0; j <= 20; j++) {
for (int x = 0; x < 16; x++) { //Randomize the card order
t = random.nextInt(1000) % 15;
temp = num[x];
num[x] = num[t];
num[t] = temp;
}
t = 0;
for (int r = 0; r < 4; r++) // Cards receive Numbers
{
for (int s = 0; s < 4; s++) {
cards[r][s] = num[t];
t = t + 1;
}
}
}
return cards;
}
//Start the Game
public static void game(boolean[][] upDown, int[][] cards) {
}
//shuffle the cards
public static int[][] shuf() {
int start[] = {1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8};
int cards[][] = new int[4][4];
Random ran = new Random();
int tmp, i;
for (int s = 0; s <= 20; s++) {
for (int x = 0; x < 16; x++) //randomize the card placements
{
i = ran.nextInt(100000) % 15;
tmp = start[x];
start[x] = start[i];
start[i] = tmp;
}
}
i = 0;
for (int r = 0; r < 4; r++) // put values in cards here
{
for (int c = 0; c < 4; c++) {
cards[r][c] = start[i];
i = i + 1;
}
}
return cards;
}
}
# 1 楼答案
这是非常基本的。但它展示了如何运行游戏的基本理念。左上=“11”,右下=“44”。您可以将其作为练习进行清理(包括静态引用作为方法参数传递,检查输入是否不会导致数组越界错误等)