java__含类图__五子棋小游戏.wps
2 2 说明:表 2.2 表 2.2 Chess方法名功能备注ChessPad()初始化棋盘设置棋盘颜色,Void paint(Graphics g)对棋子等图像进行初始化设置棋子大小,颜色等表 1.1 表 1.1 java 源文件及其功能序号文件名主要功能备注1Chess本程序的主函数初始化棋盘2Autoplay实现电脑可以自动下棋分为 8 种情况3Scan扫描整个棋盘记录连在一起的黑白棋子数为判断胜负服务4Sort实现人与电脑有次序的下棋黑棋白棋有序的下5Judge判断人与电脑的胜负可以在文本框中显示胜负结果6Evaluate电脑估计为电脑下棋提供帮助表 2.1 表 2.1 Chess成员变量描述变量类型名称控制棋子颜色inti重新开局ButtonButton请黑棋下子TextFieldtext_1请白棋下子TextFieldtext_2这是第+i+步TextFieldtext_3public void mousePressed(MouseEvent e)鼠标的实践监听单击实践public void paint(Graphics g)画出图像控制图像的大小范围public void mousePressed(MouseEvent e)处理按下鼠标的事件选中下棋的位置public void mouseReleased(MouseEvent e)处理鼠标离开的事件本步下棋结束public void mouseExited(MouseEvent e)处理鼠标离开棋盘时鼠标离开组件不实现任何事件public void mouseClicked(MouseEvent e)处理发生单击的事件实现下棋以及重新开始游戏表 2.3 表 2.3 Autoplay成员变量描述变量类型名称统计玩家的连起来的棋子数intX电脑玩家的连起来的棋子数IntY表 2.4 表 2.4 Autoplay方法名功能备注void autoPlay(int chesspad,int a,int b)分情况实现电脑自动下棋使用 switch 语句分为 8 种情况来下棋表 2.5表 2.5Scan成员变量描述变量类型名称统计棋盘种五个连起来的棋子数数组int shape控制行数inti控制列数intj表 2.6表 2.6 Scan方法名功能备注 void scan(int chesspad,int colour)判断棋盘上连在一起的黑白棋子个数。表 2.7 表 2.7 Sort成员变量描述变量类型名称保存临时变量 inttemp表 2.8 表 2.8 Sort方法名功能备注public void sort(int shape)实现黑白棋子的交替下数组为参数表 2.9 表 2.9 Judge成员变量描述变量类型名称控制行数inti控制列数intj记录连在一起的黑白棋子数intflag表 2.10 表 2.10 Judge方法名功能备注static boolean judge(int a,int color)判断玩家与电脑的游戏胜负使用静态函数,分多钟情况判断表 2.11 表 2.11 Evaluate成员变量描述变量类型名称玩家最可能下的棋子的行数intmax_x玩家最可能下的棋子的列数intmax_y保存最大可能下棋的位置intmax控制行数inti控制列数intj表 2.12 表 2.12 Evaluate方法名功能备注public void evaluate(int shape)电脑用于估计玩家下步棋的走法三、运行结果:三、运行结果:四、源代码:四、源代码:import java.awt.*;import java.awt.event.*;class ChessPad extends Panel implements MouseListener,ActionListener int array=new int1919;Scan scanp=new Scan();Scan scanc=new Scan();AutoPlay autoPlay=new AutoPlay();Evaluate evaluatep=new Evaluate();Evaluate evaluatec=new Evaluate();Sort sort=new Sort();int i=0;int x=-1,y=-1,棋子颜色=1;Button button=new Button(重新开局);TextField text_1=new TextField(请黑棋下子),text_2=new TextField(),text_3=new TextField();ChessPad()setSize(440,440);setLayout(null);setBackground(Color.pink);addMouseListener(this);add(button);button.setBounds(10,5,60,26);button.addActionListener(this);add(text_1);text_1.setBounds(90,5,90,24);add(text_2);text_2.setBounds(290,5,90,24);add(text_3);text_3.setBounds(200,5,80,24);for(int i=0;i19;i+)for(int j=0;j19;j+)arrayij=0;for(int i=0;i19;i+)for(int j=0;j19;j+)for(int h=0;h5;h+)scanp.shapeijh=0;scanc.shapeijh=0;text_1.setEditable(false);text_2.setEditable(false);public void paint(Graphics g)for(int i=40;i=400;i=i+20)g.drawLine(40,i,400,i);for(int j=40;j=400;j=j+20)g.drawLine(j,40,j,400);g.fillOval(97,97,6,6);g.fillOval(337,97,6,6);g.fillOval(97,337,6,6);g.fillOval(337,337,6,6);g.fillOval(217,217,6,6);public void mousePressed(MouseEvent e)int a=0,b=0;if(e.getModifiers()=InputEvent.BUTTON1_MASK)x=(int)e.getX();y=(int)e.getY();ChessPoint_black chesspoint_black=new ChessPoint_black(this);ChessPoint_white chesspoint_white=new ChessPoint_white(this);i+;text_3.setText(这是第+i+步);if(x+5)/202|(y+5)/2019|(y-5)/2019)else a=(x+10)/20;b=(y+10)/20;if(arrayb-2a-2=0&棋子颜色=1)this.add(chesspoint_black);chesspoint_black.setBounds(a*20-9,b*20-9,18,18);棋子颜色=棋子颜色*(-1);arrayb-2a-2=1;if(Judge.judge(array,1)text_1.setText(黑棋赢!);棋子颜色=2;removeMouseListener(this);else text_1.setText();if(i2&棋子颜色=-1)scanp.scan(array,1);scanc.scan(array,-1);sort.sort(scanp.shape);sort.sort(scanc.shape);evaluatep.evaluate(scanp.shape);evaluatec.evaluate(scanc.shape);棋子颜色=棋子颜色*(-1);this.add(chesspoint_white);if(evaluatep.maxevaluatec.max)text_2.setText(evaluatep.max_x+evaluatep.max_y+evaluatep.max);chesspoint_white.setBounds(evaluatep.max_y+2)*20-9,(evaluatep.max_x+2)*20-9,18,18);arrayevaluatep.max_xevaluatep.max_y=-1;text_1.setText(请黑棋下子);for(int i=0;i19;i+)for(int j=0;j19;j+)for(int h=0;h5;h+)scanp.shapeijh=0;scanc.shapeijh=0;else text_2.setText(evaluatec.max_x+evaluatec.max_y+evaluatec.max);chesspoint_white.setBounds(evaluatec.max_y+2)*20-9,(evaluatec.max_x+2)*20-9,18,18);arrayevaluatec.max_xevaluatec.max_y=-1;if(Judge.judge(array,-1)text_2.setText(白棋赢!);棋子颜色=2;removeMouseListener(this);else text_1.setText(请黑棋下子);for(int i=0;i19;i+)for(int j=0;j19;j+)for(int h=0;h5;h+)scanp.shapeijh=0;scanc.shapeijh=0;if(i=2&棋子颜色=-1)autoPlay.autoPlay(array,b-2,a-2);this.add(chesspoint_white);棋子颜色=棋子颜色*(-1);chesspoint_white.setBounds(autoPlay.y+2)*20-9,(autoPlay.x+2)*20-9,18,18);arrayautoPlay.xautoPlay.y=-1;if(Judge.judge(array,-1)text_2.setText(白棋赢!);棋子颜色=2;removeMouseListener(this);else text_1.setText(请黑棋下子);text_2.setText(autoPlay.x+autoPlay.y);public void mouseReleased(MouseEvent e)public void mouseEntered(MouseEvent e)public void mouseExited(MouseEvent e)public void mouseClicked(MouseEvent e)public void actionPerformed(ActionEvent e)this.removeAll();棋子颜色=1;add(button);button.setBounds(10,5,60,26);add(text_1);text_1.setBounds(90,5,90,24);text_2.setText();text_1.setText(请黑棋下子);add(text_2);text_2.setBounds(290,5,90,24);add(text_3);text_3.setBounds(200,5,80,24);i=0;text_3.setText(这是第+i+步);for(int i=0;i19;i+)for(int j=0;j19;j+)arrayij=0;for(int i=0;i19;i+)for(int j=0;j19;j+)for(int h=0;h5;h+)scanp.shapeijh=0;scanc.shapeijh=0;addMouseListener(this);class ChessPoint_black extends Canvas implements MouseListener ChessPad chesspad=null;ChessPoint_black(ChessPad p)setSize(20,20);addMouseListener(this);chesspad=p;public void paint(Graphics g)g.setColor(Color.black);g.fillOval(0,0,18,18);public void mousePressed(MouseEvent e)/*if(e.getModifiers()=InputEvent.BUTTON3_MASK)chesspad.remove(this);chesspad.棋子颜色=1;chesspad.text_2.setText();chesspad.text_1.setText(请黑棋下子);*/public void mouseReleased(MouseEvent e)public void mouseEntered(MouseEvent e)public void mouseExited(MouseEvent e)public void mouseClicked(MouseEvent e)class ChessPoint_white extends Canvas implements MouseListener ChessPad chesspad=null;ChessPoint_white(ChessPad p)setSize(20,20);addMouseListener(this);chesspad=p;public void paint(Graphics g)g.setColor(Color.white);g.fillOval(0,0,18,18);public void mousePressed(MouseEvent e)/*if(e.getModifiers()=InputEvent.BUTTON3_MASK)chesspad.remove(this);chesspad.棋子颜色=-1;chesspad.text_2.setText(请白棋下子);chesspad.text_1.setText();*/public void mouseReleased(MouseEvent e)public void mouseEntered(MouseEvent e)public void mouseExited(MouseEvent e)public void mouseClicked(MouseEvent e)public class Chess extends Frame ChessPad chesspad=new ChessPad();Chess()setVisible(true);setLayout(null);Label label=new Label(五子棋,Label.CENTER);add(label);label.setBounds(70,55,440,26);label.setBackground(Color.orange);add(chesspad);chesspad.setBounds(70,90,440,440);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0););pack();setSize(600,550);public static void main(String args)Chess chess=new Chess();class AutoPlay int x,y;void autoPlay(int chesspad,int a,int b)int randomNumber=(int)(Math.random()*8)+1;switch(randomNumber)case(1):if(chesspada-1b-1=0)x=a-1;y=b-1;else if(chesspada-2b-2=0)x=a-2;y=b-2;else x=a-3;y=b-3;break;case(2):if(chesspada-1b=0)x=a-1;y=b;else if(chesspada-2b=0)x=a-2;y=b;else x=a-3;y=b;break;case(3):if(chesspada-1b+1=0)x=a-1;y=b+1;else if(chesspada-2b+2=0)x=a-2;y=b+2;else x=a-3;y=b+3;break;case(4):if(chesspadab+1=0)x=a;y=b+1;else if(chesspadab+2=0)x=a;y=b+2;else x=a;y=b+3;break;case(5):if(chesspada+1b+1=0)x=a+1;y=b+1;else if(chesspada+2b+2=0)x=a+2;y=b+2;else x=a+3;y=b+3;break;case(6):if(chesspada+1b=0)x=a+1;y=b;else if(chesspada+2b=0)x=a+2;y=b;else x=a+3;y=b;break;case(7):if(chesspada+1b-1=0)x=a+1;y=b-1;else if(chesspada+2b-2=0)x=a+2;y=b-2;else x=a+3;y=b-3;break;case(8):if(chesspadab-1=0)x=a;y=b-1;else if(chesspadab-2=0)x=a;y=b-2;elsex=a;y=b+3;break;class Evaluate int max_x,max_y,max;public void evaluate(int shape)int i=0,j=0;for(i=0;i19;i+)for(j=0;j19;j+)switch(shapeij0)case 5:shapeij4=200;break;case 4:switch(shapeij1)case 4:shapeij4=150+shapeij2+shapeij3;break;case 3:shapeij4=100+shapeij2+shapeij3;break;default:shapeij4=50+shapeij2+shapeij3;break;case 3:switch(shapeij1)case 3:shapeij4=75+shapeij2+shapeij3;break;default:shapeij4=20+shapeij2+shapeij3;break;case 2:shapeij4=10+shapeij1 +shapeij2 +shapeij3;break;case 1:shapeij4=shapeij0+shapeij1 +shapeij2 +shapeij3;default:shapeij4=0;int x=0,y=0;max=0;for(x=0;x19;x+)for(y=0;y19;y+)if(maxshapexy4)max=shapexy4;max_x=x;max_y=y;class Judge static boolean judge(int a,int color)int i,j,flag;for(i=0;i19;i+)flag=0;for(j=0;j19;j+)if(aij=color)flag+;if(flag=5)return true;else flag=0;for(j=0;j19;j+)flag=0;for(i=0;i19;i+)if(aij=color)flag+;if(flag=5)return true;else flag=0;for(j=4;j19;j+)flag=0;int m=j;for(i=0;i=0;j-)flag=0;int m=j;for(i=0;i=0;i-)flag=0;int n=i;for(j=0;j=0;j-)flag=0;int m=j;for(i=18;i=j;i-)if(aim+=color)flag+;if(flag=5)return true;else flag=0;return false;class Scan int shape=new int19195;void scan(int chesspad,int colour)int i,j;for(i=0;i=18;i+)for(j=0;j=0&chesspadm-n=colour)shapeij0+;n=j;while(n+1=0&chesspad-mn=colour)shapeij1+;m=i;while(m+1=0&n+1=18&chesspad-m+n=colour)shapeij2+;m=i;n=j;while(m+1=0&chesspad+m-n=colour)shapeij2+;m=i;n=j;while(m-1=0&n-1=0&chesspad-m-n=colour)shapeij3+;m=i;n=j;while(m+1=18&n+1=18&chesspad+m+n=colour)shapeij3+;class Sort public void sort(int shape)int temp;for(int i=0;i19;i+)for(int j=0;j19;j+)for(int h=1;h=h;w-)if(shapeijw-1shapeijw)temp=shapeijw-1;shapeijw-1=shapeijw;shapeijw=temp;