Interpret the Pig series
Bash, 251 246 bytes
r=$RANDOM
((r%5<4))||exit
[[ $1 =~ PIG ]]||(echo "File must contain the string 'PIG'.";exit)
s=(GRUNT MOAN OINK BURP GROAN WHINE)
m=("${1#*PIG}" ${s[r%6]}
"Your pig has unfortunately died. Please try again." "$1")
echo -n "${m[r%5]}">"${1%%PIG*}"
This would be a lot shorter if deaf pigs could at least read...
Python 2, 296 286 278 bytes
def g(p): import random;f=random.randint;r=f(0,4);i=p.find("PIG") if r: if i+1:open(p[:i],"w").write([0,p[i+3:],["GRUNT","MOAN","OINK","BURP","GROAN","WHINE"][f(0,5)],"Your pig has unfortunately died. Please try again.",p][r]) else:print"File must contain the string 'PIG'."
The last two lines start with a tab, instead of the rendered 4 spaces.
Takes input program as function argument.
Batch, 409 406 405 bytes
@echo off
set/ar=%random%%%5
if 0==%r% exit/b
set p=x%1
set q=%p:*PIG=%
if %q%==%p% echo File must contain the string 'PIG'.&exit/b
set p=%1
call set p=%%p:PIG%q%=%%
goto %r%
:1
echo %q%>%p%
exit/b
:2
for %%a in (GRUNT.0 MOAN.1 OINK.2 BURP.3 GROAN.4 WHINE.5)do if %%~xa==.%time:~6,1% echo %%~na
exit/b
:3
echo Your pig has unfortunately died. Please try again.>%p%
exit/b
:4
echo %1>%p%
Sadly %p:*PIG=%
fails if p is blank, thus the x%1
hack. call set
is a nice way to avoid enabledelayedexpansion that I found on Stack Overflow; while the %%~xa==.
was a flash of inspiration on my part.
Edit: Saved 3 bytes thanks to @CᴏɴᴏʀO'Bʀɪᴇɴ. Saved 1 byte thanks to @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ.