practical session 8

19
Practical session 8 Assignment 3

Upload: luyu

Post on 14-Jan-2016

36 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Practical session 8

Practical session 8Assignment 3

Page 2: Practical session 8

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

Page 3: Practical session 8

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

Page 4: Practical session 8

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.

Page 5: Practical session 8

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.

* * * * *

CODE FLAGS SP Co-Routine Stack

Page 6: Practical session 8

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

Page 7: Practical session 8

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

Page 8: Practical session 8

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)

STATUS

FLAGS (memory)

TEAM # STUN COUNTER

Page 9: Practical session 8

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.

Page 10: Practical session 8

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.

Page 11: Practical session 8

Example

>corewars dkksds

*d1 k2 k3 s4 d5 s6

0)d1k2k3s4d5s6* * * * * * * * * * * *

* …d1**

Page 12: Practical session 8

Example

>corewars dkksds

*d1 k2 s4 d5 s6

0)d1k2k3s4d5s6* * * * * * * * * * * *

* …d1*

2 2

Page 13: Practical session 8

Example

>corewars dkksds

*d1 k2 s4 d5 s6

0)d1k2k3s4d5s6* * * * * * * * * * * *

* …d1*

2 21 1

d1

Page 14: Practical session 8

Example

>corewars dkksds

d1 k2 s4 d5 s6

0)d1k2k3s4d5s6* * * * * * * * * * * *1)d1k2d1s4D5S6d1* * * * * * * * * * *

* …d1*

1 1

d1 d1**

Page 15: Practical session 8

Example

>corewars dkksds

d1 k2 s4 d5 s6

0)d1k2k3s4d5s6* * * * * * * * * * * *1)d1k2d1s4D5S6d1* * * * * * * * * * *

* …d1*

1 1

d1*

2 21 1

Page 16: Practical session 8

Example

>corewars dkksds

d1 k2 s4 d5 s6

0)d1k2k3s4d5s6* * * * * * * * * * * *1)d1k2d1s4D5S6d1* * * * * * * * * * *2)d1k2* s4d5s6D1D1* * * * * * * * * *

* …d1 d1

1 1

d1*

Page 17: Practical session 8

Example

>corewars dkksds

d1 k2 s4 d5 s6

0)d1k2k3s4d5s6* * * * * * * * * * * *1)d1k2d1s4D5S6d1* * * * * * * * * * *2)d1k2* s4d5s6D1D1* * * * * * * * * *

* …d1 d1

1 1

d1*

2 21 1

Page 18: Practical session 8

Example

>corewars dkksds

d1 k2 s4 d5 s6

0)d1k2k3s4d5s6* * * * * * * * * * * *1)d1k2d1s4D5S6d1* * * * * * * * * * *2)d1k2* s4d5s6D1D1* * * * * * * * * *3)d1k2* s4D5S6d1d1* * * * * * * * * *

* …d1 d1*

1 1

Page 19: Practical session 8

Example

>corewars dkksds

0)d1k2k3s4d5s6* * * * * * * * * * * *1)d1k2d1s4D5S6d1* * * * * * * * * * *2)d1k2* s4d5s6D1D1* * * * * * * * * *3)d1k2* s4D5S6d1d1* * * * * * * * * *

….

100)d1k2* s4d5s6D1D1* * * * * * * * * * Winning team: 1