190 likes | 306 Views
Practical session 8. Assignment 3. Core Wars. Core Wars is a programming game in which two or more battle programs ("warriors") compete for control Programs move themselves around, modify themselves, attempt to destroy others by writing on them etc.
E N D
Practical session 8 Assignment 3
Core Wars • Core Wars is a programming game in which two or more battle programs ("warriors") compete for control • Programs move themselves around, modify themselves, attempt to destroy others by writing on them etc. • The object of the game is to cause all processes of the opposing program(s) to terminate
Core Wars • In the real core-wars: • A supervisory program runs one instruction of each program in turn. • No program knows where the others reside, and other complications
Our game version • You use the co-routine mechanism to implement a simplified version of the core wars game: • Each warrior program is implemented as a co-routine. The warrior belongs to a “team” • Warrior performs some action. When done, control is transferred to a scheduler. • scheduler is our supervisor program (implemented as a co-routine, also). It selects the next co-routine to get its time-slice action. Selection is done in a round robin fashion.
Co-routine table • Each co-routine warrior is settled in a co-routine table slot. The first table slot is dedicated to the scheduler co-routine. * * * * * SP CODE FLAGS Co-Routine Stack
Warrior actions • Warrior performs one of the following actions (implemented as functions): • DUPLICATE: creates a clone of the current co-routine that belongs to the same “team”, in the first empty slot. Otherwise fails. • NUKE(i): kills a co-routine in slot i. That will cause the scheduler not to run the dead co-routine. • STUN: stuns (or freezes) up to m co-routines to be chosen by the currently running co-routine. Stunned co-routines do not get their turn to execute for x rounds
Warrior type • You will be implementing three types of fighting co-routines, differentiated by their fighting strategies: • DUPLICATOR: applies the DUPLICATE action, unless no slots remain, in which case instead of DUPLICATE, it performs NUKE to the first slot containing a living enemy* co-routine (giving preference to non-stunned warriors). • Killer: whenever its turn occurs, NUKEs the next enemy co-routine. If no such co-routine exists, no action is taken. • Stunner: Whenever its turn occurs, stuns the next m living enemy co-routines that are not currently stunned. If there is no enemy to STUN, apply DUPLICATE. If DUPLICATE is also not possible, use NUKE on the next enemy. *enemy – co-routine from other team
Warrior state • The 'flags' space in a co-routine structure can hold the status of a co-routine, and its team number. • The status is in the low order byte. • The stun counter is in the high-order word of the flags. • The team number is in the next to lowest order byte of the flags. • Possible status number for a warrior: • 0 - Dead/Empty space • 1 - Alive and well • 3- Stunned (any k>1) FLAGS (memory) STATUS STUN COUNTER TEAM #
Scheduler • The scheduler decides according to the rules above which co-routine gets to run, and then resumes it. • It is a simple 'round-robin' mechanism. • Performs 100 iterations over the entire table, and at the end of each, it prints the population status. • Prints the winning team - one with most living team members in the table.
Technical Issues • From the command line, the user types: • The program name ‘corewars’. • A string argument, composed from ‘d’, ‘s, and ’k’ letters. • The string argument determines the number of participating co-routines, the warriors types (‘d’ for duplicator, ‘s’ for stunner and ‘k’ for killer) and their order. Each warrior is given a team number (starting from 1) • The created co-routine table will be of size 3*(input length)+1. • By default, a stunner selects two co-routines for the operation. A stunned warrior needs two turns to wait.
Example >corewars dkksds d1 s6 s4 d5 d1 k2 k3 * * * * … 0)d1k2k3s4d5s6* * * * * * * * * * * *
Example >corewars dkksds 2 2 d1 s6 s4 d5 d1 k2 * * * … 0)d1k2k3s4d5s6* * * * * * * * * * * *
Example >corewars dkksds 2 2 d1 s6 d1 s4 d5 d1 k2 * * * … 0)d1k2k3s4d5s6* * * * * * * * * * * * 1 1
Example >corewars dkksds d1 d1 s6 d1 s4 d5 d1 k2 * * * * … 0)d1k2k3s4d5s6* * * * * * * * * * * * 1)d1k2d1s4D5S6d1* * * * * * * * * * * 1 1
Example >corewars dkksds 1 1 d1 d1 s6 s4 d5 d1 k2 * * * … 0)d1k2k3s4d5s6* * * * * * * * * * * * 1)d1k2d1s4D5S6d1* * * * * * * * * * * 1 2 1 2
Example >corewars dkksds d1 d1 s6 d1 s4 d5 d1 k2 * * … 0)d1k2k3s4d5s6* * * * * * * * * * * * 1)d1k2d1s4D5S6d1* * * * * * * * * * * 2)d1k2* s4d5s6D1D1* * * * * * * * * * 1 1
Example >corewars dkksds d1 d1 s6 d1 s4 d5 d1 k2 * * … 0)d1k2k3s4d5s6* * * * * * * * * * * * 1)d1k2d1s4D5S6d1* * * * * * * * * * * 2)d1k2* s4d5s6D1D1* * * * * * * * * * 1 2 1 1 2 1
Example >corewars dkksds d1 d1 s6 s4 d5 d1 k2 * * … 0)d1k2k3s4d5s6* * * * * * * * * * * * 1)d1k2d1s4D5S6d1* * * * * * * * * * * 2)d1k2* s4d5s6D1D1* * * * * * * * * * 3)d1k2* s4D5S6d1d1* * * * * * * * * * 1 1
Example >corewars dkksds 0)d1k2k3s4d5s6* * * * * * * * * * * * 1)d1k2d1s4D5S6d1* * * * * * * * * * * 2)d1k2* s4d5s6D1D1* * * * * * * * * * 3)d1k2* s4D5S6d1d1* * * * * * * * * * …. 100)d1k2* s4d5s6D1D1* * * * * * * * * * Winning team: 1