暑假第三周总结
这周完成了数据结构课程设计的第二阶段,我选择的是五子棋游戏,开始的时候不知道如何下手,感觉好多知识无法应用,于是在网上找了一些文章,去学习他们的算法,如何做出一个像样的程序,其中很多东西字节没有学过,所以程序代码看起来比较晦涩,不易理解,过程比较艰辛,做成的程序也不是很完美,但最后也是完成了程序
package wuziqi; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JRadioButtonMenuItem; import javax.swing.SwingUtilities; import javax.swing.UIManager; /** * * * * */ @SuppressWarnings("serial") public class ChessFrame extends JFrame implements ActionListener { private String[] strsize= {"20x15","30x20","40x30","50x50"}; private String[] strmode= {"人机对弈","人人对弈"}; public static boolean iscomputer=true; public static boolean checkcomputer=true; private int width; private int height; private ChessModel chessmodel; private MainPanel mainpanel; public ChessFrame() { this.setTitle("五子棋"); chessmodel=new ChessModel(3); mainpanel=new MainPanel(chessmodel); Container container=this.getContentPane(); container.add(mainpanel,"Center"); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); MapSize(20,15); JMenuBar mbar=new JMenuBar(); this.setJMenuBar(mbar); JMenu gameMenu=new JMenu("游戏"); mbar.add(makeMenu(gameMenu,new Object[] { "开局", "棋盘","模式",null,"退出" },this)); JMenu lookMenu=new JMenu("视图"); mbar.add(makeMenu(lookMenu,new Object[] { "Metal","Motif","Windows" },this)); JMenu helpMenu=new JMenu("帮助"); mbar.add(makeMenu(helpMenu,new Object[] { "游戏介绍" },this)); } public JMenu makeMenu(Object parent,Object items[],Object target){ JMenu m=null; if(parent instanceof JMenu) m=(JMenu)parent; else if(parent instanceof String) m=new JMenu((String)parent); else return null; for(int i= 0;i<items.length;i++) if(items[i] == null) m.addSeparator(); else if(items[i] == "棋盘"){ JMenu jm=new JMenu("棋盘"); ButtonGroup group=new ButtonGroup(); JRadioButtonMenuItem rmenu; for(int j=0;j<strsize.length;j++){ rmenu=makeRadioButtonMenuItem(strsize[j],target); if(j==0) rmenu.setSelected(true); jm.add(rmenu); group.add(rmenu); } m.add(jm); }else if(items[i] == "模式"){ JMenu jmenu=new JMenu("模式"); ButtonGroup buttongroup=new ButtonGroup(); JRadioButtonMenuItem rmenu; for(int h=0;h<strmode.length;h++){ rmenu=makeRadioButtonMenuItem(strmode[h],target); if(h==0) rmenu.setSelected(true); jmenu.add(rmenu); buttongroup.add(rmenu); } m.add(jmenu); }else m.add(makeMenuItem(items[i],target)); return m; } public JMenuItem makeMenuItem(Object item,Object target){ JMenuItem jmenuitem= null; if(item instanceof String) jmenuitem=new JMenuItem((String)item); else if(item instanceof JMenuItem) jmenuitem=(JMenuItem)item; else return null; if(target instanceof ActionListener) jmenuitem.addActionListener((ActionListener)target); return jmenuitem; } public JRadioButtonMenuItem makeRadioButtonMenuItem(Object item,Object target){ JRadioButtonMenuItem jradiobuttonmenuitem= null; if(item instanceof String) jradiobuttonmenuitem=new JRadioButtonMenuItem((String)item); else if(item instanceof JRadioButtonMenuItem) jradiobuttonmenuitem=(JRadioButtonMenuItem)item; else return null; if(target instanceof ActionListener) jradiobuttonmenuitem.addActionListener((ActionListener)target); return jradiobuttonmenuitem; } public void MapSize(int width,int height){ setSize(width*20+50,height*20+100); if(this.checkcomputer) this.iscomputer=true; else this.iscomputer=false; mainpanel.setModel(chessmodel); mainpanel.repaint(); } public boolean getiscomputer(){ return this.iscomputer; } public void restart(){ int modeChess=chessmodel.getModeChess(); if(modeChess<= 4&&modeChess>= 1){ chessmodel=new ChessModel(modeChess); MapSize(chessmodel.getWidth(),chessmodel.getHeight()); System.out.println("重新开局"); }else{ System.out.println("重新开局失败"); } } @SuppressWarnings("static-access") public void actionPerformed(ActionEvent actionevent){ String arg=actionevent.getActionCommand(); try{ if(arg.equals("Motif")) UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel"); else if(arg.equals("Windows")) UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); else UIManager.setLookAndFeel( "javax.swing.plaf.metal.MetalLookAndFeel"); SwingUtilities.updateComponentTreeUI(this); }catch(Exception ee){} if(arg.equals("20x15")){ this.width=20; this.height=15; chessmodel=new ChessModel(1); MapSize(this.width,this.height); SwingUtilities.updateComponentTreeUI(this); } if(arg.equals("30x20")){ this.width=30; this.height=20; chessmodel=new ChessModel(2); MapSize(this.width,this.height); SwingUtilities.updateComponentTreeUI(this); } if(arg.equals("40x30")){ this.width=40; this.height=30; chessmodel=new ChessModel(3); MapSize(this.width,this.height); SwingUtilities.updateComponentTreeUI(this); } if(arg.equals("50x50")){ this.width=50; this.height=50; chessmodel=new ChessModel(4); MapSize(this.width,this.height); SwingUtilities.updateComponentTreeUI(this); } if(arg.equals("人机对弈")){ System.out.println("进入人机对弈模式"); this.checkcomputer=true; this.iscomputer=true; chessmodel=new ChessModel(chessmodel.getModeChess()); MapSize(chessmodel.getWidth(),chessmodel.getHeight()); SwingUtilities.updateComponentTreeUI(this); } if(arg.equals("人人对弈")){ System.out.println("进入人人对弈模式"); this.checkcomputer=false; this.iscomputer=false; chessmodel=new ChessModel(chessmodel.getModeChess()); MapSize(chessmodel.getWidth(),chessmodel.getHeight()); SwingUtilities.updateComponentTreeUI(this); } if(arg.equals("开局")){ restart(); } if(arg.equals("游戏介绍")) { System.out.println("打开游戏介绍"); JOptionPane.showMessageDialog(this,"五子棋是一种简单的游戏," + "\n基本规则:" + "\n黑白双方依次落子。棋盘上形成横向、竖向、斜向的连续的相同颜色的五个棋子称为'五连' 。" + "\n黑白双方先在棋盘上形成五连的一方为胜。" + "\n若对局双方均认为不可能形成五连或是剩余棋盘空间已不足以形成五连则为和棋。", "游戏介绍",0); } if(arg.equals("退出")) { System.out.println("成功退出五子棋游戏"); System.exit(0); } } }
package wuziqi; import javax.swing.JOptionPane; import javax.swing.JPanel; /** * * * */ public class ChessModel { private int width,height,modeChess; private int x = 0; private int y = 0; private int[][]arrMapShow; public boolean getisExist() { return this.isExist; } public int getWidth() { return this.width; } public int getHeight() { return this.height; } public int getModeChess() { return this.modeChess; } public int[][]getarrMapShow(){ return arrMapShow; } public void setX(int x) { this.x=x; } public int getX() { return this.x; } public void setY(int y) { this.y=y; } public int getY() { return this.y; } private boolean isOdd; private boolean isExist; public boolean getisOdd() { return this.isOdd; } public void setisOdd(boolean isOdd) { if(isOdd) this.isOdd = true; else this.isOdd = false; } public ChessModel() {} public ChessModel(int modeChess) { this.isOdd = true; if(modeChess == 1 ) { Panellnit(20,15,modeChess); } if(modeChess == 2) { Panellnit(30,20,modeChess); } if(modeChess == 3) { Panellnit(40,30,modeChess); } if(modeChess == 4) { Panellnit(50,50,modeChess); } } private void Panellnit(int width,int height,int modeChess) { this.width = width; this.height = height; this.modeChess = modeChess; arrMapShow = new int[width+1][height+1]; for(int i = 0;i <= width;i++) { for(int j = 0;j <= height;j++) { arrMapShow[i][j] = -5; } } } private boolean badxy(int x,int y) { if(x >= width+20 || x<0) return true; return y>=height||y<0; } public int checkMax(int x,int y,int black_or_white) { int num=0; int max_num; int max_temp=0; int x_temp=x; int y_temp=y; int x_temp1=x_temp; int y_temp1=y_temp; for(int i=1;i<5;i++) { x_temp1+=1; if(x_temp1>this.width) break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++; else break; } x_temp1=x_temp; for(int i=1;i<5;i++) { x_temp1-=1; if(x_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++; else break; } if(num<5) max_temp=num; x_temp1=x_temp; y_temp1=y_temp; num=0; for(int i=1;i<5;i++){ y_temp1-=1; if(y_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++; else break; } y_temp1=y_temp; for(int i=1;i<5;i++){ y_temp1+=1; if(y_temp1>this.height) break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++; else break; } if(num>max_temp&&num<5) max_temp=num; x_temp1=x_temp; y_temp1=y_temp; num=0; for(int i=1;i<5;i++){ x_temp1-=1; y_temp1-=1; if(y_temp1<0||x_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++; else break; } x_temp1=x_temp; y_temp1=y_temp; for(int i=1;i<5;i++){ x_temp1+=1; y_temp1+=1; if(y_temp1>this.height || x_temp1>this.width) break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++; else break; } if(num>max_temp&&num<5) max_temp=num; x_temp1=x_temp; y_temp1=y_temp; for(int i=1;i<5;i++){ x_temp1-=1; y_temp1+=1; if(y_temp1>this.height || x_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++; else break; } if(num>max_temp&&num<5) max_temp=num; max_num=max_temp; x_temp1=x_temp; y_temp1=y_temp; num=0; for(int i=1;i<5;i++){ x_temp1+=1; y_temp1-=1; if(y_temp1<0||x_temp1>this.width) break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++; else break; } return max_num; } public boolean chessExist(int i,int j) { if(this.arrMapShow[i][j] == 1 || this.arrMapShow[i][j] == 2) return true; return false; } public void readyplay(int x,int y) { if(badxy(x,y)) return; if(chessExist(x,y)) return; this.arrMapShow[x][y]=3; } public void play(int x,int y) { if(badxy(x,y)) return; if(chessExist(x,y)) { this.isExist=true; return; }else { this.isExist=false; } if(getisOdd()) { setisOdd(false); this.arrMapShow[x][y]=1; }else { setisOdd(true); this.arrMapShow[x][y]=2; } } public void computerDo(int width,int height) { int max_black; int max_white; int max_temp; int max=0; setisOdd(true); for(int i=0;i<=width;i++) { for(int j=0;j<=height;j++) { if(!chessExist(i,j)) { max_white=checkMax(i,j,2); max_black=checkMax(i,j,1); max_temp=Math.max(max_white, max_black); if(max_temp>max) { max=max_temp; this.x=i; this.y=j; } } } } setX(this.x); setY(this.y); this.arrMapShow[this.x][this.y]=2; } public boolean judgeSuccess(int x,int y,boolean isOdd) { int num=1; int arrvalue; int x_temp=x; int y_temp=y; if(isOdd) arrvalue=2; else arrvalue=1; int x_temp1=x_temp; int y_temp1=y_temp; for(int i=1;i<6;i++) { x_temp1+=1; if(x_temp1>this.width) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } x_temp1=x_temp; for(int i=1;i<6;i++){ x_temp1-=1; if(x_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } if(num==5) return true; x_temp1=x_temp; y_temp1=y_temp; num=1; for(int i=1;i<6;i++){ y_temp1-=1; if(y_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } y_temp1=y_temp; for(int i=1;i<6;i++){ y_temp1+=1; if(y_temp1>this.height) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } if(num==5) return true; x_temp1=x_temp; y_temp1=y_temp; num=1; for(int i=1;i<6;i++){ x_temp1-=1; y_temp1-=1; if(y_temp1<0||x_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } x_temp1=x_temp; y_temp1=y_temp; for(int i=1;i<6;i++){ x_temp1+=1; y_temp1+=1; if(y_temp1>this.height || x_temp1>this.width) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } if(num==5) return true; x_temp1=x_temp; y_temp1=y_temp; num=1; for(int i=1;i<6;i++){ x_temp1+=1; y_temp1-=1; if(y_temp1<0||x_temp1>this.width) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } x_temp1=x_temp; y_temp1=y_temp; for(int i=1;i<6;i++){ x_temp1-=1; y_temp1+=1; if(y_temp1>this.height || x_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } if(num==5) return true; return false; } public void showSuccess(JPanel jpanel) { if(ChessFrame.iscomputer) { JOptionPane.showMessageDialog(jpanel, "你赢啦!","win",JOptionPane.INFORMATION_MESSAGE); System.out.println("win"); }else { if(isOdd) { JOptionPane.showMessageDialog(jpanel, "白棋赢!","win",JOptionPane.INFORMATION_MESSAGE); System.out.println("白棋win"); }else { JOptionPane.showMessageDialog(jpanel, "黑棋赢!","win",JOptionPane.INFORMATION_MESSAGE); System.out.println("黑棋win"); } } } public void showDefault(JPanel jpanel) { System.out.println("lost"); JOptionPane.showMessageDialog(jpanel, "很遗憾,你输了!请重新开始!","lost",JOptionPane.INFORMATION_MESSAGE); } }
package wuziqi; public class Main { @SuppressWarnings("deprecation") public static void main(String[] args) { ChessFrame chess_frame = new ChessFrame(); chess_frame.show(); } }
package wuziqi; import java.awt.Color; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import javax.swing.JPanel; /** * * * * */ @SuppressWarnings("serial") public class MainPanel extends JPanel implements MouseListener,MouseMotionListener { private int width; private int height; private ChessModel chessmodel; MainPanel(ChessModel cm){ chessmodel=cm; width=chessmodel.getWidth(); height=chessmodel.getHeight(); addMouseListener(this); } public void setModel(ChessModel cm){ chessmodel=cm; width=chessmodel.getWidth(); height =chessmodel.getHeight(); } public void paintComponent(Graphics graphics){ super.paintComponent(graphics); for(int j= 0;j<=height; j++){ for(int i=0;i <=width; i++){ int v= chessmodel.getarrMapShow()[i][j]; draw(graphics,i,j,v); } } } //画棋子 public void draw(Graphics graphics,int i, int j,int v){ int x=20*i+20; int y=20*j+20; if(i!=width&&j!=height){ graphics.setColor(Color.white); graphics.drawRect(x,y,20,20); } if(v== 1){ graphics.setColor(Color.gray); graphics.drawOval(x-8,y-8,16,16); graphics.setColor(Color.black); graphics.fillOval(x-8,y-8,16,16); } if(v== 2){ graphics.setColor(Color.gray); graphics.drawOval(x-8,y-8,16,16); graphics.setColor(Color.white); graphics.fillOval(x-8,y-8,16,16); } if(v==3){ graphics.setColor(Color.cyan); graphics.drawOval(x-8,y-8,16,16); } } public void mousePressed(MouseEvent mouseevent){ int x=(mouseevent.getX()-10)/20; int y=(mouseevent.getY()-10)/20; System.out.println("----------------------------------"); System.out.println("x="+x+",y="+y); if(mouseevent.getModifiers()==MouseEvent.BUTTON1_MASK){ chessmodel.play(x,y); System.out.println("黑棋(false),白棋(true):"+chessmodel.getisOdd()); System.out.println("黑棋(1),白棋(2):"+chessmodel.getarrMapShow()[x][y]); repaint(); if(chessmodel.judgeSuccess(x,y,chessmodel.getisOdd())){ chessmodel.showSuccess(this); mouseevent.consume(); ChessFrame.iscomputer=false; } if(ChessFrame.iscomputer&&!chessmodel.getisExist()){ chessmodel.computerDo(chessmodel.getWidth(),chessmodel.getHeight()); repaint(); if(chessmodel.judgeSuccess(chessmodel.getX(),chessmodel.getY(),chessmodel.getisOdd())){ chessmodel.showDefault(this); mouseevent.consume(); } } } } public void mouseClicked(MouseEvent evt){} public void mouseReleased(MouseEvent evt){} public void mouseEntered(MouseEvent mouseevt){} public void mouseExited(MouseEvent mouseevent){} public void mouseDragged(MouseEvent evt){} public void mouseMoved(MouseEvent mouseevent){ int x=(mouseevent.getX()-10)/ 20; int y=(mouseevent.getY()-10)/ 20; chessmodel.readyplay(x,y); repaint(); } }
浙公网安备 33010602011771号