Chesspointq=(Chesspoint)(v.elementAt(i));
if(i==v.size()-1){
map[q.x][q.y]=null;
intindex=chessman.indexOf(v);
chessman.setElementAt(q,index);
currentTurn=q.color;
}else{
map[q.x][q.y]=q;
}
}
}
controlPanel.setLabel();
repaint();
}
《4》在该类中使用Vector类型保存踢掉的棋子及这个棋子本身。
2.棋子主类主要功能有:
获得实际的棋子位置,程序实现如下:
publicclassChesspoint
{
publicstaticintblack=0,white=1;
intx,y;
intcolor;
publicChesspoint(inti,intj,intc)
{
x=i;
y=j;
color=c;
}
publicStringtoString()
{//储存x,y位置和颜色
Stringc=(color==black?
"black":
"white");
return"["+x+","+y+"]:
"+c;
}
}
3.围棋主类主要功能:
(1)对围棋界面外观进行初始化,构造方法Chess实现如下:
publicChess(){
this.setTitle("围棋程序");
this.setLayout(newBorderLayout());
jp.setSize(550,490);
jp.setLayout(newBorderLayout());
jp.add(Chessboard.controlPanel,"West");
jp.add(qipan,"Center");
add(jp);
jp.setSize(550,490);
this.setResizable(false);
this.setLayout(newBorderLayout());
this.setSize(550,490);
this.setVisible(true);
}
(2)创建围棋对象,main方法实现如下:
publicstaticvoidmain(String[]args){
//开始下棋程序
ChessIgo=newChess();
五、测试数据及运行结果
1.初始界面如图图5.1所示:
图5.1运行结果图
2.18x18棋盘测试如图:
图5.2运行结果图
3.2.7x7棋盘测试如图:
图5.3运行结果图
4.悔棋标签反映无子可悔如图:
图5.4运行结果图
六、总结
时光如梭,光阴似箭,java实训一晃而过,通过这一期的实训,虽然实训的时间不长,但是总体上收获挺大的,当我们正式准备学习实训java编程技术时,让我感到非常高兴,因为java一直学的是课本知识,所以实训对于我来说是必须要学会熟练操作的。
当然开始学习后也并非是想象中那样顺利,开始的学习让我异常感到学习任务的艰巨,因为学习中我遇到了很多以前未曾遇到的难点,有时后也难免会失去耐心,但是,通过老师的指导,自己的努力的练习,我顺利的化解了一道道的障碍。
克服了java学习上的一道道难关,现在自己已经基本掌握了java的基础知识。
我很清楚以后的路还很长,再长的路,一步步也能走完,再短的路,不迈开双脚也无法到达。
任何业绩的质变都来自于量变的积累,成功不是将来才有的,而是从决定去做的那一刻起,持续累积而成,让我们将事前的忧虑,换为事前的思考和计划吧!
在实训的过程中,我深深感觉到自身所学知识的有限。
以后要更加努力学习.
源代码:
1、Chessboard类
importjava.awt.*;
importjava.awt.event.*;
importjava.util.*;
importjavax.swing.JPanel;
publicclassChessboardextendsJPanel{
//默认的棋盘方格长度及数目
publicstaticfinalint_gridLen=22,_gridNum=19;
/*
*利用Vector保存所有已下的棋子,包括在棋盘上的所有棋子和被踢掉的,若某一次
*落子没有造成踢子,包括所有被这个棋子提掉的棋子及这个棋子本身,Vector最后
*/
privateVectorchessman;
privateintalreadyNum;//已下数目
privateintcurrentTurn;//轮到谁下
privateintgridNum,gridLen;//方格长度及数目
privateintchessmanLength;//棋子的直径
privateChesspoint[][]map;//在棋盘上的所有棋子
privateImageoffscreen;
privateGraphicsoffg;
privateintsize;//棋盘的宽度及高度
privateinttop=13,left=13;//棋盘左边及上边的边距
privatePointmouseLoc;//鼠标的位置,即map数组中的下标
publicstaticControlPanelcontrolPanel;//控制面板
//获得控制板的距离
publicintgetWidth(){
returnsize;
}
publicintgetHeight(){
returnsize;
}
//绘制棋盘外观
publicChessboard(){
gridNum=_gridNum;
gridLen=_gridLen;
chessmanLength=gridLen*9/10;
size=2*left+gridNum*gridLen;
addMouseListener(newPutChess());
addMouseMotionListener(newMML());
setLayout(newBorderLayout());
controlPanel=newControlPanel();
startGame();
}
publicvoidaddNotify(){
super.addNotify();
offscreen=createImage(size,size);
offg=offscreen.getGraphics();
}
publicvoidpaint(Graphicsg){
offg.setColor(newColor(180,150,100));
offg.fillRect(0,0,size,size);
//画出棋盘格子
offg.setColor(Color.black);
for(inti=0;iintx1=left+i*gridLen;
intx2=x1;
inty1=top;
inty2=top+gridNum*gridLen;
offg.drawLine(x1,y1,x2,y2);
x1=left;
x2=left+gridNum*gridLen;
y1=top+i*gridLen;
y2=y1;
offg.drawLine(x1,y1,x2,y2);
}
//画出棋子
for(inti=0;ifor(intj=0;jif(map[i][j]==null)
continue;
offg.setColor(map[i][j].color==Chesspoint.black?
Color.black
:
Color.white);
offg.fillOval(left+i*gridLen-chessmanLength/2,top+j
*gridLen-chessmanLength/2,chessmanLength,
chessmanLength);
}
//画出鼠标的位置,即下一步将要下的位置
if(mouseLoc!
=null){
offg.setColor(currentTurn==Chesspoint.black?
Color.gray
:
newColor(200,200,250));
offg.fillOval(left+mouseLoc.x*gridLen-chessmanLength/2,top
+mouseLoc.y*gridLen-chessmanLength/2,
chessmanLength,chessmanLength);
}
//把画面一次性画出
g.drawImage(offscreen,3,0,this);
}
//更新棋盘
publicvoidupdate(Graphicsg){
paint(g);
}
//下棋子
classPutChessextendsMouseAdapter{//放一颗棋子
publicvoidmousePressed(MouseEventevt){
intxoff=left/2;
intyoff=top/2;
intx=(evt.getX()-xoff)/gridLen;
inty=(evt.getY()-yoff)/gridLen;
if(x<0||x>gridNum||y<0||y>gridNum)
return;
if(map[x][y]!
=null)
return;
//*****************清除多余的棋子**********************
if(alreadyNumintsize=chessman.size();
for(inti=size-1;i>=alreadyNum;i--)
chessman.removeElementAt(i);
}
//****************************************************
Chesspointqizi=newChesspoint(x,y,currentTurn);
map[x][y]=qizi;
//****************************************************
chessman.addElement(qizi);
alreadyNum++;
if(currentTurn==Chesspoint.black)
currentTurn=Chesspoint.white;
else
currentTurn=Chesspoint.black;
//**************判断在[x,y]落子后,是否可以提掉对方的子
tizi(x,y);
//***************判断是否挤死了自己,若是则已落的子无效
if(allDead(qizi).size()!
=0){
map[x][y]=null;
repaint();
controlPanel.setMsg("挤死自己");
//******************back**************
chessman.removeElement(qizi);
alreadyNum--;
if(currentTurn==Chesspoint.black)
currentTurn=Chesspoint.white;
else
currentTurn=Chesspoint.black;
return;
}
mouseLoc=null;
//更新控制面板
controlPanel.setLabel();
}
publicvoidmouseExited(MouseEventevt){//鼠标退出时,清除将要落子的位置
mouseLoc=null;
repaint();
}
}
privateclassMMLextendsMouseMotionAdapter{//取得将要落子的位置
publicvoidmouseMoved(MouseEventevt){
intxoff=left/2;
intyoff=top/2;
intx=(evt.getX()-xoff)/gridLen;
inty=(evt.getY()-yoff)/gridLen;
if(x<0||x>gridNum||y<0||y>gridNum)
return;
if(map[x][y]!
=null)
return;
mouseLoc=newPoint(x,y);
repaint();
}
}
//判断在[x,y]落子后,是否可以踢掉对方的子
publicstaticint[]xdir={0,0,1,-1};
publicstaticint[]ydir={1,-1,0,0};
publicvoidtizi(intx,inty){
Chesspointqizi;
if((qizi=map[x][y])==null)
return;
intcolor=qizi.color;
//取得棋子四周围的几个子
Vectorv=around(qizi);
for(intl=0;lChesspointq=(Chesspoint)(v.elementAt(l));
if(q.color==color)
continue;
//若颜色不同,取得和q连在一起的所有已死的子,
//若没有已死的子则返回一个空的Vector
Vectordead=allDead(q);
//移去所有已死的子
removeAll(dead);
//如果踢子,则保存所有被踢掉的棋子
if(dead.size()!
=0){
Objectobj=chessman.elementAt(alreadyNum-1);
if(objinstanceofChesspoint){
qizi=(Chesspoint)(chessman.elementAt(alreadyNum-1))