Sandbox: Difference between revisions
Content added Content deleted
(26 intermediate revisions by 11 users not shown) | |||
Line 1: | Line 1: | ||
=={{header|Liberty Basic}}== |
|||
== Which mu is the correct mu? == |
|||
{{works with|LB Booster}} |
|||
* µ µ U+00B5 [[:Category:µC++]] |
|||
<syntaxhighlight lang="Liberty Basic> |
|||
* μ μ U+03BC [[:Category:μC++]] |
|||
* M U+004D [[:Category:MC++]] |
|||
* Μ U+039C [[:Category:ΜC++]] |
|||
NoMainWin |
|||
== Can you use a data URL? == |
|||
WindowWidth=900 |
|||
[[#Text|Text]] |
|||
WindowHeight=720 |
|||
[data:text/html,Hello%20World No.] |
|||
BackgroundColor$ = "191 191 255" ' buttonface default |
|||
Global Deck, MaxDecks |
|||
StaticText #1.Debug "", 0, 0, 600, 20 |
|||
== What if you transclude a category? == |
|||
StaticText #1.StaticText "Ten Decks of Poker Hands", 50, 50, 3000, 40 |
|||
{{Category:Maintenance}} |
|||
Button #1.Deal "Deal", [Start], UL, 700, 180, 80, 40 |
|||
-- Okay, the member list doesn't show up. No good. |
|||
Button #1.TenThousand "10,000", [TenThousand], UL, 700, 250, 80, 40 |
|||
Button #1.Stats "History", ShowStats, UL, 700, 320, 80, 40 |
|||
Button #1.Quit "Quit", Quit, UL, 700, 390, 80, 40 |
|||
TextEditor #1.TextEditor 50, 100, 600, 500 |
|||
open "POKER HANDS" for dialog as #1 |
|||
=== MultiCategorySearch === |
|||
#1 "TrapClose Quit" |
|||
{{Special:MultiCategorySearch/include=Maintenance}} |
|||
#1 "Font Ariel 12 Bold" |
|||
#1.StaticText "!Font Ariel 16 Bold" |
|||
#1.TextEditor "!Font Courier_New 14 Bold" |
|||
if not(exists("Poker Hands.txt")) then #1.Stats "!Disable" |
|||
MaxDecks=10 |
|||
wait |
|||
[TenThousand] |
|||
=== Semantic MediaWiki (SMW) === |
|||
TenThousand = 1 |
|||
* http://semantic-mediawiki.org/wiki/Help:Inline_queries |
|||
[Start] |
|||
* http://semantic-mediawiki.org/wiki/Help:Result_formats |
|||
if TenThousand then |
|||
MaxDecks=10000 |
|||
#1.TextEditor "!Hide" |
|||
#1.Deal "!Hide" |
|||
#1.TenThousand "!Hide" |
|||
#1.Stats "!Hide" |
|||
#1.Quit "!Hide" |
|||
#1.StaticText "Ten Thousand Decks of Poker Hands" |
|||
end if |
|||
Deck += 1 |
|||
if TenThousand then #1.Debug Str$(Deck) |
|||
if Deck>MaxDecks then Deck -= 1: call Quit |
|||
#1.TextEditor "!cls" |
|||
call ShuffleDeck 0 |
|||
'call TestDeck |
|||
NextCard=1 |
|||
for y=1 to 10 |
|||
for x=1 to 5 |
|||
y$ = A$(NextCard) |
|||
B$(x) = ConvertHiCard$(y$) |
|||
NextCard += 1 |
|||
next x |
|||
sort B$(), 1, 5 |
|||
for x=NextCard-5 to NextCard-1 |
|||
#1.TextEditor A$(x)+" "; |
|||
next x |
|||
#1.TextEditor " "; |
|||
Values$="" 'determine high value of hand |
|||
for x=1 to 5 |
|||
Values$ = Values$ + left$(B$(x),1) |
|||
next x |
|||
HiValue$ = RealValue$(right$(Values$,1)) |
|||
z=0: Flush=0: Straight=0: Royal=0: FourKind=0 |
|||
{{#ask: [[Category:Maintenance]] |
|||
ThreeKind=0: Pair=0: TwoPair=0: FullHouse=0 |
|||
| intro=These pages need the maintenance!!! (This shows all the pages in [[:Category:Maintenance]] or its subcategories.) |
|||
if Flush() then Flush=1 |
|||
| format=category |
|||
x = Straight() |
|||
| limit=20 |
|||
if x then Straight=1: if x=9 then Royal=1 |
|||
}} |
|||
z$ = Kind$() |
|||
Value$ = RealValue$(right$(z$,1)) |
|||
z=val(left$(z$, len(z$)-1)) |
|||
if z=41 then FourKind=1 |
|||
if z=32 then FullHouse=1 |
|||
if z=31 then ThreeKind=1 |
|||
if z=22 then TwoPair=1 |
|||
if z=21 then Pair=1 |
|||
select case |
|||
case Straight and Royal and Flush: #1.TextEditor "Royal Flush": Stats(1) += 1 |
|||
case Straight and Flush: #1.TextEditor "Straight Flush, " + HiValue$ + " high": Stats(2) += 1 |
|||
case FourKind: #1.TextEditor "Four of a kind, " + Value$ + "s": Stats(3) += 1 |
|||
case FullHouse: #1.TextEditor "Full House, " + Value$ + "s high": Stats(4) += 1 |
|||
case Flush: #1.TextEditor "Flush, " + HiValue$ + " high": Stats(5) += 1 |
|||
case Straight: #1.TextEditor "Straight, " + HiValue$ + " high": Stats(6) += 1 |
|||
case ThreeKind: #1.TextEditor "Three of a kind, " + Value$ + "s": Stats(7) += 1 |
|||
case TwoPair: #1.TextEditor "Two Pair, " + Value$ + " high": Stats(8) += 1 |
|||
case Pair: #1.TextEditor "Pair " + Value$ + "s": Stats(9) += 1 |
|||
case else: #1.TextEditor HiValue$ + " high" |
|||
end select |
|||
next y |
|||
#1.TextEditor "" |
|||
#1.TextEditor "Deck #" + str$(Deck) |
|||
if TenThousand then goto [Start] else wait |
|||
function RealValue$(Value$) |
|||
===Other pages=== |
|||
select case Value$ |
|||
case "A": RealValue$="T" |
|||
case "B": RealValue$="J" |
|||
case "C": RealValue$="Q" |
|||
case "D": RealValue$="K" |
|||
case "E": RealValue$="A" |
|||
case else: RealValue$=Value$ |
|||
end select |
|||
end function |
|||
sub SaveStats Deck |
|||
These are other pages related to [[AWK]] OR [[Dc]] which require attention; with your knowledge and assistance, we can improve the quality of the site's content. |
|||
Stats(0) = 10*Deck |
|||
if not(exists("Poker Hands.txt")) then |
|||
open "Poker Hands.txt" for output as #2 |
|||
for x=0 to 9 |
|||
print #2 Stats(x) |
|||
next |
|||
close #2 |
|||
#1.Stats "!Enable" |
|||
else |
|||
open "Poker Hands.txt" for input as #2 |
|||
for x=0 to 9 |
|||
input #2 History(x) |
|||
next |
|||
close #2 |
|||
for x=0 to 9 |
|||
History(x) += Stats(x) |
|||
next |
|||
open "Poker Hands.txt" for output as #2 |
|||
for x=0 to 9 |
|||
print #2 History(x) |
|||
next |
|||
close #2 |
|||
end if |
|||
end sub |
|||
sub ShowStats |
|||
{{#ask: <q>[[implementation of::AWK]] OR [[implementation of::Dc]]</q> [[is stub::true]] |
|||
if exists("Poker Hands.txt") then |
|||
| format=category |
|||
open "Poker Hands.txt" for input as #2 |
|||
| limit=500 |
|||
for x=0 to 9 |
|||
| default=<h3>No pages found.</h3> |
|||
input #2 History(x) |
|||
}} |
|||
next |
|||
close #2 |
|||
#1.TextEditor "!cls" |
|||
for x=1 to 9 |
|||
Total += History(x) |
|||
next x |
|||
Nothing = History(0) - Total |
|||
for x=0 to 9 |
|||
==TITLE== |
|||
#1.TextEditor using("###,### ", History(x)); |
|||
code |
|||
select case x |
|||
---- |
|||
case 0: #1.TextEditor "hands " |
|||
== pre test == |
|||
case 1: #1.TextEditor "royal flush " + using("##.# %", History(x)/History(0)*100) |
|||
<div class="infobox" style="width: 2in"> |
|||
case 2: #1.TextEditor "straight flush " + using("##.# %", History(x)/History(0)*100) |
|||
<big>'''Programming Task'''</big><br /> |
|||
case 3: #1.TextEditor "four of a kind " + using("##.# %", History(x)/History(0)*100) |
|||
This is a programming task. It lays out a problem which Rosetta Code users are encouraged to solve, using languages they know. |
|||
case 4: #1.TextEditor "full house " + using("##.# %", History(x)/History(0)*100) |
|||
case 5: #1.TextEditor "flush " + using("##.# %", History(x)/History(0)*100) |
|||
case 6: #1.TextEditor "straight " + using("##.# %", History(x)/History(0)*100) |
|||
case 7: #1.TextEditor "three of a kind " + using("##.# %", History(x)/History(0)*100) |
|||
case 8: #1.TextEditor "two pair " + using("##.# %", History(x)/History(0)*100) |
|||
case 9: #1.TextEditor "pair " + using("##.# %", History(x)/History(0)*100) |
|||
end select |
|||
next |
|||
#1.TextEditor using("###,### ", Nothing) + "nothing " + using("###.# %", Nothing/History(0)*100) |
|||
end if |
|||
end sub |
|||
function Kind$() |
|||
Code examples should be formatted along the lines of one of the existing [[Help:Example_Programming_Example|prototypes]].{{proggit}} |
|||
for x=1 to 5 |
|||
</div> |
|||
C$(x) = left$(B$(x), 1) |
|||
<pre style="height:30ex;overflow:scroll"> |
|||
next x |
|||
Test |
|||
if C$(1) = C$(2) then 'check for Lo4 |
|||
Another line |
|||
Lo2=1 |
|||
Yet another line, this time one which clearly is much longer than the width of the browser window, at least assuming normal screen dimensions and font settings, and assuming the browser window is not wider than the screen (which would be impractical anyway). |
|||
if C$(2) = C$(3) then |
|||
1 |
|||
Lo2=0: Lo3=1 |
|||
2 |
|||
if C$(3) = C$(4) then |
|||
3 |
|||
Lo3=0: Kind$="41" + left$(C$(4),1): exit function |
|||
4 |
|||
end if |
|||
5 |
|||
end if |
|||
6 |
|||
end if |
|||
7 |
|||
8 |
|||
if C$(5) = C$(4) then 'check for Hi4 |
|||
9 |
|||
Hi2=1 |
|||
10 |
|||
if C$(4) = C$(3) then |
|||
11 |
|||
Hi2=0: Hi3=1 |
|||
12 |
|||
if C$(3) = C$(2) then |
|||
13 |
|||
Hi3=0: Kind$="41" + left$(C$(5),1): exit function |
|||
14 |
|||
end if |
|||
15 |
|||
end if |
|||
16 |
|||
end if |
|||
17 |
|||
18 |
|||
if Lo3 then 'check for Full House and 3Kind |
|||
19 |
|||
if C$(4) = C$(5) then |
|||
20 |
|||
Kind$="32" + left$(C$(3),1): exit function |
|||
21 |
|||
else |
|||
</pre> |
|||
Kind$="31" + left$(C$(3),1): exit function |
|||
==Testing new language tags== |
|||
end if |
|||
===Z80=== |
|||
end if |
|||
<lang z80>; some random instructions |
|||
if Hi3 then |
|||
if C$(1) = C$(2) then |
|||
ld b, 9Ah |
|||
Kind$="32" + left$(C$(5),1): exit function |
|||
add a, b |
|||
else |
|||
Kind$="31" + left$(C$(5),1): exit function |
|||
rst 0 ; reboot :-)</lang> |
|||
end if |
|||
end if |
|||
if C$(2) = C$(3) and C$(3) = C$(4) then 'Mid3 |
|||
Kind$="31" + left$(C$(4),1): exit function |
|||
end if |
|||
if Lo2 and Hi2 then 'check for pairs |
|||
Kind$="22" + left$(C$(5),1): exit function |
|||
end if |
|||
if Lo2 and (C$(3)=C$(4)) then |
|||
Kind$="22" + left$(C$(4),1): exit function |
|||
end if |
|||
if Hi2 and (C$(3)=C$(2)) then |
|||
Kind$="22" + left$(C$(5),1): exit function |
|||
end if |
|||
if Lo2 then Kind$="21" + left$(C$(2),1) |
|||
if Hi2 then Kind$="21" + left$(C$(5),1) |
|||
if C$(2)=C$(3) then Kind$="21" + left$(C$(3),1) |
|||
if C$(3)=C$(4) then Kind$="21" + left$(C$(4),1) |
|||
end function |
|||
function Straight() |
|||
===Whitespace=== |
|||
Order$="23456789ABCDEF" |
|||
<lang whitespace>This may not be a meaningful whitespace |
|||
for x=1 to 5 |
|||
program, |
|||
Ranks$ = Ranks$ + left$(B$(x), 1) |
|||
but it's |
|||
next x |
|||
just a highlighting test |
|||
x = instr(Order$, Ranks$) |
|||
anyway.</lang> |
|||
if x then Straight=x |
|||
end function |
|||
function Flush() |
|||
===AviSynth=== |
|||
Flush=1 |
|||
<lang avisynth>AviSource("D:\clip.avi") |
|||
for x=1 to 5 |
|||
ConvertToYUY2() |
|||
Suits$ = Suits$ + right$(B$(x), 1) |
|||
PixieDust(5)</lang> |
|||
next x |
|||
for x=2 to 5 |
|||
if mid$(Suits$, x, 1) <> left$(Suits$, 1) then Flush=0: exit function |
|||
next x |
|||
end function |
|||
sub ShuffleDeck Jokers |
|||
===POV-Ray=== |
|||
Jokers = int(abs(Jokers)): if Jokers>4 then Jokers=4 |
|||
<lang povray>#declare factorial = function(C) { prod(i, 1, C, i) } |
|||
Size=52 + Jokers |
|||
#declare A = factorial(5); |
|||
dim CardDeck$(Size+10,1), A$(Size+10) 'Open new card deck |
|||
sphere{0,A} // a sphere</lang> |
|||
[Start] |
|||
for x=1 to Size |
|||
CardDeck$(x,0) = "99" |
|||
next x |
|||
for x=1 to Size |
|||
===Text=== |
|||
1 y=RandomNumber(1,Size) |
|||
<lang text>Is this actually a programming language, or really just plain text?</lang> |
|||
if CardDeck$(y,0)="99" then CardDeck$(y,0)=str$(x) else goto 1 |
|||
next x |
|||
for x=1 to Size 'Examine shuffled deck |
|||
===What happens if a language doesn't exist?=== |
|||
if CardDeck$(x,0)=str$(x) then z = 1: exit for |
|||
<lang somelanguagethatdoesnotexist>some code |
|||
next x |
|||
more code; |
|||
if z then z=0: goto [Start] |
|||
for x=1 to Size 'Save shuffled deck |
|||
(Probably this is an obfuscated language :-))</lang> |
|||
A$(x) = CardFace$(val(CardDeck$(x,0))) |
|||
next x |
|||
A$(0) = str$(Size) |
|||
end sub |
|||
function CardFace$(n) |
|||
==Whitespace test with unicode ZERO WIDTH NO-BREAK SPACE character (U+FEFF)== |
|||
select case n |
|||
<lang whitespace> |
|||
case 1: CardFace$="AD" |
|||
case 2: CardFace$="2D" |
|||
case 3: CardFace$="3D" |
|||
case 4: CardFace$="4D" |
|||
case 5: CardFace$="5D" |
|||
</lang> |
|||
case 6: CardFace$="6D" |
|||
testing |
|||
case 7: CardFace$="7D" |
|||
case 8: CardFace$="8D" |
|||
case 9: CardFace$="9D" |
|||
case 10: CardFace$="TD" |
|||
case 11: CardFace$="JD" |
|||
case 12: CardFace$="QD" |
|||
case 13: CardFace$="KD" |
|||
case 14: CardFace$="AC" |
|||
case 15: CardFace$="2C" |
|||
case 16: CardFace$="3C" |
|||
case 17: CardFace$="4C" |
|||
case 18: CardFace$="5C" |
|||
case 19: CardFace$="6C" |
|||
case 20: CardFace$="7C" |
|||
case 21: CardFace$="8C" |
|||
case 22: CardFace$="9C" |
|||
case 23: CardFace$="TC" |
|||
case 24: CardFace$="JC" |
|||
case 25: CardFace$="QC" |
|||
case 26: CardFace$="KC" |
|||
case 27: CardFace$="AH" |
|||
case 28: CardFace$="2H" |
|||
case 29: CardFace$="3H" |
|||
case 30: CardFace$="4H" |
|||
case 31: CardFace$="5H" |
|||
case 32: CardFace$="6H" |
|||
case 33: CardFace$="7H" |
|||
case 34: CardFace$="8H" |
|||
case 35: CardFace$="9H" |
|||
case 36: CardFace$="TH" |
|||
case 37: CardFace$="JH" |
|||
case 38: CardFace$="QH" |
|||
case 39: CardFace$="KH" |
|||
case 40: CardFace$="AS" |
|||
case 41: CardFace$="2S" |
|||
case 42: CardFace$="3S" |
|||
case 43: CardFace$="4S" |
|||
case 44: CardFace$="5S" |
|||
case 45: CardFace$="6S" |
|||
case 46: CardFace$="7S" |
|||
case 47: CardFace$="8S" |
|||
case 48: CardFace$="9S" |
|||
case 49: CardFace$="TS" |
|||
case 50: CardFace$="JS" |
|||
case 51: CardFace$="QS" |
|||
case 52: CardFace$="KS" |
|||
case 53: CardFace$="X1" |
|||
case 54: CardFace$="X2" |
|||
case 55: CardFace$="X3" |
|||
case 56: CardFace$="X4" |
|||
end select |
|||
end function |
|||
function RandomNumber(a, b) |
|||
== Impl needed == |
|||
smaller = min(a, b) |
|||
Tcl pages: {{PAGESINCAT:Tcl}} |
|||
range = abs(int(a-b))+1 |
|||
if range < 1 then exit function |
|||
r = int(rnd()*range) |
|||
RandomNumber = r + smaller |
|||
end function |
|||
function ConvertHiCard$(Card$) |
|||
Tcl/Omit pages: {{PAGESINCAT:Tcl/Omit}} |
|||
select case left$(Card$,1) |
|||
case "T": left$(Card$,1)="A" |
|||
case "J": left$(Card$,1)="B" |
|||
case "Q": left$(Card$,1)="C" |
|||
case "K": left$(Card$,1)="D" |
|||
case "A": left$(Card$,1)="E" |
|||
case "X": left$(Card$,1)="F" |
|||
end select |
|||
ConvertHiCard$ = Card$ |
|||
end function |
|||
sub TestDeck |
|||
Programming tasks: {{PAGESINCAT:Programming Tasks}} |
|||
data KD, KC, KS, AH, AS ' full house |
|||
data 6D, 6C, 6S, 6H, 8H ' four of a kind (io) |
|||
data 2D, 4S, 4C, TH, TD ' two pair |
|||
data 4H, 5H, 6H, 7H, 8H ' straight flush |
|||
data 9S, QD, QC, QH, 3D ' three of a kind |
|||
data 6H, 7D, 8C, 9C, TS ' straight |
|||
data TH, AH, AS, AC, AD ' four of a kind (hI) |
|||
data 3S, 5S, 7S, 9S, JS ' flush |
|||
data AD, KD, QD, JD, TD ' royal flush |
|||
data 2C, 2D, 3H, 4S, 5C ' one pair |
|||
dim A$(50) |
|||
for x=1 to 50 |
|||
read A$(x) |
|||
next x |
|||
end sub |
|||
function exists(FileName$) |
|||
== Property query test == |
|||
files "", FileName$, FileDir$() |
|||
The programming languages are {{#ask: [[is language::true]]}} |
|||
FileCount$ = FileDir$(0, 0) |
|||
: This uses [[Property:is language]] |
|||
exists = val(FileCount$) |
|||
end function |
|||
sub Quit |
|||
---- |
|||
if Deck = MaxDecks then call SaveStats Deck |
|||
close #1 |
|||
end |
|||
end sub |
|||
</syntaxhighlight> |
|||
This should turn up all languages and libraries which provide Graphics: |
|||
{{out}} |
|||
{{#ask: [[provides::Capability:Graphics]] | default=Or not.}} |
|||
<pre> |
|||
JH 8C 8S TH KC Pair 8s |
|||
9C JS 3S 5D 3D Pair 3s |
|||
TD QH 6S TS AD Pair Ts |
|||
AH 9D KD 3H AC Pair As |
|||
QS 6D JC QD 2H Pair Qs |
|||
4H 2D 5S 4C JD Pair 4s |
|||
KH 6C 4S 7C 5H K high |
|||
3C 7D 8D 4D 7H Pair 7s |
|||
9S 2S 7S 9H 6H Pair 9s |
|||
AS QC 5C TC 2C A high |
|||
Deck #1 |
|||
This should turn up anything which does ''not'' provide Graphics: |
|||
</pre> |
|||
{{#ask: [[provides::!Capability:Graphics]] | default=Unless it doesn't.}} |
|||
: as I learned now, it instead turns up anything which provides something other than Graphics. |
|||
=={{header|Swift}}== |
|||
This should turn up anything which provides first class functions: |
|||
{{works with|swift|5.9}} |
|||
{{#ask: [[provides::Capability:First class functions]] | default=I would have hoped.}} |
|||
<syntaxhighlight lang="Swift"> |
|||
struct SplitMix64: RandomNumberGenerator { |
|||
var state: UInt64 |
|||
init(seed: UInt64) { |
|||
state = seed |
|||
} |
|||
mutating func next() -> UInt64 { |
|||
state &+= 0x9e3779b97f4a7c15 |
|||
var z = state |
|||
z = (z ^ (z >> 30)) &* 0xbf58476d1ce4e5b9 |
|||
z = (z ^ (z >> 27)) &* 0x94d049bb133111eb |
|||
return z ^ (z >> 31) |
|||
} |
|||
mutating func nextFloat() -> Float64 { |
|||
Float64(next() >> 11) * 0x1.0p-53 |
|||
} |
|||
} |
|||
do { |
|||
And this should be the intersection of the previous two sets: |
|||
var split = SplitMix64(seed: 1234567) |
|||
{{#ask: [[provides::!Capability:Graphics]] [[provides::Capability:First class functions]] | default=Which shouldn't be empty.}} |
|||
print(split) |
|||
for _ in 0..<5 { |
|||
print(split.next()) |
|||
} |
|||
split = .init(seed: 987654321) |
|||
print("\n\(split)") |
|||
var counts = [0, 0, 0, 0, 0] |
|||
for _ in 0..<100_000 { |
|||
let i = Int(split.nextFloat() * 5.0) |
|||
counts[i] += 1 |
|||
} |
|||
for (i, count) in zip(0..., counts) { |
|||
print("\(i): \(count)") |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
SplitMix64(state: 1234567) |
|||
6457827717110365317 |
|||
3203168211198807973 |
|||
9817491932198370423 |
|||
4593380528125082431 |
|||
16408922859458223821 |
|||
SplitMix64(state: 987654321) |
|||
This should turn up anything that provides ''or'' allows Graphics: |
|||
0: 20027 |
|||
{{#ask: [[provides::Capability:Graphics]] OR [[allows::Capability:Graphics]] | default=There should be something here.}} |
|||
1: 19892 |
|||
2: 20073 |
|||
3: 19978 |
|||
4: 20030 |
|||
</pre> |
|||
=={{header|Java}}== |
|||
This should turn up anything implemented in a language that provides graphics: |
|||
{{#ask: [[Implemented in language.provides::Capability:Graphics]] | default=Nothing!?}} |
|||
This is a direct translation of [[Tamagotchi emulator#Go|the Go version]]. As such, the output will be similar if not identical. |
|||
Does "implemented in language" work? Let's see everything implemented in Unlambda: |
|||
{{#ask: [[Implemented in language::Unlambda]] | default=Empty!}} |
|||
The code does not use any Java 8+ features so it may function on lower versions. |
|||
This should turn up anything implemented in a language that allows graphics: |
|||
<syntaxhighlight lang="Java"> |
|||
{{#ask: [[Implemented in language.allows::Capability:Graphics]] | default=Nothing!?}} |
|||
package com.mt; |
|||
import java.util.ArrayList; |
|||
This might turn up anything which has a "provides" property: |
|||
import java.util.List; |
|||
{{#ask: [[provides::Capability:+]] | default=Or it might not. :-)}} |
|||
import java.util.Random; |
|||
import java.util.Scanner; |
|||
class Tamagotchi { |
|||
What about this: |
|||
public String name; |
|||
{{#ask: [[provides::+]] | default=No.}} |
|||
public int age,bored,food,poop; |
|||
} |
|||
public class TamagotchiGame { |
|||
Is [[Classes]] implemented in C++? |
|||
Tamagotchi tama;//current Tamagotchi |
|||
{{#ask: [[Classes]] [[Implemented in language::C++]] | format=count}} (1 = yes, 0 = no). |
|||
Random random = new Random(); // pseudo random number generator |
|||
String[] verbs = { |
|||
"Ask", "Ban", "Bash", "Bite", "Break", "Build", |
|||
"Cut", "Dig", "Drag", "Drop", "Drink", "Enjoy", |
|||
"Eat", "End", "Feed", "Fill", "Force", "Grasp", |
|||
"Gas", "Get", "Grab", "Grip", "Hoist", "House", |
|||
"Ice", "Ink", "Join", "Kick", "Leave", "Marry", |
|||
"Mix", "Nab", "Nail", "Open", "Press", "Quash", |
|||
"Rub", "Run", "Save", "Snap", "Taste", "Touch", |
|||
"Use", "Vet", "View", "Wash", "Xerox", "Yield", |
|||
}; |
|||
String[] nouns = { |
|||
"arms", "bugs", "boots", "bowls", "cabins", "cigars", |
|||
"dogs", "eggs", "fakes", "flags", "greens", "guests", |
|||
"hens", "hogs", "items", "jowls", "jewels", "juices", |
|||
"kits", "logs", "lamps", "lions", "levers", "lemons", |
|||
"maps", "mugs", "names", "nests", "nights", "nurses", |
|||
"orbs", "owls", "pages", "posts", "quests", "quotas", |
|||
"rats", "ribs", "roots", "rules", "salads", "sauces", |
|||
"toys", "urns", "vines", "words", "waters", "zebras", |
|||
}; |
|||
String[] boredIc ons = {"💤", "💭", "❓"}; |
|||
String[] foodIcons = {"🍼", "🍔", "🍟", "🍰", "🍜"}; |
|||
String[] poopIcons = {"💩"}; |
|||
String[] sickIcons1 = {"😄", "😃", "😀", "😊", "😎", "👍"};//ok |
|||
String[] sickIcons2 = {"😪", "😥", "😰", "😓"};//ailing |
|||
String[] sickIco ns3 = {"😩", "😫"};//bad |
|||
String[] sickIcons4 = {"😡", "😱"};//very bad |
|||
String[] sickIcons5 = {"❌", "💀", "👽", "😇"};//dead |
|||
String brace(String string) { |
|||
return String.format("{ %s }", string); |
|||
} |
|||
void create(String name) { |
|||
tama = new Tamagotchi(); |
|||
tama.name = name; |
|||
tama.age = 0; |
|||
tama.bored = 0; |
|||
tama.food = 2; |
|||
tama.poop = 0; |
|||
} |
|||
boolean alive() { // alive if sickness <= 10 |
|||
return sickness() <= 10; |
|||
} |
|||
void feed() { |
|||
tama.food++; |
|||
} |
|||
void play() {//may or may not help with boredom |
|||
tama.bored = Math.max(0, tama.bored - random.nextInt(2)); |
|||
} |
|||
void talk() { |
|||
String verb = verbs[random.nextInt(verbs.length)]; |
|||
String noun = nouns[random.nextInt(nouns.length)]; |
|||
System.out.printf("😮 : %s the %s.%n", verb, noun); |
|||
tama.bored = Math.max(0, tama.bored - 1); |
|||
} |
|||
void clean() { |
|||
tama.poop = Math.max(0, tama.poop - 1); |
|||
} |
|||
void idle() {//renamed from wait() due to wait being an existing method from the Object class |
|||
tama.age++; |
|||
tama.bored += random.nextInt(2); |
|||
tama.food = Math.max(0, tama.food - 2); |
|||
tama.poop += random.nextInt(2); |
|||
} |
|||
String status() {// get boredom/food/poop icons |
|||
if(alive()) { |
|||
StringBuilder b = new StringBuilder(), |
|||
f = new StringBuilder(), |
|||
p = new StringBuilder(); |
|||
for(int i = 0; i < tama.bored; i++) { |
|||
b.append(boredIcons[random.nextInt(boredIcons.length)]); |
|||
} |
|||
for(int i = 0; i < tama.food; i++) { |
|||
f.append(foodIcons[random.nextInt(foodIcons.length)]); |
|||
} |
|||
for(int i = 0; i < tama.poop; i++) { |
|||
p.append(poopIcons[random.nextInt(poopIcons.length)]); |
|||
} |
|||
return String.format("%s %s %s", brace(b.toString()), brace(f.toString()), brace(p.toString())); |
|||
} |
|||
return " R.I.P"; |
|||
} |
|||
//too much boredom/food/poop |
|||
int sickness() { |
|||
//dies at age 42 at the latest |
|||
return tama.poop + tama.bored + Math.max(0, tama.age - 32) + Math.abs(tama.food - 2); |
|||
} |
|||
//get health status from sickness level |
|||
void health() { |
|||
int s = sickness(); |
|||
String icon; |
|||
switch(s) { |
|||
case 0: |
|||
case 1: |
|||
case 2: |
|||
icon = sickIcons1[random.nextInt(sickIcons1.length)]; |
|||
break; |
|||
case 3: |
|||
case 4: |
|||
icon = sickIcons2[random.nextInt(sickIcons2.length)]; |
|||
break; |
|||
case 5: |
|||
case 6: |
|||
icon = sickIcons3[random.nextInt(sickIcons3.length)]; |
|||
break; |
|||
case 7: |
|||
case 8: |
|||
case 9: |
|||
case 10: |
|||
icon = sickIcons4[random.nextInt(sickIcons4.length)]; |
|||
break; |
|||
default: |
|||
icon = sickIcons5[random.nextInt(sickIcons5.length)]; |
|||
break; |
|||
} |
|||
System.out.printf("%s (🎂 %d) %s %d %s%n%n", tama.name, tama.age, icon, s, status()); |
|||
} |
|||
void blurb() { |
|||
System.out.println("When the '?' prompt appears, enter an action optionally"); |
|||
System.out.println("followed by the number of repetitions from 1 to 9."); |
|||
System.out.println("If no repetitions are specified, one will be assumed."); |
|||
System.out.println("The available options are: feed, play, talk, clean or wait.\n"); |
|||
} |
|||
public static void main(String[] args) { |
|||
TamagotchiGame game = new TamagotchiGame(); |
|||
game.random.setSeed(System.nanoTime()); |
|||
System.out.println(" TAMAGOTCHI EMULATOR"); |
|||
System.out.println(" ===================\n"); |
|||
Scanner scanner = new Scanner(System.in); |
|||
System.out.print("Enter the name of your tamagotchi : "); |
|||
String name = scanner.nextLine().toLowerCase().trim(); |
|||
game.create(name); |
|||
System.out.printf("%n%s (age) health {bored} {food} {poop}%n%n", "name"); |
|||
game.health(); |
|||
game.blurb(); |
|||
ArrayList<String> commands = new ArrayList<>(List.of("feed", "play", "talk", "clean", "wait")); |
|||
int count = 0; |
|||
while(game.alive()) { |
|||
System.out.print("? "); |
|||
String input = scanner.nextLine().toLowerCase().trim(); |
|||
String[] items = input.split(" "); |
|||
if(items.length > 2) continue; |
|||
String action = items[0]; |
|||
if(!commands.contains(action)) { |
|||
continue; |
|||
} |
|||
int reps = 1; |
|||
if(items.length == 2) { |
|||
reps = Integer.parseInt(items[1]); |
|||
} else { |
|||
reps = 1; |
|||
} |
|||
for(int i = 0; i < reps; i++) { |
|||
switch(action) { |
|||
case "feed": |
|||
game.feed(); |
|||
break; |
|||
case "play": |
|||
game.play(); |
|||
break; |
|||
case "talk": |
|||
game.talk(); |
|||
break; |
|||
case "wait": |
|||
game.idle(); |
|||
break; |
|||
} |
|||
//simulate a wait on every third (non-wait) action |
|||
if(!action.equals("wait")) { |
|||
count++; |
|||
if(count%3==0) { |
|||
game.idle(); |
|||
} |
|||
} |
|||
} |
|||
game.health(); |
|||
} |
|||
scanner.close(); |
|||
} |
|||
} |
|||
Is [[Classes]] implemented in Unlambda? |
|||
{{#ask: [[Classes]] [[Implemented in language::Unlambda]] | format=count}} (1 = yes, 0 = no). |
|||
</syntaxhighlight> |
|||
The following assumes a (currently non-existent) template "ignore" which takes an argument and ignores it. With that template, if the task [[Mutex]] is implemented in C++, nothing would be printed, otherwise a nested ask is executed, which just lists all requirements of Mutex (assuming I've got everything right): |
|||
{{out}} |
|||
{{#ask: [[Mutex]] [[Implemented in language::C++]] | format=template | template=ignore | default={{#ask: [[Mutex]] | ?requires}}}} |
|||
<pre> |
|||
TAMAGOTCHI EMULATOR |
|||
=================== |
|||
Enter the name of your tamagotchi : jeremy |
|||
And now the same with Unlambda instead of C++ (there's of course no Unlambda implementation): |
|||
{{#ask: [[Mutex]] [[Implemented in language::Unlambda]] | format=template | template=ignore | default={{#ask: [[Mutex]] | ?requires}}}} |
|||
name (age) health {bored} {food} {poop} |
|||
The same without the "Requires" text: |
|||
{{#ask: [[Mutex]] [[Implemented in language::Unlambda]] | format=template | template=ignore | default={{#ask: [[Mutex]] | ?requires =}}}} |
|||
jeremy (🎂 0) 😀 0 { } { 🍜🍜 } { } |
|||
---- |
|||
When the '?' prompt appears, enter an action optionally |
|||
==Memoization== |
|||
followed by the number of repetitions from 1 to 9. |
|||
Memoization is a method used to reduce function calls in recursive functions or other functions that are called very frequently. The basic idea behind memoizing is to store results for new sets of inputs (typically in a key-value style) so that the function will not have to re-compute those results later. |
|||
If no repetitions are specified, one will be assumed. |
|||
The available options are: feed, play, talk, clean or wait. |
|||
? feed 4 |
|||
Functions which can be memoized are ones that give the same answer for a set of inputs each time those inputs are used. [[Fibonacci sequence|Fibonacci number functions]] are often memoized to reduce their call trees and calculation times over time. The basic operation of a memoized function would look something like this: |
|||
jeremy (🎂 1) 😥 3 { } { 🍟🍜🍟🍜 } { 💩 } |
|||
function a with inputs |
|||
if inputs have been seen before |
|||
return a stored answer from when they were seen |
|||
else |
|||
compute the answer for inputs |
|||
store that (inputs, answer) pair |
|||
return that answer |
|||
end function |
|||
Some programs may negate the condition in the "if" and swap the operations. |
|||
? wait 4 |
|||
The overall benefit is that a function frequently called with the same set of inputs can save time by remembering the answer after computing it once -- sacrificing memory for computation time. In systems where memory (or storage depending on the implementation of storing old results) comes at a premium, memoization is not a good option. As long as memory is available and input sets are used repeatedly, memoization can save lots of computation time. |
|||
jeremy (🎂 5) 😫 5 { 💭💤 } { } { 💩 } |
|||
? clean 2 |
|||
For tasks that use/can benefit from/(whatever) memoization, see [[:Category:Memoization]] |
|||
jeremy (🎂 6) 😫 6 { 💭💭💭 } { } { 💩 } |
|||
? talk 4 |
|||
<nowiki>[[Encyclopedia]][[Category:Memoization]]</nowiki> |
|||
😮 : Grasp the names. |
|||
😮 : Vet the greens. |
|||
😮 : Grasp the urns. |
|||
😮 : Dig the zebras. |
|||
jeremy (🎂 7) 😰 3 { } { } { 💩 } |
|||
? feed 6 |
|||
==Category:Memoization== |
|||
jeremy (🎂 9) 😊 2 { } { 🍼🍟 } { 💩💩 } |
|||
{{Alertbox||The main page for this category is [[Memoization]].}} |
|||
Memoization is a method used to reduce function calls in recursive functions or other functions that are called very frequently. The basic idea behind memoizing is to store results for new sets of inputs (typically in a key-value style) so that the function will not have to re-compute those results later. |
|||
? play 2 |
|||
For more information, see [[Memoization]]. |
|||
jeremy (🎂 10) 😩 5 { } { } { 💩💩💩 } |
|||
? talk |
|||
😮 : Touch the eggs. |
|||
jeremy (🎂 10) 😫 5 { } { } { 💩💩💩 } |
|||
</pre> |
Latest revision as of 12:33, 8 June 2024
Liberty Basic
NoMainWin
WindowWidth=900
WindowHeight=720
BackgroundColor$ = "191 191 255" ' buttonface default
Global Deck, MaxDecks
StaticText #1.Debug "", 0, 0, 600, 20
StaticText #1.StaticText "Ten Decks of Poker Hands", 50, 50, 3000, 40
Button #1.Deal "Deal", [Start], UL, 700, 180, 80, 40
Button #1.TenThousand "10,000", [TenThousand], UL, 700, 250, 80, 40
Button #1.Stats "History", ShowStats, UL, 700, 320, 80, 40
Button #1.Quit "Quit", Quit, UL, 700, 390, 80, 40
TextEditor #1.TextEditor 50, 100, 600, 500
open "POKER HANDS" for dialog as #1
#1 "TrapClose Quit"
#1 "Font Ariel 12 Bold"
#1.StaticText "!Font Ariel 16 Bold"
#1.TextEditor "!Font Courier_New 14 Bold"
if not(exists("Poker Hands.txt")) then #1.Stats "!Disable"
MaxDecks=10
wait
[TenThousand]
TenThousand = 1
[Start]
if TenThousand then
MaxDecks=10000
#1.TextEditor "!Hide"
#1.Deal "!Hide"
#1.TenThousand "!Hide"
#1.Stats "!Hide"
#1.Quit "!Hide"
#1.StaticText "Ten Thousand Decks of Poker Hands"
end if
Deck += 1
if TenThousand then #1.Debug Str$(Deck)
if Deck>MaxDecks then Deck -= 1: call Quit
#1.TextEditor "!cls"
call ShuffleDeck 0
'call TestDeck
NextCard=1
for y=1 to 10
for x=1 to 5
y$ = A$(NextCard)
B$(x) = ConvertHiCard$(y$)
NextCard += 1
next x
sort B$(), 1, 5
for x=NextCard-5 to NextCard-1
#1.TextEditor A$(x)+" ";
next x
#1.TextEditor " ";
Values$="" 'determine high value of hand
for x=1 to 5
Values$ = Values$ + left$(B$(x),1)
next x
HiValue$ = RealValue$(right$(Values$,1))
z=0: Flush=0: Straight=0: Royal=0: FourKind=0
ThreeKind=0: Pair=0: TwoPair=0: FullHouse=0
if Flush() then Flush=1
x = Straight()
if x then Straight=1: if x=9 then Royal=1
z$ = Kind$()
Value$ = RealValue$(right$(z$,1))
z=val(left$(z$, len(z$)-1))
if z=41 then FourKind=1
if z=32 then FullHouse=1
if z=31 then ThreeKind=1
if z=22 then TwoPair=1
if z=21 then Pair=1
select case
case Straight and Royal and Flush: #1.TextEditor "Royal Flush": Stats(1) += 1
case Straight and Flush: #1.TextEditor "Straight Flush, " + HiValue$ + " high": Stats(2) += 1
case FourKind: #1.TextEditor "Four of a kind, " + Value$ + "s": Stats(3) += 1
case FullHouse: #1.TextEditor "Full House, " + Value$ + "s high": Stats(4) += 1
case Flush: #1.TextEditor "Flush, " + HiValue$ + " high": Stats(5) += 1
case Straight: #1.TextEditor "Straight, " + HiValue$ + " high": Stats(6) += 1
case ThreeKind: #1.TextEditor "Three of a kind, " + Value$ + "s": Stats(7) += 1
case TwoPair: #1.TextEditor "Two Pair, " + Value$ + " high": Stats(8) += 1
case Pair: #1.TextEditor "Pair " + Value$ + "s": Stats(9) += 1
case else: #1.TextEditor HiValue$ + " high"
end select
next y
#1.TextEditor ""
#1.TextEditor "Deck #" + str$(Deck)
if TenThousand then goto [Start] else wait
function RealValue$(Value$)
select case Value$
case "A": RealValue$="T"
case "B": RealValue$="J"
case "C": RealValue$="Q"
case "D": RealValue$="K"
case "E": RealValue$="A"
case else: RealValue$=Value$
end select
end function
sub SaveStats Deck
Stats(0) = 10*Deck
if not(exists("Poker Hands.txt")) then
open "Poker Hands.txt" for output as #2
for x=0 to 9
print #2 Stats(x)
next
close #2
#1.Stats "!Enable"
else
open "Poker Hands.txt" for input as #2
for x=0 to 9
input #2 History(x)
next
close #2
for x=0 to 9
History(x) += Stats(x)
next
open "Poker Hands.txt" for output as #2
for x=0 to 9
print #2 History(x)
next
close #2
end if
end sub
sub ShowStats
if exists("Poker Hands.txt") then
open "Poker Hands.txt" for input as #2
for x=0 to 9
input #2 History(x)
next
close #2
#1.TextEditor "!cls"
for x=1 to 9
Total += History(x)
next x
Nothing = History(0) - Total
for x=0 to 9
#1.TextEditor using("###,### ", History(x));
select case x
case 0: #1.TextEditor "hands "
case 1: #1.TextEditor "royal flush " + using("##.# %", History(x)/History(0)*100)
case 2: #1.TextEditor "straight flush " + using("##.# %", History(x)/History(0)*100)
case 3: #1.TextEditor "four of a kind " + using("##.# %", History(x)/History(0)*100)
case 4: #1.TextEditor "full house " + using("##.# %", History(x)/History(0)*100)
case 5: #1.TextEditor "flush " + using("##.# %", History(x)/History(0)*100)
case 6: #1.TextEditor "straight " + using("##.# %", History(x)/History(0)*100)
case 7: #1.TextEditor "three of a kind " + using("##.# %", History(x)/History(0)*100)
case 8: #1.TextEditor "two pair " + using("##.# %", History(x)/History(0)*100)
case 9: #1.TextEditor "pair " + using("##.# %", History(x)/History(0)*100)
end select
next
#1.TextEditor using("###,### ", Nothing) + "nothing " + using("###.# %", Nothing/History(0)*100)
end if
end sub
function Kind$()
for x=1 to 5
C$(x) = left$(B$(x), 1)
next x
if C$(1) = C$(2) then 'check for Lo4
Lo2=1
if C$(2) = C$(3) then
Lo2=0: Lo3=1
if C$(3) = C$(4) then
Lo3=0: Kind$="41" + left$(C$(4),1): exit function
end if
end if
end if
if C$(5) = C$(4) then 'check for Hi4
Hi2=1
if C$(4) = C$(3) then
Hi2=0: Hi3=1
if C$(3) = C$(2) then
Hi3=0: Kind$="41" + left$(C$(5),1): exit function
end if
end if
end if
if Lo3 then 'check for Full House and 3Kind
if C$(4) = C$(5) then
Kind$="32" + left$(C$(3),1): exit function
else
Kind$="31" + left$(C$(3),1): exit function
end if
end if
if Hi3 then
if C$(1) = C$(2) then
Kind$="32" + left$(C$(5),1): exit function
else
Kind$="31" + left$(C$(5),1): exit function
end if
end if
if C$(2) = C$(3) and C$(3) = C$(4) then 'Mid3
Kind$="31" + left$(C$(4),1): exit function
end if
if Lo2 and Hi2 then 'check for pairs
Kind$="22" + left$(C$(5),1): exit function
end if
if Lo2 and (C$(3)=C$(4)) then
Kind$="22" + left$(C$(4),1): exit function
end if
if Hi2 and (C$(3)=C$(2)) then
Kind$="22" + left$(C$(5),1): exit function
end if
if Lo2 then Kind$="21" + left$(C$(2),1)
if Hi2 then Kind$="21" + left$(C$(5),1)
if C$(2)=C$(3) then Kind$="21" + left$(C$(3),1)
if C$(3)=C$(4) then Kind$="21" + left$(C$(4),1)
end function
function Straight()
Order$="23456789ABCDEF"
for x=1 to 5
Ranks$ = Ranks$ + left$(B$(x), 1)
next x
x = instr(Order$, Ranks$)
if x then Straight=x
end function
function Flush()
Flush=1
for x=1 to 5
Suits$ = Suits$ + right$(B$(x), 1)
next x
for x=2 to 5
if mid$(Suits$, x, 1) <> left$(Suits$, 1) then Flush=0: exit function
next x
end function
sub ShuffleDeck Jokers
Jokers = int(abs(Jokers)): if Jokers>4 then Jokers=4
Size=52 + Jokers
dim CardDeck$(Size+10,1), A$(Size+10) 'Open new card deck
[Start]
for x=1 to Size
CardDeck$(x,0) = "99"
next x
for x=1 to Size
1 y=RandomNumber(1,Size)
if CardDeck$(y,0)="99" then CardDeck$(y,0)=str$(x) else goto 1
next x
for x=1 to Size 'Examine shuffled deck
if CardDeck$(x,0)=str$(x) then z = 1: exit for
next x
if z then z=0: goto [Start]
for x=1 to Size 'Save shuffled deck
A$(x) = CardFace$(val(CardDeck$(x,0)))
next x
A$(0) = str$(Size)
end sub
function CardFace$(n)
select case n
case 1: CardFace$="AD"
case 2: CardFace$="2D"
case 3: CardFace$="3D"
case 4: CardFace$="4D"
case 5: CardFace$="5D"
case 6: CardFace$="6D"
case 7: CardFace$="7D"
case 8: CardFace$="8D"
case 9: CardFace$="9D"
case 10: CardFace$="TD"
case 11: CardFace$="JD"
case 12: CardFace$="QD"
case 13: CardFace$="KD"
case 14: CardFace$="AC"
case 15: CardFace$="2C"
case 16: CardFace$="3C"
case 17: CardFace$="4C"
case 18: CardFace$="5C"
case 19: CardFace$="6C"
case 20: CardFace$="7C"
case 21: CardFace$="8C"
case 22: CardFace$="9C"
case 23: CardFace$="TC"
case 24: CardFace$="JC"
case 25: CardFace$="QC"
case 26: CardFace$="KC"
case 27: CardFace$="AH"
case 28: CardFace$="2H"
case 29: CardFace$="3H"
case 30: CardFace$="4H"
case 31: CardFace$="5H"
case 32: CardFace$="6H"
case 33: CardFace$="7H"
case 34: CardFace$="8H"
case 35: CardFace$="9H"
case 36: CardFace$="TH"
case 37: CardFace$="JH"
case 38: CardFace$="QH"
case 39: CardFace$="KH"
case 40: CardFace$="AS"
case 41: CardFace$="2S"
case 42: CardFace$="3S"
case 43: CardFace$="4S"
case 44: CardFace$="5S"
case 45: CardFace$="6S"
case 46: CardFace$="7S"
case 47: CardFace$="8S"
case 48: CardFace$="9S"
case 49: CardFace$="TS"
case 50: CardFace$="JS"
case 51: CardFace$="QS"
case 52: CardFace$="KS"
case 53: CardFace$="X1"
case 54: CardFace$="X2"
case 55: CardFace$="X3"
case 56: CardFace$="X4"
end select
end function
function RandomNumber(a, b)
smaller = min(a, b)
range = abs(int(a-b))+1
if range < 1 then exit function
r = int(rnd()*range)
RandomNumber = r + smaller
end function
function ConvertHiCard$(Card$)
select case left$(Card$,1)
case "T": left$(Card$,1)="A"
case "J": left$(Card$,1)="B"
case "Q": left$(Card$,1)="C"
case "K": left$(Card$,1)="D"
case "A": left$(Card$,1)="E"
case "X": left$(Card$,1)="F"
end select
ConvertHiCard$ = Card$
end function
sub TestDeck
data KD, KC, KS, AH, AS ' full house
data 6D, 6C, 6S, 6H, 8H ' four of a kind (io)
data 2D, 4S, 4C, TH, TD ' two pair
data 4H, 5H, 6H, 7H, 8H ' straight flush
data 9S, QD, QC, QH, 3D ' three of a kind
data 6H, 7D, 8C, 9C, TS ' straight
data TH, AH, AS, AC, AD ' four of a kind (hI)
data 3S, 5S, 7S, 9S, JS ' flush
data AD, KD, QD, JD, TD ' royal flush
data 2C, 2D, 3H, 4S, 5C ' one pair
dim A$(50)
for x=1 to 50
read A$(x)
next x
end sub
function exists(FileName$)
files "", FileName$, FileDir$()
FileCount$ = FileDir$(0, 0)
exists = val(FileCount$)
end function
sub Quit
if Deck = MaxDecks then call SaveStats Deck
close #1
end
end sub
- Output:
JH 8C 8S TH KC Pair 8s 9C JS 3S 5D 3D Pair 3s TD QH 6S TS AD Pair Ts AH 9D KD 3H AC Pair As QS 6D JC QD 2H Pair Qs 4H 2D 5S 4C JD Pair 4s KH 6C 4S 7C 5H K high 3C 7D 8D 4D 7H Pair 7s 9S 2S 7S 9H 6H Pair 9s AS QC 5C TC 2C A high Deck #1
Swift
struct SplitMix64: RandomNumberGenerator {
var state: UInt64
init(seed: UInt64) {
state = seed
}
mutating func next() -> UInt64 {
state &+= 0x9e3779b97f4a7c15
var z = state
z = (z ^ (z >> 30)) &* 0xbf58476d1ce4e5b9
z = (z ^ (z >> 27)) &* 0x94d049bb133111eb
return z ^ (z >> 31)
}
mutating func nextFloat() -> Float64 {
Float64(next() >> 11) * 0x1.0p-53
}
}
do {
var split = SplitMix64(seed: 1234567)
print(split)
for _ in 0..<5 {
print(split.next())
}
split = .init(seed: 987654321)
print("\n\(split)")
var counts = [0, 0, 0, 0, 0]
for _ in 0..<100_000 {
let i = Int(split.nextFloat() * 5.0)
counts[i] += 1
}
for (i, count) in zip(0..., counts) {
print("\(i): \(count)")
}
}
- Output:
SplitMix64(state: 1234567) 6457827717110365317 3203168211198807973 9817491932198370423 4593380528125082431 16408922859458223821 SplitMix64(state: 987654321) 0: 20027 1: 19892 2: 20073 3: 19978 4: 20030
Java
This is a direct translation of the Go version. As such, the output will be similar if not identical.
The code does not use any Java 8+ features so it may function on lower versions.
package com.mt;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
class Tamagotchi {
public String name;
public int age,bored,food,poop;
}
public class TamagotchiGame {
Tamagotchi tama;//current Tamagotchi
Random random = new Random(); // pseudo random number generator
String[] verbs = {
"Ask", "Ban", "Bash", "Bite", "Break", "Build",
"Cut", "Dig", "Drag", "Drop", "Drink", "Enjoy",
"Eat", "End", "Feed", "Fill", "Force", "Grasp",
"Gas", "Get", "Grab", "Grip", "Hoist", "House",
"Ice", "Ink", "Join", "Kick", "Leave", "Marry",
"Mix", "Nab", "Nail", "Open", "Press", "Quash",
"Rub", "Run", "Save", "Snap", "Taste", "Touch",
"Use", "Vet", "View", "Wash", "Xerox", "Yield",
};
String[] nouns = {
"arms", "bugs", "boots", "bowls", "cabins", "cigars",
"dogs", "eggs", "fakes", "flags", "greens", "guests",
"hens", "hogs", "items", "jowls", "jewels", "juices",
"kits", "logs", "lamps", "lions", "levers", "lemons",
"maps", "mugs", "names", "nests", "nights", "nurses",
"orbs", "owls", "pages", "posts", "quests", "quotas",
"rats", "ribs", "roots", "rules", "salads", "sauces",
"toys", "urns", "vines", "words", "waters", "zebras",
};
String[] boredIc ons = {"💤", "💭", "❓"};
String[] foodIcons = {"🍼", "🍔", "🍟", "🍰", "🍜"};
String[] poopIcons = {"💩"};
String[] sickIcons1 = {"😄", "😃", "😀", "😊", "😎", "👍"};//ok
String[] sickIcons2 = {"😪", "😥", "😰", "😓"};//ailing
String[] sickIco ns3 = {"😩", "😫"};//bad
String[] sickIcons4 = {"😡", "😱"};//very bad
String[] sickIcons5 = {"❌", "💀", "👽", "😇"};//dead
String brace(String string) {
return String.format("{ %s }", string);
}
void create(String name) {
tama = new Tamagotchi();
tama.name = name;
tama.age = 0;
tama.bored = 0;
tama.food = 2;
tama.poop = 0;
}
boolean alive() { // alive if sickness <= 10
return sickness() <= 10;
}
void feed() {
tama.food++;
}
void play() {//may or may not help with boredom
tama.bored = Math.max(0, tama.bored - random.nextInt(2));
}
void talk() {
String verb = verbs[random.nextInt(verbs.length)];
String noun = nouns[random.nextInt(nouns.length)];
System.out.printf("😮 : %s the %s.%n", verb, noun);
tama.bored = Math.max(0, tama.bored - 1);
}
void clean() {
tama.poop = Math.max(0, tama.poop - 1);
}
void idle() {//renamed from wait() due to wait being an existing method from the Object class
tama.age++;
tama.bored += random.nextInt(2);
tama.food = Math.max(0, tama.food - 2);
tama.poop += random.nextInt(2);
}
String status() {// get boredom/food/poop icons
if(alive()) {
StringBuilder b = new StringBuilder(),
f = new StringBuilder(),
p = new StringBuilder();
for(int i = 0; i < tama.bored; i++) {
b.append(boredIcons[random.nextInt(boredIcons.length)]);
}
for(int i = 0; i < tama.food; i++) {
f.append(foodIcons[random.nextInt(foodIcons.length)]);
}
for(int i = 0; i < tama.poop; i++) {
p.append(poopIcons[random.nextInt(poopIcons.length)]);
}
return String.format("%s %s %s", brace(b.toString()), brace(f.toString()), brace(p.toString()));
}
return " R.I.P";
}
//too much boredom/food/poop
int sickness() {
//dies at age 42 at the latest
return tama.poop + tama.bored + Math.max(0, tama.age - 32) + Math.abs(tama.food - 2);
}
//get health status from sickness level
void health() {
int s = sickness();
String icon;
switch(s) {
case 0:
case 1:
case 2:
icon = sickIcons1[random.nextInt(sickIcons1.length)];
break;
case 3:
case 4:
icon = sickIcons2[random.nextInt(sickIcons2.length)];
break;
case 5:
case 6:
icon = sickIcons3[random.nextInt(sickIcons3.length)];
break;
case 7:
case 8:
case 9:
case 10:
icon = sickIcons4[random.nextInt(sickIcons4.length)];
break;
default:
icon = sickIcons5[random.nextInt(sickIcons5.length)];
break;
}
System.out.printf("%s (🎂 %d) %s %d %s%n%n", tama.name, tama.age, icon, s, status());
}
void blurb() {
System.out.println("When the '?' prompt appears, enter an action optionally");
System.out.println("followed by the number of repetitions from 1 to 9.");
System.out.println("If no repetitions are specified, one will be assumed.");
System.out.println("The available options are: feed, play, talk, clean or wait.\n");
}
public static void main(String[] args) {
TamagotchiGame game = new TamagotchiGame();
game.random.setSeed(System.nanoTime());
System.out.println(" TAMAGOTCHI EMULATOR");
System.out.println(" ===================\n");
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the name of your tamagotchi : ");
String name = scanner.nextLine().toLowerCase().trim();
game.create(name);
System.out.printf("%n%s (age) health {bored} {food} {poop}%n%n", "name");
game.health();
game.blurb();
ArrayList<String> commands = new ArrayList<>(List.of("feed", "play", "talk", "clean", "wait"));
int count = 0;
while(game.alive()) {
System.out.print("? ");
String input = scanner.nextLine().toLowerCase().trim();
String[] items = input.split(" ");
if(items.length > 2) continue;
String action = items[0];
if(!commands.contains(action)) {
continue;
}
int reps = 1;
if(items.length == 2) {
reps = Integer.parseInt(items[1]);
} else {
reps = 1;
}
for(int i = 0; i < reps; i++) {
switch(action) {
case "feed":
game.feed();
break;
case "play":
game.play();
break;
case "talk":
game.talk();
break;
case "wait":
game.idle();
break;
}
//simulate a wait on every third (non-wait) action
if(!action.equals("wait")) {
count++;
if(count%3==0) {
game.idle();
}
}
}
game.health();
}
scanner.close();
}
}
- Output:
TAMAGOTCHI EMULATOR =================== Enter the name of your tamagotchi : jeremy name (age) health {bored} {food} {poop} jeremy (🎂 0) 😀 0 { } { 🍜🍜 } { } When the '?' prompt appears, enter an action optionally followed by the number of repetitions from 1 to 9. If no repetitions are specified, one will be assumed. The available options are: feed, play, talk, clean or wait. ? feed 4 jeremy (🎂 1) 😥 3 { } { 🍟🍜🍟🍜 } { 💩 } ? wait 4 jeremy (🎂 5) 😫 5 { 💭💤 } { } { 💩 } ? clean 2 jeremy (🎂 6) 😫 6 { 💭💭💭 } { } { 💩 } ? talk 4 😮 : Grasp the names. 😮 : Vet the greens. 😮 : Grasp the urns. 😮 : Dig the zebras. jeremy (🎂 7) 😰 3 { } { } { 💩 } ? feed 6 jeremy (🎂 9) 😊 2 { } { 🍼🍟 } { 💩💩 } ? play 2 jeremy (🎂 10) 😩 5 { } { } { 💩💩💩 } ? talk 😮 : Touch the eggs. jeremy (🎂 10) 😫 5 { } { } { 💩💩💩 }