อลิซและบ็อบต้องต่อสู้


24
  • อลิซ (A) และบ๊อบ (B) ตัดสินใจต่อสู้
  • นักสู้แต่ละคนมี 10 พลังชีวิต
  • พวกเขาผลัดกันหมุนดาย 6 ด้านเพื่อสร้างความเสียหาย
  • ความเสียหายนั้นจะถูกลบออกจากสุขภาพของคู่ต่อสู้
  • ในท้ายที่สุดทั้งอลิซและบ๊อบจะเอาชนะศัตรูของพวกเขา

แสดงให้ฉันเห็นว่าการต่อสู้ดำเนินไปอย่างไร เอาท์พุทรหัสเหล่านี้สำหรับการกระทำที่เกิดขึ้น

โจมตี

B a A    
^ Combatant
  ^ Action (attack)
    ^ Target

ม้วน

B r 4
^ Combatant
  ^ Action (roll)
    ^ Value

สุขภาพเปลี่ยนไป

A h 6
^ Combatant
  ^ Attribute (health)
    ^ Value   

ชนะ

A w 
^ Combatant
  ^ Action (win)

ตัวอย่างผลลัพธ์:

A a B
A r 4
B h 6
B a A
B r 6
A h 4
A a B
A r 6
B h 0        
A w

นี่คือกฎ:

  • เขียนในภาษาใดก็ได้
  • การตายครั้งเดียวควรมีโอกาสเท่ากันในการเกิดหมายเลข 1, 2, 3, 4, 5, หรือ 6
  • อลิซเริ่มต้นเสมอ (บ๊อบเป็นคนกล้าในแบบที่ล้าสมัย)
  • เอาท์พุทการกระทำสำหรับแต่ละเทิร์น
  • คุณต้องรายงานการโจมตีม้วนความเสียหายและการกระทำที่ชนะ
  • คู่ต่อสู้เป็นตัวพิมพ์ใหญ่การกระทำเป็นตัวพิมพ์เล็ก
  • จะต้องไม่ให้ผลลัพธ์เดียวกันอย่างสม่ำเสมอ
  • ต้องมีอักขระช่องว่างอย่างน้อยหนึ่งตัวระหว่างตัวต่อสู้เอาต์พุตแอคชันและค่า
  • การกระทำที่ชนะเกิดขึ้นเมื่อฝ่ายตรงข้ามมีศูนย์หรือน้อยกว่าสุขภาพ
  • ทุกส่วนของการกระทำจะต้องอยู่ในบรรทัดเดียวกัน
  • ควรมีหนึ่งการกระทำต่อบรรทัด
  • ไบต์ที่น้อยที่สุดจะเป็นผู้ชนะ

มีมัน!


9
ชื่อ Alice (A) และ Bob (B) กำลังทำให้ฉันนึกถึงคลาสความปลอดภัยเครือข่าย นักแสดงอลิซ (A) ส่งแพ็กเก็ตไปยัง Bob (B) พร้อมคีย์ ... ฯลฯ ...
Magic Octopus Urn

21
@MagicOctopusUrn นั่นคือพวกเขา พวกเขามักจะพยายามสื่อสาร ความขัดแย้งที่น่าเศร้ามักเกิดขึ้นเมื่อการสื่อสารหยุดชะงัก
AJFaraday

7
ฉันคิดถึงวันที่เราพยายามหาวิธีซ่อนความลับของเราจากมัลลอรี่ ... มันง่ายกว่า ...
Bob

4
@Bob Mallory เป็นสิ่งที่ทำให้ไขว้เขวจริงๆ มันคือวันก่อนวันหยุดที่คุณต้องระวัง
AJFaraday

3
@ msh210 รายละเอียดที่สำคัญในการตีกอล์ฟคือทุกคนต้องใช้ความท้าทายเดียวกัน แต่นี่คือตรรกะ: - ถ้าคุณเล่นดันเจี้ยนและมังกรคุณจะพูดว่า“ ฉันจะเตะกอบลิน” แล้วคุณล่ะ d roll for ประสิทธิผลจากนั้นใช้ผลลัพธ์ของการหมุน ม้วนไม่มีความหมายถ้าไม่มีใครรู้ว่าคุณกำลังกลิ้งไป
AJFaraday

คำตอบ:


5

05AB1E , 49 ไบต์

"BaABr0Aha"S3ô»D„AB‡[6LΩ©Tǝ¤H®-©16ǝ=®0‹#s]н…ÿ w?

ลองออนไลน์!

คำอธิบาย

"BaABr0Aha"                                        # push the initial state of B
           S                                       # split to list of characters
            3ô                                     # divide into 3 parts
              »                                    # join each part on space and all on nl
               D„AB‡                              # make a copy with A and B inverted
                     [                             # start a loop
                      6LΩ©                         # pick a random number in [1 ... 6]
                          Tǝ                       # insert at position 10 of the string
                            ¤H                     # get the last char of the string and
                                                   # convert from hex
                              ®-©                  # subtract the random number
                                 16ǝ=              # insert at position 16 and print
                                     ®0‹#          # if the hp is less than 0, break
                                         s         # swap the other string to the stack top
                                          ]        # end loop
                                           н…ÿ w?  # print the winner

13

Python 3 , 131 ไบต์

x,y="AB"
from random import*
X=Y=10
p=print
while X>0:p(x,"a",y);d=randint(1,6);p(x,"r",d);Y-=d;p(y,"h",Y);x,y,X,Y=y,x,Y,X
p(y,"w")

ลองออนไลน์!

-8 ไบต์ต้องขอบคุณอย่างเป็นทางการว่า
-2 ไบต์ขอบคุณ ChooJeremy


5
การกำหนดล่วงหน้าp=printจะช่วยให้คุณประหยัดได้ประมาณ 8 ไบต์
อย่างเป็นทางการ

เนื่องจาก y ชนะ ณ จุดนี้เสมอ (และมีเพียงการโจมตี X ในลูปซึ่งต่อมาเปลี่ยนเป็น Y) คุณไม่จำเป็นต้องตรวจสอบว่า y หายไปหรือไม่ - ChooJeremy - จากรีวิว
NoOneIsHere

@NoOneIs ขอขอบคุณที่ส่งข้อความถึงฉัน: D
HyperNeutrino

randint(1,6)อาจถูกแทนที่ด้วยid(X+Y)//3%6+1แม้ว่าการกระจายไม่ได้ค่อนข้างสม่ำเสมอ
Vincent

@ วินเซนต์ฉันไม่เห็นจุดในการดัดกฏถ้ามันไม่ได้ช่วยทำให้มันสั้นลง ...
HyperNeutrino

7

C (gcc) , 146 141 bytes

f(A,B,r,t,a,b){for(A=B=10;r=1+clock()%6,A*B>0;t=!t)printf("%c a %c\n%c r %u\n%c h %i\n",a=65+t,b=66-t,a,r,b,t?A-=r:(B-=r));printf("%c w",a);}

ลองออนไลน์!

De กอล์ฟ:

f(A,B,r,t,a,b){
    for(A=B=10; //Initialize HP
        r=1+clock()%6, // Get the number of processor cycles the program has consumed. This is relatively random, so I call it good enough.
        A*B>0;t=!t) // Flip t for change of turns
        printf("%c a %c\n%c r %u\n%c h %i\n", // Print the turn
            a=65+t,b=65+!t,a,r,b, // 65 is ASCII for 'A', 66 for 'B'
            t?A-=r:(B-=r)); // Deduct the damage.
    printf("%c w",a); // Print the winner
}

2
คุณสามารถบันทึก byte โดยใช้a=65+t,b=66-t?
moopet

A*B>0จะช่วยให้คุณประหยัดไม่กี่ไบต์
Olivier Grégoire

A*Bจะประหยัดได้มากขึ้น แต่ฉันก็รีบเร่ง ฉันจะอัปเดตในตอนเย็น

พบข้อบกพร่องในลูกเต๋า dq seq {6,4,3,1,5} b ชนะด้วยสุขภาพ -4 ดู TIOฉันเปลี่ยนเครื่องคิดเลขลูกเต๋าของคุณเพื่อสาธิตข้อผิดพลาดนี้
GPS

@GPS ขอบคุณฉันจะแก้ไขทันที

7

Python 3 , 127 ไบต์

นี่คือการปรับปรุงคำตอบ @HyperNeutrinoที่ไม่เหมาะสมกับความคิดเห็น ดูคำอธิบายด้านล่าง

x,y="AB"
s=id(0)
X=Y=10
p=print
while X>0:p(x,"a",y);s=s**7%~-2**67;d=s%6+1;p(x,"r",d);Y-=d;p(y,"h",Y);x,y,X,Y=y,x,Y,X
p(y,"w")

ลองออนไลน์!


การแสวงหามหากาพย์สำหรับการทอยลูกเต๋าหลามสั้น

TL; DR: เป็นไปได้ที่จะโกน 4 ไบต์ออกจากลูกเต๋าหลามมาตรฐานโดยใช้การเข้ารหัส RSA

ฉันต้องการดูว่าการทอยลูกเต๋าหลามแบบมาตรฐาน ( 32 ไบต์ ) อาจสั้นลงเล็กน้อย:

from random import*;randint(1,6)

โดยเฉพาะอย่างยิ่งid(x)สะดวกในการนำค่าที่ไม่ได้กำหนดไว้ในโปรแกรมมาใช้ ความคิดของฉันคือการแฮชค่านี้เพื่อสร้างการสุ่มที่แท้จริง ฉันพยายามวิธีไม่กี่และหนึ่งของพวกเขาจ่ายเงินออก: การเข้ารหัส RSA

การเข้ารหัส RSA m**e%nเนื่องจากความเรียบง่ายต้องใช้เพียงไม่กี่ไบต์: ค่าสุ่มถัดไปนั้นสามารถสร้างขึ้นได้โดยการเข้ารหัสค่าก่อนหน้า สมมติว่ามี(e,n)คีย์ม้วนลูกเต๋าสามารถเขียนได้22 ไบต์ :

s=id(0);s=s**e%n;s%6+1

นั่นหมายความว่าเรามีประมาณ 10 ไบต์เพื่อกำหนดคีย์ RSA ที่ถูกต้อง ที่นี่ฉันโชคดี ในระหว่างการทดลองของฉันฉันเริ่มใช้Mersenne prime M67เท่านั้นที่จะรู้ในภายหลังว่าMersenne ทำผิดพลาดรวมถึงM67ในรายการของเขา มันจะออกมาเป็นผลิตภัณฑ์ของและp=193707721 q=761838257287ฉันพบโมดูลัสของฉัน:

n=~-2**67

ทีนี้ค่าเลขชี้กำลังและจำนวนเต็มทั้งหมดของ Charmichael (p-1)*(q-1)ต้องเป็น coprime โชคดีอีกครั้งหมายเลขเฉพาะแรกที่ไม่หารผลรวมของ n มีความยาวเพียงหนึ่งหลัก: 7. ลูกเต๋าสามารถเขียนได้โดยใช้28 ไบต์ (4 ไบต์น้อยกว่าวิธีมาตรฐาน):

s=id(0);s=s**7%~-2**67;s%6+1

ข้อดีอย่างหนึ่งของM67คือค่าสุ่มที่สร้างขึ้นมี 66 บิตซึ่งมากกว่า 64 บิต RNG ปกติ นอกจากนี้การใช้ RSA ทำให้สามารถย้อนเวลากลับไปได้ด้วยการถอดรหัสค่าปัจจุบันหลาย ๆ ครั้ง นี่คือคีย์การเข้ารหัสและถอดรหัส:

Encryption: (7,                    147573952589676412927)
Decryption: (42163986236469842263, 147573952589676412927)

ฉันไม่ใช่ผู้เชี่ยวชาญในด้านสถิติหรือการเข้ารหัสดังนั้นฉันจึงไม่สามารถบอกได้เลยว่า RNG นี้ตรวจสอบเกณฑ์สำหรับ "การสุ่มดี" หรือไม่ ฉันได้เขียนเกณฑ์มาตรฐานขนาดเล็กที่เปรียบเทียบค่าเบี่ยงเบนมาตรฐานของการเกิดของลูกเต๋า 1 ถึง 6 ก้อนโดยใช้ RNG ที่แตกต่างกัน ดูเหมือนว่าโซลูชันที่เสนอนั้นจะทำงานได้เหมือนโซลูชันอื่น ๆ


3
งานที่น่าประทับใจ! :)
HyperNeutrino


4

Java (JDK 10) , 180 ไบต์

v->{var r="";int p=0,H[]={10,10},h=0;for(;H[0]*H[1]>0;)r+=r.format("%3$c a %4$c%n%3$c r %d%n%4$c h %d%n",h+=Math.random()*6-h+1,H[p]-=h,p+65,(p^=1)+65);return r+(char)(66-p)+" w";}

ลองออนไลน์!

เครดิต


1
Java 10 มีvar? o.Ôฉันต้องตรวจสอบรายละเอียดใหม่บางอย่างในเร็ว ๆ นี้ .. อย่างไรก็ตามคุณสามารถเล่นกอล์ฟขนาด 4 ไบท์ได้โดยเปลี่ยนถ่านเป็นอาร์เรย์:v->{var r="";int P[]={65,66},p=0,H[]={10,10},h=0;for(;H[0]*H[1]>0;)r+=r.format("%3$c a %4$c%n%3$c r %d%n%4$c h %d%n",h+=Math.random()*6-h+1,H[p]-=h,P[p],P[p^=1]);return r+=P[p^1]+" w";}
Kevin Cruijssen

1
@KevinCruijssen Yep, Java 10 มี var ไม่จำเป็นต้องอ่านเพิ่มเติมมันเป็นการเปลี่ยนแปลงเพียงอย่างเดียวที่นักกอล์ฟของเราสามารถทำได้ และไม่ฉันไม่สามารถทำในสิ่งที่คุณแนะนำ: ตรวจสอบบรรทัดสุดท้ายของผลลัพธ์: มันจะกลายเป็นแทน65 w A wนั่นเป็นเหตุผลที่ฉันแยกมันออกมาจากint ...คำสั่ง: เพื่อกอล์ฟไม่กี่ไบต์ ;-)
Olivier Grégoire

1
@KevinCruijssen ฉันปฏิบัติตามตัวอย่างเล็ก ๆ น้อย ๆ ที่นี่: codegolf.stackexchange.com/a/159922/16236
Olivier Grégoire



3

Ruby , 122 120 96 92 91 ไบต์

f=->x=?A,y=?B,m=10,n=m{p [x,?a,y],[x,?r,r=1+rand(6)],[y,?h,t=n-r]
t<1?p([x,?w]):f[y,x,t,m]}

ที่บันทึกไว้ 1 ขอบคุณไบต์Asone Tuhid

ลองออนไลน์!


1
มันเหมือนว่าฉันไม่รู้จักรูบีอีกต่อไป;)
AJFaraday

ฉันกลัวว่าทางเลือกของคุณจะไม่ทำงาน "ทุกส่วนของการกระทำต้องอยู่ในบรรทัดเดียวกัน" แม้ว่าเป็นไปได้หรือไม่ที่จะทำให้การเพิ่มประสิทธิภาพเดียวกันกับอักขระแท็บ
AJFaraday

@AJFaraday เป็นที่ยอมรับสำหรับบรรทัดเอาต์พุตในรูปแบบ["A", "a", "B"]หรือไม่? ถ้าเป็นเช่นนั้นฉันมีนี้การแก้ปัญหา 96 ไบต์
Cristian Lupascu

หากพวกเขากำลังส่งออกหนึ่งต่อบรรทัด ที่ควรทำ
AJFaraday

-1 ไบต์ถ้าคุณแทนที่?(p [x,?w]):ด้วย?p([x,?w]):
Asone Tuhid

3

Java 8, 230 ไบต์

v->{for(int h=104,a=h,x=0,y=1,A=10,B=A,r=0,t=0,T;a<119;)System.out.printf("%c %3$c %c%n",(x=a>h|A*B<1?x^1:x)+65,y=(a<98?t=r+=Math.random()*6-r+1:a>h?(T=x<1?A-=t:(B-=t))<0?0:T:A*B<1?-16:(x^1)+17)+48,a=a<98?114:a>h?104:A*B<1?119:97);}

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

คำอธิบาย:

ลองออนไลน์

v->{                     // Method with empty unused parameter and no return-type
  for(int h=104,         //  Temp integer with unicode for 'h' to save bytes
          a=h,           //  Second part (Action)
          x=0,           //  First part
          y=1,           //  Third part
          A=10,          //  Score player A, starting at 10
          B=A,           //  Score player B, starting at 10
          r=0,           //  Random dice-roll
          t=0,           //  Previous dice-roll
          T;             //  Temp integer
      a<119;)            //  Loop until there is a winner
     System.out.printf(  //   Print
      "%c %3$c %c,%n",   //    The three parts with spaces, and a new-line
      (x=                //    First part:
         a>h             //     If the previous action is 'r',
         |A*B<1?         //     or there is a winner:
           x^1           //      Change A→B or B→A
         :               //     Else:
          x)             //      A/B remains unchanged
       +65,              //     Add 65 to convert 0/1 to 65/66 (unicode values of A/B)
      (y=                //    Third part:
         (a<98?          //     If the previous action was 'a'
           t=r+=Math.random()*6-r+1
                         //      Roll the dice, and save it in `t`
          :a>h?          //     Else-if the previous action was 'r':
           (T=x<1?       //      If the first part changed to player A:
            A-=t         //       Subtract the previous dice-roll from A
           :             //      Else:
            (B-=t))      //       Subtract the previous dice-roll from B
           <0?           //      If this score is below 0:
            0            //       Use 0
           :             //      Else:
            T            //       Use this score
         :               //     Else (the previous action was 'h'):
          A*B<1?         //      Is there a winner:
           -16           //       Change the third part to a space
          :              //      Else:
           (x^1)+17)     //       Change the third part to the other player
       +48,              //     Add 48 to convert it to unicode
       a=                //    Second part:
         a<98?           //     If the previous action was 'a': 
          114            //      Change it to 'r'
         :a>h?           //     Else-if the previous action was 'r':
          h              //      Change it to 'h'
         :               //     Else (the previous action was 'h'):
          A*B<1?         //      If either score is 0:
           119           //       Use 'w'
          :              //      Else:
           97);}         //       Use 'a'


2

แบตช์ 174 ไบต์

@set/aA=B=10
@set c=A
@set d=B
:g
@set/ar=%random%%%6+1,h=%d%-=r
@echo %c% a %d%
@echo %c% r %r%
@echo %d% h %h%
@if %h% gtr 0 set c=%d%&set d=%c%&goto g
@echo %c% w

คำอธิบาย: %การอ้างอิงตัวแปรถูกแทนที่ในเวลาวิเคราะห์คำ สิ่งนี้มีประโยชน์ที่มีประโยชน์สองประการ:

  • %d%-=r ลบ rจากตัวแปรที่ตั้งชื่อโดยd(เช่นการอ้างอิงทางอ้อม)
  • set c=%d%&set d=%c% เป็นเพียงการสลับตรง

2

PHP 7.1: 159 ไบต์

<?php $A=$B=10;$t='AB';while($A>0&&$B>0){$a=$t[0];$b=$t[1];$d=rand(1,6);$$b-=$d;echo"$a a $b\n$a r $d\n$b h {$$b}\n";$t=strrev($t);}echo($A>0?'A':'B')." w\n";

เรียกใช้ในเบราว์เซอร์ที่นี่!

PHP 5.6: 156 ไบต์

<?php $A=$B=10;$t='AB';while($A>0&&$B>0){list($a,$b)=$t;$d=rand(1,6);$$b-=$d;echo"$a a $b\n$a r $d\n$b h {$$b}\n";$t=strrev($t);}echo($A>0?'A':'B')." w\n";

เรียกใช้ในเบราว์เซอร์ที่นี่!

นี่คือลักษณะของโซลูชัน PHP 5.6 ที่มีลักษณะเหมือนกับการจัดรูปแบบและความคิดเห็น:

<?php
// Initialize both HP counters
$A = $B = 10;

// Set the turn order as a string (which 5.6 allows to be unpacked into a list)
$t = 'AB';

// Run this loop as long as both players have HP
while ($A > 0 && $B > 0) {
    // Unpack the turn string into $a and $b variables; on the first run, $a = 'A'
    // and $b = 'B'. This is no longer possible in PHP 7.0, so the PHP 7.0
    // solution needed to use an array instead.
    list($a, $b) = $t;

    // Set damage to a random number between 1 and 6
    $d = rand(1, 6);

    // Subtract the damage from the referenced value $b. On the first turn, this
    // is 'B', so this ends up subtracting $d from $B. Next turn, $b will be 'A',
    // so it'll subtract $d from $A
    $$b -= $d;

    // Echo the string (interpolated values)
    echo "$a a $b\n$a r $d\n$b h {$$b}\n";

    // Reverse the turn order string ('AB' becomes 'BA', which will affect the
    // call to list in the first line of the while-loop)
    $t = strrev($t);
}

// Someone's run out of HP; figure out whom by figuring out who still has HP
echo ($A > 0 ? 'A' : 'B') . " w\n";

1

Bash, 178 ไบต์

A=10 B=10 e=echo
a(){ $e $1 a $2;d=$((RANDOM%6+1));$e $1 r $d;eval $2=$((${!2}-$d));$e $2 h ${!2};[ ${!2} -gt 0 ];}
while a A B && a B A;do cd;done;[ $A -gt 0 ]&&$e A w||$e B w

1

F #, 238 235 ไบต์

ฉันคิดว่าฉันทำได้ดี แต่คุณก็ทำได้ดีกว่าฉัน!

let p=printfn
let mutable A=10
let mutable B=A
let x h a d=
 p"%s a %s"a d
 let i=(new System.Random()).Next(1,7)
 let j=h-i
 p"%s r %i"a i
 p"%s h %i"d j
 if j<=0 then p"%s w"a
 j
while A*B>0 do
 B<-x B"A""B"
 if B>0 then A<-x A"B""A"

ลองออนไลน์!

ขอบคุณ Rogem สำหรับคำแนะนำที่ยอดเยี่ยมในการใช้ A * B> 0 แทนที่จะเป็น A> 0 && B> 0 (ถอดออก 3 ไบต์)

ขอบคุณอย่างเป็นทางการที่คำแนะนำเกี่ยวกับการกำหนด printf ในคำตอบของ Python ช่วยให้ฉันสามารถปัดทิ้งได้ไม่กี่ไบต์เช่นกัน


1
คำแนะนำที่ฉันได้รับจาก @OlivierGregoire: A*B>0จะช่วยให้คุณประหยัดได้มากขึ้น

นั่นยอดเยี่ยมจริงๆ รักมัน ขอบคุณมาก!
Ciaran_McCarthy

1

Haskell , 204 ไบต์

ความพยายามของฉันกับ Haskell ฉันไม่สามารถแข่งขันได้มากขึ้น

import System.Random
main=getStdGen>>= \g->putStr$q(randomRs(1,6)g)10(10::Int)"A ""B "
(!)=(++)
l="\n"
q(x:z)a b p o=p!"a "!o!l!p!"r "!show x!l!o!"h "!show n!l!if n<1then p!"w"else q z n a o p where n=b-x

ลองออนไลน์!

คำอธิบาย:

import System.Random  --import random module
main=                        --main function, program entry point
 getStdGen                   -- get the global random number generator
   >>= \g->                  --using the random generator g
       putStr $ q            --print the result of function q, passing in ..
          (randomRs (1,6) g) --an infinite list of random numbers, 1 to 6 generated by g
           10 (10::Int)      --the starting health of both players, 
                             --type annotation sadly seems to be required
           "A " "B "         --The names of the players,
                             --with an extra space for formatting
(!)=(++) --define the operator ! for list (String) concatenation, 
         -- we do this a lot so we save a bit by having a one byte operator
l="\n"   -- define l as the newline character

q      --define function q                         
 (x:z) --our list of random numbers, split into the next number (x) and the rest (z)
 a     -- the health of the active player
 b     -- the health of the player getting attacked
 p     -- the name of the active player
 o     -- the name of the player getting attacked
=
  p!"a "!o!l --create the attack action string with a newline
 !p!"r "!show x!l -- append the roll action
 !o!"h "!show n!l -- append the health remaining
 !           -- append the result of the following if
  if n<1     -- if the player being attacked has been defeated
  then p!"w" -- append the win string for the active player
  else q z n a o p  --otherwise append the result of calling q again with 
                    --rest of the random numbers, and the active players swapped
  where n=b-x -- define the attacked player's new health n
              -- their current health b - the random roll x

คุณสามารถดูได้ที่เราเคล็ดลับสำหรับการเล่นกอล์ฟใน Haskell เช่นสามารถใส่ลงในยาม:where m=b-x |m<-b-x=
Laikoni

main=putStr=<<q"A "10"B "10.randomRs(1,6::Int)<$>getStdGenคุณสามารถสูญเสียแลมบ์ดาและเป็นหนึ่งในชุดของวงเล็บโดยการจัดเรียงพารามิเตอร์บางอย่าง: นอกจากนี้คุณยังสามารถใช้รายการและ concat มันจะได้รับการกำจัดของ (++)Redefining ครั้งสุดท้ายที่ดูเหมือนจะไม่เป็นประโยชน์เพียงแค่ใช้b-xทุกที่
Angs

1

Julia 0.6 , 175 bytes

p=println()
f(l="AB",h=[10,10],a=1)=(while min(h...)>0;d=3-a;p(l[a]," a ",l[d]);r=rand(1:6);h[d]-=r;p(l[a]," r ",r);p(l[d]," h ",max(h[d],0));a=d;end;p(l[findmax(h)[2]]," w"))

ลองออนไลน์!

รุ่นที่ยาวและไม่อัปโหลด:

function status(player, health)
    println("$player h $(max(0,health))")
end

function roll(player)
    x = rand(1:6)
    println("$player r $x")
    x
end

function play()
    players = ["A","B"]
    healths = [10, 10]
    attacker = 1

    while min(healths...) > 0
        println("$(players[attacker]) a $(players[3-attacker])")
        healths[3-attacker]-=roll(players[attacker])
        status(players[3-attacker], healths[3-attacker])

        attacker = 3 - attacker
    end

    winner = findmax(healths)[2]
    println("$(players[winner]) w")
end

ดูเหมือนจะไม่มีผลลัพธ์ในลิงค์ TIO ของคุณ
AJFaraday

ใช่ฉันไม่รู้ว่าทำไมทิโอไม่ชอบ มันทำงานได้ดีบนเครื่องของฉัน ฉันจะดูมันถ้าฉันมีเวลา
niczky12

1

VBA, 222 185 179 Bytes

โซลูชันแบบเรียกซ้ำนี้เกี่ยวข้องกับ 3 ส่วนย่อย

  1. g คือเกมที่เริ่มจากการเลี้ยวครั้งแรก
  2. t ถูกเรียกในแต่ละเทิร์น มันใช้การสอบถามซ้ำ
  3. หน้าสั้นกว่า Debug.Print เมื่อใช้มากกว่า 3 ครั้ง (เพียง 4 ในการแก้ปัญหานี้) แก้ไข:ตอนนี้ที่ผมได้เรียนรู้ว่าDebug.?เป็นทางเลือกที่ได้รับการยอมรับDebug.Print, Debug.?xจะสั้นกว่าการเรียกย่อยเพื่อการพิมพ์
Sub g()
t "A",10,"B",10
End Sub
Sub t(i,j,x,h)
d=Int(Rnd()*6)+1
Debug.?i &" a "&x
Debug.?i &" r "&d
h=h-d
If h<1Then
Debug.?i &" w"
Else
Debug.?x &" h "&h
t x,h,i,j
End If
End Sub

นี่เป็นความท้าทายที่สนุก ถ้าคุณรู้ว่าล่ามออนไลน์เช่น TIO สำหรับ VB6 / VBScript / VBA โปรดแสดงความคิดเห็น จากนั้นฉันสามารถโพสต์ลิงก์ไปยังโซลูชันที่ใช้งานได้

หากคุณต้องการทดสอบรหัสนี้และติดตั้ง Microsoft Excel, Word, Access หรือ Outlook (Windows เท่านั้น) ให้กด Alt + F11 เพื่อเปิด VBA IDE ใส่โมดูลรหัสใหม่ (Alt + I, M) และลบตัวเลือกที่ชัดเจน จากนั้นวางรหัสและกด F5 เพื่อเรียกใช้ ผลลัพธ์ควรปรากฏในหน้าต่างทันที (กด Ctrl + G หากคุณไม่เห็น)

แก้ไข 1: . ช่องว่างลบที่แก้ไข VBA จะเพิ่มกลับโดยอัตโนมัติลดลง 37 ไบต์
แก้ไข 2: ลบ Sub P () * เพื่อประหยัด 6 ไบต์หลังจากการเรียนรู้ที่เป็นทางเลือกที่ได้รับการยอมรับDebug.? Debug.Printการเรียกใช้Subเพื่อจัดการDebug.?บันทึกเป็นไบต์หลังจากการโทรเกินหกครั้ง

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.