Information
Here are a few examples showing 3 successful random walks:
A B C D . . I J . . . E F G H K . . . . . N M L . . . . . O R S . . . . . P Q T . . . . X W V U . . . . Y Z . . . . . . . . . . A . . . . . . . B . . . . . . . C . . . . . . . D . . . . . . . E . . . O P . Z F . . . N Q R Y G . . . M T S X H I J K L U V W A . . . . . . . B . . . . . . . C D . . . . . . . E . . . Y Z . . F . . . X W . . G H . . . V U . J I . O P Q T . K L M N . R S
Here are a few examples showing 3 unsuccessful random walks:
A B . . . . . . D C . . . . . . E J I . . . . . F G H . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A H I J . . . . B G . K . . . . C F . L . . . . D E N M . . . . . . O . . . . . . . P Q R . . . . X Y T S . . . . W V U . . . . A . E F G . . . B C D . H . . . . . K J I . . . . M L U T . . . . N . R S . . . . O P Q . . . . . . . . . . . . . . . . . . . .
This is the prototype of the function that the driver will call:
The showall parameter is a boolean (integer) that tells the function if it should show every board (each new character placement) or just show the final board (like the boards above). If showall is 0 (false), then you will only show the final board. If it is non-zero (true), you will show every board. You can see all of the boards for the first example here: all-sample1.txt. Notice that there is an extra blank line printed after the board when all boards are printed.void random_walk(int showall);
Having the ability to show all of the boards is necessary to debug your programs. Otherwise, you won't know which step is producing incorrect results.
This is the command line to compile it:
Here is a simple driver file: (main.c, HTML Text) The program requires 2 command line parameters:gcc -O -Werror -Wall -Wextra -ansi -pedantic main.c randwalk.c PRNG.c -o randwalk
Examples: (The text from the semi-colon on is just for descriptive purposes)
See the driver to see how it works.randwalk 0 123 ; Just show final board, seed is 123 randwalk 1 123 ; Show all boards, seed is 123 randwalk 1 ; Show all boards, seed is time(NULL) this is random randwalk ; Just show final board, seed is time(NULL) this is random
Random number generator files (put these in the same directory as your C file)
Sample output files:
Final board | All boards |
---|---|
out-sc12.txt | out-sc12a.txt |
out-se22.txt | out-se22a.txt |
out-si213.txt | out-si213a.txt |
out-ss1.txt | out-ss1a.txt |
The seemingly cryptic file names actually reflect what's in the file as well as the command line arguments used to create them. All of the files start with out-s. The first character after that prefix is one of c e i s:
The number in the filename is the seed used for the PRNG. This will allow you to reproduce these boards.
If the letter a follows the seed, it means that all of the boards are in the file. If no letter a follows, the file contains just the final board.
So, to generate the first file on the first line:
To generate the second file on the first line:randwalk 0 12
This file contains 36 test cases: allfiles.zip. The reason there are so many tests is because there are many cases you need to be able to handle and these represent most (if not all) of the cases. There are actually only 13 tests, but there are two files for each test: one file that just contains the final board, and another file that contains all of the boards for the test.randwalk 1 12
This file contains the output from all 36 test cases: output-all.txt.
There is also a stress test here, which you shouldn't test until you've passed all of the above tests. It basically does 500 runs (seeding 1 to 500) and puts the output into a file: stress.txt.
There are batch files (Windows) and bash scripts (Linux and Mac) that will help you to automate the running of all of the tests:
You need to remove the .txt extension on these files for them to work. All of the files are also in allfiles.zip.
Update:
Here's another stress driver: HTML TextRun it like this:
And the output: stress2.txtrandwalk 0 -100
If you can't complete the walk, you must display something like this:All 26 steps were completed.
Only completed 22 steps.
There are 4 possible directions: UP, DOWN, RIGHT, and LEFT. You must use this enumeration in your file:int direction = RandomInt(0, 3);
This will essentially give the enumerators the values 0 to 3. It's very important that you order the enumerators like this. If you use other values for those directions, you will not get the correct output because of the PRNG.typedef enum DIRECTION {dirUP, dirDOWN, dirRIGHT, dirLEFT} DIRECTION;
This is by far the most complex practice. However, you have all of the C knowledge required. You just need to apply good coding practices.
There are two important helper functions. The first is for determining the next move based on the current position. The second is to determine if a particular character is surrounded. If the last character placed is surrounded so that another character cannot be placed, the program must end. You should call this second helper function before attempting to place another character. If you don't, you may find yourself in an infinite loop looking for an open spot that doesn't exist.
That is the proper way to write code.#define BOARD_SIZE_ROWS 8 #define BOARD_SIZE_COLS 8
This practice has been purposely kept simple. If you feel you are up to a bigger challenge, here are some ideas. If you've been using good programming habits, these additional features are not difficult at all.
A D E . . . . . . . . . B C F I J K L M N . Z Y . . G H . . . . O . . X . . . . . . . Q P . V W . . . . . . . R S T U . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
And an example with 2 rows and 24 columns:A . . F G H . . . . . . . . . . . . . . B C D E . I J K . . . . . . . . . . . . . . U T . . . L . . . . . . . . . . . . . . V S R Q P M . . . . . . . . . . . . . . W . . . O N . . . . . . . . . . . . . . X . . . . . . . . . . . . . . . . . . . Y Z . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A B C F G J K L . P Q T U X Y . . . . . . . . . . . D E H I . M N O R S V W Z . . . . . . . . .
. J I H . . . . . . K . G F . . . . . L . . E . . . . . M N C D . . . . . P O B A . . . . . Q . . . . . . . S R . . . . . . . T W X Y Z . . . . U V . . . . . . .
Another strategy to having more than 26 characters is to start over at A after you place Z on the board. Yet another strategy would be to switch to lowercase characters after the uppercase characters have been exhausted. Of course, using numbers instead of letters will allow the most flexibility, at the cost of more code to format the output, as well as possibly having to use ints instead of chars in the 2D array.A B . . . X Y Z D C . U V W \ [ E F G T . . ] ^ J I H S . e f _ K L Q R . d c ` . M P . . . b a . N O . . . . . . . . . . . . .
Example of a 5x5 board with a limit of only 10 moves:
A B C D . . . . E . I H G F . J . . . . . . . . .
and a possible enumeration:NW N NE W· E SW S SE
Here's a sample walk:typedef enum DIRECTION {dirN, dirS, dirE, dirW, dirNE, dirSE, dirSW, dirNW} DIRECTION;
A . . . . . . . . . . B . . . . . . . . . C E . . . . . . . . . D F . . . . . . . . . G . . . . . . . . . . H . . . . . . . . R S I . . Z . . . Q T J K . . Y . . . P . U M L X . . . . . O N V W . . .