Sandbox: Difference between revisions
Content added Content deleted
m (Links, 2, 3, 4) |
BodkinsOdds (talk | contribs) (Added Swift task for SplitMix64) |
||
(96 intermediate revisions by 18 users not shown) | |||
Line 1: | Line 1: | ||
=={{header|Swift}}== |
|||
{|class="wikitable sortable" |
|||
{{works with|swift|5.9}} |
|||
! [[Programming Language|Language]] |
|||
<syntaxhighlight lang="Swift"> |
|||
! Paradigm(s) |
|||
struct SplitMix64: RandomNumberGenerator { |
|||
! Standarized |
|||
var state: UInt64 |
|||
! [[Type strength]] |
|||
init(seed: UInt64) { |
|||
! [[Type safety]] |
|||
state = seed |
|||
! Expression of types |
|||
} |
|||
! Type Compatability |
|||
mutating func next() -> UInt64 { |
|||
! Type checking |
|||
state &+= 0x9e3779b97f4a7c15 |
|||
! Intended use |
|||
var z = state |
|||
! Design goals |
|||
z = (z ^ (z >> 30)) &* 0xbf58476d1ce4e5b9 |
|||
|- |
|||
z = (z ^ (z >> 27)) &* 0x94d049bb133111eb |
|||
! [[ActionScript]] |
|||
return z ^ (z >> 31) |
|||
| [[imperative programming]], [[object-oriented]], [[event-driven programming]] |
|||
} |
|||
| {{yes}}, [[ECMA]] |
|||
mutating func nextFloat() -> Float64 { |
|||
| strong |
|||
Float64(next() >> 11) * 0x1.0p-53 |
|||
| safe |
|||
} |
|||
| |
|||
} |
|||
| |
|||
| static |
|||
| Web design |
|||
| |
|||
|- |
|||
! [[Ada]] |
|||
| [[concurrent programming|concurrent]], distributed programming, [[generic programming]], [[imperative programming]], [[object-oriented]] |
|||
| {{yes}}, [[ANSI]], [[ISO]] |
|||
| strong |
|||
| safe |
|||
| explicit |
|||
| nominative |
|||
| static |
|||
| Embedded, Realtime applications |
|||
| Reliability |
|||
|- |
|||
! [[ALGOL 68]] |
|||
| [[concurrent programming|concurrent]], [[imperative programming]] |
|||
| {{no}} |
|||
| strong |
|||
| safe |
|||
| |
|||
| structural |
|||
| static or dynamic |
|||
| Application |
|||
| Readability, Structure |
|||
|- |
|||
! [[BASIC]] |
|||
| [[procedural programming]] |
|||
| {{yes}}, [[ANSI]], [[ISO]] |
|||
| varies by dialect |
|||
| |
|||
| |
|||
| |
|||
| |
|||
| Education |
|||
| Simplicity |
|||
|- |
|||
! [[C]] |
|||
| [[imperative programming]] |
|||
| {{yes}}, [[ANSI]] [[C89]], [[ISO]] [[C90]]/[[C99]] |
|||
| weak |
|||
| unsafe |
|||
| explicit |
|||
| nominative |
|||
| static |
|||
| System |
|||
| Low level access, Minimal constraint |
|||
|- |
|||
! [[C sharp|C#]] |
|||
| [[imperative programming]], [[object-oriented]], [[generic programming]], [[reflective programming]] |
|||
| {{yes}}, [[ECMA]], [[ISO]] |
|||
| strong |
|||
| safe (but unsafe allowed) |
|||
| implicit |
|||
| nominative |
|||
| static |
|||
| Application |
|||
| Rapid application development |
|||
|- |
|||
! [[C++]] |
|||
| [[imperative programming]], [[object-oriented]], [[generic programming]] |
|||
| {{yes}}, [[ISO]] |
|||
| strong |
|||
| safe (but unsafe allowed) |
|||
| explicit, partially implicit |
|||
| nominative, structural |
|||
| static, dynamic |
|||
| Application, System |
|||
| Abstraction, Efficiency, Compatibility |
|||
|- |
|||
! [[Clean]] |
|||
| [[functional programming]], [[generic programming]] |
|||
| {{no}} |
|||
| strong |
|||
| |
|||
| implicit |
|||
| |
|||
| static |
|||
| General |
|||
| Correctness, Modularity |
|||
|- |
|||
! [[COBOL]] |
|||
| [[imperative programming]], [[object-oriented]] |
|||
| {{yes}} |
|||
| strong |
|||
| |
|||
| |
|||
| |
|||
| static |
|||
| Business and Financial Applications |
|||
| Readability |
|||
|- |
|||
! [[ColdFusion]] |
|||
| [[procedural programming]], [[object-oriented]] |
|||
| {{no}} |
|||
| weak |
|||
| |
|||
| implicit |
|||
| |
|||
| dynamic |
|||
| Web Development |
|||
| Rapid Application Development, Ease of use |
|||
|- |
|||
! [[Common Lisp]] |
|||
| [[imperative programming]], [[functional programming]], [[object-oriented]] |
|||
| {{yes}} |
|||
| strong |
|||
| safe |
|||
| |
|||
| |
|||
| dynamic |
|||
| General |
|||
| Standardize [[Lisp]] |
|||
|- |
|||
! [[D]] |
|||
| [[imperative programming]], [[object-oriented]], [[generic programming]] |
|||
| {{no}} |
|||
| strong |
|||
| safe |
|||
| explicit |
|||
| |
|||
| static |
|||
| Application, System |
|||
| Compilability, Correctness, Efficiency |
|||
|- |
|||
! [[Eiffel]] |
|||
| [[imperative programming]], [[object-oriented]], [[generic programming]] |
|||
| {{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] |
|||
| strong |
|||
| safe |
|||
| |
|||
| nominative |
|||
| static |
|||
| Application |
|||
| Correctness, Efficiency, Design by contract |
|||
|- |
|||
! [[Erlang]] |
|||
| [[functional programming]], [[concurrent programming|concurrent]], distributed programming |
|||
| {{no}} |
|||
| strong |
|||
| |
|||
| |
|||
| |
|||
| dynamic |
|||
| Telecom and distributed applications |
|||
| Fault tolerance, Scalability |
|||
|- |
|||
! [[Forth]] |
|||
| [[imperative programming]], [[stack]]-oriented |
|||
| {{yes}}, [[ANSI]] |
|||
| none |
|||
| n/a |
|||
| n/a |
|||
| n/a |
|||
| n/a |
|||
| Application, Embedded systems |
|||
| Compact implementations |
|||
|- |
|||
! [[Fortran]] |
|||
| [[imperative programming]], [[procedural programming]], [[object-oriented]] |
|||
| {{yes}} |
|||
| strong |
|||
| safe |
|||
| |
|||
| nominative |
|||
| static |
|||
| Scientific and numeric applications |
|||
| Runtime efficiency, Simple syntax |
|||
|- |
|||
! [[Groovy]] |
|||
| [[imperative programming]], [[object-oriented]], [[aspect-oriented programming]] |
|||
| {{no}} |
|||
| strong |
|||
| safe |
|||
| implicit |
|||
| |
|||
| dynamic |
|||
| Application |
|||
| [[JVM]] compatibility |
|||
|- |
|||
! [[Haskell]] |
|||
| [[functional programming]], [[generic programming]], [[lazy evaluation]] |
|||
| {{no}} |
|||
| strong |
|||
| |
|||
| implicit |
|||
| structural |
|||
| static |
|||
| Application |
|||
| [[lazy evaluation]], Explicit side-effect |
|||
|- |
|||
! [[J]] |
|||
| [[array]] programming, function-level programming, [[tacit programming]] |
|||
| {{no}} |
|||
| strong |
|||
| safe |
|||
| |
|||
| |
|||
| dynamic |
|||
| Data processing |
|||
| Terseness, Expressiveness, Powerful Data Manipulation |
|||
|- |
|||
! [[Java]] |
|||
| [[imperative programming]], [[object-oriented]], [[generic programming]], [[reflective programming]] |
|||
| {{no}} |
|||
| strong |
|||
| safe |
|||
| explicit |
|||
| nominative |
|||
| static |
|||
| Application |
|||
| Write once run anywhere |
|||
|- |
|||
! [[JavaScript]] |
|||
| [[imperative programming]], [[object-oriented]], [[functional programming]], [[reflective programming]] |
|||
| {{yes}} |
|||
| weak |
|||
| |
|||
| |
|||
| |
|||
| dynamic |
|||
| Client side web scripting |
|||
| |
|||
|- |
|||
! [[Joy]] |
|||
| [[functional programming]], [[stack]]-oriented |
|||
| {{no}} |
|||
| strong |
|||
| safe |
|||
| |
|||
| |
|||
| dynamic |
|||
| [[functional programming]] research |
|||
| [[concatenative]] |
|||
|- |
|||
! [[Lisp]] |
|||
| [[functional programming]], reflective; others vary by dialect |
|||
| {{no}} |
|||
| strong |
|||
| |
|||
| |
|||
| |
|||
| dynamic |
|||
| General |
|||
| Simple notation for Lambda calculus, Homoiconicity |
|||
|- |
|||
! [[Lua]] |
|||
| [[procedural programming]], [[imperative programming]], [[reflective programming|reflective]] |
|||
| {{no}} |
|||
| strong |
|||
| safe |
|||
| implicit |
|||
| |
|||
| dynamic |
|||
| Host-driven Scripting |
|||
| Small, embedded, configuration. |
|||
|- |
|||
! [[Mathematica]] |
|||
| [[functional programming]], [[procedural programming]] |
|||
| {{no}} |
|||
| strong |
|||
| |
|||
| |
|||
| |
|||
| dynamic |
|||
| Numeric computation and visualization |
|||
| |
|||
|- |
|||
! [[Object Pascal]] ([[Delphi]]) |
|||
| [[imperative programming]], [[object-oriented]], [[generic programming]] |
|||
| {{no}} |
|||
| strong |
|||
| safe (but unsafe allowed) |
|||
| explicit |
|||
| nominative |
|||
| static |
|||
| Application, System |
|||
| Readability, Rapid application development, Modularity |
|||
|- |
|||
! [[Objective-C]] |
|||
| [[imperative programming]], [[object-oriented]], [[reflective programming]] |
|||
| {{yes}} |
|||
| weak |
|||
| |
|||
| explicit |
|||
| |
|||
| static |
|||
| Application |
|||
| Smalltalk like, Component based code reuse, C compatibility |
|||
|- |
|||
! [[Ocaml]] |
|||
| [[object-oriented]], [[functional programming]], [[imperative programming]], generic programming |
|||
| {{no}} |
|||
| strong |
|||
| safe |
|||
| implicit |
|||
| structural |
|||
| static |
|||
| Application |
|||
| Efficiency, Robustness, Correctness |
|||
|- |
|||
! [[Oz]] |
|||
| logic programming, [[functional programming]], [[imperative programming]], [[object-oriented]], [[concurrent programming]] - multi paradigm |
|||
| {{no}} |
|||
| |
|||
| |
|||
| |
|||
| |
|||
| dynamic |
|||
| Education |
|||
| |
|||
|- |
|||
! [[Pascal]] |
|||
| [[imperative programming]], [[procedural programming]] |
|||
| {{yes}} |
|||
| strong |
|||
| safe |
|||
| explicit |
|||
| |
|||
| static |
|||
| Education |
|||
| Readability, Discipline, Modularity |
|||
|- |
|||
! [[Perl]] |
|||
| [[imperative programming]], [[procedural programming]], [[reflective programming]], [[functional programming]], [[object-oriented]], [[generic programming]] |
|||
| {{no}} |
|||
| weak |
|||
| |
|||
| implicit |
|||
| |
|||
| dynamic |
|||
| Text processing, Scripting |
|||
| Terseness, Expressiveness |
|||
|- |
|||
! [[PHP]] |
|||
| [[imperative programming]], [[object-oriented]], [[reflective programming]] |
|||
| {{no}} |
|||
| weak |
|||
| |
|||
| |
|||
| |
|||
| dynamic |
|||
| Web Application, CLI |
|||
| Robustness and Simplicity |
|||
|- |
|||
! [[Prolog]] |
|||
| logic programming |
|||
| {{yes}}, [[ISO]] |
|||
| strong |
|||
| |
|||
| |
|||
| |
|||
| dynamic |
|||
| Problem solving, Artificial intelligence |
|||
| [[declarative programming]] |
|||
|- |
|||
! [[Python]] |
|||
| [[imperative programming]], [[object-oriented]], [[functional programming]], [[aspect-oriented programming]], [[reflective programming]] |
|||
| {{no}} |
|||
| strong |
|||
| safe |
|||
| implicit |
|||
| |
|||
| dynamic |
|||
| Application, Education, Scripting |
|||
| Simplicity, Readability, Expressiveness, Modularity |
|||
|- |
|||
! [[Ruby]] |
|||
| [[imperative programming]], [[object-oriented]], [[aspect-oriented programming]], [[reflective programming]] |
|||
| {{no}} |
|||
| strong |
|||
| |
|||
| implicit |
|||
| |
|||
| dynamic |
|||
| Application, Scripting |
|||
| Expressiveness, Readability |
|||
|- |
|||
! [[Scala]] |
|||
| [[object-oriented]], [[functional programming]], [[generic programming]] |
|||
| {{no}} |
|||
| strong |
|||
| |
|||
| partially implicit |
|||
| |
|||
| static |
|||
| Education |
|||
| |
|||
|- |
|||
! [[Scheme]] |
|||
| [[functional programming]] |
|||
| {{yes}} |
|||
| strong |
|||
| |
|||
| |
|||
| |
|||
| dynamic (latent) |
|||
| General, Education |
|||
| Minimalistic, Lexical Scoping |
|||
|- |
|||
! [[Smalltalk]] |
|||
| [[object-oriented]], [[concurrent programming]], [[event-driven programming]], [[imperative programming]], [[declarative programming]] |
|||
| {{yes}}, [[ANSI]] |
|||
| strong |
|||
| safe |
|||
| implicit |
|||
| |
|||
| dynamic |
|||
| Application, Education |
|||
| Uniformity, Pure object oriented |
|||
|- |
|||
! [[Tcl]] |
|||
| [[imperative programming]], [[procedural programming]], [[event-driven programming]] |
|||
| {{no}} |
|||
| |
|||
| |
|||
| |
|||
| |
|||
| dynamic |
|||
| Application, Scripting |
|||
| |
|||
|- |
|||
! [[Visual Basic]] |
|||
| component-oriented programming, [[event-driven programming]] |
|||
| {{no}} |
|||
| strong |
|||
| safe |
|||
| |
|||
| nominative |
|||
| static |
|||
| Application |
|||
| Rapid application development, Simplicity |
|||
|- |
|||
! [[Visual Basic .NET]] |
|||
| [[object-oriented]], [[event-driven programming]] |
|||
| {{no}} |
|||
| strong |
|||
| |
|||
| |
|||
| |
|||
| static |
|||
| Application |
|||
| Rapid application development, Simplicity |
|||
|- |
|||
! [[Windows PowerShell]] |
|||
| [[imperative programming]], [[object-oriented]], [[functional programming]], [[pipeline programming]], [[reflective programming]] |
|||
| {{no}} |
|||
| strong |
|||
| safe |
|||
| implicit |
|||
| |
|||
| dynamic |
|||
| |
|||
| |
|||
|-class="sortbottom" |
|||
! [[Programming Language|Language]] |
|||
! Paradigm(s) |
|||
! Standarized |
|||
! [[Type strength]] |
|||
! [[Type safety]] |
|||
! Expression of types |
|||
! Type Compatability |
|||
! Type checking |
|||
! Intended use |
|||
! Design goals |
|||
|} |
|||
{{usertop}} |
|||
|- |
|||
|<div class="infobox" style="width: 2in">DIVBOX<br>DIVBOX<br>DIVBOX<br>DIVBOX<br>DIVBOX<br></div> |
|||
|- |
|||
|<div class="infobox" style="width: 2in">DIVBOX<br>DIVBOX<br>DIVBOX<br>DIVBOX<br>DIVBOX<br></div> |
|||
|- |
|||
|<div class="infobox" style="width: 2in">DIVBOX<br>DIVBOX<br>DIVBOX<br>DIVBOX<br>DIVBOX<br></div> |
|||
|- |
|||
|<div class="infobox" style="width: 2in">DIVBOX<br>DIVBOX<br>DIVBOX<br>DIVBOX<br>DIVBOX<br></div> |
|||
{{userbottom}} |
|||
==a== |
|||
do { |
|||
[[kjhfdkjdsfosfdhlaslhdjkadsf|Non-existant link"]] |
|||
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)") |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
SplitMix64(state: 1234567) |
|||
6457827717110365317 |
|||
3203168211198807973 |
|||
9817491932198370423 |
|||
4593380528125082431 |
|||
16408922859458223821 |
|||
SplitMix64(state: 987654321) |
|||
== Math == |
|||
0: 20027 |
|||
1: 19892 |
|||
2: 20073 |
|||
3: 19978 |
|||
4: 20030 |
|||
</pre> |
|||
=={{header|Java}}== |
|||
<math>a_i = \sum_{k=0}^n 1/over k * b_i</math> |
|||
This is a direct translation of [[Tamagotchi emulator#Go|the Go version]]. As such, the output will be similar if not identical. |
|||
Doesn't work .... |
|||
The code does not use any Java 8+ features so it may function on lower versions. |
|||
==c== |
|||
<syntaxhighlight lang="Java"> |
|||
''blah'' |
|||
package com.mt; |
|||
<pre>''blaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa this looks horrible aaaaaaaaaaaaaaaaaaaa we should fix this somehow aaaaaaaaaaaaaaaaaaaaaaaaah''</pre> |
|||
<pre><nowiki>''blah''</nowiki></pre> |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
import java.util.Random; |
|||
import java.util.Scanner; |
|||
class Tamagotchi { |
|||
[[#Your Language Here]] |
|||
public String name; |
|||
public int age,bored,food,poop; |
|||
[[#Python]] |
|||
<Python> |
|||
def avr24(data): |
|||
if len(data)==0: |
|||
return 0 |
|||
else: |
|||
return sum(data)/float(len(data)) |
|||
</Python> |
|||
== XFeeds == |
|||
<xfeeds contentcolour="#eeeeee" feedlimit="3" totallimit="10"> |
|||
http://blog.rosettacode.org/?feed=rss2 |
|||
http://blog.rosettacode.org/?feed=comments-rss2 |
|||
</xfeeds> |
|||
== Syntax highlighting == |
|||
<c>#include <iostream> |
|||
#include <gmpxx.h> |
|||
mpf_class f(mpf_class x); |
|||
double f(double x); |
|||
int main() |
|||
{ |
|||
unsigned int start = 1; |
|||
unsigned int end = 1000; |
|||
mpf_class sum = 0; |
|||
double dsum = 0; |
|||
for( unsigned int x = start; |
|||
x <= end; |
|||
++x ) |
|||
{ |
|||
sum += f(mpf_class(x)); |
|||
dsum += f((double) x); |
|||
} |
|||
std::cout << "Sum of f(x) from " << start << " to " << end << " is " << sum << " (GMP float) or " << dsum << " (IEEE 64-bit float)" << std::endl; |
|||
return 0; |
|||
} |
} |
||
public class TamagotchiGame { |
|||
Tamagotchi tama;//current Tamagotchi |
|||
mpf_class f(mpf_class x) |
|||
Random random = new Random(); // pseudo random number generator |
|||
{ |
|||
return ( 1 / ( x * x ) ); |
|||
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(); |
|||
} |
|||
} |
} |
||
double f(double x) |
|||
{ |
|||
return ( 1 / ( x * x ) ); |
|||
} |
|||
</c> |
|||
'''Expression test''' |
|||
Currently there are {{NUMBEROFARTICLES}} articles, but if someone added another, there would be {{#expr: {{NUMBEROFARTICLES:R}} + 1}} articles. Totally, there are {{NUMBEROFPAGES}} pages, which means that {{#expr: {{NUMBEROFPAGES:R}} - {{NUMBEROFARTICLES:R}} }} pages are not articles. |
|||
=={{header|Your Language Here}}== |
|||
{{Works with|Yourlanguagehere}} |
|||
Testing a standard language solution header. |
|||
{{Works with|Yourlanguagehere|2.5}} |
|||
{{Works with|Yourlanguagehere|Display name|2.5}} |
|||
=={{header|Your Language Here|Display Name}}== |
|||
Testing a standard language solution header with different display name. |
|||
{{BoxImage|Fifo.gif|Animated FIFO}} |
|||
<font color=red>color</font><font color=green>Forth</font> |
|||
source |
|||
<font color=red>color</font><font color=green>Forth</font> |
|||
code |
|||
26 → |
|||
170 ¬ |
|||
224-234 αßΓπΣσµτΦΘΩ |
|||
</syntaxhighlight> |
|||
236 ∞ |
|||
{{out}} |
|||
<pre> |
|||
TAMAGOTCHI EMULATOR |
|||
=================== |
|||
Enter the name of your tamagotchi : jeremy |
|||
241-243 ±≥≤ |
|||
name (age) health {bored} {food} {poop} |
|||
jeremy (🎂 0) 😀 0 { } { 🍜🍜 } { } |
|||
244 ⌠ |
|||
When the '?' prompt appears, enter an action optionally |
|||
245 ⌡ |
|||
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 |
|||
246 ÷ |
|||
jeremy (🎂 5) 😫 5 { 💭💤 } { } { 💩 } |
|||
? clean 2 |
|||
247 ≈ |
|||
jeremy (🎂 6) 😫 6 { 💭💭💭 } { } { 💩 } |
|||
? talk 4 |
|||
248 ° |
|||
😮 : Grasp the names. |
|||
😮 : Vet the greens. |
|||
😮 : Grasp the urns. |
|||
😮 : Dig the zebras. |
|||
jeremy (🎂 7) 😰 3 { } { } { 💩 } |
|||
? feed 6 |
|||
251 √ |
|||
jeremy (🎂 9) 😊 2 { } { 🍼🍟 } { 💩💩 } |
|||
? play 2 |
|||
253 ² |
|||
jeremy (🎂 10) 😩 5 { } { } { 💩💩💩 } |
|||
? talk |
|||
<nowiki><sub> </nowiki>normal<sub>sub</sub> |
|||
😮 : Touch the eggs. |
|||
jeremy (🎂 10) 😫 5 { } { } { 💩💩💩 } |
|||
</pre> |
|||
<nowiki><sup> </nowiki>normal<sup>sup</sup> |
Latest revision as of 02:14, 9 May 2024
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 { } { } { 💩💩💩 }