Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Saving & Loading a ChessGame

  1. #1
    Member
    Join Date
    Dec 2013
    Posts
    37
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Saving & Loading a ChessGame

    Hello

    Thanks to the help of a few members on this forum I have managed to finish my school assignment which was to make a chess engine.
    The engine itself is complete but there is one thing left to do. My program has to be able to save a Game to a file and be able to load it back from that file.
    I've never done Java IO (except basic console stuff) . I watched some videos and read some tutorials but before I try it I have a few questions on how saving (serializing an object to a binary file)
    works.

    Question:

    If I have an instance of a class in my case "ChessGame" which creates instances of the figures "Pawn", "Queen" .. etc and holds many variables then if I save an object of ChessGame
    will it automatically save any object generated by ChessGame? So in other words : Can I get away with just saving an instance of ChessGame and not have to save every individual instance of every active figurine on the board?

    Thanks in advance for the answer !


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,721
    My Mood
    Mellow
    Thanks
    172
    Thanked 622 Times in 609 Posts

    Default Re: Saving & Loading a ChessGame

    I'm not sure of the answer to your question, because I haven't studied storing serialized objects, but I think there's an important key to doing what you've asked: Does the object you've chosen to store, ChessGame, completely define the current state of the game so that it can be recreated? Is the board and every location of every piece on or off the board recorded somewhere in a ChessGame object? If the answer is no, then saving the ChessGame object may not be sufficient.

    You give the impression that storing the location of every piece is somehow difficult or that there's a simpler approach. Really? There are at most 32 pieces. Why would storing the current location of 32 pieces be that hard?

  3. #3
    Member
    Join Date
    Dec 2013
    Posts
    37
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Saving & Loading a ChessGame

    Thank you for your answer Greg

    Yes the ChessGame completely defines the current state of the game. It includes all the variables and it creates all the instances of the pieces .
    The thing is that even If I store all the 32 pieces , I still have to store ChessGame because as I mentioned above it contains the current state of the game.
    What i'm insure of is if saving ChessGame automatically saves all those instances that ChessGame creates of all the other classes (pawn , queen , rook etc)
    but I guess I'll have to test that out somehow.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,721
    My Mood
    Mellow
    Thanks
    172
    Thanked 622 Times in 609 Posts

    Default Re: Saving & Loading a ChessGame

    An interesting experiment. Please report back with your results so that those of us who haven't tried to save whole objects can have some understanding of how useful a tool it is.

    I must be from the Brute Force School of Programming for the Small Minded, because I would store the state of every piece to save a game and then use that to recreate the state of the game when resumed. There may be other states to score, who's turn it is, a record of all moves made, the score, time elapsed, etc., but those also seem trivial to store.

    Good luck and keep coding.

  5. #5
    Member
    Join Date
    Dec 2013
    Posts
    37
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Saving & Loading a ChessGame

    Well of course you make a point Greg , but java has shown me miracles that I have not though possible in programming (coming from an embedded background) so I was curious if this could be done
    in one fell swoop. Why toil if the function is right there and can be accomplished with a few lines of code?
    I've done my fair share of manual labor with microcontrollers in C

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,721
    My Mood
    Mellow
    Thanks
    172
    Thanked 622 Times in 609 Posts

    Default Re: Saving & Loading a ChessGame

    I agree completely, and I'm anxious to find out what you learn by trying to store the whole object. It's a noble quest.

  7. #7
    Member
    Join Date
    Dec 2013
    Posts
    37
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Saving & Loading a ChessGame

    Hmm ... It seems to be working . I made a class "Game" and class "Donkey" and Class "IoTryout" which includes the main. The Iotryout makes and instance of Game and Game makes an instance of donkey and passes some parameters to it . I can save the Game instance and when I look inside it it has the parameters of the donkey instance , and I can load it back and access them .

    I'm going to try it now with my ChessGame and see how that works.

    --- Update ---

    Ehm...It sortof works in my chessengine but when I load a previously saved game the pieces are in their correct position and correct rank but they are all black for some reason 0.o
    Any ideas?

     
    File saveFile = new File("ChessSave.txt");
     
     try {
                      FileOutputStream fo = new FileOutputStream(saveFile);
                      ObjectOutputStream output = new ObjectOutputStream(fo);
                      output.writeObject(gameState);
                      output.close();
                      fo.close();
                  } catch (FileNotFoundException ex) {
                      Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
                  } catch (IOException ex) {
                      Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
                  }
     
                      System.out.println("Saved");
                  }
     
                  if(x_pressed > 710 && x_pressed < 770 && y_pressed > 10 && y_pressed < 30){   try {
     
                     FileInputStream fi = new FileInputStream(saveFile);
                     ObjectInputStream input =  new ObjectInputStream(fi);
                     gameState = (ChessGame)input.readObject();
     
     
                  } catch (FileNotFoundException ex) {
                      Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
                  } catch (IOException ex) {
                      Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
                  } catch (ClassNotFoundException ex) {
                      Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
                  }
     
                      System.out.println("Loaded");
                      repaint();
                  }

  8. #8
    Member
    Join Date
    Dec 2013
    Posts
    37
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Saving & Loading a ChessGame

    Oh wow.. I solved it.
    The mistake was in an If statement which painted the pictures . I used the == operand to equate two strings instead of .equals function.

Similar Threads

  1. Getting extra lines when saving and loading into a JTextArea
    By GoodbyeWorld in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 19th, 2013, 01:52 PM
  2. Saving and loading
    By wuppy29 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: June 21st, 2012, 10:38 AM
  3. saving / loading a file
    By Herah in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 9th, 2011, 06:04 AM
  4. Saving and Loading
    By nitwit3 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: August 2nd, 2011, 01:31 PM