首页 > 代码库 > 五子棋游戏源码
五子棋游戏源码
这是五子棋的最终效果图,棋盘以及棋子均是程序作图。下边奉献个人的源代码。
GoBangGUI.java 这个源文件主要是界面的实现
package game.gobang; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.Box; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; public class GoBangGUI extends JFrame{ private JPanel mainframe; private GoBangBoard board; private JPanel control; private GridLayout grid; private BorderLayout border; private JButton start; private JButton end; private JLabel label; private int row; private int column; private Gobang gobang; private MouseAdapter mouselistener; private boolean flag; private boolean ok; private ActionListener actionlistener; public GoBangGUI(){ this("GoBang Game", 10, 10); } public GoBangGUI(String title, int row, int column){ super(title); // this.setUndecorated(true); this.row = row; this.column = column; this.flag = false; this.ok = false; initial(); setSize(600, 600); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationByPlatform(true); this.setVisible(true); this.requestFocus(); } private void initial() { createComponent(); layOut(); listener(); } private void createComponent() { mainframe = new JPanel(); control = new JPanel(); border = new BorderLayout(); grid = new GridLayout(); start = new JButton("Start"); label = new JLabel(); gobang = new Gobang(row, column); board = new GoBangBoard(gobang, row, column); end = new JButton("Exit"); // gobang.getData()[0][0] = GobangColor.WHITE; } private void layOut() { this.getContentPane().add(mainframe); // setBackpicture(); // mainframe.setBackground(Color.yellow); mainframe.setLayout(border); mainframe.add(board, BorderLayout.CENTER); // board.setBounds(0, 0, 500, 500); mainframe.add(control, BorderLayout.EAST); Box ve = Box.createVerticalBox(); ve.add(start); ve.add(Box.createVerticalStrut(50)); end.setSize(start.getWidth(), start.getHeight()); ve.add(end); control.add(ve); } private void setBackpicture() { ImageIcon a = new ImageIcon("/home/qcq/1.jpg"); JLabel p = new JLabel(a); p.setBounds(0, 0, a.getIconWidth(), a.getIconHeight()); this.getLayeredPane().add(p, Integer.MIN_VALUE); } /* * 判断鼠标落入棋盘窗格的位置索引 (x, y)为当前鼠标的坐标。 */ private Place judgePlace(int indexx, int indexy, int x, int y, int squre){ for (int i = 0; i < row; i++){ for (int j = 0; j < column; j++){ if (x >= indexx + j * squre && x <= indexx + (j + 1) * squre &&y >= indexy + i * squre && y <= indexy + (i + 1) * squre){ return new Place(i, j); } } } return null; } private void listener() { mouselistener = new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); /* * (25, 10) is a special coordinate. the board draw begins here, * when the board added to the this JFrame the begin change to * (25, 10 + 25), However, I do not know why. just keep it; * */ if (ok){ Place temp = judgePlace(25, 10, x, y, board.getSqure()); System.out.println(temp); if (flag){ if (!gobang.check(temp.getX(), temp.getY())){ JOptionPane.showMessageDialog(mainframe, "Change another place"); } else { gobang.setChess(temp.getX(), temp.getY(), GobangColor.WHITE); repaint(); if (gobang.isSuccess(new Place(0, 0), new Place(row - 1, column - 1), GobangColor.WHITE)){ // JOptionPane.showMessageDialog(mainframe, "The white player is win"); int choice = JOptionPane.showConfirmDialog(mainframe, "The white player is win", "DO you want try again", JOptionPane.YES_NO_OPTION); if (choice == JOptionPane.YES_OPTION){ gobang.initial(); ok = false; repaint(); } } flag = false; } } else { if (!gobang.check(temp.getX(), temp.getY())){ JOptionPane.showMessageDialog(mainframe, "Change another place"); } else { gobang.setChess(temp.getX(), temp.getY(), GobangColor.BLACK); repaint(); if (gobang.isSuccess(new Place(0, 0), new Place(row - 1, column - 1), GobangColor.BLACK)){ // JOptionPane.showMessageDialog(mainframe, "The black player is win"); int choice = JOptionPane.showConfirmDialog(mainframe, "The black player is win", "DO you want try again", JOptionPane.YES_NO_OPTION); if (choice == JOptionPane.YES_OPTION){ gobang.initial(); ok = false; repaint(); } } flag = true; } } } else { JOptionPane.showMessageDialog(mainframe, "please start the game"); } } }; actionlistener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (((JButton)(e.getSource())).getText().equals("Start")){ ok = true; } else if (((JButton)(e.getSource())).getText().equals("Exit")){ System.exit(0); } } }; board.addMouseListener(mouselistener); start.addActionListener(actionlistener); end.addActionListener(actionlistener); } public static void main(String[] args) { GoBangGUI game = new GoBangGUI(null, 20, 20); } }
GoBangBoard.java本文件是棋盘的绘图
package game.gobang; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.LayoutManager; import javax.swing.JPanel; public class GoBangBoard extends JPanel { private Gobang gobang; private int row; private int column; private int squre; public GoBangBoard() { //this.setBounds(x, y, width, height); } public GoBangBoard(Gobang gobang, int row, int column){ this.gobang = gobang; this.column = column; this.row = row; } public int getSqure(){ return squre; } public void paint(Graphics g){ Graphics2D gg = (Graphics2D)g; squre = (this.getWidth() - 50) / Math.max(row, column); /** * from the point (25, 10) draw the game's background. * */ /* * below draw the background gird. * */ int x = 25; int y = 10; gg.setStroke(new BasicStroke(0.5f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,new float[]{5f, 5f},0f)); gg.setColor(Color.blue); for (int i = 0; i < row; i++){ for (int j = 0; j < column; j++){ gg.drawRect(x + j * squre, y + i * squre, squre, squre); } } for (int i = 0; i < gobang.getData().length; i++){ for (int j = 0; j < gobang.getData()[i].length; j++){ if (GobangColor.WHITE == gobang.getData()[i][j]){ gg.setColor(Color.blue); gg.fillOval(x + j * squre, y + i * squre, squre, squre); } else if (GobangColor.BLACK == gobang.getData()[i][j]){ gg.setColor(Color.BLACK); gg.fillOval(x + j * squre, y + i * squre, squre, squre); } } } gg.setStroke(new BasicStroke(5f)); gg.setColor(Color.blue); gg.drawRect(x - 1, y - 1, squre * column + 2, squre * row + 2); gg.dispose(); } }GobangColor.java 本文件是棋子颜色的枚举定义。
package game.gobang; public enum GobangColor { BLACK, WHITE, NULL }
Placejava 本文件是关于棋子位置的定义
package game.gobang; /** * here lays the snake's body coordinate information. * */ public class Place { private int x; private int y; public Place(){ this(0, 0); } public Place(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } @Override public String toString() { return "Place [x=" + x + ", y=" + y + "]"; } public void setY(int y) { this.y = y; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + x; result = prime * result + y; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Place other = (Place) obj; if (x != other.x) return false; if (y != other.y) return false; return true; } }
Gobang.java本文件是五子棋实现的核心逻辑代码
package game.gobang; import java.util.Scanner; /** * Question 19、利用二维数组实现五子棋下棋功能 * */ public class Gobang { private GobangColor[][] data; private int row = 10; private int column = 10; public Gobang() { /* * The default checkerboard is 10 * 10 boxes; * */ this(10, 10); initial(); } /* * Initial the Game * */ public void initial(){ for (int i = 0; i < row; i++){ for (int j = 0; j < column; j++){ data[i][j] = GobangColor.NULL; } } } /* * The piece move * */ public void setChess(int row, int column,GobangColor color){ data[row][column] = color; } public Gobang(int row, int column){ /* * set the Chess squre to row * line * */ this.row = row; this.column = column; data = http://www.mamicode.com/new GobangColor[row][];>五子棋游戏源码
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。