import java.awt.*; import java.applet.Applet; import java.awt.event.*; import java.awt.image.*; import java.util.*; public class Igo extends Applet implements WindowListener,LayoutManager { Frame frame; public void init() { setBackground(Color.white); setLayout(new BorderLayout()); Zu z=new Zu(); add(z.message,BorderLayout.NORTH); Panel panel=new Panel(new FlowLayout()); panel.add(z); add(panel,BorderLayout.CENTER); panel=new Panel(new FlowLayout()); panel.add(z.halt); add(panel,BorderLayout.SOUTH); } static public void main(String[] args) { Igo igo=new Igo(); igo.init(); igo.frame=new Frame("ˆÍŒé"); igo.frame.setSize(400,400); igo.frame.setBackground(Color.white); igo.frame.setLayout(igo); igo.frame.add(igo); igo.frame.addWindowListener(igo); igo.frame.show(); } public void addLayoutComponent(String name,Component comp) {} public void layoutContainer(Container parent) { setBounds((frame.getSize().width-512)/2,(frame.getSize().height-384)/2,512,384); } public Dimension minimumLayoutSize(Container container) { return new Dimension(512,384); } public Dimension preferredLayoutSize(Container container) { return new Dimension(640,450); } public void removeLayoutComponent(Component comp) {} public void windowOpened(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowClosing(WindowEvent e) { System.exit(0); } public void windowClosed(WindowEvent e) {} } class Zu extends Canvas implements MouseListener,MouseMotionListener,ActionListener { Ban ban; Image offscreen; Button halt=new Button("‚Ü‚Á‚½"); Label message=new Label(new String(),Label.CENTER); int i=0,j=0; boolean guide=false; Zu() { ban=new Ban(); setSize(19*16,19*16); addMouseListener(this); addMouseMotionListener(this); halt.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (ban.previous!=null) ban=ban.previous; repaint(); } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { if (offscreen==null) offscreen=createImage(getSize().width,getSize().height); Graphics og=offscreen.getGraphics(); og.clearRect(0,0,getSize().width,getSize().height); og.setColor(Color.gray); for (int i=0;i<19;i++) og.drawLine(i*16+8,8,i*16+8,18*16+8); for (int j=0;j<19;j++) og.drawLine(8,j*16+8,18*16+8,j*16+8); for (int i=-1;i<=1;i++) for (int j=-1;j<=1;j++) og.fillOval((9+i*6)*16+6,(9+j*6)*16+6,5,5); if (guide) if (ban.te) { og.fillOval(i*16+2,j*16+2,12,12); } else { og.setColor(Color.white); og.fillOval(i*16+2,j*16+2,12,12); og.setColor(Color.gray); og.drawOval(i*16+2,j*16+2,12,12); } og.setColor(Color.black); for (int i=0;i<19;i++) for (int j=0;j<19;j++) if (ban.exist[i][j]) if (ban.color[i][j]) { og.fillOval(i*16+2,j*16+2,12,12); } else { og.setColor(Color.white); og.fillOval(i*16+2,j*16+2,12,12); og.setColor(Color.black); og.drawOval(i*16+2,j*16+2,12,12); } g.drawImage(offscreen,0,0,this); message.setText((ban.te?"•”Ô":"”’”Ô")+" (—g•l "+"•"+ban.black+" ”’"+ban.white+")"); } public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) { i=e.getX()/16; j=e.getY()/16; guide=true; repaint(); } public void mouseReleased(MouseEvent e) { if (guide) { ban=ban.put(i,j); guide=false; repaint(); } } public void mouseDragged(MouseEvent e) { guide=(i==e.getX()/16 && j==e.getY()/16); repaint(); } public void mouseMoved(MouseEvent e) {} } class Ban { static final int[] DI={1,0,-1,0},DJ={0,1,0,-1}; boolean[][] exist=new boolean[19][19]; boolean[][] color=new boolean[19][19]; boolean te; int black,white; Ban previous; Ban() { te=true; black=0; white=0; } Ban(Ban ban) { for (int i=0;i<19;i++) for (int j=0;j<19;j++) { exist[i][j]=ban.exist[i][j]; color[i][j]=ban.color[i][j]; } black=ban.black; white=ban.white; te=!ban.te; previous=ban; } Ban put(int i,int j) { if (exist[i][j]) return this; else { Ban ban=new Ban(this); ban.exist[i][j]=true; ban.color[i][j]=te; for (int d=0;d<4;d++) try { boolean[][] taken=new boolean[19][19]; ban.scan(taken,i+DI[d],j+DJ[d],!te); for (int k=0;k<19;k++) for (int l=0;l<19;l++) if (taken[k][l]) { if (te) ban.white++; else ban.black++; ban.exist[k][l]=false; } } catch (AliveException e) { } try { ban.scan(new boolean[19][19],i,j,te); return this; } catch (AliveException e) { return ban; } } } void scan(boolean[][] taken,int i,int j,boolean te) throws AliveException { try { if (!exist[i][j]) throw new AliveException(); if (!taken[i][j] && color[i][j]==te) { taken[i][j]=true; for (int d=0;d<4;d++) scan(taken,i+DI[d],j+DJ[d],te); } } catch (ArrayIndexOutOfBoundsException e) { } } } class AliveException extends Exception { }