纯Java写的中国象棋.docx

上传人:b****6 文档编号:8832319 上传时间:2023-05-15 格式:DOCX 页数:67 大小:29.20KB
下载 相关 举报
纯Java写的中国象棋.docx_第1页
第1页 / 共67页
纯Java写的中国象棋.docx_第2页
第2页 / 共67页
纯Java写的中国象棋.docx_第3页
第3页 / 共67页
纯Java写的中国象棋.docx_第4页
第4页 / 共67页
纯Java写的中国象棋.docx_第5页
第5页 / 共67页
纯Java写的中国象棋.docx_第6页
第6页 / 共67页
纯Java写的中国象棋.docx_第7页
第7页 / 共67页
纯Java写的中国象棋.docx_第8页
第8页 / 共67页
纯Java写的中国象棋.docx_第9页
第9页 / 共67页
纯Java写的中国象棋.docx_第10页
第10页 / 共67页
纯Java写的中国象棋.docx_第11页
第11页 / 共67页
纯Java写的中国象棋.docx_第12页
第12页 / 共67页
纯Java写的中国象棋.docx_第13页
第13页 / 共67页
纯Java写的中国象棋.docx_第14页
第14页 / 共67页
纯Java写的中国象棋.docx_第15页
第15页 / 共67页
纯Java写的中国象棋.docx_第16页
第16页 / 共67页
纯Java写的中国象棋.docx_第17页
第17页 / 共67页
纯Java写的中国象棋.docx_第18页
第18页 / 共67页
纯Java写的中国象棋.docx_第19页
第19页 / 共67页
纯Java写的中国象棋.docx_第20页
第20页 / 共67页
亲,该文档总共67页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

纯Java写的中国象棋.docx

《纯Java写的中国象棋.docx》由会员分享,可在线阅读,更多相关《纯Java写的中国象棋.docx(67页珍藏版)》请在冰点文库上搜索。

纯Java写的中国象棋.docx

纯Java写的中国象棋

运行效果如下图所示:

源代码如下(可以直接运行出结果):

importjava.awt.*;

importjavax.swing.*;

importjava.util.*;

importjava.io.*;

//主类

publicclassChess{

publicstaticvoidmain(Stringargs[]){

newChessMainFrame("中国象棋,博大精深");

}

}

//主框架类

classChessMainFrameextendsJFrameimplementsActionListener,MouseListener,Runnable{

//玩家

JLabelplay[]=newJLabel[32];

//棋盘

JLabelimage;

//窗格

Containercon;

//工具栏

JToolBarjmain;

//重新开始

JButtonanew;

//悔棋

JButtonrepent;

//退出

JButtonexit;

//当前信息

JLabeltext;

//保存当前操作

VectorVar;

//规则类对象(使于调用方法)

ChessRulerule;

/**

**单击棋子

**chessManClick=true闪烁棋子并给线程响应

**chessManClick=false吃棋子停止闪烁并给线程响应

*/

booleanchessManClick;

/**

**控制玩家走棋

**chessPlayClick=1黑棋走棋

**chessPlayClick=2红棋走棋默认红棋

**chessPlayClick=3双方都不能走棋

*/

intchessPlayClick=2;

//控制棋子闪烁的线程

Threadtmain;

//把第一次的单击棋子给线程响应

staticintMan,i;

ChessMainFrame(){

newChessMainFrame("中国象棋");

}

/**

**构造函数

**初始化图形用户界面

*/

ChessMainFrame(StringTitle){

//获行客格引用

con=this.getContentPane();

con.setLayout(null);

//实例化规则类

rule=newChessRule();

Var=newVector();

//创建工具栏

jmain=newJToolBar();

text=newJLabel("欢迎使用象棋对弈系统");

//当鼠标放上显示信息

text.setToolTipText("信息提示");

anew=newJButton("新游戏");

anew.setToolTipText("重新开始新的一局");

exit=newJButton("退出");

exit.setToolTipText("退出象棋程序程序");

repent=newJButton("悔棋");

repent.setToolTipText("返回到上次走棋的位置");

//把组件添加到工具栏

jmain.setLayout(newGridLayout(0,4));

jmain.add(anew);

jmain.add(repent);

jmain.add(exit);

jmain.add(text);

jmain.setBounds(0,0,558,30);

con.add(jmain);

//添加棋子标签

drawChessMan();

//注册按扭监听

anew.addActionListener(this);

repent.addActionListener(this);

exit.addActionListener(this);

//注册棋子移动监听

for(inti=0;i<32;i++){

con.add(play[i]);

play[i].addMouseListener(this);

}

//添加棋盘标签

con.add(image=newJLabel(newImageIcon("image\\Main.GIF")));

image.setBounds(0,30,558,620);

image.addMouseListener(this);

//注册窗体关闭监听

this.addWindowListener(

newWindowAdapter(){

publicvoidwindowClosing(WindowEventwe){

System.exit(0);

}

}

);

//窗体居中

DimensionscreenSize=Toolkit.getDefaultToolkit().getScreenSize();

DimensionframeSize=this.getSize();

if(frameSize.height>screenSize.height){

frameSize.height=screenSize.height;

}

if(frameSize.width>screenSize.width){

frameSize.width=screenSize.width;

}

this.setLocation((screenSize.width-frameSize.width)/2-280,(screenSize.height-frameSize.height)/2-350);

//设置

this.setIconImage(newImageIcon("image\\红将.GIF").getImage());

this.setResizable(false);

this.setTitle(Title);

this.setSize(558,670);

this.show();

}

/**

**添加棋子方法

*/

publicvoiddrawChessMan(){

//流程控制

inti,k;

//图标

Iconin;

//黑色棋子

//车

in=newImageIcon("image\\黑车.GIF");

for(i=0,k=24;i<2;i++,k+=456){

play[i]=newJLabel(in);

play[i].setBounds(k,56,55,55);

play[i].setName("车1");

}

//马

in=newImageIcon("image\\黑马.GIF");

for(i=4,k=81;i<6;i++,k+=342){

play[i]=newJLabel(in);

play[i].setBounds(k,56,55,55);

play[i].setName("马1");

}

//相

in=newImageIcon("image\\黑象.GIF");

for(i=8,k=138;i<10;i++,k+=228){

play[i]=newJLabel(in);

play[i].setBounds(k,56,55,55);

play[i].setName("象1");

}

//士

in=newImageIcon("image\\黑士.GIF");

for(i=12,k=195;i<14;i++,k+=114){

play[i]=newJLabel(in);

play[i].setBounds(k,56,55,55);

play[i].setName("士1");

}

//卒

in=newImageIcon("image\\黑卒.GIF");

for(i=16,k=24;i<21;i++,k+=114){

play[i]=newJLabel(in);

play[i].setBounds(k,227,55,55);

play[i].setName("卒1"+i);

}

//炮

in=newImageIcon("image\\黑炮.GIF");

for(i=26,k=81;i<28;i++,k+=342){

play[i]=newJLabel(in);

play[i].setBounds(k,170,55,55);

play[i].setName("炮1"+i);

}

//将

in=newImageIcon("image\\黑将.GIF");

play[30]=newJLabel(in);

play[30].setBounds(252,56,55,55);

play[30].setName("将1");

//红色棋子

//车

in=newImageIcon("image\\红车.GIF");

for(i=2,k=24;i<4;i++,k+=456){

play[i]=newJLabel(in);

play[i].setBounds(k,569,55,55);

play[i].setName("车2");

}

//马

in=newImageIcon("image\\红马.GIF");

for(i=6,k=81;i<8;i++,k+=342){

play[i]=newJLabel(in);

play[i].setBounds(k,569,55,55);

play[i].setName("马2");

}

//相

in=newImageIcon("image\\红象.GIF");

for(i=10,k=138;i<12;i++,k+=228){

play[i]=newJLabel(in);

play[i].setBounds(k,569,55,55);

play[i].setName("象2");

}

//士

in=newImageIcon("image\\红士.GIF");

for(i=14,k=195;i<16;i++,k+=114){

play[i]=newJLabel(in);

play[i].setBounds(k,569,55,55);

play[i].setName("士2");

}

//兵

in=newImageIcon("image\\红卒.GIF");

for(i=21,k=24;i<26;i++,k+=114){

play[i]=newJLabel(in);

play[i].setBounds(k,398,55,55);

play[i].setName("卒2"+i);

}

//炮

in=newImageIcon("image\\红炮.GIF");

for(i=28,k=81;i<30;i++,k+=342){

play[i]=newJLabel(in);

play[i].setBounds(k,455,55,55);

play[i].setName("炮2"+i);

}

//帅

in=newImageIcon("image\\红将.GIF");

play[31]=newJLabel(in);

play[31].setBounds(252,569,55,55);

play[31].setName("帅2");

}

/**

**线程方法控制棋子闪烁

*/

publicvoidrun(){

while(true){

//单击棋子第一下开始闪烁

if(chessManClick){

play[Man].setVisible(false);

//时间控制

try{

tmain.sleep(200);

}

catch(Exceptione){

}

play[Man].setVisible(true);

}

//闪烁当前提示信息以免用户看不见

else{

text.setVisible(false);

//时间控制

try{

tmain.sleep(250);

}

catch(Exceptione){

}

text.setVisible(true);

}

try{

tmain.sleep(350);

}

catch(Exceptione){

}

}

}

/**

**单击棋子方法

*/

publicvoidmouseClicked(MouseEventme){

//当前坐标

intEx=0,Ey=0;

//启动线程

if(tmain==null){

tmain=newThread(this);

tmain.start();

}

//单击棋盘(移动棋子)

if(me.getSource().equals(image)){

//该红棋走棋的时候

if(chessPlayClick==2&&play[Man].getName().charAt

(1)=='2'){

Ex=play[Man].getX();

Ey=play[Man].getY();

//移动卒、兵

if(Man>15&&Man<26){

rule.armsRule(Man,play[Man],me);

}

//移动炮

elseif(Man>25&&Man<30){

rule.cannonRule(play[Man],play,me);

}

//移动车

elseif(Man>=0&&Man<4){

rule.cannonRule(play[Man],play,me);

}

//移动马

elseif(Man>3&&Man<8){

rule.horseRule(play[Man],play,me);

}

//移动相、象

elseif(Man>7&&Man<12){

rule.elephantRule(Man,play[Man],play,me);

}

//移动仕、士

elseif(Man>11&&Man<16){

rule.chapRule(Man,play[Man],play,me);

}

//移动将、帅

elseif(Man==30||Man==31){

rule.willRule(Man,play[Man],play,me);

}

//是否走棋错误(是否在原地没有动)

if(Ex==play[Man].getX()&&Ey==play[Man].getY()){

text.setText("红棋走棋");

chessPlayClick=2;

}

else{

text.setText("黑棋走棋");

chessPlayClick=1;

}

}//if

//该黑棋走棋的时候

elseif(chessPlayClick==1&&play[Man].getName().charAt

(1)=='1'){

Ex=play[Man].getX();

Ey=play[Man].getY();

//移动卒、兵

if(Man>15&&Man<26){

rule.armsRule(Man,play[Man],me);

}

//移动炮

elseif(Man>25&&Man<30){

rule.cannonRule(play[Man],play,me);

}

//移动车

elseif(Man>=0&&Man<4){

rule.cannonRule(play[Man],play,me);

}

//移动马

elseif(Man>3&&Man<8){

rule.horseRule(play[Man],play,me);

}

//移动相、象

elseif(Man>7&&Man<12){

rule.elephantRule(Man,play[Man],play,me);

}

//移动仕、士

elseif(Man>11&&Man<16){

rule.chapRule(Man,play[Man],play,me);

}

//移动将、帅

elseif(Man==30||Man==31){

rule.willRule(Man,play[Man],play,me);

}

//是否走棋错误(是否在原地没有动)

if(Ex==play[Man].getX()&&Ey==play[Man].getY()){

text.setText("黑棋走棋");

chessPlayClick=1;

}

else{

text.setText("红棋走棋");

chessPlayClick=2;

}

}//elseif

//当前没有操作(停止闪烁)

chessManClick=false;

}//if

//单击棋子

else{

//第一次单击棋子(闪烁棋子)

if(!

chessManClick){

for(inti=0;i<32;i++){

//被单击的棋子

if(me.getSource().equals(play[i])){

//告诉线程让该棋子闪烁

Man=i;

//开始闪烁

chessManClick=true;

break;

}

}//for

}//if

//第二次单击棋子(吃棋子)

elseif(chessManClick){

//当前没有操作(停止闪烁)

chessManClick=false;

for(i=0;i<32;i++){

//找到被吃的棋子

if(me.getSource().equals(play[i])){

//该红棋吃棋的时候

if(chessPlayClick==2&&play[Man].getName().charAt

(1)=='2'){

Ex=play[Man].getX();

Ey=play[Man].getY();

//卒、兵吃规则

if(Man>15&&Man<26){

rule.armsRule(play[Man],play[i]);

}

//炮吃规则

elseif(Man>25&&Man<30){

rule.cannonRule(0,play[Man],play[i],play,me);

}

//车吃规则

elseif(Man>=0&&Man<4){

rule.cannonRule(1,play[Man],play[i],play,me);

}

//马吃规则

elseif(Man>3&&Man<8){

rule.horseRule(play[Man],play[i],play,me);

}

//相、象吃规则

elseif(Man>7&&Man<12){

rule.elephantRule(play[Man],play[i],play);

}

//士、仕吃棋规则

elseif(Man>11&&Man<16){

rule.chapRule(Man,play[Man],play[i],play);

}

//将、帅吃棋规则

elseif(Man==30||Man==31){

rule.willRule(Man,play[Man],play[i],play);

play[Man].setVisible(true);

}

//是否走棋错误(是否在原地没有动)

if(Ex==play[Man].getX()&&Ey==play[Man].getY()){

text.setText("红棋走棋");

chessPl

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2