CS253 HW3: Options!                
Changes                
The option -t +5
is undefined behavior. That is, a leading plus-sign on
the number argument for -t
may or may not work. We will not test this case.
                
Description                
For this assignment, you will build upon your previous work in HW1,
adding command-line options parsed with getopt(). Also, more than one
dictionary file may be given as arguments, with the target word randomly
selected from all words in all files. This is a complete program,
not a library.
                
Arguments                
The first command-line arguments should be options :
                
-
-v
-
User guesses must be valid words—they must appear in a dictionary
file. If not, emit an error message.
-
-t
turns -
Use this number of turns (guesses) instead of the default, 6.
-
-g
good-string -
For a letter in the correct place, emit the characters in
good-string instead of
=
.
-
-w
wrong-place-string -
For a letter in the wrong place, emit the characters in
wrong-place-string instead of
-
.
-
-b
bad-string -
For a letter that doesn’t appear anywhere in the target,
emit the characters in bad-string instead of a space.
-
-3
or -4
or -5
or -6
or -7
or -8
or -9
-
Select random words of the given length, instead of the default
word length of 5.
After the options come dictionary filenames. If no dictionaries
are given, produce an error.
                
This is the Colorado State University CS253 web page
https://cs.colostate.edu/~cs253/Fall22/HW3
fetched by unknown <unknown> with Linux UID 65535
at 2024-08-30T16:18:17 from IP address 18.117.184.133.
Registered CSU students are permitted to copy this web page for personal
use, but it is forbidden to repost the information from this web page to the
internet. Doing so is a violation of the rules in the CS253 syllabus,
will be considered cheating, and will get you an F in CS253.
Sample Runs                
Here are sample runs, where %
is my prompt.
                
% cat CMakeLists.txt
cmake_minimum_required(VERSION 3.11)
project(hw3)
# Are we in the wrong directory?
if (CMAKE_SOURCE_DIR MATCHES "[Hh][Ww]([0-9])$")
if (NOT PROJECT_NAME MATCHES "${CMAKE_MATCH_1}$")
message(FATAL_ERROR "Building ${PROJECT_NAME} in ${CMAKE_SOURCE_DIR}")
endif()
endif()
# Using -Wall is required:
add_compile_options(-Wall)
# These compile flags are highly recommended, but not required:
add_compile_options(-Wextra -Wpedantic)
# Optional super-strict mode:
add_compile_options(-fmessage-length=80 -fno-diagnostics-show-option
-fstack-protector-all -g -O3 -std=c++17 -Walloc-zero -Walloca
-Wconversion -Wctor-dtor-privacy -Wduplicated-cond
-Wduplicated-branches -Werror -Wextra-semi -Wfatal-errors
-Winit-self -Wlogical-op -Wold-style-cast -Wshadow
-Wunused-const-variable=1 -Wzero-as-null-pointer-constant)
# add_compile_options must be BEFORE add_executable.
# Create the executable from the source file main.cc:
add_executable(${PROJECT_NAME} main.cc)
# Create a tar file every time:
add_custom_target(${PROJECT_NAME}.tar ALL COMMAND
tar -cf ${PROJECT_NAME}.tar *.cc CMakeLists.txt)
% cmake . && make
… cmake output appears here …
… make output appears here …
% head -6 words.txt
scramble
grass
theological
trail
accent
devil
% ./hw3 -bX -g☺ -w- -t 3 words.txt
I am thinking of a 5-letter word.
Guess: mercy
X-X☺X
Guess: space
XX-☺☺
Guess: dance
☺☺☺☺☺
You won!
% ./hw3 -6t2 ~cs253/pub/common-words.txt
I am thinking of a 6-letter word.
Guess: classy
-=
Guess: leader
=== -
You lost, the word was "league".
Debugging                
If you encounter “STACK FRAME LINK OVERFLOW”, then try this:
export STACK_FRAME_LINK_OVERRIDE=ffff-ad921d60486366258809553a3db49a4a
Hints                
- If my
CMakeLists.txt
mentions other files, that’s because
my implementation uses those files. Yours doesn’t have to.
- Don’t take any significant action when an option (e.g.,
-4
) is
encountered. Instead, just remember what needs to be done later.
Options don’t usually do things; instead, they modify how things
will be done later. In the case of -4
, the code might do some
error-checking, then put 4
into the integer word_length
.
- Don’t guess what the user meant. If the arguments don’t match the
specification, complain and stop.
- Don’t make up your own rules. Don’t produce an error for situations
that you don’t like, such as a file being empty, or the argument to
-w
being more than one character. Do what the specification
says.
- However, if the specification seems ridiculous, ask.
Who knows—it could be a mistake in the spec!
- For a colorful time, try:
./hw3 -t99 -g $'\e[102m=\e[m' -w $'\e[103m-\e[m' -b $'\e[101m \e[m' words.txt
Requirements                
- All the requirements from HW1 apply, except that several
dictionary files may be given.
- You must use the library function getopt() to parse the options.
If you do not, then you will receive a three-point penalty.
- Produce an error message if:
- none of the dictionary files contain any words of the desired length
- any option but
-v
is given more than once
- the word length is specified twice
- a bad option (e.g.,
-Z
) or bad option argument
(e.g., -tΩ
) is given
(the message must contain the bad option and argument, if present).
- A space after an option taking an argument is optional.
-g osh
and -gosh
are both valid.
- Options must precede filenames. Option processing must stop at the
first argument that isn’t an option (or an argument to an option,
e.g.,
-t 42
).
This must attempt to treat -v
as a file, which will probably fail: ./hw3 infile -v
- Option bundling:
./hw3 -vt65535
is equivalent to
./hw3 -v -t 65535
./hw3 -v-t3
is invalid.
- The next non-space character after an option that requires an
argument must be the argument itself, not another option flag.
./hw3 -g G -wb W B
will treat b
as the argument to -w
, and will then regard
W
and B
as a filenames, which will probably fail.
- Dictionaries:
- The dictionary files may consist of any number of lines.
- Each input line may be arbitrarily long.
- You may not use any external programs. You may not use
system(), fork(), popen(), execl(), execvp(), etc.
- You may not use C-style I/O
such as printf(), scanf(), fopen(), and getchar().
- You may not use endl. Use flush if needed.
- You may not use dynamic memory via new, delete,
malloc(), calloc(), realloc(), free(), strdup(), etc.
- It’s ok to implicitly use dynamic memory via containers
such as string or vector.
- You may not use the .eof() method.
- No global variables.
- Except for an optional single global string called
program_name
containing argv[0]
.
- For readability, don’t use ASCII int constants (
65
) instead of
char constants ('A'
) for printable characters.
- We will compile your program like this:
cmake . && make
- If that generates warnings, you will lose a point.
- If that generates errors, you will lose all points.
- There is no automated testing/pre-grading/re-grading.
- Test your code yourself. It’s your job.
- Test with the CSU compilers, not just your laptop’s compiler.
- Even if you only change it a little bit.
- Even if all you do is add a comment.
- You must use getopt()!
Tar file                
- The tar file for this assignment must be called:
hw3.tar
- It must contain:
- source files (
*.cc
) required to build
- any header files (
*.h
) required to build
CMakeLists.txt
- This command must produce the program
hw3
(note the dot):
cmake . && make
- At least
-Wall
must be used every time g++ runs.
How to submit your work:                
In Canvas, check in the
file
hw3.tar
to the assignment “HW3”.
It’s due 11:59ᴘᴍ MT Saturday, with a 24-hour late period for a 25% penalty.
                
How to receive negative points:                
Turn in someone else’s work.