PD-Home   ·   Introduction   ·   PDS-Instructions    ·   PDS-Examples    ·   PDS-Exercises    ·   Tournament-Entries

Sample PD Examples

( and how to program them in PDS )

; Always defect
report Defect

; Always cooperate
report Coop

; Cooperate 15% of the time
set voldemort random 15
report voldemort

; Do the opposite of what your partner just
;   did on her last move.

if num-moves > 0 goto rest
report defect  ; defect on your first move

rest:
  if last-move-other != coop goto Do-Cooperate
    report Defect

Do-Cooperate:
    report Coop

; Do the opposite of what
   your partner's most
;  frequent move has been.
;  Defect on the first move.

if Num-moves > 0 goto Find-most-frequent-move
  report Defect

; this subroutine sets the variable
;   "most-frequent-move" to either Coop or Defect
;   depending upon the partner's most frequent move 
 
Find-most-frequent-move:
set ncoops 0   ; this will count Coop moves
set ndefects 0 ; this will count Defect moves
set n 1        ; which move we look at

; Here, we count the number of Coops 
;   and Defects
;   made by our partner so far
Loop:
set pmove move-other n
if pmove = coop goto Add-coop
set ndefects ndefects + 1
goto Loop-End
Add-coop:
set ncoops ncoops + 1
Loop-End:
set n n + 1
if n <= num-moves goto Loop

; Here, we decide what to do
if ncoops >= ndefects goto Do-defect
report coop 
Do-defect:
report defect

; Coop on the first move, 
;   and defect on the second. 
;   Then, if your partner cooperated 
;   the previous 2 moves, then coop, 
;   otherwise defect.

if Num-moves > 0 goto Rest
  report Coop

Rest:
if Num-moves > 1 goto Rest2
  report Defect

Rest2:
if last-move-other != coop goto Do-Defect
if move-other 2 != coop goto Do-defect

report coop

Do-Defect:
report defect

 

 

; Try to detect whether your partner is "special":
; Play randomly for the first 5 moves.
; Then figure out whether your partner started
; her strategy with the 5-move sequence:
; coop,coop,defect,defect,coop
; If so, then she's "special" 
; and cooperate with her after 5 moves, 
; otherwise defect always


if num-moves >= 5 goto Detect-Special
report random 50

Detect-Special:
set nthmove num-moves

; check if first move is coop
if move-other nthmove != coop goto Not-Special

; check if second move is coop
set nthmove nthmove - 1
if move-other nthmove != coop goto Not-Special

; check if third move is defect
set nthmove nthmove - 1
if move-other nthmove != defect goto Not-Special

; check if fourth move is defect
set nthmove nthmove - 1
if move-other nthmove != defect goto Not-Special

; check if fifth move is coop
set nthmove nthmove - 1
if move-other nthmove != coop goto Not-Special

; OK, she's "special", cooperate
report coop

Not-Special:
report defect
; Play randomly for the first 3 moves,
; then Defect if your partner defected more often
; than she cooperated,
; otherwise Cooperate

if num-moves >= 3 goto Majority-Calculation
report random 50

Majority-Calculation:
set She-defected defects-other num-moves
set She-cooped num-moves - She-defected

if She-defected > She-cooped goto Revenge
report coop

Revenge:
report defect