结对作业:五子棋
本次结对作业我们组做的是五子棋,代码如下:
package com.temptation.gobang; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; public class FiveChessGame { private JFrame frame; private GameMap map; private JMenuBar bar; private JMenu game; private JMenuItem[] items; private static final int BOARD_SIZE = 15; private static final int ROW_WIDTH = 36; private static final int SPACE = ROW_WIDTH / 2; private boolean player; private char[][] board; public FiveChessGame() { player = true; board = new char[BOARD_SIZE][BOARD_SIZE]; clearMap(); createGUI(); } private void createGUI() { frame = new JFrame("五子棋"); map = new GameMap(); bar = new JMenuBar(); game = new JMenu("开始"); items = new JMenuItem[2]; game.add(items[0] = new JMenuItem("重新开始")); game.add(items[1] = new JMenuItem("关闭")); } protected void clearMap() { for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { board[i][j] = '+'; } } } public void start() { map.setPreferredSize(new Dimension(ROW_WIDTH * (BOARD_SIZE - 1) + SPACE * 2, ROW_WIDTH * (BOARD_SIZE - 1) + SPACE * 2)); map.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { int xPos = (int) (e.getX() / ROW_WIDTH); int yPos = (int) (e.getY() / ROW_WIDTH); if (board[xPos][yPos] == '+') {// board[xPos][yPos] = player ? 'b' : 'w'; // map.repaint(); if (isWin(xPos, yPos)) { map.repaint(); String str = player ? "黑棋获胜" : "白棋获胜"; JOptionPane.showMessageDialog(null, str, "游戏结束", JOptionPane.PLAIN_MESSAGE); clearMap(); map.repaint(); player = true; } else { player = player ? false : true; } } } }); // items[0].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String str = "是否重新开始"; // if (JOptionPane.showConfirmDialog(null, str, "重新开始", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) { clearMap(); // map.repaint(); // player = true; // } } }); items[1].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String str = "是否关闭游戏"; if (JOptionPane.showConfirmDialog(null, str, "关闭游戏", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) { System.exit(0); } } }); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { String str = "是否关闭游戏"; if (JOptionPane.showConfirmDialog(null, str, "关闭游戏", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) { System.exit(0); } } }); bar.add(game); frame.setJMenuBar(bar); frame.add(map); frame.pack(); frame.setLocation(250, 50); frame.setResizable(false); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); } protected boolean find(int x, int y) { int i, num = 1; char temp = player ? 'b' : 'w'; for (i = x + 1; i < 15; i++) { if (board[i][y] == temp) { num++; if (num == 5) { return true; } } else { break; } } for (i = x - 1; i >= 0; i--) { if (board[i][y] == temp) { num++; if (num == 5) { return true; } } else { break; } } return false; } protected boolean find2(int x, int y) { int i, num = 1; char temp = player ? 'b' : 'w'; for (i = y + 1; i < 15; i++) { if (board[x][i] == temp) { num++; if (num == 5) { return true; } } else { break; } } for (i = y - 1; i >= 0; i--) { if (board[x][i] == temp) { num++; if (num == 5) { return true; } } else { break; } } return false; } protected boolean find3(int x, int y) { int i, j, num = 1; char temp = player ? 'b' : 'w'; for (i = x + 1, j = y + 1; i < 15 && j < 15; i++, j++) { if (board[i][j] == temp) { num++; if (num == 5) { return true; } } else { break; } } for (i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--) { if (board[i][j] == temp) { num++; if (num == 5) { return true; } } else { break; } } return false; } protected boolean find4(int x, int y) { int i, j, num = 1; char temp = player ? 'b' : 'w'; for (i = x + 1, j = y - 1; i < 15 && j >= 0; i++, j--) { if (board[i][j] == temp) { num++; if (num == 5) { return true; } } else { break; } } for (i = x - 1, j = y + 1; i >= 0 && j < 15; i--, j++) { if (board[i][j] == temp) { num++; if (num == 5) { return true; } } else { break; } } return false; } protected boolean isWin(int x, int y) { return (find(x, y) || find2(x, y) || find3(x, y) || find4(x, y)); } private class GameMap extends JPanel { private static final long serialVersionUID = 16578987565248L; public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(new Color(200, 100, 50)); g.fillRect(0, 0, ROW_WIDTH * (BOARD_SIZE + 1), ROW_WIDTH * (BOARD_SIZE + 1)); g.setColor(Color.black); for (int i = 0; i < BOARD_SIZE; i++) { g.drawLine(SPACE + ROW_WIDTH * i, SPACE, SPACE + ROW_WIDTH * i, SPACE + ROW_WIDTH * (BOARD_SIZE - 1)); } for (int i = 0; i < BOARD_SIZE; i++) { g.drawLine(SPACE, SPACE + ROW_WIDTH * i, SPACE + ROW_WIDTH * (BOARD_SIZE - 1), SPACE + ROW_WIDTH * i); } g.fillOval(SPACE + 3 * ROW_WIDTH - 3, SPACE + 3 * ROW_WIDTH - 3, 7, 7); g.fillOval(SPACE + 11 * ROW_WIDTH - 3, SPACE + 3 * ROW_WIDTH - 3, 7, 7); g.fillOval(SPACE + 3 * ROW_WIDTH - 3, SPACE + 11 * ROW_WIDTH - 3, 7, 7); g.fillOval(SPACE + 7 * ROW_WIDTH - 3, SPACE + 7 * ROW_WIDTH - 3, 7, 7); g.fillOval(SPACE + 11 * ROW_WIDTH - 3, SPACE + 11 * ROW_WIDTH - 3, 7, 7); for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { if (board[i][j] == 'b') { g.setColor(Color.black); g.fillOval(SPACE + i * ROW_WIDTH - ROW_WIDTH / 2, SPACE + j * ROW_WIDTH - ROW_WIDTH / 2, ROW_WIDTH, ROW_WIDTH); } if (board[i][j] == 'w') { g.setColor(Color.white); g.fillOval(SPACE + i * ROW_WIDTH - ROW_WIDTH / 2, SPACE + j * ROW_WIDTH - ROW_WIDTH / 2, ROW_WIDTH, ROW_WIDTH); } } } } } public static void main(String[] args) { FiveChessGame game = new FiveChessGame(); game.start(); } }
运行截图如下
程序能够良好运行