Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,69 @@
session_start('user')
session_addvar('user', 'historic_choices')
session_addvar('user', 'win_record')
session_addvar('user', 'plays')
var(historic_choices)->isNotA(::map) ? var(historic_choices = map('rock'=0, 'paper'=0, 'scissors'=0))
var(plays)->isNotA(::integer) ? var(plays = 0)
var(win_record)->isNotA(::array) ? var(win_record = array)
define br => '<br>'
define winner(c::string,p::string) => {
if(#c == $superior->find(#p)) => {
$win_record->insert('lasso')
return 'Lasso'
else(#p == $superior->find(#c))
$win_record->insert('user')
return 'User'
else
$win_record->insert('tie')
return 'Nobody'
}
}
var(
choice = web_request->param('choice')->asString,
lookup = array('rock', 'paper', 'scissors'),
computer_choice = $lookup->get(math_random(3,1)),
superior = map('rock'='paper', 'paper'='scissors', 'scissors'='rock'),
controls = '<a href=?choice=rock>Rock</a> <a href=?choice=paper>Paper</a> <a href=?choice=scissors>Scissors</a> <a href=?choice=quit>Quit</a><br/>'
)
if($choice == 'quit') => {^
'See ya. <a href=?>Start over</a>'
session_end('user')
$historic_choices = map('rock'=0, 'paper'=0, 'scissors'=0)
$plays = 0
$win_record = array
else(array('rock','paper','scissors') >> $choice)
$controls
if($plays != 0) => {
local('possibilities') = array
with i in $lookup do => {
loop($historic_choices->find(#i)) => { #possibilities->insert(#i) }
}
$computer_choice = $superior->find(#possibilities->get(math_random($plays,1)))
}
'User chose ' + $choice + br
'Lasso chose ' + $computer_choice + br
winner($computer_choice->asString, $choice) + ' wins!'
$historic_choices->find($choice) = $historic_choices->find($choice)+1
$plays += 1
else($choice->size == 0)
$controls
else
'Invalid Choice.'+ br + $controls
^}
if($win_record->size) => {^
br
br
'Win record: '+br
'Lasso: '+($win_record->find('lasso')->size)+br
'User: '+($win_record->find('user')->size)+br
'Tie: '+($win_record->find('tie')->size)+br
^}

View file

@ -0,0 +1,102 @@
--standard game
constant rule3 = {"rock blunts scissors",
"paper wraps rock",
"scissors cut paper"}
--extended version
constant rule5 = {"rock blunts scissors",
"rock crushes lizard",
"paper wraps rock",
"paper disproves spock",
"scissors cut paper",
"scissors decapitate lizard",
"lizard eats paper",
"lizard poisons spock",
"spock smashes scissors",
"spock vaporizes rock"}
constant rules = iff(rand(2)=1?rule3:rule5)
sequence what = {}
sequence beats = {}
string wkeys = ""
string question = "What is your move "
integer choices, hsum
sequence history, cplays, pplays
object x, verb, y
for i=1 to length(rules) do
{x} = split(rules[i])
if not find(x,what) then
what = append(what,x)
if find(x[1],wkeys) then
wkeys = append(wkeys,x[$])
question &= x[1..-2]&"("&x[$]&"), "
else
wkeys = append(wkeys,x[1])
question &= "("&x[1]&")"&x[2..$]&", "
end if
end if
end for
choices = length(wkeys)
history = repeat(1,choices)
hsum = 3
cplays = repeat(0,choices)
pplays = repeat(0,choices)
beats = repeat(repeat(0,choices),choices)
question[-2] = '?'
for i=1 to length(rules) do
{x,verb,y} = split(rules[i])
beats[find(x,what)][find(y,what)] = verb
end for
integer cmove, pmove, draws = 0, pwins = 0, cwins = 0
while 1 do
cmove = rand(hsum)
for i=1 to choices do
cmove -= history[i]
if cmove<=0 then
-- predicted user choice of i, find whatever beats it
for j=1 to choices do
if string(beats[j][i]) then
cmove = j
exit
end if
end for
exit
end if
end for
puts(1,question)
while 1 do
pmove = lower(wait_key())
if pmove='q' then exit end if
pmove = find(pmove,wkeys)
if pmove!=0 then exit end if
end while
if pmove='q' then exit end if
printf(1,"you: %s, me: %s, ",{what[pmove],what[cmove]})
cplays[cmove] += 1
pplays[pmove] += 1
if cmove=pmove then
printf(1,"a draw.\n")
draws += 1
else
if string(beats[cmove][pmove]) then
printf(1,"%s %s %s. I win.\n",{what[cmove],beats[cmove][pmove],what[pmove]})
cwins += 1
elsif string(beats[pmove][cmove]) then
printf(1,"%s %s %s. You win.\n",{what[pmove],beats[pmove][cmove],what[cmove]})
pwins += 1
else
?9/0 -- sanity check
end if
end if
history[pmove] += 1
hsum += 1
end while
printf(1,"\n\nYour wins:%d, My wins:%d, Draws:%d\n",{pwins,cwins,draws})
printf(1,"\n\nYour wins:%d, My wins:%d, Draws:%d\n",{pwins,cwins,draws})
printf(1," ") for i=1 to choices do printf(1,"%9s",what[i]) end for
printf(1,"\nyou: ") for i=1 to choices do printf(1,"%9d",pplays[i]) end for
printf(1,"\n me: ") for i=1 to choices do printf(1,"%9d",cplays[i]) end for

View file

@ -0,0 +1,54 @@
const rps = %w(r p s)
const msg = [
"Rock breaks scissors",
"Paper covers rock",
"Scissors cut paper",
]
say <<"EOT";
\n>> Rock Paper Scissors <<\n
** Enter 'r', 'p', or 's' as your play.
** Enter 'q' to exit the game.
** Running score shown as <your wins>:<my wins>
EOT
var plays = 0
var aScore = 0
var pScore = 0
var pcf = [0,0,0] # pcf = player choice frequency
var aChoice = 3.irand # ai choice for first play is completely random
loop {
var pi = Sys.scanln("Play: ")
pi == 'q' && break
var pChoice = rps.index(pi)
if (pChoice == -1) {
STDERR.print("Invalid input!\n")
next
}
++pcf[pChoice]
++plays
# show result of play
">> My play: %-8s".printf(rps[aChoice])
given ((aChoice - pChoice + 3) % 3) {
when (0) { say "Tie." }
when (1) { "%-*s %s".printlnf(30, msg[aChoice], 'My point'); aScore++ }
when (2) { "%-*s %s".printlnf(30, msg[pChoice], 'Your point'); pScore++ }
}
# show score
"%-6s".printf("%d:%d" % (pScore, aScore))
# compute ai choice for next play
given (plays.irand) { |rn|
case (rn < pcf[0]) { aChoice = 1 }
case (pcf[0]+pcf[1] > rn) { aChoice = 2 }
default { aChoice = 0 }
}
}