Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Rock, Paper, Scissors on 1 Tweet
Juan Carlos
Juan Carlos

Posted on • Edited on

     

Rock, Paper, Scissors on 1 Tweet

This is my "Rock, Paper and Scissors" Game on 1 Tweet.

importrandomrandomize()echo"Rock r\nPaper p\nScissor s"leto=["rock","paper","scissor"]pc=rand0..2us=casestdin.readLine[0]of'r':0of'p':1else:2echo"\nPC\tYou\tWinner\n",o[pc],'\t',o[us],'\t',case(3+pc.ord-us.ord)mod3of1:"PC"of2:"You"else:"No"
Enter fullscreen modeExit fullscreen mode

How to run the code:

$ nim r file.nimRock    rPaper   pScissor spPC      You     Winnerpaper   paper   No$
Enter fullscreen modeExit fullscreen mode

Keys:

  • Rock = r
  • Paper = p
  • Scissors = s

It is not even too code-golfed, because we can remove the lineimport random by compiling with--import:random.

We can remove therandomize() but then we need to recompile to make the PC choose another option, so is not a good idea.

It uses therandom module from standard library:
https://nim-lang.github.io/Nim/random.html

case switch from from standard library:
https://nim-lang.github.io/Nim/manual.html#statements-and-expressions-case-statement

readLine to read a line from standard input:
https://nim-lang.github.io/Nim/io.html#readLine%2CFile

  • How your Rock, Paper and Scissors game looks like?

Thank you for reading

Come learn programming with us...
👑

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

.10x frAgile FullStuck Midend Devlooper, Python, Nim, Arch, OpenSource, EN|ES, Argentina, UTC-3, Atheist, WFH Nim Team Leader
  • Location
    Argentina, Buenos Aires
  • Education
    UTN
  • Work
    Developer
  • Joined

More fromJuan Carlos

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp