Penney's game: Difference between revisions
Content added Content deleted
m (→{{header|VBA}}) |
|||
Line 1,846: | Line 1,846: | ||
Hah, I win, and you lose! |
Hah, I win, and you lose! |
||
Wanna play again? n</pre> |
Wanna play again? n</pre> |
||
=={{header|Phix}}== |
|||
{{trans|C}} |
|||
Robert's robot's name is Robort. |
|||
<lang Phix>function trio(integer pick) |
|||
return substitute_all(sprintf("%03b",pick),"10","HT") |
|||
end function |
|||
function getuser(integer bot) |
|||
integer user |
|||
while 1 do |
|||
user = 8 -- (a bit that clears after 3 shifts) |
|||
printf(1,"Enter your sequence of 3 (H/T):"); |
|||
while user>7 do |
|||
integer c = upper(wait_key()) |
|||
if c=#1B then abort(0) end if -- (Escape) |
|||
if find(c,"HT") then |
|||
puts(1,c) |
|||
user = and_bits(user*2+(c='H'),0b111111) |
|||
end if |
|||
end while |
|||
if user!=bot then exit end if |
|||
printf(1,"\nYou may not pick the same as Robort!\n") |
|||
end while |
|||
printf(1,"\n") |
|||
return user |
|||
end function |
|||
function getbot(int user) |
|||
int bot = iff(user=-1?rand(8)-1 |
|||
:4-and_bits(user,2)*2+floor(user/2)) |
|||
printf(1,"Robort picks %s\n", {trio(bot)}) |
|||
return bot |
|||
end function |
|||
function rungame(integer user, bot) |
|||
/* We only need to store the last 3 tosses, as 0..7 */ |
|||
int last3 = rand(8)-1 |
|||
printf(1,"Rolling: %s",{trio(last3)}) |
|||
while 1 do |
|||
if user=last3 then |
|||
printf(1,"\nUser wins!\n") |
|||
return 1 |
|||
elsif bot=last3 then |
|||
printf(1,"\nRobort wins!\n") |
|||
return 0 |
|||
end if |
|||
last3 = and_bits(last3,3)*2+(rand(2)=1) |
|||
printf(1,"%c", iff(and_bits(last3,1) ? 'H' : 'T')) |
|||
sleep(0.5) |
|||
end while |
|||
end function |
|||
procedure main() |
|||
integer playerwins = 0, |
|||
totalgames = 0, |
|||
robortwins = 0 |
|||
/* Just use ctrl-c or Escape to exit */ |
|||
while 1 do |
|||
integer user = -1, |
|||
bot = -1 |
|||
printf(1,"\n") |
|||
if rand(2)=1 then |
|||
bot = getbot(-1) |
|||
user = getuser(bot) |
|||
else |
|||
user = getuser(-1) |
|||
bot = getbot(user) |
|||
end if |
|||
playerwins += rungame(user, bot) |
|||
totalgames += 1 |
|||
robortwins = totalgames-playerwins |
|||
printf(1,"Robort:%d You:%d out of %d games\n", |
|||
{robortwins, playerwins, totalgames}) |
|||
printf(1,"==================================\n") |
|||
end while |
|||
end procedure |
|||
main()</lang> |
|||
{{out}} |
|||
<pre> |
|||
Robort picks TTT |
|||
Enter your sequence of 3 (H/T):TTT |
|||
You may not pick the same as Robort! |
|||
Enter your sequence of 3 (H/T):HHH |
|||
Rolling: HTTT |
|||
Robort wins! |
|||
Robort:1 You:0 out of 1 games |
|||
================================== |
|||
Enter your sequence of 3 (H/T):HTH |
|||
Robort picks HHT |
|||
Rolling: THTTTHHHHHHT |
|||
Robort wins! |
|||
Robort:2 You:0 out of 2 games |
|||
================================== |
|||
Enter your sequence of 3 (H/T):TTH |
|||
Robort picks HTT |
|||
Rolling: THTT |
|||
Robort wins! |
|||
Robort:3 You:0 out of 3 games |
|||
================================== |
|||
Enter your sequence of 3 (H/T):HTH |
|||
Robort picks HHT |
|||
Rolling: TTHHHHHHT |
|||
Robort wins! |
|||
Robort:4 You:0 out of 4 games |
|||
================================== |
|||
Enter your sequence of 3 (H/T):HHT |
|||
Robort picks THH |
|||
Rolling: TTHH |
|||
Robort wins! |
|||
Robort:5 You:0 out of 5 games |
|||
================================== |
|||
Enter your sequence of 3 (H/T):THH |
|||
Robort picks TTH |
|||
Rolling: THH |
|||
User wins! |
|||
Robort:5 You:1 out of 6 games |
|||
================================== |
|||
</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |