java小游戏第四弹 五子棋
2016-03-02 16:09
567 查看
/*
* 本版不涉及人工智能,一人分饰俩角的双人对战
* 主要是图形界面的处理
* 和棋的处理
* 集合的思想简单使用
*/
* 本版不涉及人工智能,一人分饰俩角的双人对战
* 主要是图形界面的处理
* 和棋的处理
* 集合的思想简单使用
*/
package cn.hncu.games; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.ButtonGroup; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JRadioButton; import cn.hncu.set.MySet; public class Gobang extends JFrame { private String dir="./images_Gobang/"; private JLabel jlbback; private JMenu[] jmenu; private JMenuItem[][] jmenuItem; private JRadioButton[] jradiobtn; private MySet mySet; private MySet mySetPoint; private boolean flag_godraw=false; /* * 鼠标监听,下棋处理 */ private MouseListener qiPanListeren=new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { int x=e.getX(); int y=e.getY(); if(!(x>=14&&x<=336&&y>=14&&y<=336))//棋盘范围 return; x-=14; y-=14; int j=x/23,i=y/23;//棋盘点距 j+=(x%23*1.0>11.5?1:0); i+=(y%23*1.0>11.5?1:0); int index=0; if(!jradiobtn[0].isSelected()){ index=1; } //同一位置不能放俩个 if(chessPoint[i][j].falg) return; //放棋子 JLabel label=new JLabel(jradiobtn[index].getIcon()); mySet.add(label); getContentPane().add(label); label.setBounds(chessPoint[i][j].x-12, chessPoint[i][j].y-12, label.getIcon().getIconWidth(), label.getIcon().getIconHeight()); chessPoint[i][j].falg=true; chessPoint[i][j].chessType=jradiobtn[index].getText(); mySetPoint.add(chessPoint[i][j]); //判断游戏结束及处理 if(gameOver(j, i)){ JOptionPane.showMessageDialog(null, chessPoint[i][j].chessType+"获胜"); int option=JOptionPane.showConfirmDialog(null, "是否再来一局?"); if(option==JOptionPane.OK_OPTION){ newGame(); return; }else if(option==JOptionPane.NO_OPTION){ //System.exit(0); dispose(); }else{ flag_godraw=false; jlbback.removeMouseListener(qiPanListeren); jmenuItem[0][1].setEnabled(false); jmenuItem[0][2].setEnabled(false); } } if(jradiobtn[0].isSelected()){ jradiobtn[1].setSelected(true); jradiobtn[1].setFont(new Font("叶根友毛笔行书2.0版", Font.ITALIC+Font.BOLD, 20)); jradiobtn[0].setFont(new Font("仿宋", Font.PLAIN, 15)); }else{ jradiobtn[0].setSelected(true); jradiobtn[0].setFont(new Font("叶根友毛笔行书2.0版", Font.ITALIC+Font.BOLD, 20)); jradiobtn[1].setFont(new Font("仿宋", Font.PLAIN, 15)); } } //游戏结束判断 private boolean gameOver(int j, int i) { if(lineJudge(j,i)) return true; if(listJudge(j,i)) return true; if(rightItalicJudge(j,i)) return true; if(liftItalicJudge(j,i)) return true; return false; } //是否/和 private boolean liftItalicJudge(int j, int i) { int count=0; boolean up=true,down=true; for (int first=i,end=i-1,state=j,last=j+1; true; first++,state--,end--,last++) { if(first>=chessPoint.length||state<0) up=false; if(end<0||last>=chessPoint[i].length) down=false; if(!up&&!down) break; if(up&&chessPoint[first][state].chessType.equals(chessPoint[i][j].chessType)) count++; else up=false; if(down&&chessPoint[end][last].chessType.equals(chessPoint[i][j].chessType)) count++; else down=false; } if(count>=5) return true; else return false; } //是否\和 private boolean rightItalicJudge(int j, int i) { int count=0; boolean up=true,down=true; for (int first=i,end=i+1,state=j,last=j+1; true; first--,state--,end++,last++) { if(first<0||state<0) up=false; if(end>=chessPoint.length||last>=chessPoint[i].length) down=false; if(!up&&!down) break; if(up&&chessPoint[first][state].chessType.equals(chessPoint[i][j].chessType)) count++; else up=false; if(down&&chessPoint[end][last].chessType.equals(chessPoint[i][j].chessType)) count++; else down=false; } if(count>=5) return true; else return false; } //是否|和 private boolean listJudge(int j, int i) { int count=0; boolean up=true,down=true; for (int first=i,end=i+1; true; first--,end++) { if(first<0) up=false; if(end>=chessPoint.length) down=false; if(!up&&!down) break; if(up&&chessPoint[first][j].chessType.equals(chessPoint[i][j].chessType)) count++; else up=false; if(down&&chessPoint[end][j].chessType.equals(chessPoint[i][j].chessType)) count++; else down=false; } if(count>=5) return true; else return false; } //是否—和 private boolean lineJudge(int j,int i){ int count=0; boolean lift=true,right=true; for (int first=j,end=j+1; true; first--,end++) { if(first<0) lift=false; if(end>=chessPoint[i].length) right=false; if(!lift&&!right) break; if(lift&&chessPoint[i][first].chessType.equals(chessPoint[i][j].chessType)) count++; else lift=false; if(right&&chessPoint[i][end].chessType.equals(chessPoint[i][j].chessType)) count++; else right=false; } if(count>=5) return true; else return false; } }; /* * 菜单处理 */ private ActionListener jmenuListener=new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("newGame")){ newGame(); }else if(e.getActionCommand().equals("break")){ flag_godraw=false; jlbback.removeMouseListener(qiPanListeren); jmenuItem[0][1].setEnabled(false); jmenuItem[0][2].setEnabled(true); }else if(e.getActionCommand().equals("continue")){ flag_godraw=true; jlbback.addMouseListener(qiPanListeren); jmenuItem[0][2].setEnabled(false); jmenuItem[0][1].setEnabled(true); }else if(e.getActionCommand().equals("exit")){ //System.exit(0); dispose(); }else if(e.getActionCommand().equals("draw")){ if(!flag_godraw) return; Object obj=mySet.get(mySet.size()-1); boolean flag_draw=false; if(obj!=null) flag_draw=mySet.remove(mySet.size()-1); if(flag_draw){ getContentPane().remove((JLabel)obj); repaint(); Point p=(Point)mySetPoint.get(mySetPoint.size()-1); mySetPoint.remove(mySetPoint.size()-1); p.falg=false; p.chessType=""; if(jradiobtn[0].isSelected()){ jradiobtn[1].setSelected(true); jradiobtn[1].setFont(new Font("叶根友毛笔行书2.0版", Font.ITALIC+Font.BOLD, 20)); jradiobtn[0].setFont(new Font("仿宋", Font.PLAIN, 15)); }else{ jradiobtn[0].setSelected(true); jradiobtn[0].setFont(new Font("叶根友毛笔行书2.0版", Font.ITALIC+Font.BOLD, 20)); jradiobtn[1].setFont(new Font("仿宋", Font.PLAIN, 15)); } } } } }; /* * 棋子位置封装为类(位置,类型 白子或黑子,是否有棋子) */ private Point[][] chessPoint; private class Point{ private int x; private int y; private String chessType; private boolean falg; public Point(int x, int y) { this.x = x; this.y = y; } } public Gobang(){ super("简易五子棋"); setBounds(220, 100, 495, 405); setDefaultCloseOperation(DISPOSE_ON_CLOSE); setchessPoint(); mySet=new MySet(); mySetPoint=new MySet(); //菜单 JMenuBar bar=new JMenuBar(); String[] str={"游戏","选项"}; String[][] strs={{"新游戏","暂停","继续","退出"}, {"悔棋"}}; String[][] strs_english={{"newGame","break","continue","exit"}, {"draw"}}; jmenu=new JMenu[str.length]; jmenuItem=new JMenuItem[strs.length][]; for (int i = 0; i < jmenu.length; i++) { jmenu[i]=new JMenu(str[i]); jmenuItem[i]=new JMenuItem[strs[i].length]; for (int j = 0; j < jmenuItem[i].length; j++) { jmenuItem[i][j]=new JMenuItem(strs[i][j]); jmenuItem[i][j].addActionListener(jmenuListener); jmenuItem[i][j].setActionCommand(strs_english[i][j]); if(jmenuItem[i][j].getActionCommand().equals("continue")) jmenuItem[i][j].setEnabled(false); if(jmenuItem[i][j].getActionCommand().equals("break")) jmenuItem[i][j].setEnabled(false); jmenu[i].add(jmenuItem[i][j]); } bar.add(jmenu[i]); } setJMenuBar(bar); setback(); //jlbback.addMouseListener(qiPanListeren); //右面板显示 getContentPane().setLayout(null); JPanel showP=new JPanel(){ @Override public void paint(Graphics g) { super.paint(g); g.setColor(Color.blue); g.setFont(new Font("叶根友毛笔行书2.0版",Font.ITALIC, 20)); g.drawString("对弈:", 20, 20); } }; getContentPane().add(showP); showP.setBounds(350, 0, 150, 30); JPanel jRadioP=new JPanel(); String[] str_={"bai_chess.jpg","hei_chess.jpg"}; String[] str_2={"白子","黑子"}; boolean[] selected={true,false}; Icon[] img=new ImageIcon[str_.length]; jradiobtn=new JRadioButton[img.length]; jRadioP.setLayout(new GridLayout(jradiobtn.length, 1)); ButtonGroup grp=new ButtonGroup(); for (int i = 0; i < jradiobtn.length; i++) { img[i]=new ImageIcon(dir+str_[i]); jradiobtn[i]=new JRadioButton(str_2[i],img[i],selected[i]); jradiobtn[i].setFont(new Font("仿宋", Font.PLAIN, 15)); jRadioP.add(jradiobtn[i]); grp.add(jradiobtn[i]); } getContentPane().add(jRadioP); jRadioP.setBounds(370, 30, 150, 60); JLabel photo=new JLabel(new ImageIcon(dir+"shaonian.jpg")); photo.setBounds(350, 100, 130, 130); getContentPane().add(photo); JPanel writtP=new JPanel(){ @Override public void paint(Graphics g) { super.paint(g); g.setColor(Color.black); g.setFont(new Font("叶根友毛笔行书2.0版",Font.ITALIC, 20)); g.drawString("只要我放下去", 0, 20); g.drawString("你就输了", 20, 60); } }; getContentPane().add(writtP); writtP.setBounds(350, 250, 130, 100); setVisible(true); } /* * 新游戏初始化 */ private void newGame() { jlbback.removeMouseListener(qiPanListeren); jlbback.addMouseListener(qiPanListeren); flag_godraw=true; jradiobtn[0].setSelected(true); jradiobtn[1].setSelected(false); jradiobtn[0].setFont(new Font("叶根友毛笔行书2.0版", Font.ITALIC+Font.BOLD, 20)); jradiobtn[1].setFont(new Font("仿宋", Font.PLAIN, 15)); jmenuItem[0][2].setEnabled(false); jmenuItem[0][1].setEnabled(true); for (int i = 0; i < mySet.size(); i++) { getContentPane().remove((JLabel)mySet.get(i)); } for (int i = 0; i < chessPoint.length; i++) { for (int j = 0; j < chessPoint[i].length; j++) { chessPoint[i][j].falg=false; chessPoint[i][j].chessType=""; } } mySet.removeAll(); mySetPoint.removeAll(); repaint(); } /* * 棋盘各点的位置 */ private void setchessPoint() { int x=14,y=14; chessPoint=new Point[15][15]; for (int i = 0; i < chessPoint.length; i++) { for (int j = 0; j < chessPoint[i].length; j++) { chessPoint[i][j]=new Point(x, y); x+=23; } x=14; y+=23; } } /* * 设置背景 */ private void setback() { ((JPanel)getContentPane()).setOpaque(false); Icon imgback=new ImageIcon(dir+"qipan.jpg"); jlbback=new JLabel(imgback); getLayeredPane().add(jlbback,new Integer(Integer.MIN_VALUE)); jlbback.setBounds(0, 20, imgback.getIconWidth(), imgback.getIconHeight()); } public static void main(String[] args) { new Gobang(); } }
相关文章推荐
- spring-cloud-bus
- Hibernate在Spring工程上的配置详解
- 【Android注释技巧】Android函数上面的注释你是怎么写的?(Eclipse中)
- ZigZag Conversion
- JAVA学习笔记 JDK下载、安装与测试
- Java 机器学习 工具 & 库
- 搜狗2015校园招聘javaproject师面经
- java判断字符串中是否包含字母字符
- [疯狂Java笔记]AWT:BoxLayout盒式布局、Box容器
- JAVA 5.17习题
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-002-激活PROFILE、设置默认值、@ActiveProfiles
- Java排序算法总结之(三)——选择排序(简单选择排序、堆排序)
- 深入理解java动态代理机制
- Spring工作原理
- SpringMVC系列之基本配置
- Java内部类总结
- java生成二维码到页面、到本地
- Java中的main()方法详解
- Spring MVC 解读——@Autowired、@Controller、@Service从原理层面来分析
- 获取本机IP地址(java)