2017-04-26 133 views
0

我想要做的是給用戶5個輸入(棋子),白嘴鴉,主教,國王,女王,騎士。並將它們輸出到一個網格中,我不知道如何將用戶輸入限制爲5次,並用「。」打印網格的其餘部分。如何限制二維數組用戶輸入並輸出其餘值「。」?

我的輸出結果現在在下面的網格中,但是我怎樣才能得到Qb2的用戶輸入(女王,在第2列,在第2行?)),並把它放在網格中?等等碎片 ?

Output of the result I get vs the output of the sample given

注意:列是從「A」到「H」和行從底部開始,所以從「第1行」至行「8」。

. . . . . . . . 
. . . . . . . . 
. . . . . . . . 
. . . . . . . . 
. . . . . . . . 
. . . . . . . . 
. . . . . . . . 
. . . . . . . . 

我的代碼:(?女王,第2欄第2行)

import java.util.Scanner; 
     class chessMoves 
     { 
      //MAIN CODE AT THE VERY BOTTOM OF THE CLASS 
      Scanner sc = new Scanner(System.in); 

      private String[][] grid = new String[8][8]; 
      //target is x, so if x is at location in the grid, your program should determine if 
      // any of the pieces can move to that position. 
      private String king,queen,rook,bishop,knight,target; 

      public void chessPieces(){ 

      //remember you're only using the peices in scanner for a string just for a test 
     //switch them to 2D Array so that i can as 
     System.out.println("Hello Guest00129, Welcome to Chess."); 
     System.out.println("In order to play this game, input pieces like below(cap;atilaized)"); 
     System.out.println("Rook at column c and at row 5 then: Rc5"); 

     System.out.println("Please enter a position for Rook"); 
     rook = sc.nextLine(); 
     System.out.println("Please enter a position for King"); 
     king = sc.nextLine(); 
     System.out.println("Please enter a position for Queen"); 
     queen = sc.nextLine(); 
     System.out.println("Please enter a position for Bishop"); 
     bishop = sc.nextLine(); 
     System.out.println("Please enter a position for Knight"); 
     knight = sc.nextLine(); 
     System.out.println("Please enter a position for Target(X) to move the peices to that position"); 
     target = sc.nextLine(); 
     } 
     public void printGrid(){  
      for(int row = 0; row <grid.length; row++){ 
      for (int column = 0;column <grid[row].length; column++){ 
      grid[row][column] = "."; 
      System.out.printf("%2s",grid[row][column] + " "); 
      } 
      System.out.println(); 
     } 
     } 
     //get the userinput working first then make a file that gets the information for that userinput and outputs here 
     public void readChessPositions(){ 
     } 
     //the file created from the method above read it print the grid here like printout here and show the possible 
     //positons that can attack 
     public void chessOutput(){ 
     } 
     //method that prints the grid with the positiosn showed in the outputfile of chess moves 
     //print all empty spaces with dot(.) and the postiions 
     public static void main (String[] args){ 
     chessMoves test1 = new chessMoves(); 
     test1.chessPieces(); 
     test1.printGrid(); 
     } 
    } 
+0

怎麼樣:用「。」填充網格。然後將每個棋子一個接一個放入網格中(並在5之後停止)? – 2017-04-26 15:43:45

+0

你跟蹤棋子的位置,詢問他們想要移動哪一個,然後移動它。不要問他們每個單件應該在哪裏。提示:不要使用數組。 – jiveturkey

+0

@RC請寫一個示例代碼或者我不明白你的意思 –

回答

1

「我的輸出結果,現在下面的網格,但我怎麼能得到QB2的userinput)和把它放在網格中,其餘的都是這樣的?「 用戶將始終輸入三個字符的字符串。 使用charAt(int index)。 編輯1 :(閱讀評論後) 這裏是代碼,運行它,並告訴我,如果這是你想要的程序。

import java.util.Scanner; 
    class chessMoves 
    { 
     //MAIN CODE AT THE VERY BOTTOM OF THE CLASS 
     Scanner sc = new Scanner(System.in); 

     private String[][] grid = new String[8][8]; 
     //target is x, so if x is at location in the grid, your program should determine if 
     // any of the pieces can move to that position. 
     private String king,queen,rook,bishop,knight,target; 

     public void chessPieces(){ 

     //remember you're only using the peices in scanner for a string just for a test 
    //switch them to 2D Array so that i can as 
    System.out.println("Hello Guest00129, Welcome to Chess."); 
    System.out.println("In order to play this game, input pieces like below(cap;atilaized)"); 
    System.out.println("Rook at column c and at row 5 then: Rc5"); 

    System.out.println("Please enter a position for Rook"); 
    rook = sc.nextLine(); 
    System.out.println("Please enter a position for King"); 
    king = sc.nextLine(); 
    System.out.println("Please enter a position for Queen"); 
    queen = sc.nextLine(); 
    System.out.println("Please enter a position for Bishop"); 
    bishop = sc.nextLine(); 
    System.out.println("Please enter a position for Knight"); 
    knight = sc.nextLine(); 
    System.out.println("Please enter a position for Target(X) to move the peices to that position"); 
    target = sc.nextLine(); 
    } 
    public void printGrid(){  
     for(int row = 0; row <grid.length; row++){ 
     for (int column = 0;column <grid[row].length; column++){ 
     grid[row][column] = "."; 
     } 
    }  
    grid[7-rook.charAt(2)+49][(int)rook.charAt(1)-97] = "r"; 
    grid[7-bishop.charAt(2)+49][(int)bishop.charAt(1)-97] = "b"; 
    grid[7-queen.charAt(2)+49][(int)queen.charAt(1)-97] = "q"; 
    grid[7-king.charAt(2)+49][(int)king.charAt(1)-97] = "k"; 
    grid[7-knight.charAt(2)+49][(int)knight.charAt(1)-97] = "i"; 
    grid[7-target.charAt(2)+49][(int)target.charAt(1)-97] = "x"; 


     for(int row = 0; row <grid.length; row++){ 
     for (int column = 0;column <grid[row].length; column++){ 
      System.out.printf("%2s",grid[row][column] + " "); 
     } 
     System.out.println(); 
    } 

    } 
    //get the userinput working first then make a file that gets the information for that userinput and outputs here 
    public void readChessPositions(){ 
    } 
    //the file created from the method above read it print the grid here like printout here and show the possible 
    //positons that can attack 
    public void chessOutput(){ 
    } 
    //method that prints the grid with the positiosn showed in the outputfile of chess moves 
    //print all empty spaces with dot(.) and the postiions 
    public static void main (String[] args){ 
    chessMoves test1 = new chessMoves(); 
    test1.chessPieces(); 
    test1.printGrid(); 
    } 
} 

爲了解釋我在做什麼,讓我解釋的charAt()時類型強制轉換成整數會給值97,98,99返回character.Character A,B,C,d ...等等。同樣的字符1,2,3,4 ...會給47,48,49。 現在,當用戶輸入rc7(比如說)時,代碼將對c和7.c進行腐蝕的2d矩陣位置腐蝕到99,您需要將其設置爲2,以便減去97(您可以在代碼中看到)。類似地你可以看到7將變爲1. 如果你不明白,我建議閱讀關於ASCII和Unicode.basically ecah字符是分配一個數字代碼,就是這樣。 我把你的角色輸入,並將它們轉換成整數,把它們放在網格上。

現在,讓你的作品去了target.To這樣做定義的另一種方法 布爾inScope(INT [] []格,串片),定義在funtion二維數組(本地)讓它成爲8X8 並且讓它成爲int類型,並且將所有元素初始化爲零。製作可以通過一個片段到達的數組元素1.如果目標站在一個位置上,那麼您的片段可以到達那裏,返回true。

+0

你能解釋一下那些代碼的功能嗎?就像爲什麼只有「r」我想知道那個車「r」在哪一列和哪一排國際象棋呢? –

+0

嘿,非常感謝你,但有一個錯誤,它不能正常工作,看看我上面上傳的圖像,編輯該主題,它顯示我得到的結果與示例輸入的結果 –

+0

另外我想知道,如果我這樣做,我以後可以使用這些位置來利用算法來確定哪個部分可以攻擊位置基本上移動到這個項目中的位置),因爲它只有一個字符串而不是一個2D字符串 –