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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
| import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException;
import javax.swing.*; class chess { public int X,Y; public int flag; public chess() { X=0; Y=0; flag=0; } public chess(int x,int y) { X=x; Y=y; flag=0; } } public class FiveChess extends JPanel { public Graphics g; public chess [][]xy=new chess[15][15]; public static void main(String[] args) { FiveChess fc=new FiveChess(); fc.go(); } public void go() { JFrame frame = new JFrame("五子棋"); frame.setLocation(200,150); frame.setSize(800,530); frame.setDefaultCloseOperation(3); JMenuBar menubar=new JMenuBar(); JMenu menu1=new JMenu("开始"); JMenu menu2=new JMenu("帮助"); JMenuItem bstart=new JMenuItem("新游戏"); JMenuItem op=new JMenuItem("如何获胜"); menu1.add(bstart); menu2.add(op); menubar.add(menu1); menubar.add(menu2); frame.setJMenuBar(menubar); JTextArea text=new JTextArea(); text.setPreferredSize(new Dimension(200,100)); text.setEditable(false); JPanel panel1=new JPanel(); panel1.setPreferredSize(new Dimension(300,500)); panel1.setBackground(Color.white); JButton bquit=new JButton("认输"); JButton bundo=new JButton("悔棋"); bquit.setPreferredSize(new Dimension(200,70)); bundo.setPreferredSize(new Dimension(200,70)); panel1.add(bquit); panel1.add(bundo); JRadioButton button1=new JRadioButton("人机对战"); JRadioButton button2=new JRadioButton("玩家对战"); button1.setOpaque(false); button2.setOpaque(false); button1.setPreferredSize(new Dimension(200,70)); button2.setPreferredSize(new Dimension(200,70)); ButtonGroup group=new ButtonGroup(); group.add(button1); group.add(button2); panel1.add(button1); panel1.add(button2); panel1.add(text); this.setBackground(Color.orange); frame.setLayout(new BorderLayout()); frame.add(this,BorderLayout.CENTER); frame.add(panel1,BorderLayout.EAST); frame.setVisible(true); frame.setResizable(false); g=this.getGraphics(); setChess(); listener l=new listener(); l.setG(g); l.setP(this); l.setChess(xy); l.setText(text); this.addMouseListener(l); bstart.addActionListener(l); bquit.addActionListener(l); op.addActionListener(l); bundo.addActionListener(l); button1.addActionListener(l); button2.addActionListener(l); } public void setChess() { for(int i=0;i<15;i++){ for(int j=0;j<15;j++){ chess ch=new chess((j+1)*30,(i+1)*30); xy[i][j]=ch; } } } public void draw(Graphics ag) { Graphics2D g=(Graphics2D)ag; g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); for(int i=0;i<15;i++) { for(int j=0;j<15;j++) { g.setColor(Color.black); g.drawLine(30,30+i*30,450,30+i*30); g.drawLine(30+i*30,30,30+i*30,450); } } //System.out.println("黑方首下"); } public void paint(Graphics g) { super.paint(g); draw(g); } } class listener extends MouseAdapter implements ActionListener{ int mode; int x,y; Graphics2D g; int chesscount; //连成一条线的同一颜色棋子数量 chess [][]xy; int col=2; chess chessnew=new chess(); JPanel panel; JTextArea text; public void setText(JTextArea text) { this.text=text; } public void setG(Graphics g){ this.g=(Graphics2D)g; } public void setP(JPanel panel){ this.panel=panel; } public void setChess(chess [][]xy){ this.xy=xy; } public void actionPerformed(ActionEvent e) { String str= e.getActionCommand(); if(str.equals("如何获胜")) { int b; String str1=""; byte []tom=new byte[2]; try { FileInputStream in=new FileInputStream("帮助.txt"); while((b=in.read(tom,0,2))!=-1) { String s=new String(tom,0,b); str1+=s; //System.out.print(s); } text.setText(str1); in.close(); }catch(IOException ee) { text.setText("没找到帮助文档");//System.out.println("没找到帮助文档"); } } if(str.equals("新游戏")) { for(int i=0;i<15;i++){ for(int j=0;j<15;j++){ xy[i][j].flag=0; } } col=2; panel.paint(g); } if(str.equals("认输")){ if(col==1){ JOptionPane.showMessageDialog(null, "白棋认输,黑棋胜"); } else{ JOptionPane.showMessageDialog(null, "黑棋认输,白棋胜"); } for(int i=0;i<15;i++){ for(int j=0;j<15;j++){ xy[i][j].flag=0; } } panel.paint(g); } if(str.equals("悔棋")) { if(chessnew.flag==1) { col=1; text.setText("白方悔棋,请白方在落一子");//System.out.println("白方悔棋,请白方在落一子"); } else { col=2; text.setText("黑方悔棋,请黑方在落一子");//System.out.println("黑方悔棋,请黑方在落一子"); } chessnew.flag=0; panel.paint(g); for(int i=0;i<15;i++) { for(int j=0;j<15;j++) { if(xy[i][j].flag!=0) { if(xy[i][j].flag==1) { g.setColor(Color.white); g.fillOval(xy[i][j].X-10, xy[i][j].Y-10,20,20); } else { g.setColor(Color.black); g.fillOval(xy[i][j].X-10, xy[i][j].Y-10,20,20); } } } } } if(str.equals("人人对战")){ mode=11; } } public void mouseClicked(MouseEvent e) { g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); x=e.getX(); y=e.getY(); for(int i=0;i<15;i++){ for(int j=0;j<15;j++) { if(Math.abs(x-xy[i][j].X)<10&&Math.abs(y-xy[i][j].Y)<10) { if(xy[i][j].flag==0) { if(col == 1){ g.setColor(Color.white);//椭圆的左上角xy坐标,椭圆的宽度、高度 g.fillOval(xy[i][j].X-12, xy[i][j].Y-12,20,20); xy[i][j].flag=1; chessnew=xy[i][j]; col=2; text.setText("该黑方下棋");//System.out.println("该黑方下棋"); } else if(col==2){ g.setColor(Color.black); g.fillOval(xy[i][j].X-12, xy[i][j].Y-12,20,20); xy[i][j].flag=2; col=1; text.setText("该白方下棋");//System.out.println("该白方下棋"); } checkChess( xy[i][j]); } else text.setText("这里不能落子");//System.out.println("这里不能落子"); } } } } public void checkChess(chess ch){ int count1=1; int count2=1; int count3=1; int count4=1; int x=ch.Y/30-1; int y=ch.X/30-1; for(int j=y+1;j<15;j++){ if(xy[x][y].flag==xy[x][j].flag){ count1++; } else break; } for(int j=y-1;j>=0;j--){ if(xy[x][y].flag==xy[x][j].flag){ count1++; } else break; } if(count1>=5){ if(xy[x][y].flag==1) JOptionPane.showMessageDialog(null, "白棋胜出"); else JOptionPane.showMessageDialog(null, "黑棋胜出"); for(int i=0;i<15;i++){ for(int j=0;j<15;j++){ xy[i][j].flag=0; } } panel.paint(g); } for(int i=x-1;i>=0;i--){ if(xy[x][y].flag==xy[i][y].flag&&xy[x][y].flag!=0){ count2++; } else break; } for(int i=x+1;i<15;i++){ if(xy[x][y].flag==xy[i][y].flag&&xy[x][y].flag!=0){ count2++; } else break; } if(count2>=5){ if(xy[x][y].flag==1) JOptionPane.showMessageDialog(null, "白棋胜出"); else JOptionPane.showMessageDialog(null, "黑棋胜出"); for(int i=0;i<15;i++){ for(int j=0;j<15;j++){ xy[i][j].flag=0; } } panel.paint(g); } for(int i=x-1,j=y+1;i>=0&&j<15;i--,j++){ if(xy[x][y].flag==xy[i][j].flag&&xy[x][y].flag!=0){ count3++; } else break; } for(int i=x+1,j=y-1;j>=0&&i<15;j--,i++){ if(xy[x][y].flag==xy[i][j].flag&&xy[x][y].flag!=0){ count3++; } else break; } if(count3>=5){ if(xy[x][y].flag==1) JOptionPane.showMessageDialog(null, "白棋胜出"); else JOptionPane.showMessageDialog(null, "黑棋胜出"); for(int i=0;i<15;i++){ for(int j=0;j<15;j++){ xy[i][j].flag=0; } } panel.paint(g); } for(int i=x+1,j=y+1;i<15&&j<15;i++,j++){ if(xy[x][y].flag==xy[i][j].flag&&xy[x][y].flag!=0){ count4++; } else break; } for(int i=x-1,j=y-1;j>=0&&i>=0;j--,i--){ if(xy[x][y].flag==xy[i][j].flag&&xy[x][y].flag!=0){ count4++; } else break; } if(count4>=5){ if(xy[x][y].flag==1) JOptionPane.showMessageDialog(null, "白棋胜出"); else JOptionPane.showMessageDialog(null, "黑棋胜出"); for(int i=0;i<15;i++){ for(int j=0;j<15;j++){ xy[i][j].flag=0; } } panel.paint(g); } } }
|