กรรไกรกระดาษหิน


21

ใช้กรรไกรกระดาษร็อคคลาสสิค

เงื่อนไข:

  • ผู้ใช้จะป้อน 'r', 'p' หรือ 's'
  • โปรแกรมจะแสดงผลลัพธ์ 'r', 'p' หรือ 's' และผลลัพธ์
  • ตัวเลือกโปรแกรม ('r', 'p' หรือ 's') จะต้องมีการสุ่มหลอก ( ฉันกำลังมองหาคุณโฮเวิร์ด )
  • ผลลัพธ์สามารถแสดงด้วยอักขระที่พิมพ์ได้ใด ๆ ควรมีผลลัพธ์ที่เป็นไปได้สามแบบเสมอสำหรับสิ่งที่ผู้ใช้มีอินพุต (ผู้ใช้ชนะแพ้หรือเสมอ)
  • จะเกิดอะไรขึ้นหากผู้ใช้ไม่ป้อนข้อมูลใด ๆ หรือสิ่งที่แตกต่างที่ 'r', 'p' หรือ 's' ไม่ควรสำคัญ

คุณต้อง:

  • ระบุรหัส golfed
  • รหัส ungolfed
  • คุณเรียกใช้โปรแกรมอย่างไร
  • ตัวอย่างการเรียกใช้

ฉันจะเลือกคำตอบที่มีตัวละครน้อยถ้าเลือกเน็คไทคำตอบที่โหวตมากที่สุดจะถูกเลือก

การเล่นกอล์ฟที่ดีและอาจเป็นโชคในความโปรดปรานของคุณ

ฉันจะโพสต์คำตอบด้วยตนเองใน Java

สำหรับคนที่อาศัยอยู่ในภูเขาใต้ก้อนหิน:

r = ร็อค

p = กระดาษ

s = กรรไกร

ร็อค: ชนะการกรรไกรสูญเสียด้วยกระดาษผูกด้วยหิน

กระดาษ: ชนะที่จะโยกสูญเสียด้วยกรรไกรผูกด้วยกระดาษ

กรรไกร: ชนะกระดาษสูญเสียด้วยหินผูกด้วยกรรไกร

ตำแหน่งปัจจุบัน:

  • UN: ชื่อผู้ใช้
  • PL: ภาษาโปรแกรม
  • CC: จำนวนตัวละคร
  • UV: คะแนนโหวตขึ้น
╔══════════════════╦════════════╦══════╦════╗
║        UN        ║     PL     ║  CC  ║ UV ║
╠══════════════════╬════════════╬══════╬════╣
║ Howard           ║ GolfScript ║    6 ║ 15 ║
║ primo            ║ Perl       ║   27 ║  7 ║
║ TwiNight         ║ APL        ║   31 ║  4 ║
║ primo            ║ Perl       ║   33 ║  7 ║
║ marinus          ║ APL        ║   36 ║  5 ║
║ primo            ║ Perl       ║   38 ║  7 ║
║ primo            ║ Perl       ║   48 ║  7 ║
║ manatwork        ║ Ruby       ║   54 ║ 13 ║
║ w0lf             ║ GolfScript ║   62 ║  4 ║
║ tmartin          ║ K          ║   67 ║  2 ║
║ Abhijit          ║ Python 3   ║   74 ║  5 ║
║ beary605         ║ Python 3   ║   76 ║  4 ║
║ rlemon           ║ javascript ║   85 ║  4 ║
║ ugoren           ║ C          ║   86 ║  3 ║
║ Egor Skriptunoff ║ LUA        ║   87 ║  4 ║
║ Shmiddty         ║ javascript ║   87 ║  3 ║
║ Fors             ║ Befunge    ║  107 ║  3 ║
║ Briguy37         ║ javascript ║  117 ║  2 ║
║ Vi.              ║ Clojure    ║  129 ║  1 ║
║ Henrik           ║ C#         ║  167 ║  4 ║
║ dystroy          ║ Go         ║  169 ║  1 ║
║ Praveen          ║ javascript ║  250 ║  0 ║
║ ryan             ║ javascript ║  256 ║  1 ║
║ primo            ║ ferNANDo   ║  259 ║  5 ║
║ anakata          ║ Java       ║  259 ║  1 ║
║ epoch            ║ Java       ║  387 ║  1 ║
║ jdstankosky      ║ LOLCODE    ║ 1397 ║ 15 ║
╚══════════════════╩════════════╩══════╩════╝

ฉันไม่สามารถเลือกคำตอบของโฮเวิร์ดได้เนื่องจากมันเป็นความพยายาม (สำเร็จ) ในการดัดกฎ แต่ฉันเปลี่ยนเพื่อทำให้ชัดเจนเป็นพิเศษ

ไม่สามารถเลือกคำตอบตัวอักษร Primo 27 ได้เนื่องจากไม่ใช่การสุ่มหลอกต่อ se

Primo-p คำตอบฉันจะไปกับ "-p จะนับเป็น 3 ไบต์: หนึ่งสำหรับ - หนึ่งสำหรับ p และอีกหนึ่งช่องว่างที่จำเป็น"

ขอบคุณทุกคนที่ตอบฉันหวังว่าคุณจะมีความสุข!

หมายเหตุ: ฉันจะพยายามแก้ไขเรื่องนี้ทุกสัปดาห์เพื่อปรับตารางและเปลี่ยนคำตอบที่ฉันเลือกถ้ามีคนเต้นคนปัจจุบันดังนั้นถ้าคุณเพิ่งมาถึงที่นี่โพสต์คำตอบถ้าคุณต้องการ!


2
คุณอาจลองเพิ่มประโยคที่ผลลัพธ์ทั้งหมด - ชนะแพ้วาด - ต้องเป็นไปได้
primo

ตั้งแต่นี้เป็นความนิยมฉันตัดสินใจไปกับ LOLZ
jdstankosky

ฉันแก้ไขกฎก่อนที่คุณจะส่งคำตอบ ความนิยมจะเป็นตัวแบ่งไทเท่านั้น
jsedano

1
มันไม่ใช่ความนิยมอีกแล้วเหรอ? น่าเบื่อ
jdstankosky

1
@anakata ตามกฎ Perlgolf แบบดั้งเดิม (รวบรวมโดยไม่มีใครอื่นนอกจาก Ton Hospel เอง) -pจะถูกนับเป็น 3 ไบต์: หนึ่งสำหรับ-หนึ่งสำหรับสำหรับpหนึ่งและอีกหนึ่งช่องว่างที่จำเป็น อย่างไรก็ตามการแข่งขันอื่น ๆ ใน CG.SE ได้นับแต่ละตัวเลือกเป็นไบต์เดียว มันขึ้นอยู่กับผู้เขียนคำถามที่จะตัดสินใจว่าระบบไหนที่ให้เกียรติ
primo

คำตอบ:


9

APL, 31

'TWL'[1+3|-/x⍳⎕←⍞,(?3)⌷x←'rps']

x←'rps'กำหนดสตริง'rps'ให้กับx

(?3)⌷ เลือกสุ่มจำนวนเต็ม 1 ~ 3 เลือกดัชนีนั้น x

⍞, เตรียมอินพุตของผู้ใช้กับตัวเลือกของเครื่อง

⎕← สตริงผลลัพธ์ที่ส่งออก

x⍳ แปลงเป็นอาร์เรย์ตัวเลขโดย indexOf in x

-/ ความแตกต่างของตัวเลขทั้งสอง

1+|3 โมดูลัส 3 และบวก 1

'TWL'[...] การจัดทำดัชนีจาก 'TWL'

ตัวอย่าง

r
rp
L

ผู้ใช้เลือกหินโปรแกรมเลือกกระดาษ: แพ้


42

LOLCODE, 1397

หมายเหตุ:ฉันส่งสิ่งนี้ก่อนที่ฉันจะสังเกตเห็นความต้องการในการชนะนั้นเปลี่ยนจากความนิยมในการตีกอล์ฟให้เป็นการแข่งขันที่มีความนิยม

ไม่มีไวยากรณ์ที่เข้มงวด แต่ฉันแน่ใจว่านี่เป็นที่ยอมรับ

HAI
    I HAS A CRAZY, LUCKY, CHALLENGE, TREAT
    I HAS YUMMY ITZ "LOL U LOZED"
    I HAS MEH ITZ "NOWAI TIED"
    I HAS GROSS ITZ "OMG U WONNED"
    I HAS BURNT ITZ "LAME"
    GIMMEH CHALLENGE
    BTW I HOPE I R TEH WINZ
    LOL CRAZY IZ BETWEEN 1 AN 3
    I HAS A SUPA ITZ A BUKKIT
    LOL SUPA'Z 1 R "ROCK"
    LOL SUPA'Z 2 R "PAPER"
    LOL SUPA'Z 3 R "SCIZZORS"
    LOL LUCKY R SUPA'Z CRAZY
    GOT CHALLENGE, WTF?
        OMG "Rock"
            GOT LUCKY, WTF?
                OMG ROCK, LOL TREAT R MEH, GTFO
                OMG PAPER, LOL TREAT R YUMMY, GTFO
                OMG SCIZZORS, LOL TREAT R GROSS, GTFO
            OIC
        OMG "Paper"
            GOT LUCKY, WTF?
                OMG ROCK, LOL TREAT R GROSS, GTFO
                OMG PAPER, LOL TREAT R MEH, GTFO
                OMG SCIZZORS, LOL TREAT R YUMMY, GTFO
            OIC
        OMG "Scissors"
            GOT LUCKY, WTF?
                OMG ROCK, LOL TREAT R YUMMY, GTFO
                OMG PAPER, LOL TREAT R GROSS, GTFO
                OMG SCIZZORS, LOL TREAT R MEH, GTFO
            OIC
        OMGWTF
            VISIBLE "WHAT U SAYZ?", LOL TREAT R BURNT
            GTFO
    OIC
        BOTH SAEM TREAT AN BURNT, O RLY?
            YARLY
                VISIBLE "YOU BURNTED MAH TREAT!"
            NOWAI
                VISIBLE SMOOSH "I GUESSED " AN LUCKY
                VISIBLE TREAT
        KTHX
KTHXBAI

ถ้านี่คือการดำเนินการที่ประสบความสำเร็จเป็นRockPaperScissors.LOLนี่คือสิ่งที่ผลลัพธ์ที่เป็นไปได้บางอย่างอาจจะเป็น:

  • อินพุต: Rock- เอาต์พุต:I GUESSED SCIZZORS U WONNED
  • อินพุต: Paper- เอาต์พุต:I GUESSED PAPER NOWAI TIED
  • อินพุต: Scissors- เอาต์พุต:I GUESSED ROCK LOL U LOZED
  • อินพุต: Tuna- เอาต์พุต:WHAT U SAYZ? YOU BURNTED MAH TREAT!

6
+1 เพียงเพื่อเป็น LOLCODE ดูเหมือนสิ่งที่ฉันควรเรียนรู้บางครั้งเพียงเพื่อ LOLz
Iszi

23

GolfScript

n"Draw"

โค้ดด้านบนใช้ฟังก์ชั่นที่ต้องการ นอกจากนี้ยังช่วยให้มั่นใจได้ว่าผู้เล่นจะไม่ถูกทิ้งให้โกรธเพราะความไม่ยุติธรรม (รับรู้) ของกลยุทธ์ของคอมพิวเตอร์

เวอร์ชันที่ไม่ดี

n"Draw"

วิธีการเรียกใช้โปรแกรม

อินพุต (อักขระเดียวของ 'r', 'p', 's') จะต้องมีการจัดเตรียมไว้ใน STDIN ซึ่งอาจถูกยกเลิกด้วยการขึ้นบรรทัดใหม่

ตัวอย่างการเรียกใช้

> echo r | ruby golfscript.rb rockpaperscissors.gsc
r
Draw

คำอธิบายของรหัส

สำหรับผู้ที่ไม่คุ้นเคยกับ GolfScript ฉันจะเพิ่มคำอธิบายโดยละเอียดเกี่ยวกับวิธีการทำงานของรหัสนี้ รหัสนั้นมีอยู่สามส่วนด้วยกัน

### Computer's strategy ###
# The strategy used to play r/p/s. 
# The computer is so fast, it can really guess in an instance 
# what the player has played. Since the computer should 
# not play unfair, the best strategy is to always go for a 
# draw and choose the same move.
        # on the stack is player's move
        # choose to play the same -> now the computer's move is on the stack

### Fiddle with input ###
# The input may of may not be delimited by newline.
# In order to make the output readable, we'll give
# a newline here.
n       # Push a newline onto the stack

### Give the result ###
# We can skip a complicated calculation of the result
# since we chose to play draw anyways.
"Draw"  # Push the result onto the stack

# Output is printed automatically when GolfScript code terminates.

หมายเหตุ

เนื่องจากนี่ไม่ใช่ code-golf แต่เป็นการประกวดความนิยมฉันไม่ได้เลือกรุ่นที่สั้นที่สุด บางทีในกรณีที่มีการผูกรหัสสั้นลงจะทำให้ทางออกของฉันหายไป อย่างไรก็ตามสำหรับผู้ที่สนใจในการเล่นกอล์ฟมีความเป็นไปได้ดังต่อไปนี้:

  • จัดการเฉพาะกับอินพุตที่เหมาะสมและบังคับให้ผู้ใช้ขึ้นบรรทัดใหม่ สิ่งนี้จะบันทึกหนึ่งอักขระ
  • กฎมีความไม่พอเพียงเล็กน้อยซึ่งช่วยให้สามารถบันทึกอักขระอื่นได้โดยการดัดกฏ สามารถพิมพ์ผลลัพธ์เป็น "ชนะ" ได้เสมอ - ไม่ได้ระบุว่าต้องพิมพ์ผลลัพธ์ที่ถูกต้อง แต่โปรดทราบว่าผู้เล่นจะโกรธเร็ว ๆ นี้หากคุณเลือกที่จะใช้โปรแกรมโกง
  • รูปแบบเอาต์พุตไม่ได้ระบุไว้อย่างดี เราสามารถเลือก0เอาท์พุทสำหรับวาด ดังนั้นโปรแกรมที่ถูกต้องสั้นที่สุดคือรหัสตัวอักษร0เดียว

1
ตกลงฉันได้เพิ่มการใช้คำฟุ่มเฟื่อยในกฎ! ฉันยอมรับว่าฉันทำผิดพลาดหน้าใหม่สองครั้ง
jsedano

"การย้ายที่ชนะอย่างเดียวไม่สามารถทำได้" : P
Viezevingertjes

1
+1 สำหรับเวอร์ชันที่ไม่ดีและคำอธิบายที่ดีจริงๆ
izlin

ฉันขอแนะนำให้n"d"เป็นคำถามที่กล่าวว่าการส่งออกจะต้องเป็นตัวละครที่พิมพ์ได้ใด ๆ มันไม่ได้พูดอะไรเกี่ยวกับการใช้คำเต็ม
ender_scythe

20

Ruby: 61 54 ตัวอักษร

o="rps";p o[c=rand(3)],%w{Draw Win Lose}[c.-o.index$_]

ยังไงก็เถอะอธิบาย:

ปัญหาทั้งหมดจะลดลงเป็นการคำนวณผลลัพธ์ต่อไปนี้:

  ╲ machine
h  ╲| 0 1 2
u ──┼──────
m 0 │ 0 1 2 
a 1 │ 2 0 1
n 2 │ 1 2 0

ที่หมายถึงตัวเลขที่:

  • ตัวเลือก: 0 rock, 1 paper, 2 scissor
  • ผล: 0 เสมอ 1 ชนะ 2 แพ้

สำหรับสิ่งนี้ฉันใช้สูตร: machine_choice - human_choice บางครั้งผลลัพธ์นี้มีค่าเป็นลบ แต่เนื่องจากถูกใช้เป็นดัชนีและดัชนีลบเท่านั้นที่จะนับถอยหลังจะเลือกองค์ประกอบอาร์เรย์ที่ถูกต้อง

# ┌── choosable object type
# │           ┌── machine's choice numeric code
# │           │                  ┌── result type
# │           │                  │                   ┌── human's choice
# │           │          ┌───────┴───────┐           │
  o="rps";p o[c=rand(3)],%w{Draw Win Lose}[c.-o.index$_]
#           └─────┬────┘                   └─────┬────┘  
#                 └── machine's choice letter    │
#                                                └── result numeric code

วิธีการใช้งาน (วิธีอื่นFixnumเป็นวิธีที่ชัดเจน):

  • Kernel.p (obj) → obj -“สำหรับแต่ละวัตถุเขียนโดยตรงobj inspectตามด้วยการขึ้นบรรทัดใหม่ไปยังเอาต์พุตมาตรฐานของโปรแกรม”
  • Kernel.rand (max = 0) → number -“ ถ้าถูกเรียกโดยไม่มีอาร์กิวเมนต์หรือถ้าmax.to_i.abs == 0rand ส่งคืนตัวเลขทศนิยมแบบหลอกเทียมระหว่าง 0.0 ถึง 1.0 รวมถึง 0.0 และยกเว้น 1.0”
  • String.index (substring [, offset]) → fixnum หรือ nil -“ ส่งคืนดัชนีการเกิดขึ้นครั้งแรกของsubstringหรือรูปแบบที่กำหนด (regexp) ในstr

Ungolfed:

object_type = "rps";
result_type = %w{Draw Win Lose}

machine_choice = rand(3)
human_choice = $_

p object_type[machine_choice]

result_code = machine_choice - object_type.index(human_choice)
p result_type[result_code]

วิ่งตัวอย่าง:

bash-4.2$ ruby -nle 'o="rps";p o[c=rand(3)],%w{Draw Win Lose}[c.-o.index$_]'
r
"p"
"Win"
p
"p"
"Draw"
s
"p"
"Lose"

1
คล้ายกับของฉันมาก [(c-o.index($_)+3)%3]สามารถถูกแทนที่ด้วย[c.-o.index$_]7 ไบต์ นอกจากนี้คุณควรเพิ่มสองคะแนนของคุณเป็น-nlเวลาอย่างน้อยที่สุด
primo

1
Doh! นั่นเป็นความตั้งใจแรกของฉันที่จะใช้ประโยชน์จากดัชนีเชิงลบ ดูเหมือนว่าฉันลืมที่จะลองวิธีนี้อีกครั้งหลังจากแก้ไขปัญหาการสั่งซื้อถูกดำเนินการ ขอบคุณ @primo และอีกหนึ่งขอบคุณสำหรับc.เคล็ดลับ
จัดการ

ฉันต้องบอกว่าฉันรักคำตอบนี้ !!
jsedano

2
@manatwork .-เคล็ดลับที่เป็นจริง ตัวดำเนินการที่เป็นจุดมีความสำคัญต่ำกว่าคู่ที่ไม่ประ ยกตัวอย่างเช่นสามารถถูกแทนที่ด้วยa/(b+c) a./b+c
primo

9

C # (167 ตัวอักษร)

ฉันลองเล่นกอล์ฟเป็นครั้งแรก

แข็งแรงเล่นกอล์ฟ

using System;class P{static void Main(string[] i){var m="rspr";var a=m[Environment.TickCount%3];Console.WriteLine(a+" "+(i[0][0]==a?"T":m.Contains(i[0]+a)?"W":"L"));}}

ยกเลิกแข็งแรงเล่นกอล์ฟ

using System;

class P
{
    static void Main(string[] i)
    {
        var m = "rspr";
        var a = m[Environment.TickCount % 3];
        Console.WriteLine(a + " " + (i[0][0] == a ? "T" : m.Contains(i[0] + a) ? "W" : "L"));
    }
}

ตัวอย่างเรียกใช้ app ที่ต้องปัจจัยการผลิตถ่านเดียวเป็นอาร์กิวเมนต์ 1 ถึงการประยุกต์ใช้อย่างใดอย่างหนึ่งr, หรือsp

cmd > app.exe r

ผลลัพธ์ที่เป็นไปได้ทั้งหมด

  • cmd > app.exe rให้ผลผลิตr T(ร็อค, เน็คไท)
  • cmd > app.exe rให้ผลผลิตp L(กระดาษหายไป)
  • cmd > app.exe rให้ผลผลิตs W(กรรไกรชนะ)
  • cmd > app.exe pให้ผลผลิตr W(ร็อคชนะ)
  • cmd > app.exe pให้ผลผลิตp T(กระดาษ, เน็คไท)
  • cmd > app.exe pให้ผลผลิตs L(กรรไกรหายไป)
  • cmd > app.exe sให้ผลผลิตr L(ร็อคหายไป)
  • cmd > app.exe sให้ผลผลิตp W(กระดาษชนะ)
  • cmd > app.exe sให้ผลผลิตs T(กรรไกรและเน็คไท)

9

Perl 48 ไบต์

$%=rand 3;print"$%
"^B,(Draw,Lose,Win)[$%-=<>^B]

สคริปต์พิมพ์ผลจากมุมมองของคอมพิวเตอร์เช่นถ้าเลือกผู้เล่นrและเลือกที่คอมพิวเตอร์ผลที่ได้คือs (หมายเลขหน้ารูปแบบ) ใช้เพื่อจัดเก็บการเคลื่อนไหวของคอมพิวเตอร์เนื่องจากอาจมีค่าจำนวนเต็มเท่านั้นซึ่งจะบันทึกการส่งคำสั่งแบบ intLose$%

Ungolfed:

# choose a random move index 0, 1, or 2
$cindex = int(rand 3);
# convert this index to a move
# 0 => r, 1 => s, 2 => p
$cmove = "$cindex" ^ B;

# read the player's move
$pmove = <>;
# convert this move to its index
$pindex = $pmove ^ B;

# print computer's move
print $cmove, $/;
# compare indices, and output result
@result = (Draw, Lose, Win);
print $result[$cindex - $pindex];

ตัวอย่างการใช้งาน:

$ echo p | perl rps.pl
s
Win

$ echo r | perl rps.pl
r
Draw

$ echo s | perl rps.pl
p
Lose

สคริปต์อาจทำงานแบบโต้ตอบโดยพิมพ์การย้ายตามด้วยEnter:

$ perl rps.pl
r
s
Lose

การยืดกล้ามเนื้อกฎ

Perl 35 +3 ไบต์

$_=($%=rand 3).(D,L,W)[$%-($_^B)]^B

ต้องการ-pสวิตช์บรรทัดคำสั่ง (นับเป็น 3 ไบต์) แต่ละผลWin, LoseและDrawได้รับการแมปไปW, ,L Dบรรทัดใหม่ระหว่างตัวเลือกของคอมพิวเตอร์และผลลัพธ์ถูกแยกออก

ตัวอย่างการใช้งาน:

$ echo r | perl -p rps.pl
sL

Perl 30 +3 ไบต์

$_=($%=rand 3).($%-($_^B))%3^B

-pอีกครั้งหนึ่งที่จำเป็นต้องใช้ ที่นี่Win, LoseและDrawได้รับการแมปไป2, 1และ0ตามลำดับ ยังคงเป็นไปตามเทคนิคในทางเทคนิคเนื่องจากเป็นอักขระที่พิมพ์ได้

ตัวอย่างการใช้งาน:

$ echo r | perl -p rps.pl
s1

Perl 24 +3 ไบต์

$_=$^T%3 .($^T-($_^B))%3^B

ต้อง-p, WLD แมปไป2, 1, 0เป็นมาก่อน แต่ละคน^Tควรถูกแทนที่ด้วยตัวอักษร ASCII ตัวอักษร 20 อันนี้เป็นที่ยอมรับเล็กน้อยยืด; $^Tส่งคืนจำนวนวินาทีตั้งแต่ epoch ตั้งแต่เมื่อสคริปต์เริ่มทำงาน ผลลัพธ์ทั้งหมดเป็นไปได้ แต่ก็ไม่ค่อยมีคุณสมบัติเป็นแบบหลอกหลอก

ตัวอย่างการใช้งาน:

$ echo r | perl -p rps.pl
s1

7

APL ( 38 36)

c[i],(⌽↑⌽∘'TWL'¨⍳3)[⍞⍳⍨c←'rps';i←?3]

เอาท์พุต 'T', 'W' และ 'L' เพื่อเสมอชนะและแพ้

วิ่งตัวอย่าง:

      c[i],(⌽↑⌽∘'TWL'¨⍳3)[⍞⍳⍨c←'rps';i←?3]
p    
rW

(ประเภทผู้ใช้ 'p' สำหรับกระดาษคอมพิวเตอร์เลือก 'r' (ร็อค) ผู้ใช้ชนะ)

คำอธิบาย:

  • ⌽↑⌽∘'TWL'¨⍳3: สร้างเมทริกซ์ต่อไปนี้:
TLW
WTL
ไลท์เวท
  • ⍞⍳⍨c←'rps': ตั้งค่าcเป็นสตริง'rps'อ่านอินพุตผู้ใช้และรับดัชนีของอินพุตผู้ใช้ในสตริง (นี่จะเป็นค่าตั้งแต่ 1 ถึง 3) ดัชนีนี้ใช้เป็นพิกัด Y ในเมทริกซ์
  • i←?3: รับตัวเลขสุ่มจาก 1 ถึง 3 และเก็บไว้ในiนี่คือตัวเลือกของคอมพิวเตอร์ ใช้เป็นพิกัด X ลงในเมทริกซ์
  • c[i]: ใช้iเป็นดัชนีลงไปcเพื่อแสดงตัวเลือกของคอมพิวเตอร์ว่า 'r', 'p' หรือ 's'

6

ferNANDo 1184 (259 golfed ) ไบต์

ล่ามที่เขียนด้วย Python สามารถพบได้ที่ด้านล่างของหน้าลิงค์

ferNANDo เป็นภาษาที่ลึกลับซึ่งรองรับเพียงหนึ่งประเภทตัวแปรบูลีนและการดำเนินการเดียวเท่านั้น NAND ดังที่คุณอาจจินตนาการถึงสิ่งนี้อาจนำไปสู่ตรรกะที่ค่อนข้างยาวพอที่จะทำให้งานง่าย ๆ ดูเหมือนจะสำเร็จ มันมีการสนับสนุนสำหรับการอ่านจาก stdin (ทีละหนึ่งไบต์), การเขียนไปยัง stdout (ยังหนึ่งไบต์ในเวลา), ลูปแบบมีเงื่อนไขและยังเป็นเครื่องกำเนิดบูลีนแบบสุ่ม

ไม่มีคำค้นหา แต่อย่างใด ทุกอย่างเป็นตัวแปร ฟังก์ชั่นของคำสั่งจะถูกกำหนดเพียงจำนวนตัวแปรที่มี นอกจากนี้ยังไม่มีความคิดเห็นดังนั้นฉันพยายามอย่างเต็มที่เพื่อให้โค้ดแสดงความคิดเห็นด้วยตนเอง สี่บรรทัดสุดท้ายอาจสับสนเล็กน้อย แต่อาจพอเพียงเพื่อบอกว่าพิมพ์Win!ออกLoseมาหรือDrawขึ้นอยู่กับผลลัพธ์

not sure, but right now i'm guessing you're_not_paper you're_scissors
you're_paper not you're_not_paper
you're_not_scissors not you're_scissors
you're_rock you're_not_paper you're_not_scissors
you're_rock you're_rock

o_shi-
i'm_not_paper right ?
i'm_scissors right ?
i'm_paper not i'm_not_paper
i'm_not_scissors not i'm_scissors
o_shi- i'm_paper i'm_scissors
o_shi- o_shi-
o_shi-
i'm_rock i'm_not_paper i'm_not_scissors
i'm_rock i'm_rock

print right now but only if i'm_not_paper i'm_scissors
print a newline here, not more, not less

i_win_if i'm_scissors you're_paper
or_if i'm_rock you're_scissors
or_even_if i'm_paper you're_rock

i_win i_win_if or_if
i_win i_win
i_win or_even_if

i_lose_if i'm_paper you're_scissors
or_if i'm_scissors you're_rock
or_even_if i'm_rock you're_paper

i_lose i_lose_if or_if
i_lose i_lose
i_lose or_even_if

i_don't_win not i_win
i_don't_lose not i_lose
we_tie i_don't_win i_don't_lose
we_tie we_tie
we_don't_tie not we_tie

print now if i_win i_lose not i_win i_win
print but not we_tie we_don't_tie i_lose i_don't_win we_don't_tie
print right now i_lose i_win i_win we_don't_tie i_don't_win
print i_don't_win but we_tie or i_don't_win we_tie now

Enterสคริปต์ที่สามารถเรียกใช้การโต้ตอบโดยการพิมพ์ของคุณย้ายตามมาด้วย

ตัวอย่างการใช้งาน (สมมติว่าคุณใช้ชื่อล่ามnand.py):

$ python nand.py rps.nand
p
s
Win!

$ python nand.py rps.nand
r
r
Draw

$ python nand.py rps.nand
s
p
Lose

แก้ไข:เพียงเพื่อพิสูจน์ว่าเฟอร์นันโดสามารถแข่งขันกับ Java นี่เป็น 'แข็งแรงเล่นกอล์ฟ' รุ่นที่259 ไบต์ ตรรกะนั้นแตกต่างอย่างเห็นได้ชัด มันตรวจสอบnot winและnot tie, ซึ่งจะช่วยประหยัดประตู NAND ไม่กี่แห่ง (เพราะฉันต้องการเพียงแค่notเวอร์ชันของการเคลื่อนไหวของผู้เล่น, และเพราะnot loseไม่จำเป็นสำหรับเอาท์พุท) แม้ว่าจะไม่น่าสนใจเท่าที่อ่าน

1 _ _ _ _ _ _ A b
B 1 b
C A B
o
P 1 ?
s 1 ?
p 1 P
S 1 s
o p s
o o
o
r P S
r r
0 1 1 1 0 0 P s
0 0 0 0 1 0 1 0
t s A
u r B
v p C
W t u
W W
W v
t p A
u s B
v r C
D t u
D D
D v
w 1 W
d 1 D
l W D
l l
0 1 0 w l 1 w w
0 1 1 d D l W D
0 1 1 l w w D W
0 W 1 d 0 W d 1

2
ฮ่าฮ่าฮ่าo_shi- o_shi- o_shi-นี่เป็นการแสดงความคิดเห็นด้วยตัวเองเฮฮา
jdstankosky

1
@jdstankosky อาจจะมีปฏิกิริยาตอบโต้เล็กน้อยต่อการกลิ้งqแต่เขาทำสิ่งที่สมเหตุสมผลและ reroll
โม่

5

Python 3.x: 74 ตัวอักษร

import time
A,f="psr",time.gmtime()[5]%3
print(A[(A.find(input())+f)%3],f)

มันทำงานอย่างไร

Before Machine Proceeds it determines the outcome of the game and based 
on the outcome, it determines the choice which would result in the outcome
viz Human's choice


\     Outcome
 \    0  1  2
  \_____________
H  | 
U 0|  0  1  2
M 1|  1  2  0
A 2|  2  0  1
N  |

Where Choices are represented as 
0 --> Paper
1 --> Scissor
2 --> Rock

Outcome (From Computer's Perspective)
0 --> Draw
1 --> Win
2 --> Fail

Given the sequence of choices as a string
"psr"
So its easy to see, if computer needs to win, it needs to choose the character
next to what human chooses. 
If computer needs to loose, it needs to choose the previous character to what
human chooses


MACHINE's|      CHOICES    |  Formulation
FATE     |-----------------|  For Machine's
         |  P     S      R |  Choice
---------|-----------------|-----------------------------
WIN(1)   |        H ---> M | (CHOICE+1) % 3 = (CHOICE+WIN)%3 
---------|-----------------|-----------------------------
LOSS(2)  |  M     H -----\ | (CHOICE+2)%3   = (CHOICE+LOSS)%3 
         |  ^            | |
         |  |____________| |
---------|-----------------|------------------------------       
DRAW(0)  |        H        | (CHOICE+0)%3   = (CHOICE+DRAW)%3
         |        M        |  
---------|-----------------|         

Combining all the above we have

MACHINE's CHOICE = (HUMAN CHOICE + MACHINE's FATE) % 3

ขึ้นอยู่กับชะตากรรมมันเป็นตัวกำหนดสิ่งที่มันควรจะเลือกขึ้นอยู่กับสูตร

result = (User_choice + machines_fate) % no_of_choices

machine_choice = "psr"[result]

เวอร์ชันที่ไม่ตีกอล์ฟ

import time
choices = "psr"
#time.gmtime() returns the time structure in gmt
#time.gmtime()[5] is the current second tick
fate = time.gmtime()[5]%3
user_choice = input()
result = (choices.find(user_choice)+fate)%len(choices)
machine_choice = choices[result]
print(machine_choice, fate)

วิ่งตัวอย่าง

D:\temp\rivalry>rps.py
r
r 0

D:\temp\rivalry>rps.py
r
p 1

D:\temp\rivalry>rps.py
p
r 2

D:\temp\rivalry>rps.py
p
r 2

D:\temp\rivalry>rps.py
p
s 1

D:\temp\rivalry>rps.py
p
s 1

D:\temp\rivalry>rps.py
p
p 0

2
ที่น่าสนใจตัดสินใจก่อนผลแล้วโยนสิ่งใดก็ตามที่เป็นสัญญาณเพื่อรับผลนั้น ...
27432

1
ฉันไม่แน่ใจว่าเวลาในไม่กี่วินาทีมีคุณสมบัติภายใต้"ทางเลือกโปรแกรม ( 'R' 'p' หรือ 's') จะต้องมีการหลอกสุ่ม" อาจเป็นมิลลิวินาที
primo

4

ลัวะ, 87

c,n=os.time()%3+1,'rps'print(n:sub(c,c),({'Draw','Win','Defeat'})[(n:find(...)-c)%3+1])

การใช้งาน:

$ lua rps.lua p
s   Defeat

Ungolfed:

names = 'rps'
comp_idx = os.time()%3 + 1                -- 1, 2 or 3 (depends on timer)
comp_move = names:sub(comp_idx, comp_idx) -- 'r', 'p' or 's'
user_move = ...                           -- input parameter: 'r', 'p' or 's'
user_idx = names:find(user_move)          -- 1, 2 or 3
delta_idx = (user_idx - comp_idx) % 3     -- 0, 1 or 2
all_results = {'Draw', 'Win', 'Defeat'}   -- [1]=='Draw', [2]=='Win', [3]=='Defeat'
game_result = all_results[delta_idx + 1]
print(comp_move, game_result)

คุณสามารถบันทึกได้ 3 ตัวอักษรโดยใช้ 'แพ้' แทน 'พ่ายแพ้' และ 'ผูก' แทน 'ดึง' ;)
Roddy of the Peas แช่แข็ง

4

GolfScript 62

วิธีการแก้ปัญหา GolfScript ทางเลือกอื่น ๆ อีกมากมายที่น่าเบื่อกว่าของฮาวเวิร์ด :)

โปรแกรมเลือกย้ายแบบสุ่มและแสดงผลลัพธ์จากมุมมองของผู้ใช้

รหัส

'rssppr'.[6rand=]''+:§@+..&,({?)§\'Lose''Win'if}{;;§'Draw'}if`

วิ่งตัวอย่าง

> echo s | rubs golfscript.rb rps.gs

r "เสีย"

ทดสอบออนไลน์

คุณสามารถรันโปรแกรมและทดสอบด้วยอินพุตที่แตกต่างกันได้ที่นี่: http://golfscript.apphb.com/?c=OydzJwoKJPJPZPFV31X3Cw สามมิติ

อย่างไรก็ตามโปรดทราบว่าพารามิเตอร์ (การย้ายผู้ใช้) ที่มักจะส่งผ่านในบรรทัดคำสั่งจะถูกเพิ่มไปยังสแต็กในรหัสตัวเอง (ไม่มีวิธีที่จะให้พารามิเตอร์บรรทัดคำสั่ง "ของจริง" ในเครื่องมือออนไลน์นี้)

รุ่น "Ungolfed"

ผมไม่ได้มีความคิดว่าungolfedหมายถึงเมื่อมันมาถึง GolfScript ดังนั้นฉันพยายามที่จะเพิ่มความคิดเห็น หวังว่าจะชี้แจงวิธีการทำงานของรหัสและทำให้อ่านง่ายขึ้นอีกเล็กน้อย:

# initially, the input (user's move) is on the stack

'rssppr'                # push the string 'rsspr' on the stack...
.                       # ...twice.

[
    6rand               # get a pseudo-random integer in the [0-5] range
    =                   # use the random index to get 
                        # a letter from the string above

]''+                    # workaroud to get the actual letter instead of the
                        # ASCII code

:§                      # assign the randomly chosen letter (computer's move)
                        # to a nice variable called "§"

@                       # rotates last 3 elements on the stack, bringing
                        # the user input in the uppermost position
                        # So, now we have: "rssppr" <computer_move> <user_move>
                        # on the stack

+                       # concatenate the two moves, so now we have a string
                        # that contains both (ex: "rs")

..                      # copy the move string twice
&                       # do a setwise AND to get the DISTINCT elements
,(                      # get the length of the resulting string and subtract 1
                        # (now we have 0 if the letters were equal and non-zero otherwise)

{                       # beginning of block to execute when the moves are different:

                        # now we have on the stack two strings:
                        #     - the string 'rssppr'
                        #     - the string containing the moves (ex: 'rs')

    ?                   # find the second string inside the first one,
                        # and get the index at which it occurs
                        # (or -1 if it does not)

    )                   # increment that number (we now get 0 if no occurrence, 1 otherwise)

    §                   # recall the § variable (so we display the computermove)
    \                   # rotate the uppermost two stack entries

    'Lose''Win'if       # if the move pair is found in the 'rssppr' string, 
                        # then print 'Lose', otherwise print 'Win'
}

{                       # beginning of block to execute when the moves are identical:

    ;;                  # discard the latest two stack items (not needed in this case)
    §                   # display computer's move
    'Draw'              # display the text 'Draw'
}
if                      # if computer's and user's moves were NOT equal, 
                        # execute the first block.
                        # if they were, execute the second block

`                       # put the last word in quotes to separate it from computer's move

4

C, 92 86 ตัวอักษร

main(y){
    srand(time(0));
    y="rps"[rand()%3];
    printf("%c%c\n",y,"LWWTLLW"[getchar()-y+3]);
}

พิมพ์ทางเลือกของเครื่องคอมพิวเตอร์และผลจากจุดของผู้ใช้ในมุมมองของ - W = คุณชนะ, L = คุณสูญเสีย t = ผูก
สูตรง่าย ๆx-yได้รับค่า ASCII ของตัวเลือกให้ 0 วาด (ชัด) และค่าที่ไม่ซ้ำกันในแต่ละกรณีอื่น ๆ


4

Python 2 ( 86 84 80 78), Python 3 - 76 ตัวอักษร

0 - เสมอ 1 - แพ้ 2 - ชนะ

from random import*
a=raw_input()
b=choice('psr')
print(a!=b)+(b+a in'rpsr'),b

from random import*
a=input()
b=choice('psr')
print((a!=b)+(b+a in'rpsr'),b)

Ungolfed

from random import*
moves = 'psr'
inp   = raw_input()
comp  = choice(moves)
match = comp+inp
is_not_tie = inp!=comp
wins = 'r' + moves         #rpsr; rock beats scissors, scissors beats paper, paper beats rock
print is_not_tie + (match in wins), comp

วิธีการใช้งาน: python file_name_here.py

ปัญหา:
คอมพิวเตอร์ AI: 35 ตัวอักษร


1
ดี! ผมไม่คิดว่าคุณต้องแม้ว่าและคุณสามารถบันทึกถ่านอีกด้วยA from random import*
grc

@grc: ขอบคุณฉันไม่ได้ลองfrom randomเพราะฉันคิดว่ามันไม่ได้เปลี่ยน ...
beary605

คุณสามารถตัดเพิ่มอีก 2 ตัวอักษรโดยการเปลี่ยน (a == b) * 2 เป็น (a! = b) และเปลี่ยนผลลัพธ์เป็น "0-tie, 1-win, 2-loss"
Dhara

ขออภัยแปลว่า "" 0-tie, 1-loss, 2-win "
Dhara

@ Dhara: ขอบคุณจะไม่คิดออกว่า การจัดการ: โอ้! ฉันจะแก้ไขมัน
beary605

4

ก่อนอื่นให้ลองโดยไม่ตรวจสอบผู้อื่น

golfed: 107 85 ไบต์

i=prompt(),c="rps"[new Date%3],w={r:"s",p:"r",s:"p"};alert(c+(i==w[c]?2:w[i]==c?1:3))

เอาต์พุตคือ [npc-choice] [1: win, 2: loss, 3: tie]

ungolfed:

var input = prompt(),
    choices = ["r","p","s"],
    computer_choice = choices[Math.floor(Math.random() * 3)],
    outcomes = {'r':'s','p':'r','s':'p'},
    winner;

    if( input == outcomes[computer_choice] ) {
        winner = 'NPC';
    } else if ( computer_choice == outcomes[input] ) {
        winner = 'You';
    } else {
        winner = 'No one, it was a Tie!';
    }

    alert('Computer chose: ' + computer_choice + '\n' +
          'The winner is: ' + winner);

4

PowerShell: 144 133 117 111 92 73

การเปลี่ยนแปลงจากต้นฉบับ:

  • สมบูรณ์อีกครั้งเขียนบทหลังจากที่ได้เห็นสามผู้เล่นหรือแม้กระทั่งวิธีการแก้ปัญหาที่แปลก Danko Durbic ของ
  • เปลี่ยน $ s เป็นสตริงเดี่ยวแทนที่จะเป็นอาร์เรย์อักขระ
  • ใช้ IndexOf เป็นวิธีการโดยตรงกับ $ s แทนที่จะสะกดคำว่า. NET class & method
  • ลบ%3s ซ้ำซ้อน

ทั้งหมดบอกว่าเกือบตัดความยาวครึ่งจากคำตอบเดิมของฉัน!

รหัส Golfed:

$p=($s='rps').IndexOf((read-host));$s[($c=Random 3)];"TWLLTWWLT"[$p+$c*3]

สามารถเรียกใช้ตรงจากคอนโซล

Ungolfed พร้อมความคิดเห็น:

# Variable $p will store the player's selection as a ternary digit by finding its position in a string containing the possible choices.
$p=(
    # Possible choices will be stored in a variable, $s, for later reuse.
    $s='rps'
# Get the position of the player's choice from $s.
).IndexOf((read-host));

# Express the computer's choice by outputting the appropriate character from $s.
$s[(
    # Computer's choice will be stored as a ternary digit in $c.
    $c=Random 3
)];

# Outcome for the player will be chosen from a string of possible outcomes by looking up the decimal repesentation of a two-digit ternary number.
# The threes digit is represented by $c, ones digit by $p.
"TWLLTWWLT"[$p+$c*3]

# Variable cleanup - do not include in golfed code.
rv p,s,c

ตัวอย่างบางส่วนทำงานที่คอนโซล:

ป้อนคำอธิบายรูปภาพที่นี่


3

JAVA 259 :(

class c {public static void main(String[]a){char y=a[0].charAt(0);char m="rps".charAt(new java.util.Random().nextInt(3));if(y==m)a[0]="T";else if((y=='r'&& m=='s')||(y=='s'&& m=='p')||(y=='p'&& m=='r'))a[0]="1";else a[0]="0";System.out.println(m+":"+a[0]);}}

รหัส ungolfed สูง:

class c {
    public static void main(String[] a) {
        char y = a[0].charAt(0);
        char m = "rps".charAt(new java.util.Random().nextInt(3));
        if (y == m) {
            a[0] = "T";
        } else if ((y == 'r' && m == 's') || (y == 's' && m == 'p') || (y == 'p' && m == 'r')) {
            a[0] = "1";
        } else {
            a[0] = "0";
        }
        System.out.println(m + ":" + a[0]);
    }
}

ตัวอย่างการวิ่ง:

C:> java cr

S: 1

C:> java cp

P: T

C:> java cs

เซนต์


3

Befunge: 107 ตัวอักษร

~v@,"w"<   <   <  <
v?v3.14159265@,"l"<
"""358979323846>3-|
rps26433832>:1+|
"""7950>:2+|
>>>:,-:|
28@,"t"<

clunky เล็กน้อย จะหดตัวได้คำถามคือเท่าไหร่


3

จาวาสคริปต์ (87)

แข็งแรงเล่นกอล์ฟ:

o='LDW'[2*((a=prompt())+(b='prs'[new Date%3])!='ps'&a<b|a+b=='sp')+ +(a==b)];alert(b+o)

Ungolfed:

var player = prompt(), 
    computer = 'prs'[new Date%3], // Date mod 3 "pseudo-random"
    outcome = 'LDW'[2*(player+computer != 'ps' 
                    & player < computer 
                    | player + computer == 'sp') // convert boolean W/L outcome to int (0,2)
              +
              +(player == computer)]; // convert D outcome to int (0,1)
alert(computer + outcome);

คุณสามารถวางรหัสในคอนโซล Javascript ของเบราว์เซอร์เพื่อเรียกใช้

หากฉันได้รับอนุญาตให้พิมพ์ผลลัพธ์ก่อนพิมพ์ตัวเลือกของคอมพิวเตอร์(83) :

alert('LDW'[2*((a=prompt())+(b='prs'[new Date%3])!='ps'&a<b|a+b=='sp')+ +(a==b)]+b)

2

K, 67

{-1'f:x,*1?"rps";$[f in b:("pr";"rs";"sp");"W";f in|:'b;"L";"D"]}

พิมพ์ W, L, D สำหรับการชนะ / แพ้ / วาด

Ungolfed:

rps:{[x]
    res:x,*1?"rps";        // join user input to random selection of r,p,s
    -1'f;                  // print the user input and the program selection to stdout
    wins:("pr";"rs";"sp"); // the universe of winning combinations
    losses:|:'wins;        // reverse each win to the get losses

    $[f in wins;    
        "win";
    f in losses;
        "lose";
    "draw"]
    }

หรือใน Q ซึ่งสามารถอ่านได้มากขึ้น:

rps:{[x]
    res:x,rand["rps"];        // join user input to random selection of r,p,s
    -1 each f;                // print the user input and the program selection to stdout
    wins:("pr";"rs";"sp");    // the universe of winning combinations
    losses:reverse each wins; // reverse each win to the get losses

    $[f in wins;    
        "win";
    f in losses;
        "lose";
    "draw"]
    }

เรียกใช้ตัวอย่าง:

k){-1'f:x,*1?"rps";$[f in b:("pr";"rs";"sp");"W";f in|:'b;"L";"D"]}"r"
r
s
"W"
k){-1'f:x,*1?"rps";$[f in b:("pr";"rs";"sp");"W";f in|:'b;"L";"D"]}"s"
s
s
"D"
k){-1'f:x,*1?"rps";$[f in b:("pr";"rs";"sp");"W";f in|:'b;"L";"D"]}"p"
p
s
"L"

2

Javascript, 117 ตัวอักษร

ต่อไปนี้เป็นวิธีแก้ไขปัญหาข้อมูล สิ่งนี้สามารถปรับให้เหมาะสมได้โดยการสร้างข้อมูล win / loss / draw แทนที่จะทำการแมปด้วยตนเอง แต่เป็นการเริ่มต้น :)

แข็งแรงเล่นกอล์ฟ:

alert((r=[{v:"r",r:d="D",p:w="W",s:l="L"},{v:"p",r:l,p:d,s:w},{v:"s",r:w,p:l,s:d}][Math.random()*3|0]).v+r[prompt()])

Ungolfed:

//Output the result to the user
alert(
    (
        //Store the random computer outcome data
        randomRPSData =
            //Create the data for if the computer chooses r, p, or s
            [
                {
                    value: "r",
                    r: (d = "Draw"),
                    p: (w = "Win"),
                    s: (l = "Lose")},
                {
                    value: "p",
                    r: l,
                    p: d,
                    s: w},
                {
                    value: "s",
                    r: w,
                    p: l,
                    s: d}
            ]
            //Have the computer pick a random variable
            [Math.random() * 3 | 0]

    //Output the value the computer chose
    ).value

    //Output whether the user won or not
    + r[prompt()]
);

ในที่สุดนี่คือซอกับทั้งสอง


1

Javascript: 256

แข็งแรงเล่นกอล์ฟ:

i=prompt(),a=['p','r','s'];a=a[Math.floor(Math.random()*(3-1+1))+1];if(i==a){alert('d');}else if(i=='p'){if(a=='s'){alert('l');}else{alert('w');}}else if(i=='r'){if(a=='s'){alert('w');}else{alert('l');}}else if(i=='s'){if(a=='r'){alert('l');}else{alert('w')}}

ungolfed:

i=prompt(),a=['p','r','s'];
a=a[Math.floor(Math.random()*(3-1+1))+1];
if(i==a){
    alert('d');
}
else if(i=='p'){
    if(a=='s'){
        alert('l');
    }else{alert('w');}
}else if(i=='r'){
    if(a=='s'){
        alert('w');
    }else{alert('l');}
}else if(i=='s'){
    if(a=='r'){
        alert('l');
    }else{alert('w')}
} 

1

Clojure:

(def r 0) (def s 1) (def p 2)
(def object-name #(get {'p "Paper", 's "Scissors", 'r "Rock"} %))
(def result-name #(get {\d "Draw", \w "Win", \l "Lose"} %))
(defn computer-choice [] (nth ['r 's 'p] (int (rand 3))))
(defn game [a b] (get "dwlldwwld" (+ (* 3 a) b) ))
(defn print-game [user comp result] (print (format
  "User: %s\nComputer: %s\nResult: %s\n" 
  (object-name user) (object-name comp) (result-name result))))
(println "Enter 'p', 's' or 'r' and press return")
(let [comp (computer-choice),  user (read)]  (print-game user comp (game (eval user) (eval comp))))

รุ่นมินิ (129 รหัสอักขระ):

java -jar clojure.jar -e \
"(def r 0)(def s 1)(def p 2)(let[u(read),c(nth['r 's 'p](int(rand 3)))](print c)(print (get \"dwlldwwld\"(+(* 3(eval u))(eval c)))))"

1

JAVA (387)รหัสแรกของกอล์ฟ!

import java.util.HashMap;public class _ {public static void main(String i[]){HashMap l = new HashMap(){{put('r',0);put('p',1);put('s',2);put(0,'T');put(1,'L');put(2,'W');}};char u =i[0].charAt(0);char c ="rps".charAt((int)(Math.random()*3)%3);int[][] m =new int[][]{{0,1,2},{2,0,1},{1,2,0}};System.out.println("U"+u+"C"+c+"R"+(Character)l.get(m[(Integer)l.get(u)][(Integer)l.get(c)]));}}

Ungolfed

import java.util.HashMap;
public class _ {
    public static void main(String[] input) {
       input = new String[] {"s"};
       HashMap lookup = new HashMap(){{
           put('r', 0);
           put('p', 1);
           put('s', 2);
           put(0, 'T');
           put(1, 'L');
           put(2, 'W');
       }};
       char user = input[0].charAt(0);
       char computer = new char[] {'r', 'p', 's'}[(int)(Math.random()*3)%3];
       int[][] matrix = new int[][] {{0,1,2}, {2,0,1}, {1,2,0}};

       Integer userChoice = (Integer) lookup.get(user);
       Integer computerChoice = (Integer) lookup.get(computer);
       Character result = (Character) lookup.get(matrix[userChoice][computerChoice]);

       System.out.println("u:" + user + ",c:" + computer + ",r:" + result);
    }
    /*
     t = 0, l = 1, w = 2
     *
        +---------------+
        | * | r | p | s |
        +---------------+
        | r | 0 | 1 | 2 |
        +---------------+
        | p | 2 | 0 | 1 |
        +---------------+
        | s | 1 | 2 | 0 |
        +---------------+
     */
}

Golfed (ระยะห่าง / เยื้อง)

import java.util.HashMap;
public class _ {
public static void main(String i[]) {
    HashMap l = new HashMap(){{
        put('r',0);put('p',1);put('s',2);put(0,'T');put(1,'L');put(2,'W');
    }};

    char u =i[0].charAt(0);char c = "rps".charAt((int)(Math.random()*3)%3);
    int[][] m =new int[][]{{0,1,2},{2,0,1},{1,2,0}};

    System.out.println("U"+u+"C"+c+"R:"+(Character)l.get(m[(Integer)l.get(u)][(Integer)l.get(c)]));
}}

ไม่ใช่รหัสที่สั้นที่สุด แต่ลองครั้งแรกของฉัน


1
การปรับปรุงบางอย่างที่คุณอาจใช้: 1) import java.util.*2) HashMap l-> Map l3) โยน(int)แทน(Integer)4) new Random().nextInt(3)5) ละเว้นnew int[][]6) หล่อลบไป(Character)7) การใช้ ints แทนตัวอักษร
Howard

1

ไป (169)

Golfed:

package main
import("fmt";"os")
func main(){v:=map[uint8]int{114:0,112:1,115:2}
u:=os.Args[1][0]
c:="rps"[os.Getpid()%3]
fmt.Printf("%c\n%c\n",c,"TWL"[(3+v[c]-v[u])%3])}

Ungolfed (จัดรูปแบบตามgo fmt):

package main

import (
    "fmt"
    "os"
)

func main() {
    v := map[uint8]int{114: 0, 112: 1, 115: 2}
    u := os.Args[1][0]
    c := "rps"[os.Getpid()%3]
    fmt.Printf("%c\n%c\n", c, "TWL"[(3+v[c]-v[u])%3])
}

วิ่ง :

ไปรัน main.go p

s

W


1

Pyth , 23

J"rps"K+wOJK%-XJ'KXJtK3

เอาต์พุตอยู่ในรูปแบบ:

เสมอ: 0 Win: 1 การสูญเสีย: 2

คำอธิบาย:

J"rps"             J="rps"
K+wOJ              K=input()+random_choice(J)
K                  print K
  XJ'K             index of K[0] in J
      XJtK         index of K[1] in J
 -XJ'KXJtK         difference of above indexes
%-XJ'KXJtK3        above difference mod 3

เรียกใช้ดังนี้:

$ cat rps
J"rps"K+wOJK%-XJ'KXJtK3
s
$ cat rps | python3 pyth.py
< Extraneous debug output removed>
sp
1

สำหรับตัวละครอีก 4 ตัวเราสามารถใช้ T สำหรับเน็คไท, W สำหรับการชนะและ L สำหรับการสูญเสีย:

J"rps"K+wOJKr@"TWL"-XJ'KXJtK

"TWL"ทุกอย่างเป็นขึ้นเดียวกันจนกระทั่งความแตกต่างของดัชนีจุดที่เราจะใช้ความแตกต่างเป็นดัชนีลงในสตริง


หมายเหตุ: ในขณะที่ฉันพัฒนาภาษานี้หลังจากการโพสต์ความท้าทายฉันไม่เห็นความท้าทายมาจนถึงทุกวันนี้ ความท้าทายไม่ได้มีอิทธิพลต่อทุกแง่มุมของภาษา


0

Groovy, 89

v='rps'
r=new Random().nextInt(3)
print"${v[r]}${'TLW'[((r-v.indexOf(this.args[0]))%3)]}"

ใช้ตัวเลือกของผู้ใช้เป็นอาร์กิวเมนต์ ตัวอย่าง:

groovy rps.groovy p
sL

0

C # & LinqPad 133

แข็งแรงเล่นกอล์ฟ:

char i,c,r;i=Console.ReadLine()[0];var a="prsp";c="prs"[new Random().Next(3)];r=i==c?'T':a.Contains(""+i+c)?'W':'L';(c+">"+r).Dump();

ungolfed:

        char i, c, r; // i for input / c for choosen by computer / r for result
        i = Console.ReadLine()[0]; //getting input in LinqPad
        var a = "prsp"; 
        c = "prs"[new Random().Next(3)]; // Computer choose a random string
        r = i == c ? 'T' : a.Contains("" + i + c) ? 'W' : 'L'; (c + ">" + r).Dump(); //check for win - dump for output in LinqPad

เอาต์พุตตัวอย่าง (อินพุตเป็น s): r> L // คอมพิวเตอร์เลือก rock (r) -> ดังนั้นเราจึงสูญเสีย


0

จาวาสคริปต์ (79)

ชนเก่า แต่ที่นี่เราไป ...

ด้วยโบนัสเพิ่มที่คุณสามารถใช้ร็อคใด ๆ ที่คุณชอบ pegnatite, pyroxenite, Scoria, sovite, kimberlite, trondjemite, tuff หรืออื่น ๆ ข้อ จำกัด เพียงอย่างเดียวคือคุณไม่สามารถใช้pหรือs ;-)


การป้อนข้อมูล:

r = rock      (Or , ...)
p = paper
s = scissors

เอาท์พุท:

[computers choice][0=Loss, 1=Win, 2=Draw]

ตัวอย่าง:

User  Computer  Result
r     r         r2
r     p         p0
r     s         s1

รหัส Golfed:

u=~~{p:1,s:2}[prompt()],c=new Date%3;alert('rps'[c]+(u==c?2:!u&c>1|u&!c|u>1&c))

เทคนิคหลักที่ใช้คือ:

1. u = ~~{}[prompt()]

       การใช้วัตถุและเรียกค่าโดยการป้อนข้อมูลของผู้ใช้จาก{} prompt()จนถึงตอนนี้ดีมาก จากนั้นแทนที่จะให้r : 0เราสามารถบังคับให้มันถูกแปลงเป็น0โดยใช้ตัว~~ดำเนินการ NOT มันมักจะถูกใช้ไปยังหมายเลขชั้น แต่ยังมี(ประโยชน์)จะไม่กลับNaNเมื่อผู้ประกอบการอื่น ๆ +{a:1, b:2}['c']จะเหมือนตัวอย่างเช่น:

2. c = new Date%3

       Date มักถูกหลอกด้วยเช่นใช้+new Dateเพื่อแปลงเป็น timestamp โดยตรงมิฉะนั้นจะมีการแสดงสตริง ที่นี่โดยใช้modหนึ่งบรรลุผลเดียวกัน

3. 'rps'[c]

       การเข้าถึงที่จัดทำดัชนีในสตริงให้คอมพิวเตอร์ทางเลือก0 = r, หรือ1 = p2 = s

4. (u==c ? 2 : !u&c>1 | u&!c | u>1&c )

   IF  u == c
       Draw                    (aka 2)
   ELSE IF !u&c>1|u&!c|u>1&c 
       Win                     (aka 1)
   ELSE
       Loss                    (aka 0)
   ENDIF


...

รหัส ungolfed สามารถเป็นดังนี้:

function rock_paper_scissors() {
    var choices = {
            r : 0,
            p : 1,
            s : 2,
        },
        tr_table = ['Rock','Paper','Scissors'],
        user, pc, result
    ;

    function get_input() {
        var input = prompt("Enter your choice `r', `p' or `s':")
                    .toLowerCase();

        if (!/^[rps]/.test(input)) {
            console.warn("Bad input. Please choose `r', `p' or `s'.");
            // return get_input();
            return null;
        }
        return input[0];
    }
    function report(c1, c2) {
        if (c1 === pc) {
            return 'Draw';
        } else if (
            (c1 === 0 && c2  >  1) ||
            (c2 === 0 && c1 === 1) ||
            (c1  >  1 && c2  >  0)
        ) {
            return 'Win';
        } else {
            return 'Loss';
        }
    }

    if ((user = get_input())) {
        user = choices[user];
        pc = Math.floor(Math.random() * 3);
        result  = 
            'Computers choice: ' + tr_table[pc] + ', ' +
            'Your choice: ' + tr_table[user] + ', ' +
            'Result=' +  report(user, pc);
        console.log(result);
    }
    return result;
}

rock_paper_scissors();

0

Golfscript - 29

นี่คือคำตอบที่แท้จริงในครั้งนี้ (ดูที่คุณn"Draw": P)

'rps'.@\?3rand.@- 3%@@[=]''+\

Ungolfed:

'rps'3rand[=]''+.2$={\;'
T'}{.@'rspr'.@?@@(;\?='
L''
W'if=}if

คุณสามารถเรียกใช้ด้วยruby golfscript.rb MyCode.txtที่ไหนMyCode.txtเป็นรหัสโปรแกรม

ตัวอย่างรัน (ทั้งหมดที่มีอินพุต "r" แต่ใช้ได้กับทุกอย่าง) (เสมอคือ 0, ชนะคือ 2, แพ้คือ 1):

ruby golfscript.rb MyCode.txt
r0
ruby golfscript.rb MyCode.txt
p2
ruby golfscript.rb MyCode.txt
s1

หากคุณต้องการคำอธิบายของทั้งเวอร์ชันและ / หรือดูการปรับปรุงที่อาจเกิดขึ้นบอกฉัน


0

Javascript, 65 71 70

ไม่มีคอมพิวเตอร์ให้เลือกเอาต์พุต: 65

alert('-+0+0+0'['rppssrr'.indexOf('rps'[new Date%3]+prompt())+1])

เพียงเพื่อทดสอบว่าโซลูชันนั้นถูกต้อง:

for(x in {r:0,p:0,s:0})
  for(y in {r:0,p:0,s:0})
    console.log("%s %s %s", x, y, '-+0+0+0'['rppssrr'.indexOf(x+y)+1])

ด้วยคอมพิวเตอร์เลือกผลลัพธ์: 71

alert((c='rps'[new Date%3])+'-+0+0+0'['rppssrr'.indexOf(c+prompt())+1])

70

alert((g='rps'[new Date%3]+prompt())+-('srps'.match(g)?-1:g[0]!=g[1]))

และการทดสอบ:

for(x in {r:0,p:0,s:0})
  for(y in {r:0,p:0,s:0})
    console.log((g=x+y)+-('srps'.match(g)?-1:g[0]!=g[1]))
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.