Rock-paper-scissors: Difference between revisions
Content added Content deleted
(→{{header|Lasso}}: Rewritten to use correct Lasso 9 code, and optimized. Added win record tracking and display.) |
m (→{{header|Lasso}}: remove extraneous indenting) |
||
Line 1,717: | Line 1,717: | ||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
Notes: This implementation uses the default session handling in Lasso, and assumes it's running on a web server. User choices are passed in via HTTP as GET query parameters. |
Notes: This implementation uses the default session handling in Lasso, and assumes it's running on a web server. User choices are passed in via HTTP as GET query parameters. |
||
<lang Lasso> |
<lang Lasso>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' |
|||
} |
} |
||
} |
} |
||
Line 1,741: | Line 1,741: | ||
var( |
var( |
||
choice = web_request->param('choice')->asString, |
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 |
$controls |
||
Line 1,766: | Line 1,766: | ||
'User chose ' + $choice + br |
'User chose ' + $choice + br |
||
'Lasso chose ' + $computer_choice + br |
|||
winner($computer_choice->asString, $choice) + ' wins!' |
|||
$historic_choices->find($choice) = $historic_choices->find($choice)+1 |
$historic_choices->find($choice) = $historic_choices->find($choice)+1 |
||
Line 1,777: | Line 1,777: | ||
else |
else |
||
'Invalid Choice.'+ br + $controls |
'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 |
|||
^}</lang> |
|||
{{out}} |
{{out}} |
||
<pre>Rock Paper Scissors Quit (<- as links) |
<pre>Rock Paper Scissors Quit (<- as links) |