如何将国际象棋游戏的当前状态保存到java中的文件中? [英] How can I save current state of the chess game into a file in java?

查看:125
本文介绍了如何将国际象棋游戏的当前状态保存到java中的文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户点击保存按钮后保存棋盘上棋子的当前位置。我在Board类中实现了'Serializable',但是当我点击save按钮时,会出现一些运行时错误。你能帮我解决这个问题吗?我是这个java GUI的新手。提前谢谢。



我的尝试:



I want to save the current position of the pieces on the board after the user clicks on the save button. I have implemented 'Serializable' in the Board class but when I click on the save button, there are some runtime errors. Can you help me solve this kind of problem? I'm new to this java GUI. Thank you in advance.

What I have tried:

public class Board extends JPanel implements ActionListener, Serializable
{
	JPanel cardss;
	JButton[] square = new JButton[42];
	JButton save = new JButton("Save");
	JButton quit = new JButton("Quit");
	MenuPage menupage;
	Tile tile = Tile.getInstance();
	boolean firstClick = true;
	int count = 0;
	int player = 1;
	JLabel player_label;
	JLabel player_status;
	JFrame frame = new JFrame();
	JFrame f;
	private Board()
	{
		// super("Chess Board");
		
		// setJMenuBar(MyChessBar);
		Dimension boardSize = new Dimension(600, 650); //(width, height)
		Dimension player_panel = new Dimension(180, 650); //(width, height)

		JPanel topPanel = new JPanel(new FlowLayout());
		JPanel boardPanel = new JPanel(new GridLayout(0,7));
		JPanel player_turn = new JPanel(new FlowLayout());
		player_turn.setBackground(Color.yellow);
		
		player_label = new JLabel("Moved : " + count);
		player_status = new JLabel("TURN : PLAYER " + player);
		// JTextField text = new JTextField(15);
		// text.setText( "Player :" + count + " turn ");
		player_turn.add(player_label);
		player_turn.add(player_status);
		save.addActionListener(this);
		quit.addActionListener(this);		
		
		topPanel.add(save);
		topPanel.add(quit);
		// setPreferredSize(boardSize);
		
		for(int i=0; i<42; i++)
		{
			square[i] = new JButton();
			if(i%2==0)
			{
				square[i].setBackground(Color.black);
				tile.setTileFalse(i);
				square[i].addActionListener(this);
			}
			else
			{
				square[i].setBackground(Color.white);
				tile.setTileFalse(i);
				square[i].addActionListener(this);
			}
			boardPanel.add(square[i]);
			tile.setTileLoc(i);

		} // close for loop
		
		boardPanel.setPreferredSize( boardSize );
		player_turn.setPreferredSize( player_panel);
		setLayout(new BorderLayout(2,2));
		add(topPanel, BorderLayout.NORTH);
		add(boardPanel, BorderLayout.WEST);
		add(player_turn, BorderLayout.EAST);
		
		// cardss = new JPanel(new CardLayout());
		// menupage = new MenuPage();
		// cardss.add(menupage, "Menu");
		// add(cardss);
		
		setSize(800,800);
		setVisible(true);

	} //close for board
	
	private static class SingletonHolder {
        private static final Board INSTANCE = new Board();
    }
	
	public static Board getInstance() {
        return SingletonHolder.INSTANCE;
    }

	public void actionPerformed(ActionEvent e)
	{
		String choice_in_game = e.getActionCommand();
		
		if(choice_in_game.equals("Save"))
		{
			
			// save game
			// saveGameDataToFile(chess);
			try
			{
                           FileOutputStream file = new FileOutputStream("chess.txt");
				ObjectOutputStream object = new ObjectOutputStream(file);
				object.writeObject(new Board());
				object.close();

			 // create a dialog Box 
            JDialog d = new JDialog(f, "Chess Game"); 
  
            // create a label 
            JLabel l = new JLabel("Game is successfully saved!"); 
  
            d.add(l); 
  
            // setsize of dialog 
            d.setSize(200,100); 
			d.setLocationRelativeTo(null);
  
            // set visibility of dialog 
            d.setVisible(true); 

			}
			catch(Exception ex)
			{
				ex.printStackTrace();
			}
		}
}

	public void setPiecePic(int i, String name){
		square[i].setIcon(loadImage(name));
		tile.setTileTrue(i);
	}
	
	public void setPieceNull(int i){
		square[i].setIcon(null);
		tile.setTileFalse(i);
	}

	private ImageIcon loadImage(String path){
        Image image = new ImageIcon(this.getClass().getResource(path)).getImage();
        Image scaledImage = image.getScaledInstance(82, 82,  java.awt.Image.SCALE_SMOOTH);
        return new ImageIcon(scaledImage);
    }
	
	public void setEveryPiece(){
		
		setPiecePic(0,"PlusB.png");
		setPiecePic(1,"TriangleB.png");
		setPiecePic(2,"ChevronB.png");
		setPiecePic(3,"SunB.png");
		setPiecePic(4,"ChevronB.png");
		setPiecePic(5,"TriangleB.png");
		setPiecePic(6,"PlusB.png");
		
		setPiecePic(35,"PlusR.png");
		setPiecePic(36,"TriangleR.png");
		setPiecePic(37,"ChevronR.png");
		setPiecePic(38,"SunR.png");
		setPiecePic(39,"ChevronR.png");
		setPiecePic(40,"TriangleR.png");
		setPiecePic(41,"PlusR.png");
		
		tile.setAllPiece();
	}

	public JButton getButton(int x){
		return square[x];
	}
	
	public JButton[] getSquare(){
		return square;
	}
	
	public void getStatus(int count, int player) {
		player_label.setText("Moved: " + count);
		player_status.setText("TURN : PLAYER " + player);	
	}
}

推荐答案

object.writeObject(new Board());



为什么要尝试保存新的Board对象,而不是应用程序中存在的对象?



你还需要准确解释你得到的运行时错误,我们无法猜测。


Why are you trying to save a new Board object, instead of the one that exists in the application?

You also need to explain exactly what runtime errors you get, we cannot guess.


这篇关于如何将国际象棋游戏的当前状态保存到java中的文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆