Save File for a Toy Project

Hi, I'm new to the forum. I have some basic understanding (very basic)
of Ruby, C/C++, Java, and Python. I just have a question or two about
how to implement a nice feature for a bash/prompt game.

I was learning about File I/O in Ruby when I realized that (if I could
do it) it would be a nice idea for use in a console RPG (role-playing
game) I'm making. (Mind you, I know that this isn't "true" game
development; it's just a toy project to build skills and examples of
Ruby for me). So, what I want to do is this:

1) Write/overwrite a new save file for each game the user decides to
save at "save game?" prompts.
2) Read in a save file with the last state of the game and player info.
3) Use this data to continue the game.

I've done a little bit of file I/O in each language I (barely) know, but
I like Ruby because it's simple, fun, and for what I'm doing, fast. I'm
mostly self-taught in computer science; I only took one CS course (intro
to CS w/ Python) at my local college, but I still love the subject.

Does anybody know how I could implement this idea, or at least have
some good psuedocode
or an algorithm or something I could learn more about it with?
Thanks in advance!

···

--
*~~~Jamey Bryce*

ACK! The formatting of the last message was horrible. Sorry guys.... here
it is in bigger font:

Hi, I'm new to the forum. I have some basic understanding (very basic)
of Ruby, C/C++, Java, and Python. I just have a question or two about
how to implement a nice feature for a bash/prompt game.

I was learning about File I/O in Ruby when I realized that (if I could
do it) it would be a nice idea for use in a console RPG (role-playing
game) I'm making. (Mind you, I know that this isn't "true" game
development; it's just a toy project to build skills and examples of
Ruby for me). So, what I want to do is this:

1) Write/overwrite a new save file for each game the user decides to
save at "save game?" prompts.
2) Read in a save file with the last state of the game and player info.
3) Use this data to continue the game.

I've done a little bit of file I/O in each language I (barely) know, but
I like Ruby because it's simple, fun, and for what I'm doing, fast. I'm
mostly self-taught in computer science; I only took one CS course (intro
to CS w/ Python) at my local college, but I still love the subject.

Does anybody know how I could implement this idea, or at least have
some good psuedocode
or an algorithm or something I could learn more about it with?
Thanks in advance!

···

On Tue, Aug 9, 2016 at 4:29 PM, Jamie C. Bryse <jbxhalite5@gmail.com> wrote:

Hi, I'm new to the forum. I have some basic understanding (very basic)
of Ruby, C/C++, Java, and Python. I just have a question or two about
how to implement a nice feature for a bash/prompt game.

I was learning about File I/O in Ruby when I realized that (if I could
do it) it would be a nice idea for use in a console RPG (role-playing
game) I'm making. (Mind you, I know that this isn't "true" game
development; it's just a toy project to build skills and examples of
Ruby for me). So, what I want to do is this:

1) Write/overwrite a new save file for each game the user decides to
save at "save game?" prompts.
2) Read in a save file with the last state of the game and player info.
3) Use this data to continue the game.

I've done a little bit of file I/O in each language I (barely) know, but
I like Ruby because it's simple, fun, and for what I'm doing, fast. I'm
mostly self-taught in computer science; I only took one CS course (intro
to CS w/ Python) at my local college, but I still love the subject.

Does anybody know how I could implement this idea, or at least have some good psuedocode
or an algorithm or something I could learn more about it with?
Thanks in advance!

--
*~~~Jamey Bryce*

--
*~~~Jamey Bryce*

The main thing to do is to have the complete state of your game contained
in a single GameState object (i.e. don't rely on any global data at all).
Your main game should look like

class Game
  def initialize
    @state = GameState.new(...)
  end

  def play_turn
    display state
    get user input
    perform calculations
    update state
  end
end

# main
game = Game.new
until game.is_over? do
  game.play_turn
end

Now the rest is relatively straightforward. In class GameState, define
to_json and from_json methods, and in class Game, define load and save
methods that return @state as a json object or convert a json object to a
GameState and replace @state with it, respectively. play_turn will just
continue from whatever state you currently have in @state, not caring
whether that was just loaded from a file or was the result of playing the
current session.

martin

···

On Tue, Aug 9, 2016 at 3:08 PM, Jamie C. Bryse <jbxhalite5@gmail.com> wrote:

ACK! The formatting of the last message was horrible. Sorry guys.... here
it is in bigger font:

Hi, I'm new to the forum. I have some basic understanding (very basic)
of Ruby, C/C++, Java, and Python. I just have a question or two about
how to implement a nice feature for a bash/prompt game.

I was learning about File I/O in Ruby when I realized that (if I could
do it) it would be a nice idea for use in a console RPG (role-playing
game) I'm making. (Mind you, I know that this isn't "true" game
development; it's just a toy project to build skills and examples of
Ruby for me). So, what I want to do is this:

1) Write/overwrite a new save file for each game the user decides to
save at "save game?" prompts.
2) Read in a save file with the last state of the game and player info.
3) Use this data to continue the game.

I've done a little bit of file I/O in each language I (barely) know, but
I like Ruby because it's simple, fun, and for what I'm doing, fast. I'm
mostly self-taught in computer science; I only took one CS course (intro
to CS w/ Python) at my local college, but I still love the subject.

Does anybody know how I could implement this idea, or at least have some good psuedocode
or an algorithm or something I could learn more about it with?
Thanks in advance!

On Tue, Aug 9, 2016 at 4:29 PM, Jamie C. Bryse <jbxhalite5@gmail.com> > wrote:

Hi, I'm new to the forum. I have some basic understanding (very basic)
of Ruby, C/C++, Java, and Python. I just have a question or two about
how to implement a nice feature for a bash/prompt game.

I was learning about File I/O in Ruby when I realized that (if I could
do it) it would be a nice idea for use in a console RPG (role-playing
game) I'm making. (Mind you, I know that this isn't "true" game
development; it's just a toy project to build skills and examples of
Ruby for me). So, what I want to do is this:

1) Write/overwrite a new save file for each game the user decides to
save at "save game?" prompts.
2) Read in a save file with the last state of the game and player info.
3) Use this data to continue the game.

I've done a little bit of file I/O in each language I (barely) know, but
I like Ruby because it's simple, fun, and for what I'm doing, fast. I'm
mostly self-taught in computer science; I only took one CS course (intro
to CS w/ Python) at my local college, but I still love the subject.

Does anybody know how I could implement this idea, or at least have some good psuedocode
or an algorithm or something I could learn more about it with?
Thanks in advance!

--
*~~~Jamey Bryce*

--
*~~~Jamey Bryce*

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Hey Jamie,

I won't boast about my code, but it occasionally works. I tend to use JSON as a datastore as it's portable and I want to move into MongoDB sooner or later.

Give this a look, holler if you have questions.

https://github.com/LeamHall/CT_Character_Generator/blob/master/bin/add_characters.rb

Leam

···

On 08/09/16 18:08, Jamie C. Bryse wrote:

ACK! The formatting of the last message was horrible. Sorry guys....
here it is in bigger font:

Hi, I'm new to the forum. I have some basic understanding (very basic)
of Ruby, C/C++, Java, and Python. I just have a question or two about
how to implement a nice feature for a bash/prompt game. I was learning
about File I/O in Ruby when I realized that (if I could do it) it would
be a nice idea for use in a console RPG (role-playing game) I'm making.
(Mind you, I know that this isn't "true" game development; it's just a
toy project to build skills and examples of Ruby for me). So, what I
want to do is this: 1) Write/overwrite a new save file for each game the
user decides to save at "save game?" prompts. 2) Read in a save file
with the last state of the game and player info. 3) Use this data to
continue the game. I've done a little bit of file I/O in each language I
(barely) know, but I like Ruby because it's simple, fun, and for what
I'm doing, fast. I'm mostly self-taught in computer science; I only took
one CS course (intro to CS w/ Python) at my local college, but I still
love the subject. Does anybody know how I could implement this idea, or
at least have some good psuedocode
or an algorithm or something I could learn more about it with? Thanks in
advance!

ACK! The formatting of the last message was horrible. Sorry guys....
here it is in bigger font:

Please don't bother about font size, the post was just fine;
in all mailing lists I know of, the preferred format is
plain text; HTML emails are rather frowned upon.

So, what I want to do is this:
1) Write/overwrite a new save file for each game the
user decides to save at "save game?" prompts. 2) Read in a save file
with the last state of the game and player info. 3) Use this data to
continue the game.

You might also consider using YAML (instead of json), which in
"Ruby land" is an often used format for config files, etc.

A common approach would be to create a Hash containing all the
relevant data, use `to_yaml` to serialize the data into a string,
and then write the string to a file.

You might also have a look at YAML::Store or PStore, which would
handle the IO for you and so avoid the need for doing "manual"
file operations.

Regards,
Marcus

···

Am 10.08.2016 um 00:08 schrieb Jamie C. Bryse:

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

I forgot to mention that I usually prefer YAML::Store,
since the data is persisted in a way that is human readable.
Btw, the way how both these libraries are used is the same.

Here a short example for YAML::Store:

require "yaml/store"

filename = "my_data.yaml"
store = YAML::Store.new(filename)

# store some data

store.transaction do
  store[:data] = {first: 1, second: 2}
end

# read data

data = store.transaction { store[:data] }
puts "The stored data was: #{data}"

Note that you can also store multiple objects
(under different keys).

See also this blog post:

Regards,
Marcus

···

Am 10.08.2016 um 10:17 schrieb sto.mar@web.de:

You might also have a look at YAML::Store or PStore

--
GitHub: https://github.com/stomar/
PGP: 0x6B3A101A