This is Part 2 of implementing my A* algorithm! Intro: A* Pathfinding – Intro

I start off with implementing the ability to read integers off a text file. The goal is to process multiple lines of integers, separated by spaces. Reading user input in C has always been one of my fears, and the scan/get functions are a little tricky, which is why it’s always good to work with them. As it turns out, because lines have variable length, I need to iterate through the given file twice, once to count each line’s length, and again to actually track the values. The result is our first function, boardToArray, which is an array of integer arrays.

Not too difficult, and we have the first step of our drawing program.

The next step is to convert this array into pixels and print that out as a .ppm. In order to make handling our array easier, I need to somehow pass in the number of lines/tiles per line, so I change my boardToArray function to output a custom struct containing all that. One benefit is I can modify this struct later to contain a highlight array also. To handle colors, I also creat a color struct with R,G,B values.

In addition, instead of having a fixed tile width and height, my function will take in parameters for them. In the future these parameters may become optional, but I haven’t learned how to do that yet, so it’s just something to keep in mind. For now my drawBoard function will handle -1 as no tile (white) and 0 as empty tile (grey). I may add borders or flashier tiles later, but that is a lot more time consuming so it will end up being in a different project.

Once again, very straightforward implementation. One issue is that I iterate through the width and height (in pixels) of each tile, as well as the width and height (in tiles) of the board, so that I end up with operations on the order of n^4. This might not really be an issue since it’s really n^2*m^2 for pixels and tiles, and that I’m only making one calculation per pixel in my image. It might be faster if I was writing into a different kind of image file, but for now .ppm is all I have.

I also implement a boardFree function to free memory from a board. Take a step back to see my completed work:
Draw.c file
– board and color structs
– boardToArray, takes a file name and spits out a board
– drawBoard, takes a board, tile width/height, and prints out ppm file
– boardFree, frees memory allocated to a board

Before advancing, a little playing around with different boards starts to crash my program. Tinkering around reveals I allocated insufficient memory to my array–using number of tiles instead of number of tiles * sizeOf(int). A reminder that alloc functions are sneaky and might now show up even when your code runs!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s