Java寻宝游戏

huangapple 未分类评论49阅读模式
英文:

Java treasure hunt game

问题

我在一个CA(课程作业)中需要创建一个寻宝游戏,但我不知道如何将用户输入转换为网格位置,然后在网格中显示带有用户输入的内容。有人可以给我一些帮助吗?我刚开始学习 Java,所以我是一个非常初学者,请耐心一点哈。以下是我到目前为止尝试使用的代码:

public void printBoard(){
    //*此方法将实际打印出带有行列标签的棋盘,
    //并将使用者的输入填入其中:如果已经挖过,则填入 D,如果找到了宝藏,则填入 X
    
    board = new int[10][10];
    String[] nums = {"  1","  2","  3","  4","  5","  6","  7","  8","  9"," 10"};
    String[] letters = {"    A", "  B", "  C", "  D", "  E","  F", "  G", "  H", "  I", "  J"};
    
    for (int i = 0; i < board.length; i++){
        System.out.print(letters[i]);
    }
    
    System.out.println("");
    
    for (int i = 0; i < board.length; i++) {
        System.out.print(nums[i]);
        
        for (int j = 0; j < board.length; j++){
            if (board[i][j] == 0) {
                System.out.print("__|");
            } else if (board[i][j] == 1){
                System.out.println("d|");
            } else if (board[i][j] == -1) {
                System.out.println("x|");
            }
        }
    }
}

public void getUserInput(){
    int userRow, userColumn;
    
    do{
        Scanner myScanner = new Scanner(System.in);
        System.out.println("Please enter row");
        userRow = myScanner.nextInt() - 1;
        System.out.println("Please enter column");
        userColumn = myScanner.nextInt() - 1;
        
        if(board[userRow][userColumn] == -1){
            System.out.println("That square has been used. Pick again");
        }
    } while (board[userRow][userColumn] != 0);
    
    if (board[userRow][userColumn] == 1){
    } else {
        board[userRow][userColumn] = 1;
    }
}

请注意,以上是您提供的代码的翻译。如果您有任何问题或需要进一步的帮助,请随时问我。

英文:

I got a CA where i have to create a treasure hunt game and I cant figure out how to take the user input convert it to grid location and then display it with the user input in the grid. Can anyone give me some help? I just started learning java so i am very beginner so please bear with me lol. this is the code i been trying to use so far:

public void printBoard(){
    //*this method will actually print the board and fill it with the rows and columns labels 
    //and will be filled with the users&#39; input: D if dug already or X if treasure been found
    
      board = new int [10][10];
      String[] nums = {&quot;  1&quot;,&quot;  2&quot;,&quot;  3&quot;,&quot;  4&quot;,&quot;  5&quot;,&quot;  6&quot;,&quot;  7&quot;,&quot;  8&quot;,&quot;  9&quot;,&quot; 10&quot;};
     // char letters = 65;
      String[] letters = { &quot;    A&quot;, &quot;  B&quot;, &quot;  C&quot;, &quot;  D&quot;, &quot;  E&quot;,&quot;  F&quot;, &quot;  G&quot;, &quot;  H&quot;, &quot;  I&quot;, &quot;  J&quot;};
      for (int i = 0 ; i &lt; board.length ; i++){
          
          System.out.print(letters[i]);
       //   letters++;
      }
      System.out.println(&quot;&quot;);
    
      for (int i = 0; i &lt; board.length; i++) { 
          System.out.print(nums[i]);
          
      for (int j = 0 ; j &lt; board.length ; j++){
 
          if (board[i][j] == 0) { //empty index 
            System.out.print(&quot;__|&quot;);
        } else if (board[i][j] == 1){ //when user input wont be an empty index so print d|
              System.out.println(&quot;d|&quot;);
        } else if(board[i][j] == -1) { //-1 already filled position with the treasure location so print x|
            
              System.out.println(&quot;x|&quot;);
             
        } 

public void getUserInput(){
     
       int userRow, userColumn;
        
        do{
            Scanner myScanner = new Scanner(System.in);
            System.out.println(&quot;Please enter row&quot;);
            userRow = myScanner.nextInt() -1;
            System.out.println(&quot;Please enter column&quot;);
            userColumn = myScanner.nextInt() -1;
            
            if(board[userRow][userColumn] == -1){
                System.out.println(&quot;That square has been used. Pick again&quot;);
            }
            
        }while (board[userRow][userColumn] != 0);
        
        if ((board[userRow][userColumn]) == 1){
        }
        else{
            board[userRow][userColumn] = 1;
        }

huangapple
  • 本文由 发表于 2020年5月5日 20:59:42
  • 转载请务必保留本文链接:https://java.coder-hub.com/61613775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定