【Java开源代码栏目提醒】:网学会员为广大网友收集整理了,BoardPanel.java,希望对大家有所帮助!
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import javax.swing.*;
import java.io.*;
public class BoardPanel extends JPanel{
private static Image white = null;
private static Image black = null;
private static int xp; //放置棋子的位置的x坐标
private static int yp; //放置棋子的位置的y坐标
private Cursor handCursor;
private Cursor defaultCursor;
protected static int board[][]; // 记录每一个黑棋或者白棋棋子的位置
private int color=1; // 记录选手的棋子的颜色1=black 2=white
int STEPCOUNTER=0;
int BASE=5;
int DEEPTH=3;
int MINDEEPTH=3;
int MAX1=5;
long INVALID=9000000;
int chessBoard[][];
//绘制出棋盘x和y轴方向的棋盘线上的数字标号
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(){
try {
handCursor=new Cursor(12);
defaultCursor = new Cursor(0);
board = new int[15][15];
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//绘制整个棋盘界面
public void paint(Graphics gc){
super.paint(gc);
gc.setColor(Color.blue);
gc.drawString(line,25,15);
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);
}
// 绘制棋盘
for (int i = 0; i < 15; i++) {
gc.drawLine(30, 30 + i * 30, 450, 30 + i * 30); //绘制棋盘的行线
gc.drawLine(30 + i * 30, 30, 30 + i * 30, 450); //绘制棋盘的列线
}
gc.drawLine(25, 25, 455, 25);
gc.drawLine(25, 25, 25, 455);
gc.drawLine(25, 455, 455, 455);
gc.drawLine(455, 25, 455, 455);
//当窗体被重画时,将绘制出棋盘重画之前的状态
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);
}
if (board[i][j] == 2){
gc.setColor(Color.white);
gc.fillOval(xp,yp,28,28);
}
}
}
}
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;
}
/**
* 当窗体更新时,清空棋盘
*/
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){
ChessWZQ.label6.setText("You may not do that");
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;
}
// 客户端的棋盘
board[xp / 30][yp / 30] = color;
Graphics g=this.getGraphics();
if(color==1){//设置画笔为黑色
g.setColor(Color.black);
}
if(color==2){//设置画笔为白色
g.setColor(Color.white);
}
//在单击位置绘制棋子
g.fillOval(xp, yp, 28, 28);
ChessWZQ.beginFlag=false;
if(ChessWZQ.ptocFlag==false){
Message msg = new Message();
msg.color = color;
msg.coordinateX = x;
msg.coordinateY = y;
msg.type = 2;
//向服务器发送Message类中封装的信息
try {
ChessWZQ.out.writeObject(msg);
}
catch (IOException ee) {
ee.printStackTrace();
}
}
char cc = (char)(x+65);
ChessWZQ.label6.setText("put on ( "+cc+" , "+(y+1)+" )");
//轮到
计算机根据算法计算应对的落子位置
if(ChessWZQ.ptocFlag==true){
if(judge(xp/30,yp/30,color)==true){
Message ms = new Message();
ms.type=20;
strToCharArray("You",ms.msg);
try{
ChessWZQ.out.writeObject(ms);
}catch(IOException er){
er.printStackTrace();
}
}
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; //设置轮到玩家落子的标志位
cc=(char)(bestX+65);
ChessWZQ.label6.setText("put on ( "+cc+" , "+(bestY+1)+" )");
if(judge(bestX,bestY,ChessWZQ.cColor)==true){
Message msg = new Message();
msg.type=20;
strToCharArray("Computer",msg.msg);
try{
ChessWZQ.out.writeObject(msg);
}catch(IOException err){
err.printStackTrace();
}
}
}
}
}
//工具函数,用来将字符串转换成字符数组
public void strToCharArray(String str,char