Advertisement
calcpage

APCS_CH8_Chess960R1Refactored.java

Mar 1st, 2013
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.15 KB | None | 0 0
  1. /*
  2. Chess960V1Refactored.java       MrG     2013.0226
  3. purpose:    randomly generate 1 of the 960 Fischer Random Chess Boards
  4. required file:  Chess960V1.java                 main class
  5. translator: javac Chess960V1Refactored.java
  6. interpreter:    java Chess960V1Refactored
  7. */
  8.  
  9. import java.util.Random;
  10.  
  11. public static class Chess960V1Refactored
  12. {
  13.     //this is a static class so:
  14.     //String[] is constructed in main()
  15.     //String[] is modified in main()
  16.     //String[] is accessed in main()
  17.     public static void main(String[] args)
  18.     {
  19.         String[] whitePieces = new String[8];
  20.         Bishop(whitePieces,0);
  21.         Bishop(whitePieces,1);
  22.         setPiece(whitePieces,"Q");
  23.         setPiece(whitePieces,"N");
  24.         setPiece(whitePieces,"N");
  25.         setLastPiece(whitePieces,"R");
  26.         setLastPiece(whitePieces,"K");
  27.         setLastPiece(whitePieces,"R");
  28.         System.out.println(toString(whitePieces));
  29.     }
  30.  
  31.     /**
  32.     modify String[] with "B" on a randomly selected even or odd index
  33.     @param wP white's first rank of pieces on the chess board
  34.     @param offset add 0 for even index, add 1 for odd
  35.     */
  36.     public static void Bishop(String[] wP, int offset)
  37.     {
  38.         Random die = new Random();
  39.         wP[die.nextInt(4)*2+offset]="B";
  40.     }
  41.  
  42.     /**
  43.     modify String[] with piece on randomly selected index for an empty square
  44.     @param wP white's first rank of pieces on the chess board
  45.     @param piece can be "Q" for Queen or "N" for Knight
  46.     */
  47.     public static void setPiece(String[] wP, String piece)
  48.     {
  49.         int pos;
  50.         Random die = new Random();
  51.         do
  52.             pos = die.nextInt(8);
  53.         while(wP[pos]!=null);
  54.         wP[pos]=piece;
  55.     }
  56.  
  57.     /**
  58.     modify String[] with piece on an index for the first empty square found
  59.     @param wP white's first rank of pieces on the chess board
  60.     @param piece can be "R" for Rook or "K" for King
  61.     */
  62.     public static void setLastPiece(String[] wP, String piece)
  63.     {
  64.         int pos=0;
  65.         while(wP[pos]!=null)
  66.         {
  67.             pos++;
  68.         }
  69.         wP[pos]=piece;
  70.     }
  71.  
  72.     /**
  73.     access contents of a String[]
  74.     @param wP white's first rank of pieces on the chess board
  75.     @return String[] first rank as a String
  76.     */
  77.     public static String toString(String[] wP)
  78.     {
  79.         String temp = "";
  80.         for(int k=0; k<wP.length; k++)
  81.         {
  82.             temp += wP[k];
  83.         }
  84.         return temp;
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement