java五子棋老师经常问的问题,java做五子棋的问题,请教高手

package day716;

import java.awt.BorderLayout;

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.image.BufferedImage;

import import import import import import java.util.ArrayList;

import java.util.List;

import javax.imageio.ImageIO;

import javax.swing.*;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.dom4j.Document;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;

import org.import org.import org.import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

public class FiveFrame extends JFrame {

JButton start = new JButton("开始");

JButton back = new JButton("悔棋");

static JButton save = new JButton("保存");

JButton show = new JButton("回放");

JPanel panel1 = new JPanel();

JPanel boardPanel = new BoardPanel();//棋盘面板//棋盘、棋子图片 BufferedImage imgBoard, imgBlack, imgWhite;

int[][] board = new int[15][15];

int currentColor = BLACK;

static final int BLACK = -1;

static final int WHITE = 1;

//保存走棋顺序的集合 List steps = new ArrayList();

public FiveFrame() throws Exception {

imgBoard = ImageIO.read(FiveFrame.class

.getResourceAsStream("/image/chessboard.jpg"));

imgBlack = ImageIO.read(FiveFrame.class

.getResourceAsStream("/image/b.gif"));

imgWhite = ImageIO.read(FiveFrame.class

.getResourceAsStream("/image/w.gif"));

start.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

start();

}

});

back.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

back();

}

});

save.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

try {

save();

} catch (Exception e1) {

e1.printStackTrace();

}

}

});

show.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

show();

}

});

panel1.add(start);

panel1.add(back);

panel1.add(save);

panel1.add(show);

//在棋盘面板上绘制棋盘图片//boardPanel.getGraphics().drawImage(imgBoard,0,0,null);

this.add(panel1, BorderLayout.NORTH);

this.add(boardPanel);

this.setSize(540, 590);

this.setVisible(true);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setTitle("五子棋");

}

public void playChess(int row, int col) {

if (row < 0 || col < 0 || row > 14 || col > 14)

return;

if (board[row][col] != 0)

return;

board[row][col] = currentColor;

currentColor = -currentColor;

//放入走棋步骤集合 steps.add(row + "," + col);

}

//开始新游戏的方法 public void start() {

board = new int[15][15];//新棋盘 currentColor = BLACK;//黑棋先行 boardPanel.repaint();//重新绘制面板 }

//悔棋方法 public void back() {

if (steps.isEmpty())

return;

String s = steps.remove(steps.size() - 1);

String[] arr = s.split(",");

int row = Integer.parseInt(arr[0]);

int col = Integer.parseInt(arr[1]);

board[row][col] = 0;

currentColor = -currentColor;

boardPanel.repaint();

}

//内部类,棋盘panel class BoardPanel extends JPanel {

public BoardPanel() {

this.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {

playChess(e.getY() / 35, e.getX() / 35);

repaint();

}

});

}

//演示Demo public void paintChess(int x, int y) {

int row = y / 35 * 35 + 4;

int col = x / 35 * 35 + 4;

Graphics g = getGraphics();

g.drawImage(imgBlack, col, row, null);

}

//重写绘制panel的方法//在屏幕上绘制自身的方法 protected void paintComponent(Graphics g) {

super.paintComponent(g);//执行父类中的代码将组件绘制//等组件绘制完成,接着绘制自己的内容 g.drawImage(imgBoard, 0, 0, null);

//画走过的棋子 for (int row = 0; row < board.length; row++) {

for (int col = 0; col < board[0].length; col++) {

if (board[row][col] != 0) {

BufferedImage img = imgBlack;

if (board[row][col] == WHITE) {

img = imgWhite;

}

g.drawImage(img, col * 35 + 4, row * 35 + 4, null);//列计算x坐标,行计算y坐标 }

}

}

}

}

//保存游戏 public void save() throws Exception {

System.out.println(steps);

Document doc = null;

OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(

new File("/home/soft13/Desktop/myjava/five.xml"), true));

doc = DocumentHelper.createDocument();

Element five = doc.addElement("five");

int i = 0;

for (String str : steps) {

Element step = five.addElement("step");

Element row = step.addElement("row");

Element col = step.addElement("col");

Element color = step.addElement("color");

str = steps.get(i);

String[] str1 = str.split(",");

step.addAttribute("id", i + "");

row.addText(str1[0]);

col.addText(str1[1]);

i++;

color.addText(-currentColor + "");

currentColor = -currentColor;

boardPanel.repaint();

//writer.close();//writer.flush();

}

OutputFormat f = OutputFormat.createPrettyPrint();

XMLWriter writer = new XMLWriter(out, f);

writer.write(doc);

writer.close();

}

/*public void show() {

try {

SAXParserFactory factory = SAXParserFactory.newInstance();

SAXParser parser = factory.newSAXParser();

//FiveFrame ff1 = new FiveFrame();

DefaultHandler dh = new DefaultHandler();

parser.parse("/home/soft13/Desktop/myjava/five.xml", dh);

} catch (Exception e) {

e.printStackTrace();

}

}*/

public static void main(String[] args) throws Exception {

//new FiveFrame().setVisible(true); FiveFrame ff = new FiveFrame();

ff.setVisible(true);

}

}

class Chess{

public static final String XPATH_STEP="/five/step";

public static final String XPATH_ROL="/five/step/rol";

public static final String XPATH_COW="/five/step/cow";

public static final String XPATH_COLOR="/five/step/color";

private String step;

private String rol;

private String cow;

private String color;

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

public String getCow() {

return cow;

}

public void setCow(String cow) {

this.cow = cow;

}

public String getRol() {

return rol;

}

public void setRol(String rol) {

this.rol = rol;

}

public String getStep() {

return step;

}

public void setStep(String step) {

this.step = step;

}

}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:游动-白 设计师:我叫白小胖 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值