📄 chesswzq.java
字号:
break;
}
case 20:{
ptocWin(msg);
break;
}
}
return 0;
}
private void ptocWin(Message ms){
String str=this.arrayToString(ms.msg);
JOptionPane.showMessageDialog(null,str+" win the game!","V the game",JOptionPane.INFORMATION_MESSAGE);
newGame();
}
/**
* 游戏失败,重新开始一局
*/
public void getFailed(Message msg){
bpanel.drawChess(msg.coordinateX,msg.coordinateY);
JOptionPane.showMessageDialog(null,
"Sorry,You've failed the game",
"Try Again",
JOptionPane.INFORMATION_MESSAGE);
label3.setText("Player2");
newGame();
}
/**
* 另外的游戏者发送断开请求
*/
public void getDisconnection(Message msg){
getVictory(msg);
}
/**
* 获得游戏胜利
*/
public void getVictory(Message msg){
JOptionPane.showMessageDialog(null,
"You Win The Game",
"Congratulations",
JOptionPane.INFORMATION_MESSAGE);
label3.setText("Player2");
newGame();
}
/**
* 开始新游戏
*/
public void newGame(){
jrbBlack.setEnabled(true);
jrbWhite.setEnabled(true);
jrbBlack.setSelected(true);
jrbWhite.setSelected(false);
list.setEnabled(true);
setting.setEnabled(true);
bpanel.clearBoard();
drawChess(1);
pColor=1;cColor=2;
if(ptocFlag==false){
Message msg = new Message();
msg.type = 19;
strToCharArray(name, msg.msg);
try {
out.writeObject(msg);
}
catch (IOException e) {
e.printStackTrace();
}
}
ptocFlag=false;
beginFlag=false;
}
/**
*获得对方落子的信息
*/
public void putChessman(Message msg){
if(ptocFlag==false){
bpanel.updateBoard(msg.coordinateX,msg.coordinateY);
bpanel.drawChess(msg.coordinateX,msg.coordinateY);
beginFlag = true;
return;
}else{
}
}
/**
* 同意对方的游戏请求
*/
public void acceptToPlay(Message msg){
String str=arrayToString(msg.msg);
String ss=null;
if(msg.color==1){
ss = new String("white");
bpanel.setColor(2);
}
else{
ss = new String("black");
bpanel.setColor(1);
}
JOptionPane.showMessageDialog(null,
"OK. "+str+" have accepted your requestion\nYour color is"+ss,
"Game will to begin...",JOptionPane.ERROR_MESSAGE);
list.setEnabled(false);
jrbBlack.setEnabled(false);
jrbWhite.setEnabled(false);
setting.setEnabled(false);
beginFlag=true;
}
/**
*得到对方的拒绝游戏回复
*/
public void getDeny(Message msg){
String str=arrayToString(msg.msg);
JOptionPane.showMessageDialog(null,
"I'm sorry\n"+str+" denied your requestion",
"Sorry...",JOptionPane.ERROR_MESSAGE);
list.setEnabled(true);
label3.setText("Player2");
}
/**
* 请求对方进行游戏
*/
public void requestAnother(Message msg){
String str=arrayToString(msg.msg);
int flag =JOptionPane.showConfirmDialog(null,
"Player "+str+" want to play with you\nAre you OK?",
"Play request...",JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(flag==0){// 按下YES
msg.type=5; //同意请求
if(msg.color==1){ // msg.color代表 B的颜色 ,different代表A的颜色
drawChess(msg.color);
jrbBlack.setSelected(true);
jrbWhite.setSelected(false);
bpanel.setColor(1);
}
else{
drawChess(msg.color);
jrbWhite.setSelected(true);
jrbBlack.setSelected(false);
bpanel.setColor(2);
}
list.setEnabled(false);
label3.setText(str);
setting.setEnabled(false);
jrbBlack.setEnabled(false);
jrbWhite.setEnabled(false);
beginFlag=true;
}
if(flag==1){// 按下 NO
msg.type=4; // 拒绝请求
}
try{
out.writeObject(msg);
}catch(IOException e){
e.printStackTrace();
}
}
public void paint(Graphics g){
super.paint(g);
drawChess(bpanel.getColor());
}
/**
* 窗口退出事件处理程序
*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(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';
}
/**
* 过滤数组中的空格
*/
public String arrayToString(char [] arr){
int i=0,length=0;
while(arr[i]!='\0' && i<50){
i++;
}
length=i;
char [] ss = new char[length];
for(i=0;i<length;i++){
ss[i]=arr[i];
}
String str = new String(ss);
return str;
}
/**
* 人际对弈
*/
public void ptoComputer(){
int x=0,y=0;
int position;
if(pFirst==false){
x=7;
y=7;
bpanel.updateBoard(x,y);
bpanel.drawChess(x,y);
beginFlag=true;
}else{
beginFlag=true;
}
}
/**
* 选择最优位置落子
*/
private void cPutChess(int x,int y){
setRect(x,y);
setWeight(x,y,pColor);
getBetter(3);
}
/**
* 设定一个 9*9的矩形
*/
private void setRect(int x,int y){
if(x-4>0) RectX1=x-4;
else RectX1=0;
if(x+4>14) RectX2=14;
else RectX2=x+4;
if(y-4>0) RectY1=y-4;
else RectY1=0;
if(y+4>14) RectY2=14;
else RectY2=y+4;
if(RectX1>RectY1) RectX1 = x-(y-RectY1);
else RectY1 = y-(x-RectX1);
if(RectX2>RectY2) RectY2 = y+(RectX2-x);
else RectX2 = x+(RectY2-y);
}
/**
* 在棋盘中中设置每一个黑色棋子的权重
*/
private void setWeight(int x,int y,int tcolor){
int i=RectX1,j=RectY1,value=0,k=0,n=0,flag=0;
// X轴方向
for(i=RectX1,j=y;i<=RectX2;i++){
if(BoardPanel.board[i][j]!=0){
continue;
}
value=0;flag=0;
for(k=1;i-k>=RectX1 && k<5;k++){
if(BoardPanel.board[i-k][j]==tcolor){
value++;
continue;
}
if(BoardPanel.board[i-k][j]==0){//black space
flag++;
break;
}
}
for(k=1;i+k<RectX2 && k<5;k++){
if(BoardPanel.board[i+k][j]==tcolor){
value++;
}
if(BoardPanel.board[i+k][j]==0){
flag++;
break;
}
}
n=weight(value,flag);
if(weightBoard[i][j]<n){
weightBoard[i][j]=n;
}
}
//y轴方向
for(i=x,j=RectY1;j<=RectY2;j++){
if(BoardPanel.board[i][j]!=0){
continue;
}
value=0;flag=0;
for(k=1;j-k>=RectY1 && k<5;k++){
if(BoardPanel.board[i][j-k]==tcolor){
value++;
continue;
}
if(BoardPanel.board[i][j-k]==0){
flag++;
break;
}
}
for(k=1;j+k<RectY2 && k<5;k++){
if(BoardPanel.board[i][j+k]==tcolor){
value++;
}
if(BoardPanel.board[i][j+k]==0){
flag++;
break;
}
}
n=weight(value,flag);
if(weightBoard[i][j]<n){
weightBoard[i][j]=n;
}
}
// 左上到右下方向
for(i=RectX1,j=RectY1;i<=RectX2;i++,j++){
if(BoardPanel.board[i][j]!=0){
continue;
}
value=0;flag=0;
for(k=1;i-k>=RectX1 && k<5 ;k++){
if(BoardPanel.board[i-k][j-k]==tcolor){
value++;
continue;
}
if(BoardPanel.board[i-k][j-k]==0){
flag++;
break;
}
}
for(k=1;i+k<RectX2 && k<5;k++){
if(BoardPanel.board[i+k][j+k]==tcolor){
value++;
}
if(BoardPanel.board[i+k][j+k]==0){
flag++;
break;
}
}
n=weight(value,flag);
if(weightBoard[i][j]<n){
weightBoard[i][j]=n;
}
}
// 左下到右上方向
for(i=RectX2,j=RectY1;i>=RectX1;i--,j++){
if(BoardPanel.board[i][j]!=0){
continue;
}
value=0;flag=0;
for(k=1;i+k<=RectX2 && k<5;k++){
if(BoardPanel.board[i+k][j-k]==tcolor){
value++;
continue;
}
if(BoardPanel.board[i+k][j-k]==0){
flag++;
break;
}
}
for(k=1;i-k>=RectX1 && k<5;k++){
if(BoardPanel.board[i-k][j+k]==tcolor){
value++;
}
if(BoardPanel.board[i-k][j+k]==0){
flag++;
break;
}
}
n=weight(value,flag);
if(weightBoard[i][j]<n){
weightBoard[i][j]=n;
}
}
}
/**
* 返回棋子的权重
*/
private int weight(int count, int flag)
{
int weight=0;
switch(count){
case 0:{
if(flag>0) weight=200;
else weight=0;
break;
}
case 1:{
if(flag>0) weight=1000;
else weight=0;
break;
}
case 2:{
if(flag>0) weight=5000;
else weight=0;
break;
}
case 3:{
if(flag>0) weight=8000;
else weight=0;
break;
}
case 4:{
if(flag>0) weight=10000;
else weight=0;
break;
}
}
return weight;
}
/**
* 在棋盘中找到最优的位置
*/
private void getBetter(int count){
int [][] better = new int [count][2];
int [][] tempArray = new int [15][15];
for(int i=0;i<15;i++){
for(int j=0;j<15;j++){
tempArray[i][j]=weightBoard[i][j];
}
}
for(int i=0;i<count;i++){
getBiggest(tempArray,better[i][0],better[i][1]);
}
bestX=better[0][0];bestY=better[0][1];
}
/**
*得到权重最大值
*/
private void getBiggest(int [][] arr,int x,int y){
int [] temp=new int[2];
int swt=arr[0][0],tmp=0;
for(int i=0;i<15;i++){
for(int j=0;j<15;j++){
if(arr[i][j]>swt){
temp[0]=i;temp[1]=j;
swt=arr[i][j];
}
}
}
x=temp[0];
y=temp[1];
arr[x][y]=0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -