您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇文章为大家展示了使用java编写一个单机版的五子棋小游戏,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
Chessframe.java
package firstgobang; import javax.swing.JFrame; import javax.swing.WindowConstants; public class ChessFrame extends JFrame{ public ChessBoard chessboard; public ChessFrame(String title){ setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setTitle(title); setVisible(true); setLocationRelativeTo(null); chessboard = new ChessBoard(); add(chessboard); pack(); } public static void main(String[] args) { ChessFrame chessframe = new ChessFrame("单机版五子棋游戏"); } }
ChessBoard.java
package firstgobang; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.Random; import java.util.Stack; import javax.swing.*; public class ChessBoard extends JComponent{ /* *board为1时是白棋,为2时是黑棋,为0时是空 *whitenow为true时,到白棋下,为false时,到黑棋下 *empty为true时,该位置为空,为false,该位置不为空 *win为true,某方胜利,为false,无胜利 *information用来储存消息 */ public static int x_start = 30; public static int y_start = 60; public static int size = 30; public static int radius = 10; private int[][] board = new int[19][19]; private boolean whitenow = false; private boolean empty = true; private boolean win = false; private JTextArea area; private String information=""; private static Stack<Chess> chessstack; //栈 class Chess{ //棋类,用于储存棋子的x,y坐标 int x; int y; public Chess(int x,int y) { this.x=x; this.y=y; } } public ChessBoard() { chessstack = new Stack<>(); area = new JTextArea(5,5); JButton button1 = new JButton("重新开始"); JButton button2 = new JButton("悔棋"); JButton button3 = new JButton("退出"); JButton button4 = new JButton("先手设置"); JButton button5 = new JButton("清空消息"); JPanel panel = new JPanel(); JScrollPane js = new JScrollPane(); button1.setBounds(620,60,100,30); button2.setBounds(620,110,100,30); button3.setBounds(620,160,100,30); button4.setBounds(620,210,100,30); button5.setBounds(620,260,100,30); panel.setBounds(600,310,140,300); js.setBounds(600,310,140,300); panel.setLayout(new BorderLayout()); add(button1); add(button2); add(button3); add(button4); add(button5); panel.add(area); js.getViewport().add(panel); js.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); add(js); button1.addMouseListener(new b1Action()); button2.addMouseListener(new b2Action()); button3.addMouseListener(new b3Action()); button4.addMouseListener(new b4Action()); button5.addMouseListener(new b5Action()); addMouseListener(new theMouseListener()); } public void paint(Graphics g) { super.paint(g); g.setColor(Color.orange); g.fillRect(x_start-size/2,y_start-size/2, size*19, size*19); g.setColor(Color.black); // 横 for (int i = 0; i < 19; i++) { g.drawLine(x_start, y_start + i * size, x_start+18*size, y_start + i * size); g.drawString(((Integer)i).toString(),x_start/2-radius,y_start + i * size); } // 竖 for (int i = 0; i < 19; i++) { g.drawLine(x_start + i * size, y_start, x_start + i * size, y_start+18*size); g.drawString(((Integer)i).toString(),x_start + i * size,y_start/2+radius); } for (int i = 0; i < 19; i++) { for (int j = 0; j < 19; j++) { if (board[i][j] == 1) { g.setColor(Color.white); g.fillOval(x_start-radius + i * size, y_start-radius + j * size, radius*2,radius*2); g.setColor(Color.black); g.drawOval(x_start-radius + i * size, y_start-radius + j * size, radius*2,radius*2); } if (board[i][j] == 2) { g.setColor(Color.black); g.fillOval(x_start-radius + i * size, y_start-radius + j * size, radius*2,radius*2); } } } g.setFont(new Font("微软雅黑",Font.BOLD,15)); g.drawString("现在轮到", 600, 35); if(whitenow==true) { g.setColor(Color.white); g.fillOval(680, 20, 20,20); g.setColor(Color.black); g.drawOval(680, 20, 20,20); } if(whitenow==false) { g.setColor(Color.black); g.fillOval(680, 20, 20,20); } } public Dimension getPreferredSize() { return new Dimension(750,650); } public class theMouseListener implements MouseListener{ //下棋 public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { int x=getx(e.getX()); int y=gety(e.getY()); try { if(board[x][y] !=0)empty = false; } catch (Exception e1) { } if (e.getX() > x_start-size/2 && e.getX() < x_start+size/2+18*size && e.getY() > y_start-size/2 && e.getY() < y_start+size/2+18*size) { if(empty == true) { Chess chess = new Chess(x,y); chessstack.push(chess); if (whitenow == true) { writeinformation("白棋下了"+"("+x+","+y+")"+"的位置"); board[x][y]=1; repaint(); } if (whitenow == false){ writeinformation("黑棋下了"+"("+x+","+y+")"+"的位置"); board[x][y]=2; repaint(); } iswin(whitenow,x,y); if(win==true) { if(whitenow==true) { writeinformation("白棋获胜!"); JOptionPane.showInternalMessageDialog(null, "白棋获胜", "提示框", JOptionPane.INFORMATION_MESSAGE); } else { writeinformation("黑棋获胜!"); JOptionPane.showInternalMessageDialog(null, "黑棋获胜", "提示框", JOptionPane.INFORMATION_MESSAGE); } } if(chessstack.size()==361) { writeinformation("和局"); JOptionPane.showInternalMessageDialog(null, "和局", "提示框", JOptionPane.INFORMATION_MESSAGE); } whitenow=!whitenow; } else { JOptionPane.showInternalMessageDialog(null, "该位置已有棋子!", "提示框", JOptionPane.INFORMATION_MESSAGE); empty=true; } } } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } } class b1Action implements MouseListener{ //重新开始按钮 public void mouseClicked(MouseEvent e) { int a = JOptionPane.showConfirmDialog(null, "你确定重新开始?", "提示框", JOptionPane.YES_NO_OPTION); if(a==0) cleanstart(); } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } } class b2Action implements MouseListener{ //悔棋按钮 public void mouseClicked(MouseEvent e) { int a = JOptionPane.showConfirmDialog(null, "你确定悔棋?", "提示框", JOptionPane.YES_NO_OPTION); if(a==0) { if(chessstack.size()>0) { Chess chess1 = chessstack.pop(); if(whitenow)writeinformation("黑棋悔棋,坐标"+"("+chess1.x+","+chess1.y+")"); if(!whitenow)writeinformation("白棋悔棋,坐标"+"("+chess1.x+","+chess1.y+")"); board[chess1.x][chess1.y]=0; whitenow=!whitenow; repaint(); } else { JOptionPane.showInternalMessageDialog(null, "不能在悔棋了!", "提示框", JOptionPane.INFORMATION_MESSAGE); } } } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } } class b3Action implements MouseListener{ //退出按钮 public void mouseClicked(MouseEvent e) { int a = JOptionPane.showConfirmDialog(null, "你确定退出游戏?", "提示框", JOptionPane.YES_NO_OPTION); if(a==0) { System.exit(0); } } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } } class b4Action implements MouseListener{ //先手设置按钮 public void mouseClicked(MouseEvent e) { if(chessstack.size()==0) { Object[] possibleValues = { "白棋", "黑棋", "随机" }; Object a = JOptionPane.showInputDialog(null, "选择先手的棋子", "提示框", JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]); if(a=="白棋") whitenow=true; if(a=="黑棋") whitenow=false; if(a=="随机") { Random random = new Random(); int b =random.nextInt(2); if(b==0)whitenow=true; if(b==1)whitenow=false; } repaint(); } else { JOptionPane.showInternalMessageDialog(null, "战局已经开始,不能设置", "提示框", JOptionPane.INFORMATION_MESSAGE); } } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } } class b5Action implements MouseListener{ //清空消息按钮 public void mouseClicked(MouseEvent e) { int a = JOptionPane.showConfirmDialog(null, "你确定清空所有消息?", "提示框", JOptionPane.YES_NO_OPTION); if(a==0) { information=""; area.setText(information); } } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } } public void writeinformation(String infor){ //消息写入 information +=infor+"\n"; area.setText(information); } public boolean iswin(boolean whitenow,int startx,int starty) { //胜利判断 int color = whitenow?1:2; int count = 1; int x=1; int y=1; //横 while((startx-x)>-1 && board[startx-x][starty]==color) { count++; x++; } x=y=1; while((startx+x)<19 && board[startx+x][starty]==color) { count++; x++; } if(count>=5) { return win = true; } count=x=y=1; //竖 while((starty-y)>-1 && board[startx][starty-y]==color) { count++; y++; } x=y=1; while((starty+y)<19 && board[startx][starty+y]==color) { count++; y++; } if(count>=5) { return win = true; } count=x=y=1; //45右斜 while((startx+x)<19 && (starty-y)>-1 && board[startx+x][starty-y]==color) { count++; x++; y++; } x=y=1; while((startx-x)>-1 && (starty+y)<19 && board[startx-x][starty+y]==color) { count++; x++; y++; } if(count>=5) { return win = true; } count=x=y=1; //135左斜 while((startx-x)>0 && (starty-y)>0 && board[startx-x][starty-y]==color) { count++; x++; y++; } x=y=1; while((startx+x)<19 && (starty+y)<19 && board[startx+x][starty+y]==color) { count++; x++; y++; } if(count>=5) { return win = true; } return false; } private void cleanstart() { //清理棋盘 for(int i=0;i<19;i++) { for(int j=0;j<19;j++) { board[i][j]=0; } } win=false; chessstack.clear(); writeinformation("重新开始战局!"); repaint(); } private int getx(int x) { //x归位 x -=x_start; if(x%size<(size/2)) { return x/size; } else { return x/size+1; } } private int gety(int y) { //y归位 y -=y_start; if(y%size<(size/2)) { return y/size; } else { return y/size+1; } } }
上述内容就是使用java编写一个单机版的五子棋小游戏,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。