Advertisement
calcpage

APCS_CH8_Chess960R2.java

Mar 19th, 2013
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.09 KB | None | 0 0
  1. /*
  2. Chess960V2.java     MrG     2013.0226
  3. purpose:    randomly generate all 960 Fischer Random Chess Boards
  4. required file:  Chess960V2.java             main class
  5. translator: javac Chess960V2.java
  6. interpreter:    java Chess960V2
  7. */
  8.  
  9. import java.util.Random;
  10. import java.util.ArrayList;
  11.  
  12. public class Chess960V2
  13. {
  14.     public static void main(String[] args)
  15.     {
  16.         int printCount=0;
  17.         ArrayList<String[]> foo = new ArrayList<String[]>();
  18.         while(printCount<960)
  19.         {
  20.             String[] whitePieces = new String[8];
  21.             setBishop(whitePieces,0);
  22.             setBishop(whitePieces,1);
  23.             setPiece(whitePieces,"Q");
  24.             setPiece(whitePieces,"N");
  25.             setPiece(whitePieces,"N");
  26.             setLastPiece(whitePieces,"R");
  27.             setLastPiece(whitePieces,"K");
  28.             setLastPiece(whitePieces,"R");
  29.             if(!found(whitePieces,foo))
  30.             {
  31.                 foo.add(whitePieces);
  32.                 System.out.println(printCount+"\t"+toString(whitePieces));
  33.                 printCount++;
  34.             }
  35.         }
  36.         fileList(foo);
  37.     }
  38.  
  39.     public static boolean found(String[] wP, ArrayList<String[]> bar)
  40.     {
  41.         for(String[] board:bar)
  42.         {
  43.             if(sameBoard(wP,board))
  44.             {
  45.                 return true;
  46.             }
  47.         }
  48.         return false;
  49.     }
  50.  
  51.     public static boolean sameBoard(String[] b1, String[] b2)
  52.     {
  53.         for(int i=0; i<b1.length; i++)
  54.         {
  55.             if(!b1[i].equals(b2[i]))
  56.             {
  57.                 return false;
  58.             }
  59.         }
  60.         return true;
  61.     }
  62.  
  63.     public static void fileList(ArrayList<String[]> bar)
  64.     {
  65.         EasyWriter ross = new EasyWriter("960.txt");
  66.         for(int j=0; j<960; j++)
  67.         {
  68.             ross.println(toString(bar.get(j)));
  69.         }
  70.         ross.close();
  71.     }
  72.  
  73.     public static void setBishop(String[] wP, int offset)
  74.     {
  75.         Random die = new Random();
  76.         wP[die.nextInt(4)*2+offset]="B";
  77.     }
  78.  
  79.     public static void setPiece(String[] wP, String piece)
  80.     {
  81.         int pos;
  82.         Random die = new Random();
  83.         do
  84.             pos = die.nextInt(8);
  85.         while(wP[pos]!=null);
  86.         wP[pos]=piece;
  87.     }
  88.  
  89.     public static void setLastPiece(String[] wP, String piece)
  90.     {
  91.         int pos=0;
  92.         while(wP[pos]!=null)
  93.         {
  94.             pos++;
  95.         }
  96.         wP[pos]=piece;
  97.     }
  98.  
  99.     public static String toString(String[] wP)
  100.     {
  101.         String temp = "";
  102.         for(int k=0; k<wP.length; k++)
  103.         {
  104.             temp += wP[k];
  105.         }
  106.         return temp;
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement