如何为这个基于文本的内存匹配游戏实现一个多尺寸板的选项?

3pmvbmvn  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(181)

我目前正在为我的一个课堂作业制作一个基于文本的单词匹配游戏,我有一个基本的程序,但它只限于4x4。
问题:我需要更改代码以允许播放器从3种尺寸中选择:4x4、6x6和8x8。我试图改变一些代码,但我觉得我做的每件事都是低效的。如果有人能提供必要的改变,使其工作,将不胜感激。
代码:

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

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); 
    }

    public static void setup() 
    {
        for (int i = 0; i < 4; i++) {
            for (int a = 0; a < 4; a++) {
                upDown[i][a]=false;
            }
        }
        cards = randomizer();      
    }

    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[][a] 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++) { 
                t = random.nextInt(1000) % 15;
                temp = num[x];
                num[x] = num[t];
                num[t] = temp;

            }
            t = 0;
            for (int r = 0; r < 4; r++) 
            {
                for (int s = 0; s < 4; s++) {
                    cards[r][s] = num[t];
                    t = t + 1;
                }
            }

        }
        return cards;
    }

    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");
    }

    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++)
            {
                i = ran.nextInt(100000) % 15;
                tmp = start[x];
                start[x] = start[i];
                start[i] = tmp;
            }
        }
        i = 0;

        for (int r = 0; r < 4; r++)
        {
            for (int c = 0; c < 4; c++) {
                cards[r][c] = start[i];
                i = i + 1;
            }
        }
        return cards;

    }

}
kx1ctssn

kx1ctssn1#

如果电路板是n乘n,那么可以创建一个变量(类中的一个静态字段,即您的工作方式) int boardSize ,并在程序开始时为其指定4、6或8。
然后更换 4boardSize 在你的节目中。
您的一些其他数字将需要替换为boardsize的函数,例如。 boardSize * boardSize .
在用户选择大小之后,需要进行一些初始化。
看看你能用这种方法走多远,如果你陷入困境,问更多的问题。

相关问题