Bulls and cows: Difference between revisions
Content added Content deleted
(Simpler D entry) |
(→{{header|Lasso}}: Adding Lasso example) |
||
Line 1,964: | Line 1,964: | ||
please, enter four distincts digits |
please, enter four distincts digits |
||
Your guess? </pre> |
Your guess? </pre> |
||
=={{header|Lasso}}== |
|||
This game uses an HTML form to submit the answer. The random number and history are stored in a session using Lasso's built in session management. |
|||
<lang Lasso>[ |
|||
define randomizer() => { |
|||
local(n = string) |
|||
while(#n->size < 4) => { |
|||
local(r = integer_random(1,9)->asString) |
|||
#n !>> #r ? #n->append(#r) |
|||
} |
|||
return #n |
|||
} |
|||
define cowbullchecker(n::string,a::string) => { |
|||
integer(#n) == integer(#a) ? return (:true,map('cows'=0,'bulls'=4,'choice'=#a)) |
|||
local(cowbull = map('cows'=0,'bulls'=0,'choice'=#a),'checked' = array) |
|||
loop(4) => { |
|||
if(#checked !>> integer(#a->values->get(loop_count))) => { |
|||
#checked->insert(integer(#a->values->get(loop_count))) |
|||
if(integer(#n->values->get(loop_count)) == integer(#a->values->get(loop_count))) => { |
|||
#cowbull->find('bulls') += 1 |
|||
else(#n->values >> #a->values->get(loop_count)) |
|||
#cowbull->find('cows') += 1 |
|||
} |
|||
} |
|||
} |
|||
#cowbull->find('bulls') == 4 ? return (:true,map('cows'=0,'bulls'=4,'choice'=#a)) |
|||
return (:true,#cowbull) |
|||
} |
|||
session_start('user') |
|||
session_addvar('user', 'num') |
|||
session_addvar('user', 'historic_choices') |
|||
// set up rand |
|||
var(num)->isNotA(::string) ? var(num = randomizer) |
|||
var(historic_choices)->isNotA(::array) ? var(historic_choices = array) |
|||
local(success = false) |
|||
// check answer |
|||
if(web_request->param('a')->asString->size) => { |
|||
local(success,result) = cowbullchecker($num,web_request->param('a')->asString) |
|||
$historic_choices->insert(#result) |
|||
} |
|||
if(web_request->params->asStaticArray >> 'restart') => { |
|||
$num = randomizer |
|||
$historic_choices = array |
|||
} |
|||
] |
|||
<h1>Bulls and Cows</h1> |
|||
<p>Guess the 4-digit number...</p> |
|||
<p>Your win if the guess is the same as the randomly chosen number.<br> |
|||
- A score of one bull is accumulated for each digit in your guess that equals the corresponding digit in the randomly chosen initial number.<br> |
|||
- A score of one cow is accumulated for each digit in your guess that also appears in the randomly chosen number, but in the wrong position. |
|||
</p> |
|||
[ |
|||
local(win = false) |
|||
if($historic_choices->size) => { |
|||
with c in $historic_choices do => {^ |
|||
'<p>'+#c->find('choice')+': Bulls: '+#c->find('bulls')+', Cows: '+#c->find('cows') |
|||
if(#c->find('bulls') == 4) => {^ |
|||
' - YOU WIN!' |
|||
#win = true |
|||
^} |
|||
'</p>' |
|||
^} |
|||
} |
|||
if(not #win) => {^ |
|||
] |
|||
<form action="?" method="post"> |
|||
<input name="a" value="[web_request->param('a')->asString]" size="5"> |
|||
<input type="submit" name="guess"> |
|||
<a href="?restart">Restart</a> |
|||
</form> |
|||
[else |
|||
'<a href="?restart">Restart</a>' |
|||
^}] |
|||
</lang> |
|||
{{out}} |
|||
Game in progress: |
|||
<pre>Bulls and Cows |
|||
Guess the 4-digit number... |
|||
Your win if the guess is the same as the randomly chosen number. |
|||
- A score of one bull is accumulated for each digit in your guess that equals the corresponding digit in the randomly chosen initial number. |
|||
- A score of one cow is accumulated for each digit in your guess that also appears in the randomly chosen number, but in the wrong position. |
|||
4567: Bulls: 0, Cows: 1 |
|||
4567: Bulls: 0, Cows: 1 |
|||
(input box) (submit button) Restart</pre> |
|||
{{out}} |
|||
Game in to completion: |
|||
<pre>Bulls and Cows |
|||
Guess the 4-digit number... |
|||
Your win if the guess is the same as the randomly chosen number. |
|||
- A score of one bull is accumulated for each digit in your guess that equals the corresponding digit in the randomly chosen initial number. |
|||
- A score of one cow is accumulated for each digit in your guess that also appears in the randomly chosen number, but in the wrong position. |
|||
1234: Bulls: 0, Cows: 2 |
|||
1256: Bulls: 0, Cows: 1 |
|||
1789: Bulls: 0, Cows: 1 |
|||
1222: Bulls: 0, Cows: 0 |
|||
3456: Bulls: 1, Cows: 2 |
|||
3564: Bulls: 0, Cows: 3 |
|||
3567: Bulls: 0, Cows: 2 |
|||
8564: Bulls: 0, Cows: 2 |
|||
3564: Bulls: 0, Cows: 3 |
|||
4365: Bulls: 0, Cows: 3 |
|||
5436: Bulls: 2, Cows: 1 |
|||
5478: Bulls: 2, Cows: 0 |
|||
5463: Bulls: 3, Cows: 0 |
|||
5468: Bulls: 2, Cows: 0 |
|||
5493: Bulls: 4, Cows: 0 - YOU WIN! |
|||
Restart</pre> |
|||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |