《五子棋(java编写).wps》由会员分享,可在线阅读,更多相关《五子棋(java编写).wps(8页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、import java.awt.Checkbox;import java.awt.CheckboxGroup;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.MouseEvent;import
2、 java.awt.event.MouseListener;import javax.swing.JButton;import javax.swing.JCheckBox;import javax.swing.JFrame;import javax.swing.JPanel;public class FivChese extends JFrame CheseGame CG;FivChese()this.CG=new CheseGame();addMouseListener(CG);add(CG);public static void main(String args)FivChese five
3、chese=new FivChese();fivechese.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);fivechese.setSize(900,700);fivechese.setTitle(五子棋);fivechese.setVisible(true);fivechese.setResizable(true);class CheseGame extends JPanel implements MouseListener,ItemListener int map=new int2020;Color color=Color.BLACK,Co
4、lor.WHITE,Color.LIGHT_GRAY;/棋子坐标 int x=-1;int y=-1;/判定是白棋还是黑棋 int flag=1;/判定胜负 int winner=3;JButton btn1=new JButton(Start);/游戏开始 JButton btn2=new JButton(again);/游戏重置 /确定白棋先还是黑棋先 CheckboxGroup cbg=new CheckboxGroup();Checkbox chb1=new Checkbox(Black First,cbg,true);Checkbox chb2=new Checkbox(White
5、First,cbg,false);Override public void itemStateChanged(ItemEvent ie)/throw new UnsupportedOperationException(Not supported yet.);if(chb1.getState()flag=1;else flag=2;class GameStart implements ActionListener public void actionPerformed(ActionEvent ae)if(ae.getSource()=btn1)/str.equals(Start)start();
6、repaint();btn1.setEnabled(false);btn2.setEnabled(true);chb1.setEnabled(false);chb2.setEnabled(false);if(ae.getSource()=btn2)start();repaint();chb1.setEnabled(true);chb2.setEnabled(true);CheseGame()this.setLayout(null);chb1.setBounds(650,50,100,30);chb2.setBounds(650,100,100,30);chb1.addItemListener(
7、this);chb2.addItemListener(this);btn1.setBounds(650,150,100,30);btn1.addActionListener(new GameStart();btn2.setBounds(650,200,100,30);btn2.addActionListener(new GameStart();btn2.setEnabled(false);this.add(btn1);this.add(btn2);this.add(chb1);this.add(chb2);void start()for(int i=0;i 20;i+)for(int j=0;
8、j 20;j+)mapij=0;winner=0;Boolean win(int x,int y)int score=1;for(int i=x;i x-4;)/横向 x-if(i=0)break;if(mapiy!=map-iy)break;score+;if(score=5)return true;else score=1;for(int j=y;j y-4;)/纵向 y-if(j=0)break;if(mapxj!=mapx-j)break;score+;if(score=5)return true;else score=1;for(int i=x;i x+4;)/斜向 x+、y+for
9、(int j=y;j x-4;)/斜向 x-、y-for(int j=y;j y-4;)if(i=0|j=0)i=x-4;break;if(mapij!=map-i-j)i=x-4;break;score+;if(score=5)return true;else score=1;for(int i=x;i x-4;)/斜向 x-、y+for(int j=y;j y+4;)if(i=0|j=19)i=x-4;break;if(mapij!=map-i+j)i=x-4;break;score+;for(int i=x;i y-4;)if(i=19|j=0)i=x+4;break;if(mapij!
10、=map+i-j)i=x+4;break;score+;if(score=5)return true;else return false;public void paintComponent(Graphics g)super.paintComponent(g);setBackground(color2);/画棋盘 for(int i=1;i=20;i+)g.setColor(color0);g.drawLine(30,i*30,600,i*30);g.drawLine(i*30,30,i*30,600);/画棋子 for(int i=0;i 20;i+)for(int j=0;j=0&x=0&
11、y=19)if(flag=1&mapxy=0)mapxy=1;if(win(x,y)winner=1;flag=2;else if(flag=2&mapxy=0)mapxy=2;if(win(x,y)winner=2;flag=1;repaint();Override public void mouseReleased(MouseEvent me)/throw new UnsupportedOperationException(Not supported yet.);Override public void mouseEntered(MouseEvent me)/throw new UnsupportedOperationException(Not supported yet.);Override public void mouseExited(MouseEvent me)/throw new UnsupportedOperationException(Not supported yet.);