hooyantsing's Blog

第51次课程

字数统计: 1k阅读时长: 6 min
2019/08/01

源辰74班

第51次课程

08.01.2019

内容

MIS系统二[廖彦]

1.五子棋(GUI版本)

效果图:

413dac85040b3f8e48325654e12c327d.png

源码:

Class WuziWin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package wuziqi;
import org.eclipse.swt.SWT;
import  org.eclipse.swt.events.SelectionAdapter;
import  org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class WuziWin {
    protected Shell shell;
    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
       try {
           WuziWin window = new WuziWin();
           window.open();
       } catch (Exception e) {
           e.printStackTrace();
       }
    }
    /**
     * Open the window.
     */
    public void open() {
       Display display =  Display.getDefault();
       createContents();
       shell.open();
       shell.layout();
       while (!shell.isDisposed()) {
           if (!display.readAndDispatch()) {
               display.sleep();
           }
       }
    }
    /**
     * Create contents of the window.
     */
    protected void createContents() {
       shell = new Shell();
       shell.setSize(1080, 813);
       shell.setText("五子棋");
       shell.setLayout(null);
       QiPan qiPan = new  QiPan(shell,SWT.NONE);
       qiPan.setBounds(0,0,764,757);
       
       Button button = new  Button(shell,SWT.NONE);
       button.setBounds(837,112,161,34);
       button.setText("");
       
       Button button_1 = new  Button(shell,SWT.NONE);
       button_1.addSelectionListener(new  SelectionAdapter() {
           @Override
           public void  widgetSelected(SelectionEvent e) {
               System.exit(0);
           }
       });
       
       button_1.setText("");
       button_1.setBounds(837,181,161,34);
    }
}

Class WuziGame

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package wuziqi;
import java.util.HashSet;
import java.util.Set;
public class WuziGame {
    
    public final static String WHITE = "O";
    public final static String BLACK = "@";
    public final static String SPACE = "+";
    //棋盘
    private String[][] pan;
    //当前棋子颜色
    private String currentColor = BLACK;
    private Set<int[]> winSet;
    //初始化棋盘
    WuziGame(int size){
       pan = new String[size][size];
    }
    
    //获取当前棋盘上的棋子颜色
    String getZi(int x,int y) {
       return pan[x][y] == null ? SPACE :  pan[x][y];
    }
    /**
     * 下子
     * @throws WuziException
     */
    String downZi(int x,int y) throws  WuziException {
       if(winSet != null) {
           throw new WuziException("棋局已经结束!");
       }
       if(pan[x][y] == null) {
           pan[x][y] = currentColor;
           currentColor = currentColor ==  BLACK ? WHITE : BLACK;    
       }
       return getZi(x,y);
    }
    
    //获取棋盘大小
    int getSize() {
       return pan.length;
    }
    
    /**
     * 判断输赢
     */
    boolean panduan(String color,int x,int  y) {
       //定义4个方向的走向 x,y的变化值
       int[][] a =  {{-1,0,1,0},{0,-1,0,1},{-1,-1,1,1},{-1,1,1,-1}};
       //判断4个方向是是否有5子连珠
       for(int i=0;i<a.length;i++) {
           Set<int[]> ziSet = new  HashSet<int[]>();
           for(int j=0;j<3;j+=2) {
               for(int  x1=x,y1=y;x1>=0&&y1>=0&&x1<getSize()&&y1<getSize();x1+=a[i][j],y1+=a[i][j+1]) {
                  if(color.equals(getZi(x1,y1))) {
                      ziSet.add(new int[]  {x1,y1});
                  }else {
                      break;
                  }
               }
           }
           if(ziSet.size()>5) {
               winSet = new  HashSet<int[]>();
               winSet.addAll(ziSet);
               return true;
           }
       }
    return false;  
    }
    
    public Set<int[]> getWinSet(){
       return winSet;
    }
}

Class WuziException

1
2
3
4
5
6
7
8
9
10
11
package wuziqi;
public class WuziException extends Exception  {
    
    public WuziException() {
       super();
    }
    
    public WuziException(String message) {
       super(message);
    }
}

Class QiPan

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package wuziqi;

import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseAdapter;

public class QiPan extends Composite{
        final static Image PAN = SWTResourceManager.getImage(WuziWin.class,"/pan.jpg");
        final static Image BAI = SWTResourceManager.getImage(WuziWin.class,"/bai.jpg");
        final static Image HEI = SWTResourceManager.getImage(WuziWin.class,"/hei.jpg");
        public QiPan(Composite parent, int style) {
            super(parent, style);
            WuziGame wg = new WuziGame(16);
            GridLayout gridLayout = new GridLayout(wg.getSize(),true);
            gridLayout.verticalSpacing = 0;
            gridLayout.horizontalSpacing = 0;
            gridLayout.marginHeight = 10;
            gridLayout.marginWidth = 10 ;
            setLayout(gridLayout);
            MouseTrackAdapter mtl = new MouseTrackAdapter() {
                
                    @Override   
                    public void mouseEnter(MouseEvent e) { //鼠标监听,当鼠标移入事件时
                        Label l = (Label) e.getSource();
                        if("+".equals(l.getText())) {
                            ((Label)e.getSource()).setText("[+]");
                        }                
                    }
                    @Override   
                    public void mouseExit(MouseEvent e) { //鼠标监听,当鼠标移出事件时
                        Label l = (Label) e.getSource();
                        if("[+]".equals(l.getText())) {
                            ((Label)e.getSource()).setText("+");
                        }                
                    }
            };
            Font f = new Font(getDisplay(),"微软雅黑",16,SWT.NORMAL);
            Font f2 = new Font(getDisplay(),"微软雅黑",16,SWT.BOLD);
            Label ls[][] = new Label[wg.getSize()][wg.getSize()];
            for(int i =0 ;i<wg.getSize();i++) {
                for(int j=0;j<wg.getSize();j++) {
                    final int x = i,y = j;
                    Label l = new Label(this,SWT.NONE);
                    ls[i][j] = l;
                    l.setImage(PAN);
                    l.setSize(20, 20);
                    l.setFont(f);
                    l.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseUp(MouseEvent e) {
                            Label l = (Label) e.getSource();
                            if(WuziGame.SPACE.equals(wg.getZi(x,y))) {
                                try {
                                    String color = wg.downZi(x, y);
                                    l.setImage("O".equals(color) ? BAI : HEI);
                                    if (wg.panduan(color, x, y)) {
                                        for (int[] xy : wg.getWinSet()) {
                                            ls[xy[0]][xy[1]].setFont(f2);
                                            ls[xy[0]][xy[1]].setForeground(parent.getDisplay().getSystemColor(style));
                                        }
                                        MessageDialog.openConfirm(null, "系统提示", color + "方赢了");
                                    }
                                } catch (Exception e2) {
                                    MessageDialog.openConfirm(null, "系统提示", e2.getMessage());
                                }
                            }
                        }
                    });
                    l.addMouseTrackListener(mtl);
                }
            }
            
        }
}
CATALOG