我有一个国际象棋游戏的java代码,我没有获得相同的整体流程,所以请尽量帮助我整个代码的整体流程.. 如果你在我的代码中写下评论,那将非常感谢你... 问候....`
enter code here
//in all there are 9 modules
//this is the main module named chess.java
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Chess extends JFrame
{
public Chess(int i)
{
MyWindowListener myWindowListener=new MyWindowListener();
this.addWindowListener(myWindowListener);
setSize(550,550);
setTitle("Chess");
init(i);
setVisible(true);
}
class MyWindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent event)
{
Object object = event.getSource();
if (object == Chess.this)
WindowClosing(event);
}
}
void WindowClosing(WindowEvent e)
{
setVisible(false);
dispose();
System.exit(0);
}
public void init(int i)
{
Container cp = getContentPane();
cp.setLayout(new GridLayout(0,1));
cp.add(new ChessComponent(i, null));
}
public static void main(String args[])
{
if(args.length>0)
new Chess(new Integer(args[0]).intValue());
else
new Chess(3);
}
}
enter code here
//module 2
//the following code is for ChessApplet.java
import java.applet.*;
import java.awt.*;
import javax.swing.*;
public class ChessApplet extends JApplet
{
public ChessApplet()
{
getRootPane().putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);
}
public void init() {
String s = getParameter("DEPTH");
if(s==null)
getContentPane().add(new ChessComponent(3, this));
else
getContentPane().add(new ChessComponent(new Integer(s).intValue(), this));
}
}
enter code here
//module 3
//ChessComponent.java
import java.applet.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class ChessComponent extends JPanel implements ActionListener
{
public Applet applet = null;
public JList moves;
public JTextArea think;
public JButton restart;
public JProgressBar progress;
public ChessListModel clm;
public JLabel max;
FieldDocument fd;
FieldView fv;
public ChessComponent(int i, Applet a)
{
applet = a;
setLayout(new BorderLayout());
setBorder(new EmptyBorder(0,0,0,0));
fd = new FieldDocument(i, this);
fd.initField();
fv = new FieldView(fd, this);
add(fv, BorderLayout.CENTER);
clm = new ChessListModel();
moves = new JList(clm);
moves.setBackground(new Color(69,76,107));
moves.setForeground(Color.white);
moves.setFont(new Font("verdena", Font.BOLD, 12));
JPanel right = new JPanel(new BorderLayout(0,5));
right.setBackground(new Color(69,76,107));
JPanel rightup = new JPanel(new BorderLayout());
rightup.setBackground(new Color(69,76,107));
rightup.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED),
"Moves", TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION,
new Font("sanserif",Font.PLAIN,12), Color.white));
JScrollPane smoves = new JScrollPane(moves);
smoves.setBorder(new EmptyBorder(0,0,0,0));
rightup.add(smoves, BorderLayout.CENTER);
right.add(rightup, BorderLayout.CENTER);
restart = new JButton(" Restart ");
restart.addActionListener(this);
restart.setBackground(new Color(69,76,107));
restart.setForeground(Color.white);
right.add(restart, BorderLayout.SOUTH);
add(right, BorderLayout.WEST);
think = new JTextArea("", 3, 50);
think.setLineWrap(true);
think.setWrapStyleWord(true);
think.setBackground(new Color(69,76,107));
think.setForeground(Color.white);
think.setFont(new Font("verdena", Font.BOLD, 12));
JPanel down = new JPanel(new BorderLayout());
down.setBackground(new Color(69,76,107));
JPanel downleft = new JPanel(new BorderLayout());
downleft.setBackground(new Color(69,76,107));
downleft.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED),
"Brain", TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION,
new Font("sanserif",Font.PLAIN,12), Color.white));
JScrollPane sthink = new JScrollPane(think);
sthink.setBorder(new EmptyBorder(0,0,0,0));
downleft.add(sthink, BorderLayout.CENTER);
JPanel pr = new JPanel(new GridLayout(2,1));
pr.setBackground(new Color(69,76,107));
pr.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED),"Progress",TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION,
new Font("sanserif",Font.PLAIN,12), Color.white));
progress = new JProgressBar(0,0);
progress.setBorderPainted(false);
progress.setStringPainted(true);
progress.setString("0%");
progress.setBorder(new LineBorder(Color.white));
progress.setBackground(new Color(69,76,107));
max = new JLabel("", SwingConstants.CENTER);
max.setForeground(Color.white);
max.setFont(new Font("verdena", Font.BOLD, 12));
pr.add(max);
pr.add(progress);
down.add(downleft, BorderLayout.CENTER);
down.add(pr, BorderLayout.EAST);
add(down, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) {
restart.setEnabled(false);
fd.stopped = true;
try{
fv.thread.join();
}catch(Exception ex){}
fd.initField();
fv.addMouse();
clm = new ChessListModel();
moves.setModel(clm);
think.setText("");
max.setText("");
fv.verdict.setText("");
progress.setValue(0);
progress.setString("0%");
fv.prepareToPaint();
fv.repaint();
restart.setEnabled(true);
}
}
enter code here
//module 4
//ChessListModel
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
public class ChessListModel extends AbstractListModel {
protected Vector list;
public ChessListModel() {
list = new Vector();
list.add("");
}
public Object getElementAt(int index) {
return list.elementAt(index);
}
public int getSize() {
return list.size();
}
public void add(String s) {
list.add(s);
fireIntervalAdded(this, list.size(), list.size());
}
}
enter code here
//module 5
//constants
public interface Constants {
String[] COORD_X = {"A", "B", "C", "D", "E", "F", "G", "H"};
int TOP_MARGIN = 16;
int LEFT_MARGIN = 46;
int FIELD_SIZE = 368;
int CELL_SIZE = 46;
byte TYPE_PAWN = 0;
byte TYPE_KNIGHT = 1;
byte TYPE_BISHOP = 2;
byte TYPE_ROOK = 3;
byte TYPE_QUEEN = 4;
byte TYPE_KING = 5;
String[] name = {"Pa", "Kn", "Bi", "Ro", "Qu", "Ki"};
int[] cost = {1,5,5,10,40,100};
}
enter code here
//module 6:Coord
public class Coord implements Constants {
public byte x;
public byte y;
public Coord(int x, int y) {
setCoord((byte)x, (byte)y);
}
public void setCoord(byte x, byte y) {
this.x = x;
this.y = y;
}
public String toString() {
return COORD_X[x]+(y+1);
}
}
enter code here
//module 7:FieldDocument
import java.util.*;
public class FieldDocument implements Constants {
public Figure[][] f;
public Coord whiteKing;
public Coord blackKing;
protected ChessComponent cc;
public boolean stopped;
Random rnd = new Random();
public FieldDocument(int i, ChessComponent cc) {
this.cc=cc;
dd = i;
}
public FieldDocument(FieldDocument fd, ChessComponent cc) {
this.cc=cc;
dd = fd.dd;
f = new Figure[8][8];
for(byte i=0;i<8;i++)
for(byte j=0;j<8;j++)
f[i][j]=fd.f[i][j];
whiteKing = new Coord(fd.whiteKing.x, fd.whiteKing.y);
blackKing = new Coord(fd.blackKing.x, fd.blackKing.y);
}
public void initField() {
f = new Figure[8][8];
for(int i=0;i<8;i++) {
f[i][1] = new Figure(Figure.TYPE_PAWN, true, cc);
f[i][6] = new Figure(Figure.TYPE_PAWN, false, cc);
}
f[0][0]=f[7][0] = new Figure(Figure.TYPE_ROOK, true, cc);
f[0][7]=f[7][7] = new Figure(Figure.TYPE_ROOK, false, cc);
f[1][0]=f[6][0] = new Figure(Figure.TYPE_KNIGHT, true, cc);
f[1][7]=f[6][7] = new Figure(Figure.TYPE_KNIGHT, false, cc);
f[2][0]=f[5][0] = new Figure(Figure.TYPE_BISHOP, true, cc);
f[2][7]=f[5][7] = new Figure(Figure.TYPE_BISHOP, false, cc);
f[3][0] = new Figure(Figure.TYPE_QUEEN, true, cc);
f[4][0] = new Figure(Figure.TYPE_KING, true, cc);
f[4][7] = new Figure(Figure.TYPE_KING, false, cc);
f[3][7] = new Figure(Figure.TYPE_QUEEN, false, cc);
whiteKing = new Coord(4,0);
blackKing = new Coord(3,7);
stopped = false;
}
public Vector getPawnMoves(Coord c) {
Vector v = new Vector();
if(f[c.x][c.y].white) {
if(f[c.x][c.y+1]==null) {
v.add(new Move(c.x,c.y,c.x,c.y+1));
if(c.y==1)
v.add(new Move(c.x,c.y,c.x,c.y+2));
}
} else {
if(f[c.x][c.y-1]==null) {
v.add(new Move(c.x,c.y,c.x,c.y-1));
if(c.y==6)
v.add(new Move(c.x,c.y,c.x,c.y-2));
}
}
return (v.size()==0)?null:v;
}
public Vector getPawnAttacks(Coord c) {
Vector v = new Vector();
if(f[c.x][c.y].white) {
if(c.x+1<=7)
v.add(new Move(c.x,c.y,c.x+1,c.y+1));
if(c.x-1>=0)
v.add(new Move(c.x,c.y,c.x-1,c.y+1));
} else {
if(c.x+1<=7)
v.add(new Move(c.x,c.y,c.x+1,c.y-1));
if(c.x-1>=0)
v.add(new Move(c.x,c.y,c.x-1,c.y-1));
}
return (v.size()==0)?null:v;
}
public Vector getKnightMoves(Coord c) {
Vector v = new Vector();
if(c.x+1<=7) {
if(c.y+2<=7)
v.add(new Move(c.x,c.y,c.x+1,c.y+2));
if(c.y-2>=0)
v.add(new Move(c.x,c.y,c.x+1,c.y-2));
if(c.x+2<=7) {
if(c.y+1<=7)
v.add(new Move(c.x,c.y,c.x+2,c.y+1));
if(c.y-1>=0)
v.add(new Move(c.x,c.y,c.x+2,c.y-1));
}
}
if(c.x-1>=0) {
if(c.y+2<=7)
v.add(new Move(c.x,c.y,c.x-1,c.y+2));
if(c.y-2>=0)
v.add(new Move(c.x,c.y,c.x-1,c.y-2));
if(c.x-2>=0) {
if(c.y+1<=7)
v.add(new Move(c.x,c.y,c.x-2,c.y+1));
if(c.y-1>=0)
v.add(new Move(c.x,c.y,c.x-2,c.y-1));
}
}
return (v.size()==0)?null:v;
}
public Vector getKnightAttacks(Coord c) {
return getKnightMoves(c);
}
public Vector getBishopMoves(Coord c) {
return getBishopAttacks(c);
}
public Vector getBishopAttacks(Coord c) {
Vector v = new Vector();
byte i = 1;
while(c.x+i<=7 && c.y+i<=7) {
v.add(new Move(c.x,c.y,c.x+i,c.y+i));
if(f[c.x+i][c.y+i]!=null)
break;
i++;
}
i = 1;
while(c.x+i<=7 && c.y-i>=0) {
v.add(new Move(c.x,c.y,c.x+i,c.y-i));
if(f[c.x+i][c.y-i]!=null)
break;
i++;
}
i = 1;
while(c.x-i>=0 && c.y-i>=0) {
v.add(new Move(c.x,c.y,c.x-i,c.y-i));
if(f[c.x-i][c.y-i]!=null)
break;
i++;
}
i = 1;
while(c.x-i>=0 && c.y+i<=7) {
v.add(new Move(c.x,c.y,c.x-i,c.y+i));
if(f[c.x-i][c.y+i]!=null)
break;
i++;
}
return (v.size()==0)?null:v;
}
public Vector getRookMoves(Coord c) {
return getRookAttacks(c);
}
public Vector getRookAttacks(Coord c) {
Vector v = new Vector();
byte i = 1;
while(c.y+i<=7) {
v.add(new Move(c.x,c.y,c.x,c.y+i));
if(f[c.x][c.y+i]!=null)
break;
i++;
}
i = 1;
while(c.y-i>=0) {
v.add(new Move(c.x,c.y,c.x,c.y-i));
if(f[c.x][c.y-i]!=null)
break;
i++;
}
i = 1;
while(c.x-i>=0) {
v.add(new Move(c.x,c.y,c.x-i,c.y));
if(f[c.x-i][c.y]!=null)
break;
i++;
}
i = 1;
while(c.x+i<=7) {
v.add(new Move(c.x,c.y,c.x+i,c.y));
if(f[c.x+i][c.y]!=null)
break;
i++;
}
return (v.size()==0)?null:v;
}
public Vector getQueenMoves(Coord c) {
return sumVectors(getBishopMoves(c), getRookMoves(c));
}
public Vector getQueenAttacks(Coord c) {
return getQueenMoves(c);
}
public Vector getBasicKingMoves(Coord c) {
Vector v = new Vector();
if(c.x+1<=7) {
v.add(new Move(c.x,c.y,c.x+1,c.y));
if(c.y+1<=7)
v.add(new Move(c.x,c.y,c.x+1,c.y+1));
if(c.y-1>=0)
v.add(new Move(c.x,c.y,c.x+1,c.y-1));
}
if(c.x-1>=0) {
v.add(new Move(c.x,c.y,c.x-1,c.y));
if(c.y+1<=7)
v.add(new Move(c.x,c.y,c.x-1,c.y+1));
if(c.y-1>=0)
v.add(new Move(c.x,c.y,c.x-1,c.y-1));
}
if(c.y+1<=7)
v.add(new Move(c.x,c.y,c.x,c.y+1));
if(c.y-1>=0)
v.add(new Move(c.x,c.y,c.x,c.y-1));
return (v.size()==0)?null:v;
}
public Vector getKingMoves(Coord c) {
Vector v = getBasicKingMoves(c);
if(v==null)
v = new Vector();
// castling
if(f[c.x][c.y].white) {
if(c.x==4 && c.y==0 && !isAttacked(new Coord(4,0), false)) {
// short
if(f[5][0]==null && !isAttacked(new Coord(5,0), false) &&
f[6][0]==null && !isAttacked(new Coord(6,0), false) &&
f[7][0]!=null && f[7][0].type==TYPE_ROOK && f[7][0].white)
v.add(new Move(4,0,6,0));
// long
if(f[3][0]==null && !isAttacked(new Coord(3,0), false) &&
f[2][0]==null && !isAttacked(new Coord(2,0), false) &&
f[1][0]==null &&
f[0][0]!=null && f[0][0].type==TYPE_ROOK && f[0][0].white)
v.add(new Move(4,0,2,0));
}
} else {
if(c.x==4 && c.y==7 && !isAttacked(new Coord(4,7), true)) {
// short
if(f[5][7]==null && !isAttacked(new Coord(5,7), true) &&
f[6][7]==null && !isAttacked(new Coord(6,7), true) &&
f[7][7]!=null && f[7][7].type==TYPE_ROOK && !f[7][7].white)
v.add(new Move(4,7,6,7));
// long
if(f[3][7]==null && !isAttacked(new Coord(3,7), true) &&
f[2][7]==null && !isAttacked(new Coord(2,7), true) &&
f[1][0]==null &&
f[0][7]!=null && f[0][7].type==TYPE_ROOK && !f[0][7].white)
v.add(new Move(4,7,2,7));
}
}
return (v.size()==0)?null:v;
}
public Vector getKingAttacks(Coord c) {
return getBasicKingMoves(c);
}
public Vector getMoves(Coord c) {
Figure fg = f[c.x][c.y];
if(fg==null)
return null;
switch(fg.type) {
case Figure.TYPE_PAWN:
return getPawnMoves(c);
case Figure.TYPE_KNIGHT:
return getKnightMoves(c);
case Figure.TYPE_BISHOP:
return getBishopMoves(c);
case Figure.TYPE_ROOK:
return getRookMoves(c);
case Figure.TYPE_QUEEN:
return getQueenMoves(c);
case Figure.TYPE_KING:
return getKingMoves(c);
}
return null;
}
public Vector getAttacks(Coord c) { // prostreleny mnoj sejchas
Figure fg = f[c.x][c.y];
if(fg==null)
return null;
switch(fg.type) {
case Figure.TYPE_PAWN:
return getPawnAttacks(c);
case Figure.TYPE_KNIGHT:
return getKnightAttacks(c);
case Figure.TYPE_BISHOP:
return getBishopAttacks(c);
case Figure.TYPE_ROOK:
return getRookAttacks(c);
case Figure.TYPE_QUEEN:
return getQueenAttacks(c);
case Figure.TYPE_KING:
return getKingAttacks(c);
}
return null;
}
public Vector getRealMoves(Coord c) { // can go there and tochno not eat
Figure fg = f[c.x][c.y];
Vector v = getMoves(c);
if(v!=null) {
for(int k=0;k<v.size();k++) {
Move mmm = (Move)v.elementAt(k);
if(f[mmm.x2][mmm.y2]!=null) {
v.remove(k);
k--;
} else {
mmm.perform(this);
if(isAttacked(fg.white?whiteKing:blackKing, !fg.white)) {
v.remove(k);
k--;
}
mmm.undo(this);
}
}
if(v.size()==0)
v = null;
}
return v;
}
public Vector getRealAttacks(Coord c) { // can go there and tochno eat
Figure fg = f[c.x][c.y];
Vector v = getAttacks(c);
if(v!=null) {
for(int k=0;k<v.size();k++) {
Move mmm = (Move)v.elementAt(k);
if(f[mmm.x2][mmm.y2]==null || f[mmm.x2][mmm.y2].white==fg.white) {
v.remove(k);
k--;
} else {
mmm.perform(this);
if(isAttacked(fg.white?whiteKing:blackKing, !fg.white)) {
v.remove(k);
k--;
}
mmm.undo(this);
}
}
if(v.size()==0)
v = null;
}
return v;
}
public Vector getRealPerformance(Coord c) {
Figure fg = f[c.x][c.y];
if(fg==null)
return null;
Vector v = null;
switch(fg.type) {
case Figure.TYPE_PAWN:
return sumVectors(getRealMoves(c), getRealAttacks(c));
case Figure.TYPE_KNIGHT:
v = getKnightMoves(c);
break;
case Figure.TYPE_BISHOP:
v = getBishopAttacks(c);
break;
case Figure.TYPE_ROOK:
v = getRookAttacks(c);
break;
case Figure.TYPE_QUEEN:
v = sumVectors(getBishopAttacks(c), getRookAttacks(c));
break;
case Figure.TYPE_KING:
v = getKingMoves(c);
break;
}
if(v!=null) {
for(int k=0;k<v.size();k++) {
Move mmm = (Move)v.elementAt(k);
if(f[mmm.x2][mmm.y2]!=null && f[mmm.x2][mmm.y2].white==fg.white) {
v.remove(k);
k--;
} else {
mmm.perform(this);
if(isAttacked(fg.white?whiteKing:blackKing, !fg.white)) {
v.remove(k);
k--;
}
mmm.undo(this);
}
}
if(v.size()==0)
v = null;
}
return v;
}
public boolean isAttacked(Coord c, boolean white) {
for(byte i=0;i<8;i++)
for(byte j=0;j<8;j++)
if(f[i][j]!=null && f[i][j].white==white) {
Vector v = getAttacks(new Coord(i,j));
if(v!=null) {
for(int k=0;k<v.size();k++) {
Move m = (Move)v.elementAt(k);
if(m.x2==c.x && m.y2==c.y)
return true;
}
}
}
return false;
}
public boolean canMove(Move m) {
if(f[m.x1][m.y1]==null)
return false;
Vector v = getRealPerformance(new Coord(m.x1,m.y1));
if(v!=null) {
for(int k=0;k<v.size();k++) {
Move mm = (Move)v.elementAt(k);
if(m.x2==mm.x2 && m.y2==mm.y2)
return true;
}
}
return false;
}
public Vector sumVectors(Vector a, Vector b) {
if(a==null && b==null)
return null;
Vector v=new Vector();
if(a!=null)
for(int i=0;i<a.size();i++)
v.add(a.elementAt(i));
if(b!=null)
for(int i=0;i<b.size();i++)
v.add(b.elementAt(i));
return v;
}
public int getCost() {
int out = 0;
for(int i=0;i<8;i++)
for(int j=0;j<8;j++)
if(f[i][j]!=null)
out+=f[i][j].getCost();
return out;
}
public int getAttackCost() {
int out = 0;
for(byte i=0;i<8;i++)
for(byte j=0;j<8;j++)
if(f[i][j]!=null) {
Vector v = getRealAttacks(new Coord(i,j));
if(v!=null)
for(int k=0;k<v.size();k++) {
Move m = (Move)v.elementAt(k);
out += f[m.x2][m.y2].getCost();
}
}
return -out;
}
public boolean checkMate() {
return isAttacked(blackKing, true) && !replyForMate(false) ||
isAttacked(whiteKing, false) && !replyForMate(true);
}
public boolean twoKings() {
for(byte i=0;i<8;i++)
for(byte j=0;j<8;j++)
if(f[i][j]!=null && f[i][j].type!=TYPE_KING)
return false;
return true;
}
public boolean replyForMate(boolean white) {
for(byte i=0;i<8;i++)
for(byte j=0;j<8;j++)
if(f[i][j]!=null && f[i][j].white==white)
if(getRealPerformance(new Coord(i,j))!=null)
return true;
return false;
}
int eb_c;
int eb_ac;
public int estimateBase() {
mm = null;
cc.think.setText("");
cc.progress.setValue(0);
stepcounter = 0;
eb_c = 0;
eb_ac = 0;
int out = estimate();
eb_c = getCost();
eb_ac = getAttackCost();
return out;
}
public int estimate() {
stepcounter++;
int dc = getCost()-eb_c;
int dac = getAttackCost()-eb_ac;
return dc*10+dac;
}
public Vector randomize(Vector v) {
int s=v.size();
int b=0;
for(int i=0;i<s;i++) {
Move m = (Move)v.elementAt(i);
if(f[m.x2][m.y2]!=null) {
v.remove(i);
v.insertElementAt(m, 0);
b++;
}
}
for(int i=b;i<s-1;i++) {
Object o = v.elementAt(i);
int j = rnd.nextInt(s-i-1)+i+1;
v.setElementAt(v.elementAt(j), i);
v.setElementAt(o, j);
}
return v;
}
public Vector successors(boolean white) {
Vector v = new Vector();
for(byte i=0;i<8;i++)
for(byte j=0;j<8;j++)
if(f[i][j]!=null && f[i][j].white==white)
v = sumVectors(v, getRealPerformance(new Coord(i,j)));
return v;
}
public int alfaBeta(boolean white, int alpha, int beta, int depth) {
if(stopped)
return 0;
if(depth==0)
return estimate();
int best = -INFINITY;
Vector v = successors(white);
if(v!=null) {
int siz = v.size();
if(depth==dd)
cc.progress.setMaximum(v.size());
v = randomize(v);
while(v.size()>0 && best<beta) {
Move m = (Move)v.remove(0);
m.perform(this);
if(best>alpha)
alpha = best;
int est = -alfaBeta(!white, -beta, -alpha, depth-1);
if(est>best) {
best = est;
if(depth==dd) {
cc.max.setText("Tendency: ["+(estnow)+"]->["+(estnow-est)+"]");
mm = m;
}
}
m.undo(this);
if(depth==dd) {
cc.think.setText((mm==null?"":mm+" - ")+v.toString());
cc.progress.setValue(siz-v.size());
cc.progress.setString((int)((double)(siz-v.size())*100/(double)siz)+"%");
}
}
}
return best;
}
public int principalVariation(boolean white, int alpha, int beta, int depth) {
if(stopped)
return 0;
if(depth==0)
return estimate();
Vector v = successors(white);
int best = -INFINITY;
if(v!=null) {
int siz = v.size();
if(depth==dd)
cc.progress.setMaximum(v.size());
v = randomize(v);
if(v.size()>0) {
Move m = (Move)v.remove(0);
m.perform(this);
best = -principalVariation(!white, -beta, -alpha, depth-1);
if(depth==dd) {
cc.max.setText("Tendency: ["+(estnow)+"]->["+(estnow-best)+"]");
mm = m;
}
m.undo(this);
}
while(v.size()>0 && best<beta) {
Move m = (Move)v.remove(0);
m.perform(this);
if(best>alpha)
alpha = best;
int est = -principalVariation(!white, -alpha-1, -alpha, depth-1);
if(est>alpha && est<beta) {
best = -principalVariation(!white, -beta, -est, depth-1);
if(depth==dd) {
cc.max.setText("Tendency: ["+(estnow)+"]->["+(estnow-est)+"]");
mm = m;
}
} else if(est>best) {
best = est;
if(depth==dd) {
cc.max.setText("Tendency: ["+(estnow)+"]->["+(estnow-est)+"]");
mm = m;
}
}
m.undo(this);
if(depth==dd) {
cc.think.setText((mm==null?"":mm+" - ")+v.toString());
cc.progress.setValue(siz-v.size());
cc.progress.setString((int)((double)(siz-v.size())*100/(double)siz)+"%");
}
}
}
return best;
}
public int negaScout(boolean white, int alpha, int beta, int depth) {
if(stopped)
return 0;
if(depth==0)
return estimate();
int best = -INFINITY;
int n = beta;
Vector v = successors(white);
if(v!=null) {
int siz = v.size();
if(depth==dd)
cc.progress.setMaximum(v.size());
v = randomize(v);
if(v.size()>0 && depth==dd)
mm = (Move)v.elementAt(0);
while(v.size()>0 && best<beta) {
Move m = (Move)v.remove(0);
m.perform(this);
int est = -negaScout(!white, -n, -Math.max(alpha, best), depth-1);
if(est>best) {
if(n==beta || depth<=2) {
best = est;
if(depth==dd) {
cc.max.setText("Tendency: ["+(estnow)+"]->["+(estnow-est)+"]");
mm = m;
}
} else {
best = -negaScout(!white, -beta, -est, depth-1);
if(depth==dd) {
cc.max.setText("Tendency: ["+(estnow)+"]->["+(estnow-est)+"]");
mm = m;
}
}
}
m.undo(this);
n = Math.max(alpha, best)+1;
if(depth==dd) {
cc.think.setText((mm==null?"":mm+" - ")+v.toString());
cc.progress.setValue(siz-v.size());
cc.progress.setString((int)((double)(siz-v.size())*100/(double)siz)+"%");
}
}
}
return best;
}
int dd; // default depth
int stepcounter; // number of field cost evaluations
Move mm; // best move
int estnow; // base estimation
int INFINITY = 100000; // unreachable game cost
public Move reply(boolean white) {
estnow = estimateBase();
alfaBeta(white, -INFINITY, INFINITY, dd);
principalVariation(white, -INFINITY, INFINITY, dd);
negaScout(white, -INFINITY, INFINITY, dd);
//System.out.println(stepcounter);
return mm;
}
}
答案 0 :(得分:1)
代码非常简单。
程序以方法main开头。
public static void main(String args[]) {
if (args.length > 0)
new Chess(new Integer(args[0]).intValue());
else
new Chess(3);
}
这是默认情况下由JVM调用的静态方法。它的作用是创建类Chess的新实例。
如果传递了一个参数,它会重试第一个并从中创建一个整数,在其他情况下使用默认值3.
从调用类Chess的构造函数的main方法。
public Chess(int i) {
MyWindowListener myWindowListener = new MyWindowListener();
this.addWindowListener(myWindowListener);
setSize(550, 550);
setTitle("Chess");
init(i);
setVisible(true);
}
在这个构造函数中,我们创建一个WindowListener并向我们的JFrame类添加id,然后我们设置一些属性,如size和title,并调用方法init(i)
public void init(int i) {
Container cp = getContentPane();
cp.setLayout(new GridLayout(0, 1));
cp.add(new ChessComponent(i, null));
}
此方法创建一个容器,使用传递的i参数分配ChessComponent
的实例。
当窗口关闭事件发生时,从WindowsListener调用最后一个方法。
class MyWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent event) {
Object object = event.getSource();
if (object == Chess.this)
WindowClosing(event);
}
}
void WindowClosing(WindowEvent e) {
setVisible(false);
dispose();
System.exit(0);
}