import javax.sound.sampled.AudioInputStream;
import javax.swing.*;
import java.applet.AudioClip;
import java.awt.*;
import java.net.URL;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Random;
@SuppressWarnings("serial")
public class ChessPanel extends JPanel{
private ImageIcon map; //棋盘背景位图
private ImageIcon blackchess; //黑子位图
private ImageIcon whitechess; //白子位图
public int isChessOn [][]; //棋局
protected boolean win = false; // 是否已经分出胜负
protected int win_bw; // 胜利棋色
protected int deep = 3, weight = 7; // 搜索的深度以及广度
public int drawn_num = 110; // 和棋步数
int chess_num = 0; // 总落子数目
public int[][] pre = new int[drawn_num + 1][2]; // 记录下棋点的x,y坐标 最多 (drawn_num + 1) 个
public int sbw = 0; //玩家棋色黑色0,白色1
public int bw = 0; // 当前应该下的棋色 0:黑色(默认), 1:白色
// 边界值,用于速度优化
protected int x_max = 15, x_min = 0;
protected int y_max = 15, y_min = 0;
protected boolean able_flag = true; // 是否选择禁手标志 0:无禁手 1:有禁手(默认
private int h; //棋子长
private int w; //棋子宽
private int insx; //插入棋子的位置
private int insy;
private Point mousePoint; //鼠标当前位置
private int winer; //获胜方
private boolean humanhuman=false; //是否是人人对弈
private int plast=0; //走了几步了,
public int BLACK_ONE; //0表黑子
public int WHITE_ONE; //1表白子
public int NONE_ONE; //2表无子
public int N; //棋盘边长
//-------声音
String[] choics = { "put.wav", "win.wav","lost.wav" }; //声音文件名数组
URL file1 = getClass().getResource(choics[0]); //落子声音文件
URL file2 = getClass().getResource(choics[1]); //获胜声音文件
URL file3 = getClass().getResource(choics[2]); //失败声音文件
AudioClip soundPut = java.applet.Applet.newAudioClip(file1); //落子声音剪辑对象
AudioClip soundWin = java.applet.Applet.newAudioClip(file2); //获胜声音剪辑对象
AudioClip soundLost = java.applet.Applet.newAudioClip(file3); //失败声音剪辑对象
public ChessPanel(){}
public ChessPanel(ImageIcon r_map,ImageIcon r_blackchess,ImageIcon r_whitechess) {
N=15;
map=new ImageIcon();
blackchess=new ImageIcon();
whitechess=new ImageIcon();
map=r_map;
blackchess=r_blackchess;
whitechess=r_whitechess;
NONE_ONE=2;
BLACK_ONE=0;
WHITE_ONE=1;
winer=NONE_ONE;
isChessOn=new int[N][N];
h=blackchess.getIconHeight()*(N-1);
w=blackchess.getIconWidth()*(N-1);
insx=0;
insy=0;
mousePoint=new Point();
}
public void reset(){ //重开一局
winer=NONE_ONE;
for(int i=0;i<N;i++)
for(int j=0;j<N;j++){
isChessOn[i][j]=NONE_ONE;
}
chess_num = 0;
win = false;
win_bw=2;
bw = 0;
x_max = 15; x_min = 0;
y_max = 15;y_min = 0;
repaint();
}
public void showMousePos(Point p){ //调试用,显示鼠标位置
int cw;
cw=h/N;
mousePoint.x=p.x/cw;
mousePoint.y=p.y/cw;
repaint();
}
public Point getPoint(int x,int y){
int cw;
insx=x;
insy=y;
cw=h/N;
Point r=new Point(x/cw,y/cw);
return r;
}
public void gameOver(int r_winer){ //游戏胜负已分
winer=r_winer;
}
public void paint(Graphics g){ //整体布局
super.paint(g);
paintChessMap(g);
paintChess(g);
if(winer==BLACK_ONE){
g.drawString(new String("游戏结束!黑棋获胜!"),500,200);
}
else if(winer==WHITE_ONE){
g.drawString(new String("游戏结束!白棋获胜!"),500,200);
}
}
private void paintChessMap(Graphics g){ //画棋盘
map.paintIcon(this,g,10,10);
int j;
g.setColor(Color.BLACK);
for(j=0;j<N;j++){ //画线
g.drawLine(h/N/2,h/N*j+h/N/2,w-w/N+(N%2)*(h/N/2),h/N*j+h/N/2);
g.drawLine(w/N*j+h/N/2,h/N/2,w/N*j+h/N/2,h-h/N+(N%2)*(h/N/2));
}
g.fillRect(w/N*7+h/N/2-3,h/N*7+h/N/2-3,6,6);//画5个黑方块
g.fillRect(w/N*3+h/N/2-3,h/N*3+h/N/2-3,6,6);
g.fillRect(w/N*11+h/N/2-3,h/N*3+h/N/2-3,6,6);
g.fillRect(w/N*3+h/N/2-3,h/N*11+h/N/2-3,6,6);
g.fillRect(w/N*11+h/N/2-3,h/N*11+h/N/2-3,6,6);
}
private void paintChess(Graphics g){ //画棋子
int i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++){
if(isChessOn[i][j]==BLACK_ONE){
blackchess.paintIcon(this,g,w/N*i,h/N*j);
}
else if(isChessOn[i][j]==WHITE_ONE){
whitechess.paintIcon(this,g,w/N*i,h/N*j);
}
}
}
//-------------------------------下棋声音设置-------------------------------------------------
//落子声音
public void putVoice(){
soundPut.play();
}
//获胜声音
public void winVoice(){
soundWin.play();
}
//失败声音
public void lostVoice(){
soundLost.play();
}
//----------------------电脑下棋-------------------------------//
public void putOne(int bwf ) { //bwf 棋色 0:黑色 1:白色
int x, y, mx = -100000000;
x = y = -1;
// 搜索最优下棋点
int[][] bests = getBests( bwf );
for (int k = 0; k < bests.length; k++) {
int i = bests[k][0];
int j = bests[k][1];
// 有成5,则直接下子,并退出循环..没有,则思考对方情况
if (getType(i, j, bwf) == 1) {
x = i;
y = j;
break;
}
if (getType(i, j,1 - bwf) == 1) {
x = i;
y = j;
break;
}
// 预存当前边界值
int temp1=x_min,temp2=x_max,temp3=y_min,temp4=y_max;
// 预设己方下棋,并更新边界值
isChessOn[i][j] = bwf;
resetMaxMin(i,j);
// 预测未来
int t = findMin(-100000000, 100000000, deep);
// 还原预设下棋位置以及边界值
isChessOn[i][j] = 2;
x_min=temp1;
x_max=temp2;
y_min=temp3;
y_max=temp4;
// 差距小于1000,50%概率随机选取
//System.out.println("外 :" + i + "," + j + " mx:" + mx + " t:" + t);
if (t - mx > 1000 || Math.abs(t - mx)<1000 && randomTest(3)) {
x = i;
y = j;
mx = t;
//System.out.println(i + "," + j + " mx:" + mx + " t:" + t);
}
}
System.out.println("x="+x+",y="+y);
// addChess(x,y,(bwf+1)%2,true);
// repaint();
int step=0;
step++;
System.out.println("step "+step+":-----------------------------------------------");
for(int i=0;i<15;i++,System.out.print("\n"))
for(int j=0;j<15;j++)
{
if(isChessOn[j][i]!=2)System.out.print(isChessOn[j][i]);
else System.out.print(isChessOn[j][i]);
}
// 判断是否已分胜负
boolean flag = haveWin(x, y, bwf);
//记录
update( x, y );
repaint();
// 重设边界值
resetMaxMin(x,y);
// 胜负已分
if (flag)
wined(bwf);
if (!flag && chess_num >= drawn_num) {
win = true;
String str = drawn_num + "步没分胜负,判和棋!";
JOptionPane.showMessageDialog(null,str);
return;
}
}
//---------搜索当前搜索状态极大值--------------------------------//
//alpha 祖先节点得到的当前最小最大值,用于alpha 剪枝
//beta 祖先节点得到的当前最大最小值,用于beta 剪枝。
//step 还要搜索的步数
//return 当前搜索子树极大值
protected int findMax(int alpha, int beta, int step) {
int max = alpha;
if (step == 0) {
return evaluate();
}
int[][] rt = getBests(1 - sbw);
for (int i = 0; i < rt.length; i++) {
int x = rt[i][0];
int y = rt[i][1];