|
|
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class ConnectFive extends Applet implements Runnable,MouseListener {
Phase current=new Phase();
Image offscreen;
public void init() {
current.resize(this);
addMouseListener(this);
}
public void start() {
current=new Phase();
}
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();
current.paint(og);
g.drawImage(offscreen,0,0,this);
}
public void mouseClicked(MouseEvent e) {
if (current.checkmate)
start();
else if (current.turn)
current=current.mouse(e.getX(),e.getY());
repaint();
new Thread(this).start();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
if (!current.turn)
current=current.put();
repaint();
}
}
class Phase {
static int size=19, pitch=20;
static int direction[][]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,1},{1,-1}};
static int valuetable[][]={{0,1,4,9,20},{2,3,5,10,21},{6,7,8,11,22},{12,13,14,15,23},{16,17,18,19,24}};
boolean color[][]=new boolean[size][size];
boolean exist[][]=new boolean[size][size];
boolean turn;
boolean brandnew=false,checkmate;
int[] point=new int[4];
Phase() {
for (int x=0; x<size; x++) for (int y=0; y<size; y++) {
exist[x][y]=false;
color[x][y]=false;}
turn=true;
brandnew=true;
}
Phase(Phase p) {
for (int x=0; x<size; x++) for (int y=0; y<size; y++) {
exist[x][y]=p.exist[x][y];
color[x][y]=p.color[x][y];}
turn=p.turn;
}
int chain (int x, int y) {
return chain(x,y,turn);
}
int chain (int x,int y,boolean t) {
int[] chains=new int[8];
for (int dir=0; dir<8; dir++) {
for (int step=1; true; step++) {
int xx=x+step*direction[dir][0];
int yy=y+step*direction[dir][1];
if (xx<0 || xx>=size || yy<0 || yy>=size || ! exist[xx][yy] || color[xx][yy]!=t) {
chains[dir]=step-1;
break;}}}
int largest=0;
for (int dir=0; dir<4; dir++)
if (chains[dir]+chains[dir+4]>largest)
largest=chains[dir]+chains[dir+4];
return largest;
}
void examinate (int x,int y) {
int[] chains=new int[8];
for (int dir=0; dir<8; dir++) {
for (int step=1; true; step++) {
int xx=x+step*direction[dir][0];
int yy=y+step*direction[dir][1];
if (xx<0 || xx>=size || yy<0 || yy>=size || ! exist[xx][yy] || color[xx][yy]==turn) {
chains[dir]=step-1;
break;}}}
int largest=0;
for (int dir=0; dir<4; dir++)
if (chains[dir]+chains[dir+4]>3) {
largest=chains[dir]+chains[dir+4];
point[0]=x+chains[dir]*direction[dir][0];
point[1]=y+chains[dir]*direction[dir][1];
point[2]=x+chains[dir+4]*direction[dir+4][0];
point[3]=y+chains[dir+4]*direction[dir+4][1];
}
}
synchronized Phase put (int x,int y) {
if (exist[x][y] || checkmate) return this;
Phase next=new Phase(this);
next.exist[x][y]=true;
next.color[x][y]=turn;
next.turn=!turn;
if (chain(x,y)>3) {
next.checkmate=true;
next.examinate(x,y);}
return next;
}
Phase mouse (int x,int y) {
return put((x-pitch/2)/pitch,(y-pitch/2)/pitch);
}
Phase put () {
int optimumX=size/2,optimumY=size/2;
for (int x=0; x<size; x++) for (int y=0; y<size; y++)
if (value(x,y)>value(optimumX,optimumY)) {
optimumX=x;
optimumY=y;}
return put(optimumX,optimumY);
}
int value(int x,int y) {
if (exist[x][y]) return 0;
try {
return valuetable[chain(x,y,!turn)][chain(x,y,turn)];
} catch (ArrayIndexOutOfBoundsException e) {
return 25;
}
}
void resize(Applet a) {
a.resize((size+1)*pitch,(size+1)*pitch);
}
void paint (Graphics g) {
if (brandnew) {
g.setColor(Color.ORANGE);
g.fillRect(0,0,(size+1)*pitch,(size+1)*pitch);
brandnew=false;}
g.setColor(Color.black);
for (int x=0; x<size; x++)
g.drawLine(pitch+pitch*x,pitch,pitch+pitch*x,pitch*size);
for (int y=0; y<size; y++)
g.drawLine(pitch,pitch+pitch*y,pitch*size,pitch+pitch*y);
for (int x=0; x<size; x++) for (int y=0; y<size; y++)
if (exist[x][y] && color[x][y])
g.fillOval(pitch/2+pitch*x+2,pitch/2+pitch*y+2,pitch-2,pitch-2);
g.setColor(Color.white);
for (int x=0; x<size; x++) for (int y=0; y<size; y++)
if (exist[x][y] && !color[x][y])
g.fillOval(pitch/2+pitch*x+2,pitch/2+pitch*y+2,pitch-2,pitch-2);
if (checkmate) {
g.setColor(Color.red);
g.drawLine(pitch+pitch*point[0],pitch+pitch*point[1],pitch+pitch*point[2],pitch+pitch*point[3]);}
}
}
このプログラムのclass phaseの内容がわかる人解説お願いします。
|
|