匹配卡程序 - 如何正确创建一个双数组,允许我将userInput的值与它进行比较并使游戏工作?

时间:2022-12-20 02:54:14

I am trying to teach myself java, and found this project online but I can't figure out how to properly put it together. I'm wondering if anyone can help me set up the double array that will ultimately allow me to compare the user input (EX: A1) to whatever the value of that piece is.

我正在尝试自学java,并在网上发现这个项目,但我无法弄清楚如何正确地把它放在一起。我想知道是否有人可以帮我设置双数组,最终允许我将用户输入(EX:A1)与该部分的值进行比较。

GAMEPLAY: Menu Comes up. User enters 1 to play. 2 to exit. Game board appears, Looks like this. Behind each box is a letter (a-h)

游戏:菜单出现。用户输入1进行播放。 2退出。游戏板出现,看起来像这样。每个方框后面都是一个字母(a-h)

    A  B  C  D 
    -  -  -  - 
1 | .  .  .  . 
2 | .  .  .  . 
3 | .  .  .  . 
4 | .  .  .  . 

Then user enters a spot (EX: A1). The board appears with the random letter showing.

然后用户输入一个点(EX:A1)。电路板出现随机字母显示。

    A  B  C  D 
    -  -  -  - 
1 | b  .  .  . 
2 | .  .  .  . 
3 | .  .  .  . 
4 | .  .  .  . 

Then it asks again. (EX: user enters C3)

然后它又问了一遍。 (EX:用户输入C3)

    A  B  C  D 
    -  -  -  - 
1 | .  .  .  . 
2 | .  .  .  . 
3 | .  .  d  . 
4 | .  .  .  . 

If it matches, they stay and the loop starts again. If they don't match, they both switch back to ".".

如果它匹配,它们会停留并且循环再次开始。如果它们不匹配,它们都会切换回“。”。

It keeps going till the whole board is filled and the game is over!

它一直持续到整个棋盘都被填满并且游戏结束了!

I'm uploading the project here, and have also included what I have so far, though i do not think it is all right!

我在这里上传了这个项目,并且还包含了我到目前为止所拥有的内容,尽管我认为它没有问题!

import java.util.*;

public class breitje_MatchUp{
   static Scanner input = new Scanner(System.in);
   static int i=0;
   static char[][] clearBoard = new char[4][4]; 
   static char[][] letterBoard = new char[4][4];
   public static void main(String[] args){
      drawBoard();


   }

   public static void drawMenu(){
      System.out.println("Welcome to the Match Up game! \n");  
      System.out.println("Please select from the following: ");  
      System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");  
      System.out.println("1. New Game");  
      System.out.println("2. Quit");  
      System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");  
      int optionSelect = input.nextInt();
      System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");  
      while(optionSelect!=1 && optionSelect !=2){
         System.out.println("Please enter 1 (New Game) or 2(Quit)");
         optionSelect = input.nextInt();}
      if (optionSelect == 1)
         System.out.print("START GAME HERE********");
      else if (optionSelect == 2)
         System.out.println("Thanks for playing! Have a great day.");  
   }


   public static void drawBoard(){
            for(int row1 = 0; row1 < 4; row1++) {
         System.out.println(""); // this will create a new line for each new row
         for(int col1 = 0; col1 < 4; col1++) {
          clearBoard[row1][col1]='.'; // this will print out the 4 row variable values            
         }        
      }

      System.out.format("   %2s %2s %2s %2s \n",'A','B','C','D');
      System.out.format("   %2s %2s %2s %2s \n",'-','-','-','-');
      System.out.format("1 |%2s %2s %2s %2s \n",clearBoard[0][0],clearBoard[0][1],clearBoard[0][2],clearBoard[0][3]);
      System.out.format("2 |%2s %2s %2s %2s \n",clearBoard[1][0],clearBoard[1][1],clearBoard[1][2],clearBoard[1][3]);
      System.out.format("3 |%2s %2s %2s %2s \n",clearBoard[2][0],clearBoard[2][1],clearBoard[2][2],clearBoard[2][3]);
      System.out.format("4 |%2s %2s %2s %2s \n",clearBoard[3][0],clearBoard[3][1],clearBoard[3][2],clearBoard[3][3]);  


      System.out.println("Please enter a column (A,B,C, or D) and row (1,2,3,or 4). Ex: A3 : ");
      String userInput = input.nextLine();
      userInput = userInput.toUpperCase().trim();


   } 
}

I am unsure how to initialize the values of letterBoard to represent the initial random placement of the cards. How can I "deal the cards" in my Java code?

我不确定如何初始化letterBoard的值来表示卡片的初始随机位置。如何在我的Java代码中“处理卡片”?

1 个解决方案

#1


0  

There are likely several ways to do this. One possible solution is to do something like this:

有几种方法可以做到这一点。一种可能的解决方案是做这样的事情:

for each letter from 'a' to 'h'
    pick two random locations
    if the given location is not yet used
        place the letter at the location

I have just sketched this out in pseudocode so that you can work out the details in Java on your own. Most likely you will want to write this in its own method and possible create helper methods that the primary method calls in order to perform specific tasks.

我刚刚用伪代码草拟了这个,以便您可以自己编写Java中的详细信息。很可能你会想要用自己的方法编写它,并且可能创建主方法为执行特定任务而调用的辅助方法。

#1


0  

There are likely several ways to do this. One possible solution is to do something like this:

有几种方法可以做到这一点。一种可能的解决方案是做这样的事情:

for each letter from 'a' to 'h'
    pick two random locations
    if the given location is not yet used
        place the letter at the location

I have just sketched this out in pseudocode so that you can work out the details in Java on your own. Most likely you will want to write this in its own method and possible create helper methods that the primary method calls in order to perform specific tasks.

我刚刚用伪代码草拟了这个,以便您可以自己编写Java中的详细信息。很可能你会想要用自己的方法编写它,并且可能创建主方法为执行特定任务而调用的辅助方法。