在Java内存游戏中,我如何触发发现的2个未知项,并使它们在游戏的其余部分保持显示状态?

在Java内存游戏中,我如何触发发现的2个未知项,并使它们在游戏的其余部分保持显示状态?,java,memory,duplicates,sparse-matrix,letters,Java,Memory,Duplicates,Sparse Matrix,Letters,我目前正在做一个java内存游戏,让用户通过输入2x2、4x4或6x6正方形的坐标猜出重复的字母,其中有两组重复的字母 例如,对于4x4,它将是 A、B、C、D E F G H A、B、C、D E F G H 但是随机的 如果他们猜对了,这些信件会一直暴露出来,直到全部找到为止 我已经成功地将正方形随机化,并用*覆盖,并使它们根据输入坐标显示 但我不知道如何做到,一旦两个公开的字母是相同的,程序将在整个游戏中保持它们的公开,直到所有重复的字母都被公开 现在您可以复制并粘贴下面的代码,所有注释都被

我目前正在做一个java内存游戏,让用户通过输入2x2、4x4或6x6正方形的坐标猜出重复的字母,其中有两组重复的字母

例如,对于4x4,它将是

A、B、C、D

E F G H

A、B、C、D

E F G H

但是随机的

如果他们猜对了,这些信件会一直暴露出来,直到全部找到为止

我已经成功地将正方形随机化,并用*覆盖,并使它们根据输入坐标显示

但我不知道如何做到,一旦两个公开的字母是相同的,程序将在整个游戏中保持它们的公开,直到所有重复的字母都被公开

现在您可以复制并粘贴下面的代码,所有注释都被注释掉:

    import java.util.Scanner;
    import java.io.IOException;

    public class a4{

// main method. DO NOT MODIFY





     public static void main(String args[]) {
            Scanner keyboard = new Scanner( System.in );

        System.out.println("Welcome to Memory Game");

        int board_side;

//this loop obtains the board size, or more specifically 
// the length of the side of the board



     do{
          System.out.println("\n For 2x2 board game press 2"+
                             "\n For 4x4 board game press 4"+
                             "\n For 6x6 board game press 6");
          board_side=keyboard.nextInt();
        }while(board_side!=2 && board_side!=4 && board_side!=6);


        char[][] board = createBoard(board_side);

// a call the the shuffle method
shuffleBoard(board);

// a call to the game playing method
playGame(board); 

     }



  // The following method should shuffle the input 2D array caled board 
  public static void shuffleBoard(char[][] board)


    {

// This creates a 1D array whose size is equal to the size of the board   

    int N = board.length*board.length;
    char[] board1D = new char[N];



// Testing to see the printed square


      for ( int i = 0; i < board.length; i++) {
          for ( int j = 0; j < board[i].length; j++) {
            System.out.print( board[i][j] + " " );
          }
          System.out.println();
        }
        System.out.println();

        for (int i =0; i < board1D.length; i++) {
          System.out.print(board1D[i] + " ");
        }

        System.out.println();



// Copy the elements of 2D array into that 1D array here

    for (int m = 0; m < N; m++){
      for (int i = 0; i < board.length; i++){
        for (int j = 0; j < board.length; j++, m++){
          board1D [m] = board[i][j];
        }
      }
    }






// Shuffle 1D array

// Shuffle the array

    for (int i = 0; i < N; i++) {

// Generate an index randomly


     int index = (int)(Math.random() * N);
      char temp = board1D[i];
      board1D[i] = board1D[index];
      board1D[index] = temp;
    }


//Testing to see the 1D array



    System.out.println();

     for (int i =0; i < board1D.length; i++) {
     System.out.print(board1D[i] + " ");
     }

     System.out.println();



//Copy shuffled 1D array back into 2D array, i.e., back to the board


    for (int m = 0; m < N; m++){
      for (int i = 0; i < board.length; i++){
        for (int j = 0; j < board.length; j++, m++){
          board[i][j] = board1D [m];
        }
      }
    }


//Testing to print the shuffled square



     for ( int i = 0; i < board.length; i++) {
          for ( int j = 0; j < board[i].length; j++) {
            System.out.print( board[i][j] + " " );
          }
          System.out.println();
        }

         System.out.println();
         System.out.println();


      }








// game playing method



     public static void playGame(char[][] board)
      {
        Scanner keyboard = new Scanner( System.in );

// this createst a 2D array indicating what locations are paired, i.e., discovered
// at the begining none are, so default initializaiton to false is ok 

    boolean[][]discovered=new boolean[board.length][board[0].length];; 




    for ( int i = 0; i < board.length; i++) {
      System.out.print(1 + i + " ");

      for ( int j = 0; j < board[i].length; j++) {
        System.out.print( "* " );
      }


      System.out.println();
    }
    System.out.print("  ");

    for ( int x = 0; x < board.length; x++) {
      System.out.print(1 + x + " ");
    }



    System.out.println();
    System.out.println();
    System.out.println("Enter a pair of undiscovered distinct locations on the board that you want revealed. i.e., a pair of integers in the range [1, 2]");
    System.out.println();


    int FirstLocationX;
    int FirstLocationY;
    int SecondLocationX;
    int SecondLocationY;


    do {
      System.out.println("Enter the first location: ");
      FirstLocationX=keyboard.nextInt();
      FirstLocationY=keyboard.nextInt();

      if (FirstLocationX > board.length && FirstLocationY > board.length){
        System.out.println("The location is invalid. It is outside of the board. ");
      }
    } while(FirstLocationX > board.length && FirstLocationY > board.length);




    System.out.println();

    do {
      System.out.println("Enter the second location: ");
      SecondLocationX=keyboard.nextInt();
      SecondLocationY=keyboard.nextInt();

      if (SecondLocationX > board.length && SecondLocationY > board.length){
        System.out.println("The location is invalid. It is outside of the board. ");
      }
      else if (SecondLocationX == FirstLocationX && SecondLocationY == FirstLocationY){
        System.out.println("The location is invalid. The second location equal to the first. ");
      }


     } while(SecondLocationX > board.length && SecondLocationY > board.length && SecondLocationX == FirstLocationX && SecondLocationY == FirstLocationY);




//reveals the letters based on the coordinate user inputed

    for ( int i = 0; i < board.length; i++) {
      System.out.print(1 + i + " ");



      for (int j = 0; j < board[i].length; j++) {

        if (FirstLocationX == i+1 && FirstLocationY == j+1){
          System.out.print( board[i][j] + " " );
        }

        else if (SecondLocationX == i+1 && SecondLocationY == j+1){
          System.out.print( board[i][j] + " " );
        }

/*This part is wrong, reveals the whole square instead of the found duplicates        
else if (discovered[0][0] = true){
  System.out.print( board[i][j] + " " );
}

else if (discovered[0][2] = true){
  System.out.print( board[i][j] + " " );
}
        */


        else {
          System.out.print( "* " );


        }

      }
      System.out.println();
    }

    System.out.print("  ");

    for ( int x = 0; x < board.length; x++) {
      System.out.print(1 + x + " ");
    }

     System.out.println();
        System.out.println();

     char[][] FirstInput = new char[FirstLocationX][FirstLocationY];
        char[][] SecondInput = new char[SecondLocationX][SecondLocationY];




/*GETTING AN ERROR HERE when I try to trigger a duplicate letter found, since the array is shuffled I don't know how exactly, just testing with the first match right now (A and A), which suppose to be coordinate 0,0 and 0,2 in the non-sorted square matrix
*/


    if (board[0][0] == FirstInput[FirstLocationX][FirstLocationY] && board[0][2] == SecondInput[SecondLocationX][SecondLocationY]){
      discovered[0][0] = true;
      discovered[0][2] = true;
    }

    waitForPlayer();


     do {
          playGame(board); 
        }while(discovered[0][0] == false && discovered[0][2] == false);



      }




  // createBoard method. DO NOT MODIFY!
  /* this method, createBoard, creates the board filled with letters of alphabet, 
   where each letter appears exactly 2 times
   e.g., for 4 x 4, the returned board would look like:
   A B C D 
   E F G H
   A B C D 
   E F G H */    



     public static char[][] createBoard(int side) 
      {
        char[][] tmp = new char[side][side];
        int letter_count=0;
        for (int row = 0; row < tmp.length/2; row++){
          for(int col = 0; col < tmp[row].length; col++)
          {
            tmp[row][col]=(char)('A'+letter_count);
            tmp[row+tmp.length/2 ][col]=tmp[row][col];
            letter_count++;
          }
        }
        return tmp;
      }





// waitForPlayer method. Do not modify!

     public static void waitForPlayer()
      {
        System.out.print("Press enter to continue");
        try {
          System.in.read();
        }
        catch (IOException e){
          System.out.println("Error reading from user");
        }
      }

    }


我建议把每一个正方形都做成一个物体,而不仅仅是一个字符。这样,您就可以控制在显示时是否显示它们

编辑


如果没有对象,您只需创建另一个布尔数组即可跟踪显示的内容。每当显示付款时,将替代板内的值设置为true,然后在显示之前在显示功能中检查其是否为true,我将执行以下操作:

创建一个char[][]数组,其中*的数量等于输入字符

e、 g:

ABCD DACB '*****' “**”

获取用户输入的坐标并进行逻辑比较,如果两个字符相同,则将相应的*替换为该字符

e、 g:

如果第一个字符的用户输入为0,0,第二个字符的用户输入为1,1,则

然后将阵列更新为:

ABCD DACB A*** *A**


所有的行将在一个数组中,但在游戏中,您将只打印后两行。前两行和后两行之间的偏移量相同,因此这样做应该没有问题。

Object?我想我还没有学会,不知道我是否能用上它。谢谢我想这就是我用char[][]FirstInput=new char[FirstLocationX][FirstLocationY]所做的;char[]SecondInput=新字符[SecondLocationX][SecondLocationY];如果棋盘[0][0]==FirstInput[FirstLocationX][FirstLocationY]&棋盘[0][2]==SecondInput[SecondLocationX][SecondLocationY]{discovered[0][0]=true;discovered[0][2]=true;}waitForPlayer;do{playGameboard;}当发现[0][0]==false和发现[0][2]==false;}但是它不起作用。你能先修改代码格式吗?很难读懂你的代码。好吧,我把它放在底部的主要问题中,因为idk如何在注释中格式化问题是我不知道字母在无序双排列中的位置你不必知道。用户输入坐标,然后检查输入坐标是否相同。假设他先输入2,3,然后输入2,1秒->如果[2][3]==[2][1],然后写在该位置找到的字母,而不是*这是我在代码中所做的,但它是错误的,不起作用
char[][] FirstInput = new char[FirstLocationX][FirstLocationY];
        char[][] SecondInput = new char[SecondLocationX][SecondLocationY];




/*when I try to trigger a duplicate letter found, since the array is shuffled I don't know how exactly, just testing with the first match right now (A and A), which suppose to be coordinate 0,0 and 0,2 in the non-sorted square matrix
*/


    if (board[0][0] == FirstInput[FirstLocationX][FirstLocationY] && board[0][2] == SecondInput[SecondLocationX][SecondLocationY]){
      discovered[0][0] = true;
      discovered[0][2] = true;
    }

    waitForPlayer();


     do {
          playGame(board); 
        }while(discovered[0][0] == false && discovered[0][2] == false);



      }
 else if (discovered[0][0] = true){
      System.out.print( board[i][j] + " " );
    }

    else if (discovered[0][2] = true){
      System.out.print( board[i][j] + " " );
    }