紫红色五子棋之人人对弈
黑红五子棋之人人对弈
import java.awt.Color; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.Timer; public class FiveChess extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private final int MAPWIDTH = 20, MAPHEIGHT = 20; private int[][] map = new int[MAPWIDTH][MAPHEIGHT]; private MyPaint paint = null; private Timer timer = null; private int delay = 1000; private boolean isColor = true;// 先画黑子 private boolean isAuto = false;// 默认与人下棋 private boolean isStart = false;// 默认不开始 private String notice = "安全落子";// 提示,注意 private Toolkit tk = null; private JMenuItem[][] menuItems = null; public static void main(String[] args) { new FiveChess(); } public FiveChess() { super("黑红代表棋子"); this.setBounds(0, 0, 1200, 730); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // new对象 paint = new MyPaint(); timer = new Timer(delay, paint); tk = Toolkit.getDefaultToolkit(); // 加菜单 addMenu(); // this加组件 this.getContentPane().add(paint); this.setVisible(true); timer.start(); } private void addMenu() { JMenuBar menubar = new JMenuBar(); String[] menustr = { "游戏", "设置", "帮助" }; String[][] itemstrs = { { "新建游戏", "暂停", "|", "结束" }, { "棋子颜色", "人人对弈", "人机对弈" }, { "关于" } }; JMenu[] menus = new JMenu[3]; menuItems = new JMenuItem[menus.length][];// 先建行数 for (int i = 0; i < itemstrs.length; i++) { menus[i] = new JMenu(menustr[i]);// new菜单 menuItems[i] = new JMenuItem[itemstrs[i].length];// new列数 for (int j = 0; j < itemstrs[i].length; j++) { if (itemstrs[i][j].equals("|")) { menus[i].addSeparator();// 加横线 } else { menuItems[i][j] = new JMenuItem(itemstrs[i][j]);// new出菜单项 menus[i].add(menuItems[i][j]); menuItems[i][j].addActionListener(this);// 加监听 } } menubar.add(menus[i]);// 把菜单加到菜单条 } // 加menubar this.setJMenuBar(menubar); } public void initMap() { for (int i = 0; i < map.length; i++) { for (int j = 0; j < map[i].length; j++) { map[i][j] = 0;// 0表示没有棋子,1,表示黑子,2表示红子 } } } public void setAuto(boolean b) { this.isAuto = b; } public void start() { end(); // paint加mouse监听 paint.addMouseListener(paint); isStart = true; // 给map初始化 initMap(); } public void end() { isStart = false; // paint去掉mouse监听 paint.removeMouseListener(paint); } public boolean isLimits(int x, int y) {// 是不是在范围内 if (!(x >= 0 && x < map.length)) { return false; } if (!(y >= 0 && y < map[0].length)) { return false; } return true; } public boolean isExist(int x, int y) {// 是否存在棋子 if (map[x][y] != 0) {// 不存在 return true; } return false; } public boolean isChess(int x, int y, boolean boo) {// 是否落子,确认落什么颜色的子 if (!isLimits(x, y)) { return false; } if (isExist(x, y)) { isColor = !isColor; JOptionPane.showMessageDialog(this, "已经存在!!!"); return false; } if (boo) {// true表示黑子 map[x][y] = 1; } else {// false表示红子 map[x][y] = 2; } tk.beep(); return true; } private class MyPaint extends JPanel implements MouseListener, ActionListener { private static final long serialVersionUID = 1L; private final int X = 2, Y = 2;// 画图的位置 private int width = 30;// 设置像素,给画笔用 private int height = 30;// 这个不是用做变量的,是像素 @Override public void paint(Graphics g) { super.paint(g); // 画框架 g.setColor(Color.blue); for (int i = 0; i < map.length; i++) { g.drawLine(X * width, (i + Y) * height, (X + map.length - 1) * width, (Y + i) * height);// 画行 } g.setColor(Color.blue); for (int i = 0; i < map[0].length; i++) { g.drawLine((X + i) * width, Y * height, (X + i) * width, (Y + map.length - 1) * height);// 画列 } // 画棋子 for (int i = 0; i < map.length; i++) { for (int j = 0; j < map[i].length; j++) { if (map[i][j] == 1) {// 1表示黑子 g.setColor(Color.black); g.fillOval((X + i) * width - (width / 2), (Y + j) * height - (height / 2), width, height); } if (map[i][j] == 2) {// 2表示红子 g.setColor(Color.red); g.fillOval((X + i) * width - (width / 2), (Y + j) * height - (height / 2), width, height); } } } // 移出一些代码 expandPaint(g); } private void expandPaint(Graphics g) {// paint的函数可能超出100行,于是移出一些代码 // 画提示 g.setColor(Color.red); g.drawString(FiveChess.this.notice, 250, 30); // 提示游戏状态 if (isAuto) { g.drawString("提示: 《人机对弈》", 100, 30); } else { g.drawString("提示: 《人人对弈》", 100, 30); } // 落子规则 g.setColor(Color.black); g.drawString("落子规则:", (X + map[0].length) * width, Y * height + height / 2); // 图形描述 for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { g.setColor(Color.red); g.fill3DRect((X + map[0].length + i) * width, (Y + 1 + j) * height, width, height, true); } } g.setColor(Color.blue); g.fill3DRect((X + map[0].length) * width + width / 2, (1 + Y) * height + height / 2, width, height, false); // 文字描述 g.setColor(Color.black); g.drawString("1) 如图(左):棋子在蓝色区域为有效区;", (X + map[0].length + 3) * width, Y * height + height / 2); g.drawString("2) 如图(左):棋子在红色区域为无效区;", (X + map[0].length + 3) * width, (Y + 1) * height + height / 2); // 游戏规则 g.setColor(Color.black); g.drawString("游戏规则:", (X + map[0].length) * width, (4 + Y) * height); // 版权所有 g.setColor(Color.red); g.drawString("版权@1306602-23刘强", (X + map[0].length) * width, (1 + map.length) * height); } @Override public void mouseClicked(MouseEvent e) {// 没用 } @Override public void mousePressed(MouseEvent e) {// 目标 if (!isStart) {// 是否开始或暂停 return; } // 希望得到数组的下标大小 int x = (e.getX() - X * width + width / 2) / width; int y = (e.getY() - Y * height + height / 2) / height; // 判断棋子是否在棋盘落子,是就落子,否则提示危险 if (isAuto) { } else {// 与自己对弈 if (isChess(x, y, isColor)) { FiveChess.this.notice = "安全落子"; } else { FiveChess.this.notice = "危险落子"; } isColor = !isColor; } this.repaint(); } @Override public void mouseReleased(MouseEvent e) {// 目标 } @Override public void mouseEntered(MouseEvent e) {// 没用 } @Override public void mouseExited(MouseEvent e) {// 没用 } @Override public void actionPerformed(ActionEvent e) { } } @Override public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("新建游戏")) { start(); } if (e.getActionCommand().equals("暂停")) { menuItems[0][1].setText("开始"); isStart = false; } if (e.getActionCommand().equals("开始")) { menuItems[0][1].setText("暂停"); isStart = true; } if (e.getActionCommand().equals("结束")) { if (JOptionPane.showConfirmDialog(this, "你确定要离开吗?") == 0) { System.exit(0); } } if (e.getActionCommand().equals("棋子颜色")) { isColor = !isColor; } if (e.getActionCommand().equals("人人对弈")) { isAuto = false; } if (e.getActionCommand().equals("人机对弈")) { isAuto = true; } if (e.getActionCommand().equals("关于")) { JDialog d = new JDialog(this, true); d.setLayout(new FlowLayout()); d.setBounds(100, 100, 300, 100); JLabel lb = new JLabel("版权所有归1306602-23刘强所有!!!"); d.add(lb); d.setVisible(true); } paint.repaint();// 重新刷新 } }
版权声明:本文为博主原创文章,未经博主允许不得转载。