0

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

游戏:菜单出现。用户输入 1 进行播放。2退出。游戏板出现,看起来像这样。每个盒子后面都有一个字母(啊)

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

然后用户输入一个点(EX:A1)。棋盘出现,随机显示字母。

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

然后它再次询问。(例如:用户输入 C3)

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

如果匹配,它们会留下来并重新开始循环。如果它们不匹配,它们都切换回“.”。

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

我在这里上传项目,并且还包括了我到目前为止所拥有的,尽管我认为这不太好!

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


   } 
}

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

4

1 回答 1

0

可能有几种方法可以做到这一点。一种可能的解决方案是执行以下操作:

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

我刚刚用伪代码勾勒了这一点,以便您可以自己在 Java 中计算出细节。您很可能希望在自己的方法中编写它,并可能创建主要方法调用以执行特定任务的辅助方法。

于 2012-11-27T04:17:35.817 回答