package luoliang.ai;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;
import java.util.concurrent.CountDownLatch;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import luoliang.bean.Point;
import luoliang.control.FiveGame;
import luoliang.util.FiveUtil;
import luoliang.util.OutSave;
import luoliang.util.RandomUtil;
public class AIThink {
public int FIVE;//100000;//五子连珠
private int SFOURTHREE;//1000;//死四活三
private int SFOURSFOUR;//1000;//双死四
public int FOUR ;// 1000;//活四
private int THREETHREE;//600;//双活三
private int STHREETHREE;//300;//死3活3
private int SFOUR ;// 300;//冲四
private int THREE ;// 200;//活三
private int TWOTWO ;// 100;//双活二
private int STHREE;// 50;//眠三
private int TWO ;// 40;//活二 *
private int STWO ;// 10;//眠二
private int ONE ;// 4;//活一
private int SONE ;// 1;//眠一
private CountDownLatch count;
FiveGame fg;
int chessType;
int deep;
private int SUM;
Vector<Point> noChess;
Vector<Point> maxChess;
int top ;
int doom ;
int left ;
int right;
// class simpleThread extends Thread{
// public String index;
// private int i,j;
//
// public simpleThread(String index,int i,int j){
// setIndex(index);
// this.i=i;
// this.j=j;
// }
// public void setIndex(String index){
// this.index = index;
// }
// @Override
// public void run() {
// OutSave.save("thread:"+index+"启动");
// int mark =-999999999;
// FiveGame f =(FiveGame) FiveUtil.copy(fg);
// AIThink think = new AIThink(f);
// if(f.getChessboard()[i][j]==FiveUtil.NO_CHESS){
// f.putChess(i, j, chessType);
// mark = think.calculate( new Point(i,j), chessType);
// OutSave.save((chessType==FiveUtil.BLACK_CHESS?"黑子":"白子")+"在第"+deep+"层:("+i+","+j+")位置的得分是:"+mark);
// mark*=deep;
// mark-=think.go(-chessType,deep-1)[0];
// OutSave.save((chessType==FiveUtil.BLACK_CHESS?"黑子":"白子")+"在:"+"("+i+","+j+")位置的得分是:"+mark);
// noChess.add(new Point(i,j));
// if(mark>SUM){
// SUM=mark;
// maxChess.clear();
// maxChess.add(new Point(i,j));
// }else if(mark==SUM){
// maxChess.add(new Point(i,j));
// }
// }
// OutSave.save("thread:"+index+"完毕");
// count.countDown();
// System.out.println("结束. 还有" + count.getCount() + " 个线程");//打印结束标记
// OutSave.save("结束. 还有" + count.getCount() + " 个线程");//打印结束标记
//
// }
// }
// private int SUM;
// ArrayList<Point> noChess = new ArrayList<Point>();
// ArrayList<Point> maxChess= new ArrayList<Point>();
// private HashMap<Point,Think> allThread;
// public AIThink(){
// SUM = -9999999;
// noChess = new ArrayList<Point>();
// maxChess= new ArrayList<Point>();
// allThread = new HashMap<Point, Think>();
// }
// class Think extends Thread{
// private FiveGame fg;
// private int x,y,chessType,mark,deep;
//
// public Think(FiveGame fg,int x,int y,int chessType,int deep){
// this.deep = deep;
// this.chessType=chessType;
// this.fg=fg;
// this.x=x;
// this.y=y;
// mark = -99999999;
// }
// @Override
// public void run() {
// FiveGame f =(FiveGame) FiveUtil.copy(fg);
// AIThink think = new AIThink();
// if(f.getChessboard()[x][y]==FiveUtil.NO_CHESS){
// f.putChess(x, y, chessType);
// mark = think.calculate(f, new Point(x,y), chessType);
// OutSave.save((chessType==FiveUtil.BLACK_CHESS?"黑子":"白子")+"在第"+deep+"层:("+x+","+y+")位置的得分是:"+mark);
// mark*=deep;
// think.go(f,-chessType,deep-1);
// int smark= think.getSUM();
// mark-=smark;
// OutSave.save((chessType==FiveUtil.BLACK_CHESS?"黑子":"白子")+"在:"+"("+x+","+y+")位置的得分是:"+mark);
// if(mark>SUM){
// synchronized (AIThink.this) {
// SUM=mark;
// maxChess.clear();
// maxChess.add(new Point(x,y));
// }
// }else if(mark==SUM){
// synchronized (AIThink.this.maxChess) {
// maxChess.add(new Point(x,y));
// }
// }
// }
// synchronized (AIThink.this.allThread) {
// allThread.remove(new Point(x,y));
// }
// }
public CountDownLatch getCount() {
return count;
}
public void setCount(CountDownLatch count) {
this.count = count;
}
public FiveGame getFg() {
return fg;
}
public void setFg(FiveGame fg) {
this.fg = fg;
}
public int getChessType() {
return chessType;
}
public void setChessType(int chessType) {
this.chessType = chessType;
}
public int getDeep() {
return deep;
}
public void setDeep(int deep) {
this.deep = deep;
}
public int getDoom() {
return doom;
}
public void setDoom(int doom) {
this.doom = doom;
}
// }
public AIThink(FiveGame fg){
this.fg = fg;
top = fg.getTop();
doom = fg.getDoom();
left = fg.getLeft();
right = fg.getRight();
count = new CountDownLatch(2);
SUM= -99999999;
noChess = new Vector<Point>();
maxChess= new Vector<Point>();
}
/**
* 计算在某个棋盘某个地方下子得到的分数
*/
public int calculate(Point p,int chessType){
int a = analyse(p,chessType,0,1);
// OutSave.save("竖直得分:"+a);
int b = analyse(p,chessType,1,0);
// OutSave.save("水平得分:"+b);
int c = analyse(p,chessType,1,1);
// OutSave.save("左斜得分:"+c);
int d = analyse(p,chessType,1,-1);
// OutSave.save("右斜得分:"+d);
// int e = analyseMore();
clear();
return a+b+c+d;
}
public void setSUM(int sum){
this.SUM=sum;
}
public int getSUM(){
return SUM;
}
public Vector<Point> getNoChess() {
return noChess;
}
public void setNoChess(Vector<Point> noChess) {
this.noChess = noChess;
}
public Vector<Point> getMaxChess() {
return maxChess;
}
public void setMaxChess(Vector<Point> maxChess) {
this.maxChess = maxChess;
}
public int[] go(int chessType,int deep){
this.chessType=chessType;
this.deep=deep;
System.out.println("deep:"+deep);
if(deep <= 0)
return new int[]{0,-1,-1};
// OutSave.save(deep);
if(fg==null){
// OutSave.save("null");
return new int[]{0,-1,-1};
}
System.out.println("go!");
System.out.println("left:"+left+";top:"+top+";right:"+right+";doom"+doom);
int num=0;
Thread one = new FiveThread(this,top,doom,left,(right+left)/2);
one.start();
Thread two = new FiveThread(this,top,doom,(right+left)/2+1,right);
two.start();
try {
count.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("count:"+count.getCount()+"--------------------");
//*********************************************************
// for(int i=left;i<=right;i++){
// p:for(int j=top;j<=doom;j++){
// int mark =-999999999;
// FiveGame f =(FiveGame) FiveUtil.copy(fg);
// AIThink think = new AIThink(f);
// if(f.getChessboard()[i][j]==FiveUtil.NO_CHESS){
// f.putChess(i, j, chessType);
// Point p=new Point(i,j);
// mark = think.calculate( p, chessType);
//
// if(mark<10&&!p.isHadAround(f))
// continue p;
// OutSave.save((chessType==FiveUtil.BLACK_CHESS?"黑子":"白子")+"在第"+deep+"层:("+i+","+j+")位置的得分是:"+mark);
// mark*=deep;
// if(mark==FiveUtil.FIVE)
// return new int[]{mark,i,j};
// mark-=think.go(-chessType,deep-1)[0];
// OutSave.save((chessType==FiveUtil.BLACK_CHESS?"黑子":"白子")+"在:"+"("+i+","+j+")位置的得分是:"+mark);
// noChess.add(new Point(i,j));
// if(mark>SUM){
// SUM=mark;
// maxChess.clear();
// maxChess.add(new Point(i,j));
// }else if(mark==SUM){
// maxChess.add(new Point(i,j));
// }
// }
//// simpleThread thread = new simpleThread("deep:"+(num++),i,j);
//// thread.start();
// }
// }
Point p = null;
if(maxChess.size()>0)
p =
五子棋-人机博弈_源代码
4星 · 超过85%的资源 需积分: 10 135 浏览量
2011-08-13
10:48:50
上传
评论 2
收藏 11KB RAR 举报
ll274888605
- 粉丝: 0
- 资源: 4