import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BoardPanel extends JPanel{
private static Image white = null;
private static Image black = null;
private static int xp; // position of x to put chessman
private static int yp; // position of y to put chessman
private Cursor handCursor;
private Cursor defaultCursor;
protected static int board[][]; // record of each position black or white
private int color=1; // record player's chess color 1=black 2=white
int STEPCOUNTER=0;
int BASE=5;
int DEEPTH=3;
int MINDEEPTH=3;
int MAX1=5;
long INVALID=9000000;
int chessBoard[][];
// draw the line number with x/y direction
String line = "a b c d e f g h i j k l m n o";
char [] rowNum1 = {'1','2','3','4','5','6','7','8','9'};
char [] rowNum2={'1','0','1','1','1','2','1','3','1','4','1','5'};
public BoardPanel(){
// this.wzq=wz;
try {
handCursor=new Cursor(12);
defaultCursor = new Cursor(0);
board = new int[15][15];
// black = wzq.black;
// white = wzq.white;
// this.setBackground(Color.yellow);
//this.setForeground(Color.BLUE);
// this.setBorder(BorderFactory.createLoweredBevelBorder());
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public void paint(Graphics gc){
super.paint(gc);
//this.setBackground(Color.gray);
//this.invalidate();
gc.setColor(Color.blue);
//gc.setColor(new Color(255, 255, 240));
// draw number of X direction
gc.drawString(line,25,15);
// draw number of Y direction
for(int i=0;i<9;i++){
gc.drawChars(rowNum1,i,1,10,35+i*30);
}
for(int i=9,j=0;i<15;i++,j+=2){
gc.drawChars(rowNum2,j,2,10,35+i*30);
}
// draw the chess board
for (int i = 0; i < 15; i++) {
gc.drawLine(30, 30 + i * 30, 450, 30 + i * 30); //draw raw
gc.drawLine(30 + i * 30, 30, 30 + i * 30, 450); //draw column
}
gc.drawLine(25, 25, 455, 25);
gc.drawLine(25, 25, 25, 455);
gc.drawLine(25, 455, 455, 455);
gc.drawLine(455, 25, 455, 455);
//when window repaint to replay the original status of board
for(int i=0;i<15;i++){
for (int j = 0; j < 15; j++) {
xp=16+i*30;
yp=16+j*30;
if (board[i][j] == 1){
gc.setColor(Color.black);
gc.fillOval(xp,yp,28,28);
//gc.drawImage(black, 16 + i * 30, 16 + j * 30, this);
}
if (board[i][j] == 2){
gc.setColor(Color.white);
gc.fillOval(xp,yp,28,28);
//gc.drawImage(white, 16 + i * 30, 16 + j * 30, this);
}
}
}
}
private void jbInit() throws Exception {
this.addMouseMotionListener(new ChessWZQ_this_mouseMotionAdapter(this));
this.addMouseListener(new ChessWZQ_this_mouseAdapter(this));
}
public int getColor(){
return color;
}
public void setColor(int cr){
color=cr;
}
/**
* clear board when updated
*/
public void clearBoard(){
for(int i=0;i<15;i++){
for(int j=0;j<15;j++)
board[i][j]=0;
}
repaint();
}
void this_mouseClicked(MouseEvent e) {
int x=0,y=0;
if(color==0){
return;
}
x=e.getX();
y=e.getY();
if(x>20&&x<460&&y>20&&y<460&&(x%30<10||x%30>20)&&(y%30<10||y%30>20)){
if(ChessWZQ.beginFlag==false){
return;
}
xp = x / 30 * 30 - 14;
yp = y / 30 * 30 - 14;
if (x % 30 > 20) {
xp += 30;
}
if (y % 30 > 20) {
yp += 30;
}
x=xp/30;y=yp/30;
if(board[x][y]!=0){
return;
}
// client 's board
board[xp / 30][yp / 30] = color;
Graphics g=this.getGraphics();
if(color==1){//black
g.setColor(Color.black);
}
if(color==2){//white
g.setColor(Color.white);
}
g.fillOval(xp, yp, 28, 28);
ChessWZQ.beginFlag=false;
char cc = (char)(x+65);
/// computer to put
if(ChessWZQ.ptocFlag==true){
ChessWZQ.beginFlag = false;
int position = 0, bestX = 0, bestY = 0;
Analyse aa = new Analyse(BoardPanel.board);
position = aa.computerDo();
bestY = position % 100 - 1;
bestX = position / 100 - 1;
updateBoard(bestX, bestY);
drawChess(bestX, bestY);
ChessWZQ.beginFlag = true; //for people to put
cc=(char)(bestX+65);
}
}
}
// convert string to array which is end with '\0'
public void strToCharArray(String str,char [] arr){
int i;
for(i=0;i<str.length()&&i<49;i++){
arr[i] = str.charAt(i);
}
arr[i]='\0';
}
/**
* return the chessman's x coordinate 0<=x<15
* @return
*/
protected int getXP(){
return xp/30;
}
protected int getYP(){
return yp/30;
}
/**
* put a chessman and display it (x,y) position on the panel
* draw chessman on (x,y)
* @param x
* @param y
*/
public void drawChess(int x,int y){
Graphics g=this.getGraphics();
char cc = (char)(x+65);
int temp = y+1;
x=x*30+16;
y=y*30+16;
if(color==1)
g.setColor(Color.white);
else
g.setColor(Color.black);
g.fillOval(x, y, 28, 28);
}
/**
* update board status when B put a chessman
* @param x B's chessman's X coordinate 0<=x<15
* @param y B's chessman's Y coordinate 0<=y<15
*/
public void updateBoard(int x,int y){
int tcolor=0;
if(color==1) tcolor=2;
else tcolor=1;
board[x][y]=tcolor;
}
public char intToChar(int x){
char temp='\0';
if(x>=0&&x<=9)
temp=(char)(48+x);
else if(x>=10 &&x<=15){
temp=(char)(55+x);
}
return temp;
}
/**
* mouseMoved Event
* @param e
*/
void this_mouseMoved(MouseEvent e) {
int x=0,y=0;
x=e.getX();
y=e.getY();
if(x>20&&x<460&&y>20&&y<460&&(x%30<10||x%30>20)&&(y%30<10||y%30>20)){
this.setCursor(handCursor); //HAND_CURSOR
}
else{
this.setCursor(defaultCursor);
}
}
/**
* judge if a man win the game
* @param x the newest kid's x coordinate
* @param y the newest kid's y coordinate
* @return
*/
protected boolean judge(int x,int y,int clr){
int i=0,j=0,count=0;
// x direction
for(i=0,count=0;x-i>=0&&i<5;i++){
if (clr==board[x-i][y]){
count++;
}else{
break;
}
// System.out.println("( "+x+" , "+y+" )"+"count = "+count);
if(count==5)
return true;
}
for(i=1;x+i<15&&i<5;i++){
if(clr==board[x+i][y]){