服务器之家

服务器之家 > 正文

java实现单人版五子棋游戏

时间:2020-03-31 11:39     来源/作者:lijiao

本文实例讲述了java实现的五子棋游戏代码,分享给大家供大家参考,具体代码如下

?
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package gyb.exam;
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
 
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
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.border.TitledBorder;
 
public class MainFrame extends JFrame{
 private static int BIG=15;
 private static final int ONE=25;
 private static final int START=10;
 private static int X=BIG*START/2;
 private static int Y=BIG*START/2;
 private int HL=BIG*ONE+START;
 private static boolean isBlack;
 CanvasFrame canvas=new CanvasFrame();
 CanvasFrame can;
 Graphics g;
 public MainFrame(){
  init();
 }
 private Point point;
 private ArrayList<Point> blacklist=new ArrayList<Point>();
 private ArrayList<Point> whitelist=new ArrayList<Point>();
 private void init() {
  JMenuBar bar=new JMenuBar();
  setJMenuBar(bar);
  setSize(600,600);
  setTitle("五子棋");
  JMenu menu=new JMenu("game");
  bar.add(menu);
  JMenuItem start=new JMenuItem("start");
  JMenuItem end=new JMenuItem("棋盘大小");
  end.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
   }
  });
  JMenuItem renew=new JMenuItem("重新开始");
  JMenuItem exit=new JMenuItem("退出");
  exit.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    System.exit(0);
   }
  });
  renew.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    blacklist.clear();
    whitelist.clear();
   }
  });
  menu.add(start);
  menu.add(renew);
  menu.add(end);
  menu.add(exit);
  setLayout(new BorderLayout());
  add(BorderLayout.CENTER,createCenter());
  add(BorderLayout.EAST,createEast());
 }
 private boolean isWin(ArrayList<Point> list){
  int x;
  int y;
  int idx=0;
  loop:for (Point p : list) {
   x=p.x;
   y=p.y;
   idx=0;
   while(list.contains(new Point(x,y))){
    idx++;
    x=x-ONE;
    if(idx==5)
     break loop;
   }
   x=p.x;
   y=p.y;
   idx=0;
   while(list.contains(new Point(x,y))){
    idx++;
    y=y-ONE;
    if(idx==5)
     break loop;
   }
   x=p.x;
   y=p.y;
   idx=0;
   while(list.contains(new Point(x,y))){
    idx++;
    x=x-ONE;
    y=y-ONE;
    if(idx==5)
     break loop;
   }
   x=p.x;
   y=p.y;
   idx=0;
   while(list.contains(new Point(x,y))){
    idx++;
    x=x-ONE;
    y=y+ONE;
    if(idx==5)
     break loop;
   }
  }
  return idx==5;
 }
  
 private JPanel createEast() {
  JPanel pane=new JPanel();
  BorderLayout border=new BorderLayout();
  pane.setLayout(border);
  pane.setBorder(new TitledBorder("五子棋"));
   
  Icon image=new ImageIcon(getClass().getResource("mm.png"));
  pane.add(BorderLayout.NORTH,new JLabel(image,JLabel.CENTER));
  pane.add(BorderLayout.CENTER,ballColor());
  pane.add(BorderLayout.SOUTH,button());
  return pane;
 }
 
 private JPanel ballColor() {
  JPanel pane=new JPanel();
   
  pane.setLayout(new BorderLayout());
  can=new CanvasFrame(){
   public void paint(Graphics g){
    g.setColor(Color.PINK);
    g.fillRect(1, 1,200,300);
    g.setFont(new Font("sans",Font.BOLD,20));
    g.setColor(Color.BLACK);
    g.fillOval(55, 20,25,30);
    g.drawString("黑子", 10, 43);
    g.setColor(Color.WHITE);
    g.fillOval(55, 60,25,30);
    g.drawString("白子", 10, 83);
    if(isBlack){
     g.setColor(Color.BLACK);
     g.drawString("黑方落子", 5, 120);
    }else{
     g.setColor(Color.WHITE);
     g.drawString("白方落子",5,120);
    }
    can.repaint();
   }
  };
  pane.add(BorderLayout.CENTER,can);
  return pane;
 }
 
 private JPanel button() {
  JPanel pane=new JPanel();
  BorderLayout bor=new BorderLayout();
  JButton start=new JButton("开始");
  JButton end=new JButton("结束");
  end.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    System.exit(0);
   }});
  pane.setLayout(bor);
  bor.setVgap(5);
  pane.setBorder(new TitledBorder(""));
  pane.add(BorderLayout.NORTH,start);
  pane.add(BorderLayout.SOUTH,end);
  return pane;
 }
 
 private JPanel createCenter() {
  JPanel pane=new JPanel();
  canvas.setPreferredSize(new Dimension(HL+10,HL+10));
  canvas.addMouseListener(new MouseAdapter() {
   public void mouseClicked(MouseEvent e){
    loop:if(e.getButton()==MouseEvent.BUTTON1){
      X=e.getX();
      Y=e.getY();
      int x=(X/ONE)*ONE;
      int y=(Y/ONE)*ONE;
      point=new Point(x,y);
       
      if(!(blacklist.contains(point)||whitelist.contains(point))){
       if(isBlack){
        blacklist.add(point);
        if(isWin(blacklist)){
         JOptionPane.showMessageDialog(null, "黑方胜利!");
         blacklist.clear();
         whitelist.clear();
        }
       }else{
        whitelist.add(point);
        if(isWin(whitelist)){
         JOptionPane.showMessageDialog(null, "白方胜利!");
         blacklist.clear();
         whitelist.clear();
        }
       }
      }
      else{
       JOptionPane.showMessageDialog(null, "这里己有子了,换个地方落子吧!");
       break loop;
      }
     canvas.repaint();
     isBlack=!isBlack;
    }
    
   }
  });
  pane.add(canvas);
  return pane;
 }
 
 public static void main(String[] args) {
  MainFrame frame=new MainFrame();
  frame.center(frame);
  frame.pack();
   
  frame.setVisible(true);
 }
 public void center(JFrame frame){
  //Toolkit 是当前 绘图系统集合工具包
  Toolkit toolkit=Toolkit.getDefaultToolkit();
  Dimension screen=toolkit.getScreenSize();//屏幕的宽高
  int frameWidth=frame.getWidth();
  int frameHigh=frame.getHeight();
  int x=(screen.width-frameWidth)/2;
  int y=(screen.height-frameHigh)/2;
  frame.setLocation(x, y);
 }
 class CanvasFrame extends JPanel {
  public void paint(Graphics g){
   g.setColor(Color.PINK);
   g.fillRect(0, 0, HL+20, HL+20);
   g.setColor(Color.BLACK);
   for(int i=START;i<=HL;i=i+ONE){
    for(int j=START;j<=HL;j=j+ONE){
     g.drawLine(i, j, HL, j);
    }
   }
   for(int i=START;i<=HL;i=i+ONE){
    for(int j=START;j<=HL;j=j+ONE){
     g.drawLine(i, j, i, HL);
    }
   }
   g.setColor(Color.YELLOW);
   g.drawRect(START/2, START/2, HL+1, HL+1);
   g.setColor(Color.BLACK);
   for(Point point : blacklist){
    g.fillOval(point.x, point.y, 20, 25);
   }
   g.setColor(Color.WHITE);
   for(Point point : whitelist){
    g.fillOval(point.x, point.y, 20, 25);
   }
  }
 }
}

效果图:

java实现单人版五子棋游戏

以上所述就是本文的全部内容了,希望能够对大家熟练掌握java有所帮助。

标签:

相关文章

热门资讯

沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
超A是什么意思 你好a表达的是什么
超A是什么意思 你好a表达的是什么 2019-06-06
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
返回顶部