Java五子棋设计流程:
1.创建窗口和设计一个棋盘界面
2.实现鼠标点击,棋子出现,黑白棋轮流下
3.能够判断输赢
4.添加按钮功能
实现结果图:
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); } } } }
The above is the detailed content of How to implement the stand-alone version of backgammon in Java. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),