【プログラムF:\ホームページ\8q\PlayApplet.java】**************************
import java.awt.*; import java.awt.event.*; import java.util.*; import java.util.regex.*; import javax.swing.*; public class PlayApplet extends JApplet { Queens qu; //Boad bo; Boad2 bo; public void init() { qu=new Queens(); //bo=new Boad(); bo=new Boad2(this); System.out.println("開始"); qu.setQueen(); bo.Display2(qu.plans); bo.Display(qu.plans); } } public class PlayApplication { public static void main(String[] args){ Queens qu=new Queens(); System.out.println("開始"); qu.setQueen(); Boad bo=new Boad(); bo.Display2(qu.plans); bo.Display(qu.plans); } } class Boad{ JFrame jf; JScrollPane js; JTextArea jt; Pattern pattern; Matcher matcher; Boad(){ JFrame jf=new JFrame("Boad"); jf.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){System.exit(0);} }); jf.setBounds(0,0,200,800); jf.setVisible(true); Container co=jf.getContentPane(); jt=new JTextArea(); js=new JScrollPane(jt); co.add(js); jf.setPreferredSize(new Dimension(200,500)); jf.pack(); } void Display2(ArrayList plans){ String raw="00000000"; String cell="□"; String queen="●"; String column=""; int count=1; int position=0; for(int i=0;i<plans.size();i++){ if(i%8==0){ jt.append("\n結果その"+count+"\n"); count++; } raw="00000000"; raw=raw+Integer.toBinaryString((Integer)plans.get(i)); raw=raw.substring(raw.length()-8)+"\n"; pattern = Pattern.compile("0"); matcher = pattern.matcher(raw); raw=matcher.replaceAll(cell); pattern = Pattern.compile("1"); matcher = pattern.matcher(raw); raw=matcher.replaceAll(queen); jt.append(raw); } js.setPreferredSize(js.getPreferredSize()); } void Display(ArrayList plans){ int count=1; for(int i=0;i<plans.size();i++){ if(i%8==0){ jt.append("\n結果その"+count+"\n"); count++; } jt.append(plans.get(i)+","); } } } class Boad2{ JScrollPane js; JTextArea jt; Pattern pattern; Matcher matcher; Boad2(JApplet c1){ Container co=c1.getContentPane(); co.setBounds(0,0,200,800); jt=new JTextArea(); js=new JScrollPane(jt); co.add(js); } void Display2(ArrayList plans){ String raw="00000000"; String cell="□"; String queen="●"; String column=""; int count=1; int position=0; for(int i=0;i<plans.size();i++){ if(i%8==0){ jt.append("\n結果その"+count+"\n"); count++; } raw="00000000"; raw=raw+Integer.toBinaryString((Integer)plans.get(i)); raw=raw.substring(raw.length()-8)+"\n"; pattern = Pattern.compile("0"); matcher = pattern.matcher(raw); raw=matcher.replaceAll(cell); pattern = Pattern.compile("1"); matcher = pattern.matcher(raw); raw=matcher.replaceAll(queen); jt.append(raw); } js.setPreferredSize(js.getPreferredSize()); } void Display(ArrayList plans){ int count=1; for(int i=0;i<plans.size();i++){ if(i%8==0){ jt.append("\n結果その"+count+"\n"); count++; } jt.append(plans.get(i)+","); } } } public class Queens{ int[] area={0,0,0,0,0,0,0,0}; int masque=0; int i; int count=1; Rule rl=new Rule(); ArrayList<Integer> plans=new ArrayList<Integer>(); void setQueen(){ for(i=0;i<area.length;i++){ masque=255-rl.setMasque(i,area); if(area[i]!=0){ area[i]=area[i]>>1; } else if(area[i]==0)area[i]=128; while((masque & area[i])==0){ area[i]=area[i]>>1; if(area[i]==0){ break; } } if(i==7 && area[i]!=0){ for(int j=0;j<area.length;j++){ plans.add(area[j]); } count++; i--; } if(area[0]==0)break; if(area[i]==0)i-=2; } System.out.println("完了"); } } public class Rule{ int masque; int setMasque(int index,int[] boad){ masque=0; if(index==0)return masque; for(int i=0;i<index;i++){ masque=masque|boad[i]|boad[i]<<index-i|boad[i]>>index-i; } return masque; } }