Time due: 11:00 PM Thursday, June 2
The Mechanoids of Planet Zork have captured you! For their entertainment, they have thrown you in an arena with killer cyborgs. Well, that's the scenario for a new video game under development. Your assignment is to complete the prototype that uses character-based graphics.
Each cyborg has a radio receiver implanted in its brain that is tuned to one of three channels. If the cyborg receives a command transmitted on the channel its receiver is tuned to, it will obey that command. There is a transmitter that lets one choose a channel and broadcast a command to all cyborgs listening on that channel. The player managed to steal the transmitter, but it was damaged in the fight; half the time, an attempt to transmit a command won't work.
If you execute this Windows program or
this Mac program or this
Linux program, you will see the player (indicated by @
)
in a rectangular arena filled with killer cyborgs (each indicated by a
digit character, the number of the channel the cyborg's receiver is tuned
to). At each turn, you'll select an action for the player to take: move
one step or stay put. The player will take the action. You will then
tell the player a command to transmit on one particular channel. Then
each cyborg will move. Normally, each cyborg will move one step in a random
direction. However, if the transmitter works this turn (half the time it
won't), those cyborgs listening on the channel selected will move one step
in the direction commanded. If a cyborg moves to the position occupied by
the player, the player dies. (If the player moves to a position
occupied by a cyborg, the player dies, so that would be a dumb move.)
A position in the arena may have a wall segment. If the player or a cyborg moving of its own free will chooses a direction such that moving would be to a position occupied by a wall or to a point out of the arena, then the player or cyborg just doesn't move that turn. If a cyborg moving under the command of the player attempts to move into a wall or to a position out of the arena, it doesn't move that turn, but suffers 1 unit of damage. A cyborg that has suffered 3 units of damage is dead and is removed from the game. Clearly, it's in the player's interest to command cyborgs to smash into walls or the edges of the arena.
This smaller Windows version or Mac version or Linux version of the game may help you see the operation of the game more clearly.
At each turn the player may take one of these actions:
The game allows the user to select the player's action: n/e/s/w for movement, or x for standing. The user may also just hit enter to have the computer select the player's move.
After the player moves, the user is prompted for a broadcast to attempt. You enter a two-character string such as "2n". The first character is the channel number and the second is n/e/s/w. At the cyborgs' turn, there is a 1/2 probability that the broadcast will succeed. If it does, all cyborgs listening on that channel move one step in the direction commanded if they can; if they can't (because they'd hit a wall or move out of the arena), they stay put, but suffer 1 unit of damage. If the broadcast doesn't succeed, the cyborgs listening on that channel act like all the other cyborgs on that turn: They move normally.
Normally, each cyborg picks a random direction (north, east, south, west) with equal probability. The cyborg moves one step in that direction if it can; if the cyborg attempts to move into a wall or out of the arena, however, it does not move and suffers no damage. More than one cyborg may occupy the same position; in that case, the display will show a digit character indicating the channel number of one of the cyborgs at that point; it doesn't matter which one.
Your assignment is to complete this C++
program skeleton to produce a program that implements the described
behavior. (We've indicated where you have work to do by comments containing
the text TODO
; remove those comments as you finish each thing you
have to do.) The program skeleton you are to flesh out defines four
classes that represent the four kinds of objects this program works with:
Game, Arena, Cyborg, and Player. Details of the interface to these classes
are in the program skeleton, but here are the essential responsibilities
of each class:
Game
Arena
Player
Cyborg
The skeleton program you are to complete has all of the class definitions and implementations in one source file, which is awkward. Since we haven't yet learned about separate compilation, we'll have to live with it.
Complete the implementation in accordance with the description of the game.
You are allowed to make whatever changes you want to the private
parts of the classes: You may add or remove private data members or private
member functions, or change their types. You must not make any
deletions, additions, or changes to the public interface of any
of these classes — we're depending on them staying the same so that
we can test your programs. You can and will, of course, make changes to the
implementations of public member functions, since the callers of
the function wouldn't have to change any of the code they write to call
the function. You must not declare any public data
members, nor use any global variables whose values may change during
execution (so global constants are OK). You may add additional
functions that are not members of any class. The word friend
must not appear in your program.
Any member functions you implement must never put an object into an
invalid state, one that will cause a problem later on. (For example, bad
things could come from placing a cyborg outside the arena.) Any function
that has a reasonable way of indicating failure through its return value
should do so. Constructors pose a special difficulty because they can't
return a value. If a constructor can't do its job, we have it write an
error message and exit the program with failure by calling
exit(1);
. (We haven't learned about throwing an exception to
signal constructor failure.)
What you will turn in for this assignment is a zip file containing this one file and nothing more:
A text file named cyborgs.cpp that contains the source code for the completed C++ program. This program must build successfully using both g31 and either Visual C++ or clang++, and its correctness must not depend on undefined program behavior. Your program must not leak memory: Any object dynamically allocated during the execution of your program must be deleted (once only, of course) by the time your main routine returns normally. For this project, you do not have to write comments in your program.
Notice that you do not have to turn in a report describing the design of the program and your test cases.
By Wednesday, June 1, there will be link on the class webpage that will enable you to turn in your zip file electronically. Turn in the file by the due time above. There will not be any extensions of the due date for this project, so start early and do not procrastinate.