本文实例为大家分享了Java实现五子棋单机版的具体代码,供大家参考,具体内容如下
Java五子棋设计流程:
1.创建窗口和设计一个棋盘界面
2.实现鼠标点击,棋子出现,黑白棋轮流下
3.能够判断输赢
4.添加按钮功能
实现结果图:
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 357 358 359 360 361 362 363 364 365 |
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Cursor; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel;
public class Test {
public static void main(String[] args) { new MyFrame();
} }
class MyFrame extends JFrame implements MouseListener{
//保存坐标 int x; int y; int x1; int y1; //黑子数 //白子数 //1是黑下,2是白下 //默认开始是黑旗先下 int flag= 1 ; //表示游戏是否结束 //true游戏开始,false游戏结束,不能再下 boolean canPlay= true ; //保存之前下过的棋子的坐标 //'0'代表没有棋子,'1'代表黑棋,'2'代表白棋 int [][]allChess= new int [ 19 ][ 19 ]; //int [][]allChess=new int[25][25]; //当前棋子的总数 int chessSum= 0 ; BufferedImage bgImage = null ;
JButton withdraw= new JButton( "悔棋" ); JButton restart= new JButton( "重新开始" ); JButton exit= new JButton( "退出" ); JPanel south= new JPanel();
public MyFrame() { this .setTitle( "五子棋" ); setSize( 630 , 700 ); setLayout( new BorderLayout()); setLocationRelativeTo( null ); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); try { bgImage=ImageIO.read( new File( "C:\\Users\\us\\Desktop\\1.jpg" )); } catch (IOException e1) {
e1.printStackTrace(); } addMouseListener( this ); //将窗体加入监听
south.setLayout( new FlowLayout(FlowLayout.LEFT, 60 , 30 ));
south.add(restart); south.add(withdraw); south.add(exit); //初始化按钮事件监听器内部类 MybuttonListener buttonListener = new MybuttonListener(); //将三个按钮事件注册监听事件 restart.addActionListener(buttonListener); withdraw.addActionListener(buttonListener); exit.addActionListener(buttonListener); //将按钮面板加到窗体的南部 this .add(south,BorderLayout.SOUTH);
setVisible( true ); }
public void paint(Graphics g) {
int tempSum=chessSum; //棋盘 g.drawImage(bgImage, 8 , 30 , this );
for ( int colum= 58 ;colum< 600 ;colum=colum+ 30 ){ //行 g.drawLine( 38 ,colum, 578 ,colum); } for ( int rand= 38 ;rand< 600 ;rand=rand+ 30 ){ //列 g.drawLine(rand, 58 ,rand, 598 ); } //黑点 g.fillOval( 122 , 143 , 10 , 10 ); g.fillOval( 484 , 143 , 10 , 10 ); g.fillOval( 122 , 504 , 10 , 10 ); g.fillOval( 303 , 353 , 10 , 10 ); g.fillOval( 484 , 503 , 10 , 10 ); g.fillOval( 122 , 355 , 10 , 10 ); g.fillOval( 484 , 355 , 10 , 10 ); g.fillOval( 303 , 145 , 10 , 10 ); g.fillOval( 303 , 503 , 10 , 10 );
for ( int i= 0 ;i<allChess.length;i++) { for ( int j= 0 ;j<allChess.length;++j) { //下黑子 if (allChess[i][j]== 1 ) { int tempX=i* 30 + 38 ; //左边界到棋盘的距离 int tempY=j* 30 + 58 ; //上边界到棋盘的距离 g.setColor(Color.black); g.fillOval(tempX- 13 ,tempY- 13 , 25 , 25 );
}
//下白子 if (allChess[i][j]== 2 ) { int tempX=i* 30 + 38 ; int tempY=j* 30 + 58 ; g.setColor(Color.white); g.fillOval(tempX- 13 ,tempY- 13 , 25 , 25 );
}
} } //最后棋子用红框表示 if (chessSum> 0 ) { g.setColor(Color.red); g.drawRect(x* 30 + 38 - 13 , y* 30 + 58 - 13 , 25 , 25 ); } //g.setColor(Color.red); //g.drawRect(x1*30+38-13, y1*30+58-13, 25,25); chessSum++; System.out.println( "总数为" +(chessSum- 1 )); }
public void mouseClicked(MouseEvent e) { x=e.getX(); y=e.getY(); //System.out.println("x="+e.getX()+" "+"y="+e.getY()); if (canPlay) {
if (x>= 38 &&x<= 588 &&y>= 58 &&y<= 620 ) {
x=(x- 38 )/ 30 ; //38起点,适应19x19 y=(y- 58 )/ 30 ; if (allChess[x][y]== 0 ){ //此点没有棋子,才可下 //判断该由哪方下棋 if (flag== 1 ) { //'1'代表由黑方下 allChess[x][y]= 1 ; //'1'表示此处放黑棋 this .checkFive(); //判断黑棋是否五子相连
flag= 2 ; } else { allChess[x][y]= 2 ; //'2'表示此处放白棋 this .checkFive(); //判断白棋是否五子相连
flag= 1 ; //'1'代表由黑方下 } this .repaint(); } } }
}
//判断五子相连 public void checkFive(){ //把要下的棋子颜色保存 int color=allChess[x][y]; //计算已连棋子个数 int count= 1 ;
//判断横向右边是否五子 for ( int i= 1 ;i< 5 ;i++) { if (x>= 15 ) break ; if (color==allChess[x+i][y]) { count++; } checkWin(count); } count= 1 ;
//判断横向左边是否五子 for ( int i= 1 ;i< 5 ;i++) { if (x<= 3 ) //当棋子左边无法连成五子,直接退出 break ;
if (color==allChess[x-i][y]) { count++; } checkWin(count); } count= 1 ;
//判断竖向下边是否五子 for ( int i= 1 ;i< 5 ;i++) { if (y>= 15 ) //当棋子左边无法连成五子,直接退出 break ; if (color==allChess[x][y+i]) { count++; } checkWin(count); } count= 1 ;
//判断竖向上边是否五子 for ( int i= 1 ;i< 5 ;i++) { if (y<= 3 ) //当棋子竖向上边无法连成五子,直接退出 break ; if (color==allChess[x][y-i]) { count++; } checkWin(count); } count= 1 ;
//判断右斜上边是否五子 for ( int i= 1 ;i< 5 ;i++) { if (y<= 3 ||x>= 15 ) //当棋子右斜上边无法连成五子,直接退出 break ; if (color==allChess[x+i][y-i]) { count++; } checkWin(count); } count= 1 ;
//判断左斜向下边是否五子 for ( int i= 1 ;i< 5 ;i++) { if (x<= 3 ||y>= 15 ) //当棋子左斜向下边无法连成五子,直接退出 break ;
if (color==allChess[x-i][y+i]) { count++; } checkWin(count); } count= 1 ;
//判断左斜向上边是否五子 for ( int i= 1 ;i< 5 ;i++) { if (x<= 3 ||y<= 3 ) break ; if (color==allChess[x-i][y-i]) { count++; } checkWin(count); } count= 1 ;
//判断右斜向下边是否五子 for ( int i= 1 ;i< 5 ;i++) { if (y>= 15 ||x>= 15 ) break ; if (color==allChess[x+i][y+i]) { count++; } checkWin(count);
} count= 1 ;
}
public void mouseEntered(MouseEvent e) { x1=e.getX(); y1=e.getY(); if (x1>= 38 &&x1<= 588 &&y1>= 58 &&y1<= 620 ) {
setCursor( new Cursor(Cursor.HAND_CURSOR));
}
}
public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent e) {
}
public void checkWin( int count) {
if (count>= 5 ) { //五子相连
if (allChess[x][y]== 1 ) { JOptionPane.showMessageDialog( this , "黑方胜出!!!!!!" ); } if (allChess[x][y]== 2 ) { JOptionPane.showMessageDialog( this , "白方胜出!!!!!!" ); } canPlay= false ; //游戏结束 } }
//重新开始 public void restartGame(){ for ( int i= 0 ;i<allChess.length;i++) { for ( int j= 0 ;j<allChess.length;j++) { allChess[i][j]= 0 ; } } flag= 1 ; //默认开始是黑旗先下 canPlay= true ; repaint(); }
//悔棋 public void goback() { if (allChess[x][y]!= 0 ) { //当棋盘有棋子,才能悔棋 allChess[x][y]= 0 ; if (flag== 1 ) { flag= 2 ; repaint(); } else { flag= 1 ; repaint(); } } }
//按钮事件监听器内部类 class MybuttonListener implements ActionListener{ public void actionPerformed(ActionEvent e) { if (e.getSource()==restart) { restartGame(); } if (e.getSource()==withdraw) { goback(); } if (e.getSource()==exit) { System.exit( 0 ); } } } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://blog.csdn.net/qq_39754389/article/details/79187794