Sandbox: Difference between revisions

10,928 bytes added ,  22 days ago
m (* Removed old stuff from sandbox)
 
(97 intermediate revisions by 20 users not shown)
Line 1:
=={{header|Liberty Basic}}==
{|class="wikitable sortable"
{{works with|LB Booster}}
! [[Programming Language|Language]]
<syntaxhighlight lang="Liberty Basic>
! Paradigm(s)
 
! Standarized
NoMainWin
! [[Type strength]]
WindowWidth=900
! [[Type safety]]
WindowHeight=720
! Expression of types
BackgroundColor$ = "191 191 255" ' buttonface default
! Type Compatability
Global Deck, MaxDecks
! Type checking
 
! Intended use
StaticText #1.Debug "", 0, 0, 600, 20
! Design goals
StaticText #1.StaticText "Ten Decks of Poker Hands", 50, 50, 3000, 40
|-
Button #1.Deal "Deal", [Start], UL, 700, 180, 80, 40
! [[ActionScript]]
Button #1.TenThousand "10,000", [TenThousand], UL, 700, 250, 80, 40
| [[imperative programming]], [[object-oriented]], [[event-driven programming]]
Button #1.Stats "History", ShowStats, UL, 700, 320, 80, 40
| {{yes}}, [[ECMA]]
Button #1.Quit "Quit", Quit, UL, 700, 390, 80, 40
| strong
TextEditor #1.TextEditor 50, 100, 600, 500
| safe
 
|
open "POKER HANDS" for dialog as #1
|
#1 "TrapClose Quit"
| static
#1 "Font Ariel 12 Bold"
| Web design
#1.StaticText "!Font Ariel 16 Bold"
|
#1.TextEditor "!Font Courier_New 14 Bold"
|-
if not(exists("Poker Hands.txt")) then #1.Stats "!Disable"
! [[Ada]]
MaxDecks=10
| [[concurrent programming|concurrent]], distributed programming, [[generic programming]], [[imperative programming]], [[object-oriented]]
wait
| {{yes}}, [[ANSI]], [[ISO]]
 
| strong
[TenThousand]
| safe
TenThousand = 1
| explicit
[Start]
| nominative
if TenThousand then
| static
MaxDecks=10000
| Embedded, Realtime applications
#1.TextEditor "!Hide"
| Reliability
#1.Deal "!Hide"
|-
#1.TenThousand "!Hide"
! [[ALGOL 68]]
#1.Stats "!Hide"
| [[concurrent programming|concurrent]], [[imperative programming]]
#1.Quit "!Hide"
| {{no}}
#1.StaticText "Ten Thousand Decks of Poker Hands"
| strong
end if
| safe
Deck += 1
|
if TenThousand then #1.Debug Str$(Deck)
| structural
if Deck>MaxDecks then Deck -= 1: call Quit
| static or dynamic
#1.TextEditor "!cls"
| Application
call ShuffleDeck 0
| Readability, Structure
'call TestDeck
|-
NextCard=1
! [[BASIC]]
for y=1 to 10
| [[procedural programming]]
for x=1 to 5
| {{yes}}, [[ANSI]], [[ISO]]
y$ = A$(NextCard)
| varies by dialect
B$(x) = ConvertHiCard$(y$)
|
NextCard += 1
|
next x
|
sort B$(), 1, 5
|
for x=NextCard-5 to NextCard-1
| Education
#1.TextEditor A$(x)+" ";
| Simplicity
next x
|-
#1.TextEditor " ";
! [[C]]
| [[imperative programming]]
Values$="" 'determine high value of hand
| {{yes}}, [[ANSI]] [[C89]], [[ISO]] [[C90]]/[[C99]]
for x=1 to 5
| weak
Values$ = Values$ + left$(B$(x),1)
| unsafe
next x
| explicit
HiValue$ = RealValue$(right$(Values$,1))
| nominative
 
| static
z=0: Flush=0: Straight=0: Royal=0: FourKind=0
| System
ThreeKind=0: Pair=0: TwoPair=0: FullHouse=0
| Low level access, Minimal constraint
if Flush() then Flush=1
|-
x = Straight()
! [[C sharp|C#]]
if x then Straight=1: if x=9 then Royal=1
| [[imperative programming]], [[object-oriented]], [[generic programming]], [[reflective programming]]
z$ = Kind$()
| {{yes}}, [[ECMA]], [[ISO]]
Value$ = RealValue$(right$(z$,1))
| strong
z=val(left$(z$, len(z$)-1))
| safe (but unsafe allowed)
if z=41 then FourKind=1
| implicit
if z=32 then FullHouse=1
| nominative
if z=31 then ThreeKind=1
| static
if z=22 then TwoPair=1
| Application
if z=21 then Pair=1
| Rapid application development
|-
select case
! [[C++]]
case Straight and Royal and Flush: #1.TextEditor "Royal Flush": Stats(1) += 1
| [[imperative programming]], [[object-oriented]], [[generic programming]]
case Straight and Flush: #1.TextEditor "Straight Flush, " + HiValue$ + " high": Stats(2) += 1
| {{yes}}, [[ISO]]
case FourKind: #1.TextEditor "Four of a kind, " + Value$ + "s": Stats(3) += 1
| strong
case FullHouse: #1.TextEditor "Full House, " + Value$ + "s high": Stats(4) += 1
| safe (but unsafe allowed)
case Flush: #1.TextEditor "Flush, " + HiValue$ + " high": Stats(5) += 1
| explicit, partially implicit
case Straight: #1.TextEditor "Straight, " + HiValue$ + " high": Stats(6) += 1
| nominative, structural
case ThreeKind: #1.TextEditor "Three of a kind, " + Value$ + "s": Stats(7) += 1
| static, dynamic
case TwoPair: #1.TextEditor "Two Pair, " + Value$ + " high": Stats(8) += 1
| Application, System
case Pair: #1.TextEditor "Pair " + Value$ + "s": Stats(9) += 1
| Abstraction, Efficiency, Compatibility
case else: #1.TextEditor HiValue$ + " high"
|-
end select
! [[Clean]]
next y
| [[functional programming]], [[generic programming]]
#1.TextEditor ""
| {{no}}
#1.TextEditor "Deck #" + str$(Deck)
| strong
if TenThousand then goto [Start] else wait
|
 
| implicit
function RealValue$(Value$)
|
select case Value$
| static
case "A": RealValue$="T"
| General
case "B": RealValue$="J"
| Correctness, Modularity
case "C": RealValue$="Q"
|-
case "D": RealValue$="K"
! [[COBOL]]
case "E": RealValue$="A"
| [[imperative programming]], [[object-oriented]]
case else: RealValue$=Value$
| {{yes}}
end select
| strong
end function
|
 
|
sub SaveStats Deck
|
Stats(0) = 10*Deck
| static
if not(exists("Poker Hands.txt")) then
| Business and Financial Applications
open "Poker Hands.txt" for output as #2
| Readability
for x=0 to 9
|-
print #2 Stats(x)
! [[ColdFusion]]
next
| [[procedural programming]], [[object-oriented]]
close #2
| {{no}}
#1.Stats "!Enable"
| weak
else
|
open "Poker Hands.txt" for input as #2
| implicit
for x=0 to 9
|
input #2 History(x)
| dynamic
next
| Web Development
close #2
| Rapid Application Development, Ease of use
for x=0 to 9
|-
History(x) += Stats(x)
! [[Common Lisp]]
next
| [[imperative programming]], [[functional programming]], [[object-oriented]]
open "Poker Hands.txt" for output as #2
| {{yes}}
for x=0 to 9
| strong
print #2 History(x)
| safe
next
|
close #2
|
end if
| dynamic
end sub
| General
 
| Standardize [[Lisp]]
sub ShowStats
|-
if exists("Poker Hands.txt") then
! [[D]]
open "Poker Hands.txt" for input as #2
| [[imperative programming]], [[object-oriented]], [[generic programming]]
for x=0 to 9
| {{no}}
input #2 History(x)
| strong
next
| safe
close #2
| explicit
#1.TextEditor "!cls"
|
| static
for x=1 to 9
| Application, System
Total += History(x)
| Compilability, Correctness, Efficiency
next x
|-
Nothing = History(0) - Total
! [[Eiffel]]
 
| [[imperative programming]], [[object-oriented]], [[generic programming]]
for x=0 to 9
| {{yes}}, [http://www.ecma-international.org/publications/standards/Ecma-367.htm ECMA-367], [http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_detail.htm?csnumber=42924 ISO/IEC 25436:2006]
#1.TextEditor using("###,### ", History(x));
| strong
select case x
| safe
case 0: #1.TextEditor "hands "
|
case 1: #1.TextEditor "royal flush " + using("##.# %", History(x)/History(0)*100)
| nominative
case 2: #1.TextEditor "straight flush " + using("##.# %", History(x)/History(0)*100)
| static
case 3: #1.TextEditor "four of a kind " + using("##.# %", History(x)/History(0)*100)
| Application
case 4: #1.TextEditor "full house " + using("##.# %", History(x)/History(0)*100)
| Correctness, Efficiency, Design by contract
case 5: #1.TextEditor "flush " + using("##.# %", History(x)/History(0)*100)
|-
case 6: #1.TextEditor "straight " + using("##.# %", History(x)/History(0)*100)
! [[Erlang]]
case 7: #1.TextEditor "three of a kind " + using("##.# %", History(x)/History(0)*100)
| [[functional programming]], [[concurrent programming|concurrent]], distributed programming
case 8: #1.TextEditor "two pair " + using("##.# %", History(x)/History(0)*100)
| {{no}}
case 9: #1.TextEditor "pair " + using("##.# %", History(x)/History(0)*100)
| strong
end select
|
next
|
#1.TextEditor using("###,### ", Nothing) + "nothing " + using("###.# %", Nothing/History(0)*100)
|
end if
| dynamic
end sub
| Telecom and distributed applications
 
| Fault tolerance, Scalability
function Kind$()
|-
for x=1 to 5
! [[Forth]]
C$(x) = left$(B$(x), 1)
| [[imperative programming]], [[stack]]-oriented
next x
| {{yes}}, [[ANSI]]
if C$(1) = C$(2) then 'check for Lo4
| none
Lo2=1
| n/a
if C$(2) = C$(3) then
| n/a
Lo2=0: Lo3=1
| n/a
if C$(3) = C$(4) then
| n/a
Lo3=0: Kind$="41" + left$(C$(4),1): exit function
| Application, Embedded systems
end if
| Compact implementations
end if
|-
end if
! [[Fortran]]
| [[imperative programming]], [[procedural programming]], [[object-oriented]]
if C$(5) = C$(4) then 'check for Hi4
| {{yes}}
Hi2=1
| strong
if C$(4) = C$(3) then
| safe
Hi2=0: Hi3=1
|
if C$(3) = C$(2) then
| nominative
Hi3=0: Kind$="41" + left$(C$(5),1): exit function
| static
end if
| Scientific and numeric applications
end if
| Runtime efficiency, Simple syntax
end if
|-
! [[Groovy]]
if Lo3 then 'check for Full House and 3Kind
| [[imperative programming]], [[object-oriented]], [[aspect-oriented programming]]
if C$(4) = C$(5) then
| {{no}}
Kind$="32" + left$(C$(3),1): exit function
| strong
else
| safe
Kind$="31" + left$(C$(3),1): exit function
| implicit
end if
|
end if
| dynamic
if Hi3 then
| Application
if C$(1) = C$(2) then
| [[JVM]] compatibility
Kind$="32" + left$(C$(5),1): exit function
|-
else
! [[Haskell]]
Kind$="31" + left$(C$(5),1): exit function
| [[functional programming]], [[generic programming]], [[lazy evaluation]]
end if
| {{no}}
end if
| strong
if C$(2) = C$(3) and C$(3) = C$(4) then 'Mid3
|
Kind$="31" + left$(C$(4),1): exit function
| implicit
end if
| structural
| static
if Lo2 and Hi2 then 'check for pairs
| Application
Kind$="22" + left$(C$(5),1): exit function
| [[lazy evaluation]], Explicit side-effect
end if
|-
if Lo2 and (C$(3)=C$(4)) then
! [[J]]
Kind$="22" + left$(C$(4),1): exit function
| [[array]] programming, function-level programming, [[tacit programming]]
end if
| {{no}}
if Hi2 and (C$(3)=C$(2)) then
| strong
Kind$="22" + left$(C$(5),1): exit function
| safe
end if
|
|
if Lo2 then Kind$="21" + left$(C$(2),1)
| dynamic
if Hi2 then Kind$="21" + left$(C$(5),1)
| Data processing
if C$(2)=C$(3) then Kind$="21" + left$(C$(3),1)
| Terseness, Expressiveness, Powerful Data Manipulation
if C$(3)=C$(4) then Kind$="21" + left$(C$(4),1)
|-
end function
! [[Java]]
 
| [[imperative programming]], [[object-oriented]], [[generic programming]], [[reflective programming]]
function Straight()
| {{no}}
Order$="23456789ABCDEF"
| strong
for x=1 to 5
| safe
Ranks$ = Ranks$ + left$(B$(x), 1)
| explicit
next x
| nominative
x = instr(Order$, Ranks$)
| static
if x then Straight=x
| Application
end function
| Write once run anywhere
 
|-
function Flush()
! [[JavaScript]]
Flush=1
| [[imperative programming]], [[object-oriented]], [[functional programming]], [[reflective programming]]
for x=1 to 5
| {{yes}}
Suits$ = Suits$ + right$(B$(x), 1)
| weak
next x
|
for x=2 to 5
|
if mid$(Suits$, x, 1) <> left$(Suits$, 1) then Flush=0: exit function
|
next x
| dynamic
end function
| Client side web scripting
 
|
sub ShuffleDeck Jokers
|-
Jokers = int(abs(Jokers)): if Jokers>4 then Jokers=4
! [[Joy]]
Size=52 + Jokers
| [[functional programming]], [[stack]]-oriented
dim CardDeck$(Size+10,1), A$(Size+10) 'Open new card deck
| {{no}}
[Start]
| strong
for x=1 to Size
| safe
CardDeck$(x,0) = "99"
|
next x
|
 
| dynamic
for x=1 to Size
| [[functional programming]] research
1 y=RandomNumber(1,Size)
| [[concatenative]]
if CardDeck$(y,0)="99" then CardDeck$(y,0)=str$(x) else goto 1
|-
next x
! [[Lisp]]
 
| [[functional programming]], reflective; others vary by dialect
for x=1 to Size 'Examine shuffled deck
| {{no}}
if CardDeck$(x,0)=str$(x) then z = 1: exit for
| strong
next x
|
if z then z=0: goto [Start]
|
for x=1 to Size 'Save shuffled deck
|
A$(x) = CardFace$(val(CardDeck$(x,0)))
| dynamic
next x
| General
A$(0) = str$(Size)
| Simple notation for Lambda calculus, Homoiconicity
end sub
|-
 
! [[Lua]]
function CardFace$(n)
| [[procedural programming]], [[imperative programming]], [[reflective programming|reflective]]
select case n
| {{no}}
case 1: CardFace$="AD"
| strong
case 2: CardFace$="2D"
| safe
case 3: CardFace$="3D"
| implicit
case 4: CardFace$="4D"
|
case 5: CardFace$="5D"
| dynamic
case 6: CardFace$="6D"
| Host-driven Scripting
case 7: CardFace$="7D"
| Small, embedded, configuration.
case 8: CardFace$="8D"
|-
case 9: CardFace$="9D"
! [[Mathematica]]
case 10: CardFace$="TD"
| [[functional programming]], [[procedural programming]]
case 11: CardFace$="JD"
| {{no}}
case 12: CardFace$="QD"
| strong
case 13: CardFace$="KD"
|
case 14: CardFace$="AC"
|
case 15: CardFace$="2C"
|
case 16: CardFace$="3C"
| dynamic
case 17: CardFace$="4C"
| Numeric computation and visualization
case 18: CardFace$="5C"
|
case 19: CardFace$="6C"
|-
case 20: CardFace$="7C"
! [[Object Pascal]] ([[Delphi]])
case 21: CardFace$="8C"
| [[imperative programming]], [[object-oriented]], [[generic programming]]
case 22: CardFace$="9C"
| {{no}}
case 23: CardFace$="TC"
| strong
case 24: CardFace$="JC"
| safe (but unsafe allowed)
case 25: CardFace$="QC"
| explicit
case 26: CardFace$="KC"
| nominative
case 27: CardFace$="AH"
| static
case 28: CardFace$="2H"
| Application, System
case 29: CardFace$="3H"
| Readability, Rapid application development, Modularity
case 30: CardFace$="4H"
|-
case 31: CardFace$="5H"
! [[Objective-C]]
case 32: CardFace$="6H"
| [[imperative programming]], [[object-oriented]], [[reflective programming]]
case 33: CardFace$="7H"
| {{yes}}
case 34: CardFace$="8H"
| weak
case 35: CardFace$="9H"
|
case 36: CardFace$="TH"
| explicit
case 37: CardFace$="JH"
|
case 38: CardFace$="QH"
| static
case 39: CardFace$="KH"
| Application
case 40: CardFace$="AS"
| Smalltalk like, Component based code reuse, C compatibility
case 41: CardFace$="2S"
|-
case 42: CardFace$="3S"
! [[Ocaml]]
case 43: CardFace$="4S"
| [[object-oriented]], [[functional programming]], [[imperative programming]], generic programming
case 44: CardFace$="5S"
| {{no}}
case 45: CardFace$="6S"
| strong
case 46: CardFace$="7S"
| safe
case 47: CardFace$="8S"
| implicit
case 48: CardFace$="9S"
| structural
case 49: CardFace$="TS"
| static
case 50: CardFace$="JS"
| Application
case 51: CardFace$="QS"
| Efficiency, Robustness, Correctness
case 52: CardFace$="KS"
|-
case 53: CardFace$="X1"
! [[Oz]]
case 54: CardFace$="X2"
| logic programming, [[functional programming]], [[imperative programming]], [[object-oriented]], [[concurrent programming]] - multi paradigm
case 55: CardFace$="X3"
| {{no}}
case 56: CardFace$="X4"
|
end select
|
end function
|
 
|
function RandomNumber(a, b)
| dynamic
smaller = min(a, b)
| Education
range = abs(int(a-b))+1
|
if range < 1 then exit function
|-
r = int(rnd()*range)
! [[Pascal]]
RandomNumber = r + smaller
| [[imperative programming]], [[procedural programming]]
end function
| {{yes}}
 
| strong
function ConvertHiCard$(Card$)
| safe
select case left$(Card$,1)
| explicit
case "T": left$(Card$,1)="A"
|
case "J": left$(Card$,1)="B"
| static
case "Q": left$(Card$,1)="C"
| Education
case "K": left$(Card$,1)="D"
| Readability, Discipline, Modularity
case "A": left$(Card$,1)="E"
|-
case "X": left$(Card$,1)="F"
! [[Perl]]
end select
| [[imperative programming]], [[procedural programming]], [[reflective programming]], [[functional programming]], [[object-oriented]], [[generic programming]]
ConvertHiCard$ = Card$
| {{no}}
end function
| weak
 
|
sub TestDeck
| implicit
data KD, KC, KS, AH, AS ' full house
|
data 6D, 6C, 6S, 6H, 8H ' four of a kind (io)
| dynamic
data 2D, 4S, 4C, TH, TD ' two pair
| Text processing, Scripting
data 4H, 5H, 6H, 7H, 8H ' straight flush
| Terseness, Expressiveness
data 9S, QD, QC, QH, 3D ' three of a kind
|-
data 6H, 7D, 8C, 9C, TS ' straight
! [[PHP]]
data TH, AH, AS, AC, AD ' four of a kind (hI)
| [[imperative programming]], [[object-oriented]], [[reflective programming]]
data 3S, 5S, 7S, 9S, JS ' flush
| {{no}}
data AD, KD, QD, JD, TD ' royal flush
| weak
data 2C, 2D, 3H, 4S, 5C ' one pair
|
dim A$(50)
|
for x=1 to 50
|
read A$(x)
| dynamic
next x
| Web Application, CLI
end sub
| Robustness and Simplicity
 
|-
function exists(FileName$)
! [[Prolog]]
files "", FileName$, FileDir$()
| logic programming
FileCount$ = FileDir$(0, 0)
| {{yes}}, [[ISO]]
exists = val(FileCount$)
| strong
end function
|
 
|
sub Quit
|
if Deck = MaxDecks then call SaveStats Deck
| dynamic
close #1
| Problem solving, Artificial intelligence
end
| [[declarative programming]]
end sub
|-
 
! [[Python]]
</syntaxhighlight>
| [[imperative programming]], [[object-oriented]], [[functional programming]], [[aspect-oriented programming]], [[reflective programming]]
| {{noout}}
<pre>
| strong
JH 8C 8S TH KC Pair 8s
| safe
9C JS 3S 5D 3D Pair 3s
| implicit
TD QH 6S TS AD Pair Ts
|
AH 9D KD 3H AC Pair As
| dynamic
QS 6D JC QD 2H Pair Qs
| Application, Education, Scripting
4H 2D 5S 4C JD Pair 4s
| Simplicity, Readability, Expressiveness, Modularity
KH 6C 4S 7C 5H K high
|-
3C 7D 8D 4D 7H Pair 7s
! [[Ruby]]
9S 2S 7S 9H 6H Pair 9s
| [[imperative programming]], [[object-oriented]], [[aspect-oriented programming]], [[reflective programming]]
AS QC 5C TC 2C A high
| {{no}}
 
| strong
Deck #1
|
</pre>
| implicit
 
|
=={{header|Swift}}==
| dynamic
{{works with|swift|5.9}}
| Application, Scripting
<syntaxhighlight lang="Swift">
| Expressiveness, Readability
struct SplitMix64: RandomNumberGenerator {
|-
var state: UInt64
! [[Scala]]
init(seed: UInt64) {
| [[object-oriented]], [[functional programming]], [[generic programming]]
state = seed
| {{no}}
}
| strong
mutating func next() -> UInt64 {
|
state &+= 0x9e3779b97f4a7c15
| partially implicit
var z = state
|
z = (z ^ (z >> 30)) &* 0xbf58476d1ce4e5b9
| static
z = (z ^ (z >> 27)) &* 0x94d049bb133111eb
| Education
return z ^ (z >> 31)
|
}
|-
mutating func nextFloat() -> Float64 {
! [[Scheme]]
Float64(next() >> 11) * 0x1.0p-53
| [[functional programming]]
}
| {{yes}}
}
| strong
 
|
|do {
var split = SplitMix64(seed: 1234567)
|
print(split)
| dynamic (latent)
for _ in 0..<5 {
| General, Education
print(split.next())
| Minimalistic, Lexical Scoping
}
|-
split = .init(seed: 987654321)
! [[Smalltalk]]
print("\n\(split)")
| [[object-oriented]], [[concurrent programming]], [[event-driven programming]], [[imperative programming]], [[declarative programming]]
var counts = [0, 0, 0, 0, 0]
| {{yes}}, [[ANSI]]
for _ in 0..<100_000 {
| strong
let i = Int(split.nextFloat() * 5.0)
| safe
counts[i] += 1
| implicit
}
|
for (i, count) in zip(0..., counts) {
| dynamic
print("\(i): \(count)")
| Application, Education
}
| Uniformity, Pure object oriented
}
|-
</syntaxhighlight>
! [[Tcl]]
{{out}}
| [[imperative programming]], [[procedural programming]], [[event-driven programming]]
<pre>
| {{no}}
SplitMix64(state: 1234567)
|
6457827717110365317
|
3203168211198807973
|
9817491932198370423
|
4593380528125082431
| dynamic
16408922859458223821
| Application, Scripting
 
|
SplitMix64(state: 987654321)
|-
0: 20027
! [[Visual Basic]]
1: 19892
| component-oriented programming, [[event-driven programming]]
2: 20073
| {{no}}
3: 19978
| strong
4: 20030
| safe
</pre>
|
 
| nominative
=={{header|Java}}==
| static
 
| Application
This is a direct translation of [[Tamagotchi emulator#Go|the Go version]]. As such, the output will be similar if not identical.
| Rapid application development, Simplicity
 
|-
The code does not use any Java 8+ features so it may function on lower versions.
! [[Visual Basic .NET]]
<syntaxhighlight lang="Java">
| [[object-oriented]], [[event-driven programming]]
package com.mt;
| {{no}}
 
| strong
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Random;
|
import java.util.Scanner;
| static
 
| Application
class Tamagotchi {
| Rapid application development, Simplicity
public String name;
|-
public int age,bored,food,poop;
! [[Windows PowerShell]]
| [[imperative programming]], [[object-oriented]], [[functional programming]], [[pipeline programming]], [[reflective programming]]
}
| {{no}}
 
| strong
public class TamagotchiGame {
| safe
Tamagotchi tama;//current Tamagotchi
| implicit
Random random = new Random(); // pseudo random number generator
|
| dynamic
String[] verbs = {
|
"Ask", "Ban", "Bash", "Bite", "Break", "Build",
|
"Cut", "Dig", "Drag", "Drop", "Drink", "Enjoy",
|-class="sortbottom"
"Eat", "End", "Feed", "Fill", "Force", "Grasp",
! [[Programming Language|Language]]
"Gas", "Get", "Grab", "Grip", "Hoist", "House",
! Paradigm(s)
"Ice", "Ink", "Join", "Kick", "Leave", "Marry",
! Standarized
"Mix", "Nab", "Nail", "Open", "Press", "Quash",
! [[Type strength]]
"Rub", "Run", "Save", "Snap", "Taste", "Touch",
! [[Type safety]]
"Use", "Vet", "View", "Wash", "Xerox", "Yield",
! Expression of types
};
! Type Compatability
! Type checking
String[] nouns = {
! Intended use
"arms", "bugs", "boots", "bowls", "cabins", "cigars",
! Design goals
"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();
}
}
 
 
</syntaxhighlight>
{{out}}
<pre>
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 { } { } { 💩💩💩 }
 
</pre>
3

edits