【问题标题】:In a Java Memory game, how do I trigger that 2 unknown has been discovered, and make them stay revealed for the rest of the game?在 Java Memory 游戏中,我如何触发已发现 2 个未知数,并让它们在游戏的其余部分中保持显示?
【发布时间】:2015-01-13 18:32:49
【问题描述】:

我目前正在做一个 java 记忆游戏,让用户通过输入 2x2、4x4 或 6x6 正方形的坐标来猜测重复的字母,这些正方形有 2 组重复字母

例如。对于 4x4,它将是

A B C D

E F G H

A B C D

E F G H

但随机

如果他们猜对了,这些字母会一直显示出来,直到找到所有字母。

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

但我不知道如何做到,一旦显示的 2 个字母相同,程序将在整个游戏中保持它们显示,直到所有重复的字母都显示出来。

现在的代码(你可以在下面复制粘贴整个代码,所有的cmet都被注释掉了):

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

    }

当前错误的部分是:

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

【问题讨论】:

    标签: java memory duplicates sparse-matrix letters


    【解决方案1】:

    我建议将每个正方形作为一个对象,而不是仅仅拥有一个字符。这样您就可以控制它们在显示时是否显示

    编辑

    没有对象,您可以创建另一个 bool 数组来跟踪显示的内容。每当显示支付时,将替代板上的值设置为 true,然后在显示功能中检查它是否为 true,然后再显示

    【讨论】:

    • 对象?我想我还没有学会它,不知道我是否可以使用它。谢谢
    • 我认为这就是我正在使用 char[][] FirstInput = new char[FirstLocationX][FirstLocationY]; char[][] SecondInput = new char[SecondLocationX][SecondLocationY]; if (board[0][0] == FirstInput[FirstLocationX][FirstLocationY] && board[0][2] == SecondInput[SecondLocationX][SecondLocationY]){ found[0][0] = true;发现[0][2] =真; } waitForPlayer();做{玩游戏(板); }while(发现[0][0] == 假 && 发现[0][2] == 假); } 但它目前不工作
    • 你能先修复代码格式吗?很难阅读您的代码。
    • 好的,我把它放在底部的主要问题中,因为我知道如何在评论中格式化
    【解决方案2】:

    我会做以下事情:

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

    例如:

    ABCD
    DACB '*****' '****'

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

    例如:

    如果用户输入的第一个字符为 0,0,第二个字符为 1,1(均为 A)

    然后将数组更新为:

    ABCD DACB 一种*** *一个**

    所有的行都将在一个数组中,但在游戏中您将只打印后两行。前两行和后两行的偏移量是一样的,这样应该没问题。

    【讨论】:

    • 问题是我不知道字母在洗牌双数组中的位置
    • 你不必知道。用户输入坐标,如果它们是相同的字符,您检查输入坐标。假设他先输入 2,3,然后输入 2,1 -> if([2][3]==[2][1]) 然后写在该位置找到的字母而不是 *
    • 这是我在代码中做的,但是错了(不起作用)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多