Yet untested (i am very unfamiliar with the java machinery).
TheThe idea of this wolf pack is that it keeps in memory (in a file called collective_memory.txt) who's alive or dead, check what the dead wolves and the alive wolves used as attacks and change the choice probability accordingly.
infile <- file("stdin")
open(infile)
repeat{
input <- readLines(file("stdin")infile,1)
type <- substr(input,1,1)
id <- substr(input,2,3)
f <- "collective_memory.txt"
if(nchar(input)>3){
info <- substr(input,4,nchar(input))
}else{
info <- NULL}
}
attack <- function(id,info,f){
if(info%in%c("B","L")){choice <- "S"}
if(info=="S"){choice <- "P"}
if(info=="W"){
if(file.exists(f"memory")){
memory <- read.table(f,sep="\t",header=TRUE)
dead <- memory$ID[memory$Status=="Dead"]
veteran <- memory[memory$Attack!="" & !is.na(memory$Attack), ]
if(nrow(veteran[!is.na(veteran[,1]),])>0){
deadvet <- table(factor(veteran$Attack[veteran$ID%in%dead],levels=c("R","P","S","")))
livevet <- table(factor(veteran$Attack[!veteran$ID%in%dead],levels=c("R","P","S","")))
probR <- (1+livevet['R'])/(1+livevet['R']+deadvet['R'])
probS <- (1+livevet['S'])/(1+livevet['S']+deadvet['S'])
probP <- (1+livevet['P'])/(1+livevet['P']+deadvet['P'])
choice <- sample(c("S","P","R"),1,prob=c(probS,probP,probR))
memory <- rbind(memory, data.frame(ID=id, Status="Alive", Attack=choice))
}else{
choice <- sample(c("S","P","R"),1)
memory <- rbind(memory, data.frame(ID=id, Status="Alive", Attack=choice))
}
}else{
choice <- sample(c("S","P","R"),1)
memory <- data.frame(ID=id, Status="Alive", Attack=choice)
}
write.table(memory,file=f,sep="\t",row.names=FALSE, col.names=TRUE)
}
paste(choice,id,sep="")
}
move <- function(id,info){
choice <- "H"
paste(choice,id,sep="")
}
initialize <- function(id,f){
if(file.exists(f"memory")){
memory <- read.table(f,sep="\t",header=TRUE)
memory <- rbind(memory,data.frame(ID=id,Status="Alive",Attack=""))
}else{
}else{ memory <- data.frame(ID=id,Status="Alive",Attack="")}
}
confirmed_dead <- memory$ID[memory$Status=="Dead"]
last_seen <- memory[!memory$ID%in%confirmed_dead,]
last_seen <- last_seen[last_seen$Attack=="",]
lid <- table(last_seen$ID)
turns <- max(lid)
dead <- lid[lid<(turns-1)]
if(length(dead)>0){
dead_id <- names(dead)
for(i in dead_id){
memory <- rbind(memory, data.frame(ID=i, Status="Dead", Attack=""))
}
}
write.table(memory,file=f,sep="\t",row.names=FALSE, col.names=TRUE)
paste("K",id,sep="")
}
result <- switch(type,"A"=attack(id,info,f),"M"= move(id,info),"S"=initialize(id,f))
cat(result,"\n",sep="")
flush(stdout())
}