Lab 10
Logistics
- Due: Friday, April 4th AoE.
- Submission instructions: ensure that you have the files for this assignment in your
~/csci112_spring2025/labs/lab10directory, and that the snapshot (commit) of your repository containing the version of that file you want us to grade has been committed and tagged aslab10. (You should have set up yourgitrepo and practiced tagging a commit in Classwork 4.)
Outside resources
On this assignment, you may not use the internet or generative AI such as ChatGPT to solicit solutions to the programming part of the assignment. If you are having trouble writing your program, please go to lab (Fridays, 10-4pm in Barnard 254) or post in Slack to get help.
However, you may use those resources for help with navigating the Linux terminal, using vim, and using git, although you may get better answers to your questions by going to lab or posting on Discord anyway.
Learning outcomes
- Practice using command line arguments
- Practice splitting your C program into separate header and C files
- Practice using
makeand writing Makefiles - Practice passing structs by reference
- Practice using the math library
Assignment
You will write a simple role playing game simulator, in which two characters
fight once. Your program should take the character info (name, experience
points, and health points) as arguments on the command line, store the
information about each character in a Character struct, and call a fight
function that takes in pointers to the two characters and modifies them based
on the result of the fight.
When two characters fight, this is what should happen:
- If the characters have equal XP, nothing.
- Otherwise, the character with higher XP is the winner and the character with
lower XP is the loser. The winner deals the difference in XP damage to the
loser’s HP. The winner’s XP increases by adding the log base 10 (
log10function frommath.h) of their current XP to their current XP, and then rounding the result to the nearest integer (roundfunction frommath.h). The loser’s XP increases by multiplying their current XP by 1.5. This should also be rounded to the nearest integer. - If the loser’s HP goes to 0 (or less), add “(deceased)” to their name.
Program specification
You have some flexibility on the implementation of this program, but you must include the following:
- a
Characterstruct with fields for name, XP, and HP - a
fightfunction that takes as input two pointers toCharacterstructs and modifies the structs that the pointers point to if needed - calls to the
log10androundfunctions from themath.hlibrary - at least two
.cfiles and one.hfile - a Makefile that correctly encodes the dependencies of your program and can be
used to create an executable called
lab10 - in the Makefile, a
cleantarget that removes the executablelab10and all intermediate object files
For creating the structs and printing them, you may choose to use separate functions if you would like, but the details are up to you.
Hints
- Remember to compile with
-lmwhen you use the math library. - You can use
->, the indirect selection operator, to both dereference a pointer to a struct and access its fields.
Sample run
As always, match the output format exactly.
[g73x595@csci112 lab10]$ ls
funcs.c lab10.c lab10.h Makefile
[g73x595@csci112 lab10]$ make
gcc -c lab10.c -Wall
gcc -c funcs.c -Wall -lm
gcc -o lab10 lab10.o funcs.o -lm
[g73x595@csci112 lab10]$ ./lab10 Bowser 10 10 Mario 5 5
### LET'S FIGHT ###
Bowser (10 XP, 10 HP) vs. Mario (5 XP, 5 HP)
Bowser deals 5 damage to Mario
Result is:
Bowser (11 XP, 10 HP)
Mario (deceased) (8 XP, 0 HP)
[g73x595@csci112 lab10]$ ./lab10 Bowser 22 100 Mario 35 100
### LET'S FIGHT ###
Bowser (22 XP, 100 HP) vs. Mario (35 XP, 100 HP)
Mario deals 13 damage to Bowser
Result is:
Bowser (33 XP, 87 HP)
Mario (37 XP, 100 HP)
[g73x595@csci112 lab10]$ ./lab10 Bowser 22 100 Mario 22 100
### LET'S FIGHT ###
Bowser (22 XP, 100 HP) vs. Mario (22 XP, 100 HP)
It's a tie!
Result is:
Bowser (22 XP, 100 HP)
Mario (22 XP, 100 HP)
[g73x595@csci112 lab10]$ ./lab10 Bowser 40 100 Mario 22 100
### LET'S FIGHT ###
Bowser (40 XP, 100 HP) vs. Mario (22 XP, 100 HP)
Bowser deals 18 damage to Mario
Result is:
Bowser (41 XP, 100 HP)
Mario (33 XP, 82 HP)
Grading–100 points
- 10:
makecompiles the executablelab10without warnings - 10:
make cleanremoves executablelab10and all.oobject files - 10: there is a
Characterstruct defined in a header file - 10: there is a function
fightthat takes in two pointers toCharacters - 5: uses the
log10function - 5: uses the
roundfunction - 5: has at least two
.cfiles - 5: has at least one
.hfiles - 40: correct output for four test cases
Autograder
You can run the autograder using
/public/labs/lab10/autograder.sh
A detailed breakdown of your score will be present in autograder.txt.
Grading turnaround
Scores will be uploaded to D2L by class time the Wednesday after the due date.