如何制作正在进行的游戏?

时间:2016-12-17 22:46:54

标签: java arrays

我正在使用2d数组进行java Memory游戏。有一个4x4网格的数字。每转一圈,玩家选择两个数字,然后翻转这两个阵列单元以显示回来。如果颜色不匹配,则翻牌并且玩家必须再次进行。到目前为止,我已经创建了游戏机制。阵列在那里,玩家可以翻转单元格,但代码在第一次尝试后停止。我需要更改它,以便程序继续运行,直到玩家成功匹配所有卡。此外,如果玩家匹配两张牌,那些牌一定不能回头。我有两个类:Card和MemoryGame。他们在这里:

public class Card{
    boolean showing; // true or false, indicates that the card is flipped
    String back;     //hidden symbol on the back of the card
    int front;       //integer on the front of the card

    public Card(String theBack, int theFront)
    {
        showing = false; 
        back = theBack;
        front = theFront;
    }

    //
    public void showCard()
    {
        if(showing)
            System.out.print(String.format(back)); //if showing is true, it shows the hidden symbol
        else
            System.out.print(String.format("["+front+"]"));
    }

    public void setShowingStatus()
    {
        if(showing)
            showing = false;
        else
            showing = true;
    }
}

这是记忆游戏课。 playGame()方法和choosePairofCards()方法是我需要帮助完成的方法。

import java.util.Random;
import java.util.Scanner;

public class MemoryGame{
    //Declaration
    private Card[][] board;
    private String[] words = {"Blue", "Blue","Red","Red","Green","Green","Yellow","Yellow","Pink","Pink","Black","Black","Brown", "Brown","White","White"};
    private Random r; 
    private Scanner reader; 

    MemoryGame(){
        r = new Random();
        reader = new Scanner(System.in);
        board = new Card[4][4]; //Creates the 4x4 array that can hold Card objects
        shuffle();
        setCells();
        printCells();
        playGame();
    }

    public void shuffle(){
        // Has a loop that goes throught the array of words and chooses a random position and swaps the words
        // For loop position to random position
        // Words array is shuffled
        for(int a=0; a<words.length; a++){
            int pos = r.nextInt(words.length);
            String temp = words[a];
            words[a] = words[pos];
            words[pos] = temp;
        }
    }

    public void setCells(){
        //Designates the words
        int a = 0; 
        for (int row = 0; row<board.length; row++){
            for (int col = 0; col<board[0].length; col++){
                {
                    board[row][col] = new Card(words[a], a); //Creates new Card that pulls words from Shuffled array
                    a++;
                }
            }
        }
    }

    public void printCells(){
        Card aCard; //Local variable = aCard, which is a type Card
        for (int row = 0; row<board.length; row++){
            for(int col = 0; col<board[0].length; col++){
                aCard = board[row][col];
                aCard.showCard();
            }
            System.out.println();
        }
    }

    public void playGame(){
        //INCOMPLETE
        choosePairOfCards();
    }

    public void choosePairOfCards(){

        int cardChoice, row1, col1, row2, col2;
        System.out.println();
        System.out.println("Enter the number on the card.");
        System.out.println("First Card Choice? >");
        cardChoice = getInputAsInt();
        row1 = cardChoice / 4;
        col1 = cardChoice % 4;
        board[row1][col1].setShowingStatus();

        System.out.print("Second Card Choice? >");
        cardChoice = getInputAsInt();
        row2 = cardChoice / 4;
        col2 = cardChoice % 4;
        board[row2][col2].setShowingStatus();

        System.out.print('\u000C'); //clears the screen

        printCells(); // INCOMPLETE
        // check to see if the "cards" match
        //if they don't then turn print game with cards turned around
    }

    public int getInputAsInt(){
        String temp = reader.nextLine();
        return Integer.parseInt(temp); //Returns a String containing integer, into an int value
    }
}

1 个答案:

答案 0 :(得分:0)

要实现无限运行游戏,您只需使用while循环即可。在那个while循环中,你只需继续转动。如果游戏结束,请停止循环并让程序芬兰化。例如,假设你得到了一个methid isGameFinnished,如果gamd已经完成,则返回true,否则返回false。

while(true)
{
    choosePairOfCards();
    if(isGameFinnished())
        break;
}

以上示例在循环中激活地中断。当您只需检查像abovr这样的布尔值时,您当然也可以

while(!isGameFinnished())
{
    choosePairOfCards();
}

我希望这能帮助你朝着正确的方向前进。