Introduction to Computer Science I by James Tam |
CPSC 231: Assignment 5 |
Due Friday December 7 at 4 PM
New concepts to be applied for this assignment
(Note: new design principles, e.g.,Object-Orientation for Part I, as well style requirements from previous assignments must be applied).
Part I: creating a fight simulator program
Write a simple a fight simulator (in case you have never seen a "2-man" kombat sequence here is a video). There are two opponents: an attacker (that only launches attacks) and a defender (that can only defend against the attacks). There's three types of attacks categorized by height: high, medium and low. (JT's hint: you can use global constants: HIGH=3, MEDIUM=2, LOW=1 [see the 'Globals.py' file] to represent each type of attack). Similarly there are three types of defenses: high, medium and low. If the heights match then the attack is blocked. Otherwise the attack is counted as a 'hit'. The simulation runs a fixed number of attack-defenses sequences (from 1 - 100 'rounds' of attacks and defenses). The user can determine the number of rounds at the start. If a value outside the range is entered then the default number of rounds (ten) will be executed.
Also when the program is first run the user can determine the proportion of attacks that originate at the three different heights (e.g., high = 50%, medium = 30%, low = 20%). These three proportions must sum to 100% otherwise the program will use the defaults (equal probability of each type of attack). The program will then randomly generate attacks using the proportions specified for that particular run (the next run it will again query the user for the proportions as well as the number of rounds).
The defender will be entirely controlled by the computer. The 'unskilled' defender has an equal chance of employing one of the three types of defenses. A program that implements an 'intelligent' defender that will analyze the pattern of attacks and adjust the probability of each type of defense being employed over time. (For example if the attacker always throws high attacks then the defender will eventually extrapolate the pattern and use high defenses more frequently). It is up to you to decide how many rounds are needed before the defender begins to adjust it's pattern of defenses to match the pattern of attacks. And this value can be hard-coded into the program (no user input needed). The minimum number of rounds needed should be one (with zero attacks thrown there is no pattern to analyze). Prior to analyzing the pattern of attacks the defender should have an equal probability of employing each type of defense.
Exactly what constitutes 'intelligent' for this assignment? Because this isn't a formal class in Artificial Intelligence the definition is fairly broad: as long the program demonstrates the ability to adapt to the pattern of attackers and the marker can clearly see the pattern as the program executes (the display of statistics each round will be very useful). If for instance the attacker throws only medium attacks, after the learning period is over the defender should eventually employ only medium height defenses (or at least have an extremely high probability of employing this type of defense). You can choose how many rounds will pass before the defender begins to adapt to the pattern.
Note: it is important that your printed output statements clearly demonstrate to your marker the basic features of the program (e.g., that the attacker and defender generated something and how that combination produced a result). Consequently your output must not only present the needed information but must also be reasonably formatted. The program output is even more crucial when you have implemented an intelligent defender. The marker must be able to see the effects of the defender adapting to the pattern of attacks otherwise you may not get credit for your work. He won't have the time to trace through your program.
You can either come up with your own algorithm for the defender or you can freely research algorithms online. In the latter case make sure you clearly cite all your sources. (Failing to list a source may be regarded as academic misconduct).
The program will tally a number of statistics and display them during each round and at the end of the simulation. At a minimum here is some of the information that should be displayed:
Each round: the round number, the type of attack and defense chosen, the result of the attack-defense combination (showing the probabilities for the three types of defenses would be useful if you implement an intelligent defender).
At the end of Kombat: the total number of successful attacks and blocks and the probabilities of each type of attack-defense at the end (the values for the attacker won't change but an intelligent defender's proportions should go from equal values to more closely reflect the proportions of the attacker).
In the assignment directory you can find [text files] that are records of the output of the program. As you will see my 'intelligent' defender is not particularly 'intelligent' but it's sufficient for this class.
Program design [Click here for animated overview]: the program will consist of 3 file modules (Attacker, Defender, Manager). Each class definition must be in it's own file and that file name must match the class name. The 'Manager' is not a class but instead a function or collection of function calls contained in it's own separate file 'Manager.py'. To keep things simple you can employ a fourth module called "Globals.py" which contains the definitions for global constants (these are globals that are used in multiple modules - constants that are used in only one classes' methods can be defined in the same file module as the definition of the class). The 'Attacker' will be an object that is responsible for generating attacks so it should track all information associated with attacking (e.g., numbers of each attack type generated). Likewise the 'Defender' object will receive the attack generated by the attacker, generate a defense and determine the result of the attack-defense combination. Also the defender should track information associated with defense (e.g., e.g., the numbers of each type of defense used). As mentioned the 'Manager' won't be a class but contain a series of functions contained in it's own file module. The Manager will play a number of roles. First it acts as starting execution point for the program (contains the 'main' function or equivalent). It should instantiate the attacker and defender and acts as an intermediary between them (communicates to the defender the type of attack generated by the attacker). Input and output should largely occur in the functions of the manager module.
Part II: working with graphical user interfaces
Modify the program found in the assignment directory (/home/231/assignments/assignment5/part2/sample.py) so that the default text of the button is "Press me" and the text changes to "Stop pressing me!" whenever someone clicks on the button. (JT: this part of the assignment will be worth far fewer marks than the first part).
Using pre-written Python code
For Part I you will need to use the built in random number generator in the ‘random’ module/library. For Part II you obviously need to be able to use the GUI components in the Tkinter module. Beyond that (and common sense operators and functions such as input/output function, mathematical operators), unless you told otherwise, you will need to write your own code and cannot use other pre-written Python modules or operators.