java Java基本内存匹配游戏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35026690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Basic Memory Matching Game
提问by William White
I am trying to code a 4x4 memory matching game that has 8 identical number pairs. All face down cards are marked by an asterisk. Then the user inputs (1,1) and (1,2) as a guess and if correct they remain face up and the game continues.
我正在尝试编写一个具有 8 个相同数字对的 4x4 内存匹配游戏。所有面朝下的卡片都标有星号。然后用户输入 (1,1) 和 (1,2) 作为猜测,如果正确,它们保持面朝上,游戏继续。
My Problem: I have most of the code complete however my method gamewhich runs the game I cannot figure out how to set it up. Any advice would be appreciated.
我的问题:我有但是大部分的代码完成我的方法的游戏,其运行游戏,我无法弄清楚如何设置它。任何意见,将不胜感激。
My Code:
我的代码:
//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;
}
}
采纳答案by Ian Mc
This is very basic. But it shows the basic idea of how to run the game. Top left = "11", Bottom right = "44". You can clean it up as an exercise (including static references are passed as method arguments, checking that input will not cause array out of bounds errors, etc)
这是非常基本的。但它显示了如何运行游戏的基本思想。左上角 =“11”,右下角 =“44”。您可以将其清理作为练习(包括静态引用作为方法参数传递,检查输入不会导致数组越界错误等)
public class MemoryGame {
static int[][] cards = new int[4][4];
static boolean upDown[][] = new boolean[4][4];
static Scanner s = new Scanner(System.in);
public static void main(String[] args) {
setup();
game(upDown, cards); // calls the game
}
//print the board
public static void setup() {
for (int i = 0; i < 4; i++) {
for (int a = 0; a < 4; a++) {
upDown[i][a]=false;
}
}
cards = randomizer(); //Shuffle cards
}
//print the board
public static void displayBoard(boolean[][] upDown, int[][] 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++) {
if (upDown[i][a]) {
System.out.print(cards[i][a]);
System.out.print(" ");
}
else
System.out.print("* ");
}
System.out.println();
}
System.out.println();
}
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) {
int noDownCards = 16;
while (noDownCards > 0) {
displayBoard(upDown, cards);
System.out.println("Enter co-oridinate 1");
String g1 = s.next();
int g1x = Integer.valueOf(g1.substring(0, 1))-1;
int g1y = Integer.valueOf(g1.substring(1, 2))-1;
System.out.println(cards[g1x][g1y]);
System.out.println("Enter co-oridinate 2");
String g2 = s.next();
int g2x = Integer.valueOf(g2.substring(0, 1))-1;
int g2y = Integer.valueOf(g2.substring(1, 2))-1;
System.out.println(cards[g2x][g2y]);
if (cards[g1x][g1y] == cards[g2x][g2y]) {
System.out.println("You found a match");
upDown[g1x][g1y] = true;
upDown[g2x][g2y] = true;
noDownCards -= 2;
}
}
displayBoard(upDown, cards);
System.out.println("You win");
}
//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;
}
}