Reading in level data from a text file

So I currently have code setup to read the following from a text file and populate a 2D array:

####
#@.#
#$ #
#  #
# $#
#. #
#  #
####

Now I’m looking to store multiple levels in the same text file, and want to know how I might read in the desired level, as well as some other information in a header.

In this case if I select a GUI Button “Level 2” - I’d like it to load the appropriate level and tell my matrix how many rows/columns to build (mapMatrix = new char[rows,cols];).

I can format the header in whatever way to make the process easier. Thanks!

Level 1, Rows 8, Columns 4:
####
#@.#
#$ #
#  #
# $#
#. #
#  #
####

Level 2, Rows 8, Columns 5:
#####
#  .#
# $ #
##@##
##$##
#   #
#.  #
#####

Level 3, Rows 5, Columns 7:
#######
#. # .#
# $@$ #
#  #  #
#######

Hey Jesse Alexander,

There are two different ways you could go here, both are good, it’s just up to your preferences.

The first way would be to add a special character at the end of each level’s information set, and read until you hit that, then add the information to an array, and keep going. The text file would look something like this:

Level 1, Rows 8, Columns 4:
####
#@.#
#$ #
#  #
# $#
#. #
#  #
####
Ω
 
Level 2, Rows 8, Columns 5:
#####
#  .#
# $ #
##@##
##$##
#   #
#.  #
#####
Ω
 
Level 3, Rows 5, Columns 7:
#######
#. # .#
# $@$ #
#  #  #
#######
Ω

The second would be to make your text file into an XML file.

<?xml version="1.0" encoding="UTF-8"?>

<Levels>
	<Level Number="1" Rows="8" Columns="4">
		<Design>
		####
		#@.#
		#$ #
		#  #
		# $#
		#. #
		#  #
		####
		</Design>
	</Level>
	<Level Number="2" Rows="8" Columns="5">
		<Design>
		#####
		#  .#
		# $ #
		##@##
		##$##
		#   #
		#.  #
		#####
		</Design>
	</Level>
	<Level Number="3" Rows="5" Columns="7">
		<Design>
		#######
		#. # .#
		# $@$ #
		#  #  #
		#######
		</Design>
	</Level>
</Levels>

And, finally, to read the information depends on which way you choose. If you use a text file, I’d search the second line in each search, and populate three ints with the only three numbers you’ll find in it. That is to say, search the second line for numbers, and pass those to your variables.

I hope this helps! If I didn’t explain something well, please let me know! -Gibson