这是一个递归的算法,我这里有完整的围棋的java程序,内涵这个内容
package game.go;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
//算目数的算法
public class Go extends JDialog
{
GamePane mainPane;
JMenuBar mainMenuBar;
public Go(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
setSize(mainPane.getWidth()+8,mainPane.getHeight()+mainMenuBar.getHeight()+50);
}
private void initComponents() {
mainMenuBar = new JMenuBar();
JMenu mainMenu = new JMenu("File");
mainMenuBar.add(mainMenu);
mainPane=new GamePane();
getContentPane().add(mainPane, BorderLayout.CENTER);
setJMenuBar(mainMenuBar);
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
closeDialog(evt);
}
public void windowClosed(java.awt.event.WindowEvent evt) {
System.exit(0);
}
});
}
public static void main(String args[])
{
new Go(null,true).show();
}
private void closeDialog(WindowEvent evt) {
setVisible(false);
dispose();
}
}
class GamePane extends JPanel
{
private static int WIDTH=19,HEIGHT=19;
private static int SQUARE_WIDTH=16,SQUARE_HEIGHT=16;
private static int STARTX=8,STARTY=8;
private static int CIRCLE_WIDTH=SQUARE_WIDTH/3,CIRCLE_HEIGHT=SQUARE_HEIGHT/3;
static final int LEFT=0;
static final int RIGHT=1;
static final int UP=2;
static final int DOWN=3;
private int area[][];
private Stack stack = new Stack();
private Stack argStack =new Stack();
static final int COLOR_BLACK=1;
static final int COLOR_WHITE=2;
int turn;
int mouseX=-1,mouseY=-1;
int argX=-1,argY=-1;
public GamePane()
{
super();
initComponents();
setSize(WIDTH*SQUARE_WIDTH+STARTX*2,HEIGHT*SQUARE_HEIGHT+STARTY*2);
area=new int[WIDTH][HEIGHT];
for(int i=0;i<WIDTH;i++)
for(int j=0;j<HEIGHT;j++)
area[i][j]=0;
turn = COLOR_BLACK;
requestFocus();
}
int getActualX(int x)
{
return (x-STARTX+SQUARE_WIDTH/2)/SQUARE_WIDTH;
}
int getActualY(int y)
{
return (y-STARTY+SQUARE_HEIGHT/2)/SQUARE_HEIGHT;
}
boolean isBeenEat(int x,int y,int turn,int[][] tmpArea)
{
//turn means the turn been eat!
if(x<0||y<0||x>=WIDTH||y>=HEIGHT||tmpArea[x][y]==1) return true;
if(area[x][y]==0) return false;
if(area[x][y]!=turn) return true;
tmpArea[x][y]=1;
if(!isBeenEat(x-1,y,turn,tmpArea))return false;
if(!isBeenEat(x,y-1,turn,tmpArea))return false;
if(!isBeenEat(x+1,y,turn,tmpArea))return false;
if(!isBeenEat(x,y+1,turn,tmpArea))return false;
return true;
}
boolean isBeenEat(int x,int y,int turn)
{
int[][] clearArea=new int[WIDTH][HEIGHT];
int i,j;
for(i=0;i<WIDTH;i++)
for(j=0;j<HEIGHT;j++)
clearArea[i][j]=0;
return isBeenEat(x,y,turn,clearArea);
}
boolean clearPoint(int x,int y,int turn)
{
int i,j;
int[][] clearArea=new int[WIDTH][HEIGHT];
if(x<0 || x>=WIDTH || y<0||y>=HEIGHT || area[x][y]!=turn ) return false;
if(!isBeenEat(x,y,turn,clearArea)) return false;
for(i=0;i<WIDTH;i++)
for(j=0;j<HEIGHT;j++)
{
if(clearArea[i][j]==1)
area[i][j]=0;
}
return true;
}
boolean isSurround(int x,int y,int turn)
{
int nextTurn=getNextTurn(turn);
if((x<1||area[x-1][y]==nextTurn)&&
(x>=WIDTH-1||area[x+1][y]==nextTurn)&&
(y<1||area[x][y-1]==nextTurn)&&
(y>=HEIGHT-1||area[x][y+1]==nextTurn))
return true;
return false;
}
boolean clearEat(int x,int y,int turn)
{//turn means the turn been eat!
boolean isClear=false;
isClear|=clearPoint(x-1,y,turn);
isClear|=clearPoint(x+1,y,turn);
isClear|=clearPoint(x,y-1,turn);
isClear|=clearPoint(x,y+1,turn);
return isClear;
}
private void push()
{
stack.push(arrayClone(area));
argStack.push(new Point(argX,argY));
}
private void pop()
{
area=(int[][])stack.pop();
Point pt=(Point)argStack.pop();
argX=pt.x;argY=pt.y;
}
private void nextTurn()
{
if(turn==COLOR_BLACK)
turn=COLOR_WHITE;
else
turn=COLOR_BLACK;
}
private int getNextTurn(int turn)
{
if(turn==COLOR_BLACK)
return COLOR_WHITE;
return COLOR_BLACK;
}
private void forward(int x,int y)
{
if(argX!=-1 && argY!=-1 && x==argX && y==argY)
{
messageBox("这是抢劫棋,你必须走另外一步去缓和");
return;
}
if(x>=0 && x<WIDTH && y>=0 && y<HEIGHT
&&area[x][y] == 0)
{
int orgColor[]=new int[4];
if(x>=1) orgColor[LEFT]=area[x-1][y];
if(x<WIDTH-1) orgColor[RIGHT]=area[x+1][y];
if(y>=1) orgColor[UP]=area[x][y-1];
if(y<HEIGHT-1) orgColor[DOWN]=area[x][y+1];
push();
area[x][y]=turn;
nextTurn();
if(!clearEat(x,y,turn))
{
if(isBeenEat(x,y,area[x][y]))
{
area[x][y]=0;
nextTurn();
pop();
}
else
{
mouseX=-1;
mouseY=-1;
argX=-1;
argY=-1;
}
}
else
{
boolean dirOK[]=new boolean[4];
dirOK[LEFT]=(x<1||area[x-1][y]==turn);
dirOK[RIGHT]=(x>=WIDTH-1||area[x+1][y]==turn);
dirOK[UP]=(y<1||area[x][y-1]==turn);
dirOK[DOWN]=(y>=HEIGHT-1||area[x][y+1]==turn);
if(orgColor[LEFT]==turn && area[x-1][y]==0&&isSurround(x-1,y,turn)
&&dirOK[RIGHT]&&dirOK[UP]&&dirOK[DOWN])
{
//<--
argX=x-1;argY=y;
}
else if(orgColor[RIGHT]==turn && area[x+1][y]==0&&isSurround(x+1,y,turn)
&&dirOK[LEFT]&&dirOK[UP]&&dirOK[DOWN])
{
//-->
argX=x+1;argY=y;
}
else if(orgColor[UP]==turn && area[x][y-1]==0&&isSurround(x,y-1,turn)
&&dirOK[LEFT]&&dirOK[RIGHT]&&dirOK[DOWN])
{
//|
//|
//v
argX=x;argY=y-1;
}
else if(orgColor[DOWN]==turn && area[x][y+1]==0&&isSurround(x,y+1,turn)
&&dirOK[LEFT]&&dirOK[RIGHT]&&dirOK[UP])
{
//^
//|
//|
argX=x;argY=y+1;
}
else
{
argX=-1;argY=-1;
}
mouseX=-1;
mouseY=-1;
}
repaint();
}
}
private static int[][] arrayClone(int [][] a)
{
int [][] c=new int[a.length][a[0].length];
int i,j;
for(i=0;i<a.length;i++)
System.arraycopy(a[i],0,c[i],0,a[i].length);
return c;
}
private void back()
{
if(stack.isEmpty()) return;
pop();
nextTurn();
repaint();
}
private void initComponents() {
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent evt) {
if((evt.getModifiers() & MouseEvent.BUTTON3_MASK )!=0)
back();
else if((evt.getModifiers() & MouseEvent.BUTTON1_MASK )!=0)
forward(getActualX(evt.getX()),getActualY(evt.getY()));
}
});
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseMoved(MouseEvent evt) {
int x=getActualX(evt.getX());
int y=getActualY(evt.getY());
if(x>=0 && x<WIDTH && y>=0 && y<HEIGHT
&&area[x][y] == 0)
{
mouseX=x;
mouseY=y;
}
else
{
mouseX=-1;
mouseY=-1;
}
repaint();
}
});
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int i,j;
Color c;
for(i=0;i<HEIGHT;i++)
g.drawLine(STARTX,STARTY+i*SQUARE_HEIGHT,STARTX+(WIDTH-1)*SQUARE_WIDTH,STARTY+i*SQUARE_HEIGHT);
for(i=0;i<WIDTH;i++)
g.drawLine(STARTX+i*SQUARE_WIDTH,STARTY,STARTX+i*SQUARE_WIDTH,STARTY+(HEIGHT-1)*SQUARE_HEIGHT);
c=g.getColor();
for(i=0;i<WIDTH;i++)
for(j=0;j<HEIGHT;j++)
{
switch(area[i][j])
{
case COLOR_BLACK:
g.setColor(Color.black);
break;
case COLOR_WHITE:
g.setColor(Color.white);
break;
default:
continue;
}
g.fillOval(STARTX+i*SQUARE_WIDTH-CIRCLE_WIDTH,
STARTY+j*SQUARE_HEIGHT-CIRCLE_HEIGHT,
CIRCLE_WIDTH*2,
CIRCLE_HEIGHT*2);
}
if(mouseX!=-1 && mouseY!=-1)
{
if(turn==COLOR_BLACK)
g.setColor(Color.black);
else
g.setColor(Color.white);
g.drawRect(STARTX