编写太低(太高)游戏的最短代码


20

您必须使用最短的代码(以字节为单位)制作游戏太低---太高(TLTH)

游戏规则:

  1. 计算机将选择一个超出整数范围(-32768..32767)的随机数。
  2. 现在,您将想到一个数字并将其输入。
  3. 电脑会告诉您您的电话号码是低于(TOO LOW)还是高于(TOO HIGH)所选号码。
  4. 当您猜到数字时,计算机应显示Congrats! You found the number!

代码规则:

  1. 不要使用字符TOLWHIG在字符串或字符文字(既不小写也不是大写)。
    例如:

    tolower(T);  // Acceptable  
    cout<<"T";   // Unacceptable
    char ch='T'; // Unacceptable
    
  2. 如果您的代码可以在开始游戏之前显示游戏规则,则删除300个字节

  3. 如果您的代码可以计算转数并显示游戏结束时的转数,则删除50个字节,如下所示:

    "Congrats! You found the number in n turns!"
    

    其中n是转弯次数,而不是

    "Congrats! You found the number!"  
    
  4. 如果您的代码可以告诉用户输入的数字超出整数范围,请从分数中删除25个字节
    例如:

    Enter the number: 40000  
    Sorry! This number is out of range
    Please Enter the number again:  
    
  5. 删除25个字节,如果您的代码从任何内置随机函数中获取随机数。

  6. 删除10个字节如果您的代码以彩色显示“祝贺”(选择除默认白色以外的任何颜色)。

提交规则:

  1. 添加带有您的语言名称的标题,以及所有赏金计算及其解释的分数。

  2. 发布您的高尔夫和非高尔夫代码。

优胜者

  1. 字节最少的答案将获胜。
  2. 如果平局,则以更多选票赢得答案。
  3. 5天后将选出优胜者。

编辑:请显示您的代码输出。
编辑2:您可以忽略代码规则中的规则2、3和4的规则1

祝您好运:]


2
这是对游戏规则的有效解释吗?WhileURong(USayNumbr;ISayBigrOrSmalr)
John Dvorak 2014年

1
根据您对得分的描述,您既不会参加代码高尔夫比赛,也不会参加人气竞赛。这是一个代码挑战(使用流行决胜局)。查看标签维基。我建议进行编辑以适当地标记挑战。ps-Jinx!@mniip
Jonathan Van Matre 2014年

1
@JonathanVanMatre对我来说,尽管是抢七局,但措辞听起来像是代码高尔夫
mniip 2014年

21
另外,我并不特别喜欢反对协助其他玩家改善答案的规则。用《新娘公主》中巨人安德烈(Andre)不朽的话来说,“那不是很像运动员”。
Jonathan Van Matre 2014年

1
这个问题是这样。。。David H. Ahl
Michael Stern

Answers:


2

JavaScript(-210点(190字节- 300 (用于规则) - 50 (用于猜测的数目) - 25 (用于不使用任何内置随机数源) -- 25 (用于告诉您输入是否超出有符号的范围)16位整数)):

打高尔夫球:

alert('Guess number',n=(m=2<<15)/2-new Date%m);for(z=0;a=+prompt(++z);)alert(a>m|a<1-m?m+'-'+-~-m:a==n?'Great! You found the number in '+z+' turns':atob('VE9P\x47E'+(a>n?'hJR0g=':'xPVw==')))

完整代码(格式正确):

var max = 2 << 15;
var random = max/2 - new Date%max;
var counter = 0;

while (1) {
    var guess = +prompt();

    ++counter;

    if (guess > max | guess < -~-max) {
        alert(-~-max + '-' + max); // Shows that the range is between -32767 and 32768
    } else if (guess > random) {
        alert('TOO HIGH');
    } else if (guess < random) {
        alert('TOO LOW');
    } else if (guess == random) {
        alert('Congrats! You found the number in ' + counter + ' turns');
        break;
    }
}

输出:

ALERT:  Guess number
PROMPT: 5
ALERT:  TOO LOW
PROMPT: 20000
ALERT:  TOO LOW
PROMPT: 30000
ALERT:  TOO HIGH
PROMPT: 25000
ALERT:  TOO HIGH
PROMPT: 22500
ALERT:  TOO HIGH
PROMPT: 21000
ALERT:  TOO HIGH
PROMPT: 20500
ALERT:  TOO HIGH
PROMPT: 20200
ALERT:  TOO LOW
PROMPT: 20400
ALERT:  TOO LOW
PROMPT: 20450
ALERT:  TOO LOW
PROMPT: 20475
ALERT:  TOO HIGH
PROMPT: 20460
ALERT:  TOO LOW
PROMPT: 20468
ALERT:  TOO HIGH
PROMPT: 20464
ALERT:  TOO LOW
PROMPT: 20466
ALERT:  TOO LOW
PROMPT: 34000
ALERT:  -32767-32768
PROMPT: 20467
ALERT:  Great! You found the number in 17 turns!
PROMPT: (nothing)

@IlmariKaronen抱歉,我犯了一个错误。我已经更新了答案。
牙刷

谢谢,现在可以运行了...但是如果我猜想的话,它似乎坏了0。:-(另外,不应该有一个空间TOO LOW/ TOO HIGH
ILMARI Karonen

19

Perl的5.10+:159个 144字节- 350 = -206点

say"Guess 16 bit signed number";$==32767-rand 65536;say(TOO.$",$_<0?LOW:HIGH)while++$i,$_=<>-$=;say"Congrats! You found the number in $i turns!"

编辑2:通过最近的规则更改,该更改使我可以将任何字符串文字用于“恭喜”消息,因此,我可以从原始的159字节解决方案中节省15个字节。与旧代码相比,上面的新代码没有什么特别新颖或有趣的(我只是摆脱了p函数,say而是直接调用了),因此本文的其余部分将描述原始代码,如下所示:

sub p{say join$",@_}p Guess,16,bit,signed,number;$==32767-rand 65536;p(TOO,$_<0?LOW:HIGH)while++$i,$_=<>-$=;p Congrats."!",You,found,the,number,in,$i,turns."!"

是的,我正在滥用规则1。当您可以使用字时,谁需要字符串?;-)

运行perl -M5.010以启用Perl 5.10+ say功能(或以5字节的额外费用替换p功能主体print join$",@_,$/)。

奖励分数:

  • −300分:“开始游戏前显示游戏规则”
  • -50分:“显示游戏结束时的转数”

从严格意义上讲,该代码不包含任何字符串文字,因此,从技术上讲,我不会违反规则1。诀窍在于,在Perl中,如果没有use strict,则与已知语言关键字或子例程不对应的任何标识符都将简单地求值为其自己的名称。p然后,该函数仅获取单词列表并将其打印出来,并用空格分隔。

播放示例:

Guess 16 bit signed number
0
TOO HIGH
-10000
TOO LOW
-5000
TOO HIGH
-7500 
TOO LOW
-6250
TOO HIGH
-6875
TOO LOW
-6553
TOO HIGH
-6700
TOO HIGH
-6790
TOO LOW
-6745
TOO HIGH
-6767
TOO LOW
-6756
TOO HIGH
-6761
Congrats! You found the number in 13 turns!

编辑:哦,对了,规则说我也需要发布一个非高尔夫版本的代码,所以就到这里了。从技术上讲,它是“去高尔夫化”的,因为我通常从一开始就以或多或少完全高尔夫化的形式编写代码高尔夫程序,有时在不从根本上改变某些“高尔夫化”优化的情况下,很难去除所有“高尔夫化”优化程序工作。尽管如此,我至少尝试添加空格,注释和更有意义的函数/变量名称:

sub output {
    # print all arguments separated by spaces, plus a newline:
    # (in the golfed code, I use the special variable $" instead of " " for a space)
    say join " ", @_;
}

# print the rules:
output Guess, 16, bit, signed, number;

# choose a random number between -32768 and 32767 inclusive:
# (in the golfed version, using the special variable $= allows
# the int() to be left out, since $= can only take integer values)
$number = int( 32767 - rand 65536 );

# loop until the input equals the chosen number, printing "TOO LOW / HIGH":
# (the loop ends when $diff == 0, since 0 is false in Perl)
output (TOO, $diff < 0 ? LOW : HIGH) while ++$count, $diff = (<> - $number);

# print congratulations, including the loop count:
output Congrats."!", You, found, the, number, in, $count, turns."!";

附言 作为替代方案,如果仅使用裸字而不是字符串对您来说太作弊,那么这是一种182字节的解决方案,TOLWHIG 即使在裸字中也不使用字母(而是在音译运算符中使用字母)。它仍然获得相同的奖金,总分为182 − 350 = −168分

sub t{pop=~y/kpqvxyz/tolwhig/r}say"Guess 16 bit signed number";$==32767-rand 65536;say uc t"kpp ".($_<0?qpv:xyzx)while++$n,$_=<>-$=;say t"Cpnzraks! Ypu fpund kxe number yn $n kurns!"

输出看起来与上面完全相同。根据(原始)规则,我会在允许的情况下使用字母ti打印规则。但是,即使消除这些使用也只会花费两个额外的字节。相反,将所有输出大写(根据上面的评论,这似乎是允许的)将使我节省三个字节。


这是一个很好的和聪明的答案!从我+1
Mukul Kumar 2014年

现在我看到了,我想知道如果您为付出代价,是否可以用Ruby 1.8击败它def method_missing *args;args.join' ';end
卡亚2014年

我用不错的旧JavaScript击败了您!
牙刷

14

Python 2:-80分(270-300-50)

print"WhileURong(USayNumbr;ISayBigrOrSmalr)"
import random
r=random.randint(-32768,32767)
g=i=1
while g:g=cmp(input(),r);print("C\x6fn\x67ra\x74s! Y\x6fu f\x6fund \x74\x68e number \x69n %d \x74urns!"%i,"\x54\x4f\x4f \x48\x49\x47\x48","\x54\x4f\x4f \x4c\x4f\x57")[g];i+=1

分数是270个字符,负号表示指示,负号300,负号表示“恭喜!”中的猜测数。字符串,总计负80分。

循环的非公开版本,带有未转义的字符串:

while g:
    g=cmp(input(),r)
    print ("Congrats! You found the number in %d turns!"%i,
           "TOO HIGH",
           "TOO LOW")[g]
    i+=1

cmp如果值相等,内置函数将返回0;如果第一个较小,则返回-1;如果第一个较大,则返回1。我使用该值索引一个字符串元组,然后再次将其作为循环的退出条件。用负索引(如-1)对序列进行索引从该序列的末尾开始,而不是从开始开始。

4根据XKCD 221,我非常想跳过导入,而只是将其用作我的随机数(是否有资格获得-25个字符的奖励?)。

运行示例(有一个错误,我不能做负数学运算):

WhileURong(USayNumbr;ISayBigrOrSmalr)
0
TOO HIGH
-16000
TOO LOW
-8000
TOO HIGH
-12000
TOO HIGH
-14000
TOO LOW
-13000
TOO HIGH
-13500
TOO HIGH
-13750
TOO LOW
-13625
TOO HIGH
-13712
TOO LOW
-13660
TOO HIGH
-13640
TOO HIGH
-13685
TOO HIGH
-13700
TOO LOW
-13695
TOO LOW
-13690
TOO LOW
-13687
Congrats! You found the number in 17 turns!

请显示代码的示例输出。
Mukul Kumar 2014年

2
这不是违反代码规则1吗?WhileURong(USayNumbr; ISayBigrOrSmalr)包含一个或多个Os,Ls,Ws,Hs,Is和Gs。
Fors 2014年

@Cruncher:我不确定您的意思,因为程序确实终止了。获得正确的值后,该值g为零,while循环结束。
Blckknght

@Blckknght糟糕,快速阅读
Cruncher

@ForsYou can ignore Rule-1 for Rules-2,3 & 4 in Code Rules
Cruncher

13

JavaScript 293,-300(规则)-50(转弯显示)-25(范围警告)-25(无随机功能)= -107(新分数)

r=new Date%65536+~(z=32767);n=x="";for((A=alert)("\107uess my number, \111 \147\151ve feedback");(x=prompt())!=r;A(0>(x-~z^x-z)?"\164\157\157 "+(x>r?"\150\151\147\150":"\154\157\167"):"Ran\147e: "+~z+" - "+z))n++;A("C\157n\147ra\164s! Y\157u f\157und \164\150e number \151n "+ ++n+"\164urns!")

我声称按照RGB惯例,黑色是一种颜色,但这有点作弊...

哦,我想补充一点,也没有违反规则#1!

在一系列警报和提示中输出

ALERT: Guess my number, I give feedback
PROMPT: 0
ALERT: too low
PROMPT: 16000
ALERT: too low
PROMPT: 24000
ALERT: too low
PROMPT: 28000
ALERT: too high
PROMPT: 26000
ALERT: too high
PROMPT: 25000
ALERT: too low
PROMPT: 25500
ALERT: too high
PROMPT: 25250
ALERT: too low
PROMPT: 25375
ALERT: too high
PROMPT: 25310
ALERT: too low
PROMPT: 25342
ALERT: too low
PROMPT: 25358
ALERT: too low
PROMPT: 25366
ALERT: too high
PROMPT: 25362
ALERT: too high
PROMPT: 25360
ALERT: too high
PROMPT: 25359
ALERT: Congrats! You found the number in 16 turns!

取消程式码:

r = (new Date)%65536+~(z=32767); //Generates random number in range based off date's milliseconds... I'd use epoch, but I need to conserve
n=x=""; // Sets count and input to blank, these change as time goes on
// The line below sets up a call for `alert`, provides the initial rules, and subsequent loops compares the prompt with the number, as well as sets up "too", "high",  "low" and "out of range" message strings, and provides the feedback
for((A=alert)("\107uess my number, \111 \147\151ve feedback");(x=prompt())!=r;A(0>(x-~z^x-z)?"\164\157\157 "+(x>r?"\150\151\147\150":"\154\157\167"):"Ran\147e: "+~z+" - "+z))
{
    n++; // adds one to count providing the number isn't guessed yet
}
alert("C\157n\147ra\164s! Y\157u f\157und \164\150e number \151n "+ ++n+" \164urns!") // Congratulates the player, and displays the correct number of turns taken

您仔细阅读了规则?好,您必须发布带有一个输出的非高尔夫代码
Mukul Kumar

@MukulKumar排序了……
WallyWest'3

你可以用前缀你的答案保存4 A=alert;和更换所有的alertA。如果您A(0>(x...在分号后输入警报,请再输入一次prompt())!=r;)n++1
DocMax 2014年

你可以改变feedback if too high/low目前采用84个字符i give feedback(这将只需要\151 \147\151ve feedback或24个字符,以节省60个字符
corsiKa

1
我可能是错的,但我认为您可以将n++for循环从for循环移到,x=prompt(n++)以省去+ ++n+在最终警报中要做的事情。它将节省
3。– DocMax

7

Javascript,324个字节,得分-76

[由于规则更改而更新]

  • 324字节
  • -300用于显示规则
  • -50用于显示用户获胜时的转弯
  • -25告诉用户何时输入超出范围的数字
  • -25(不使用内置随机数)(更新)

总分:-76

打高尔夫球:

function q(y){return y.replace(/./g,function(x){h=x.charCodeAt(0);return String.fromCharCode(h>32&h<58?h+32:h)})}d=o=32768;alert("WhileURong(USayNumbr;ISayBigrOrSmalr)");for(a=1,n=new Date%(2*o)-o;d-n;a++)d=+prompt(a),alert(d>=o|d<-o?"BAD":d<n?q("4// ,/7"):d>n?q("4// ()'("):"Congrats! You found the number in "+a+" turns!")

让高尔夫变得混乱。

首先,正确识别(但是对于混淆代码来说,这仍然不是很好):

function q(y) {
  return y.replace(/./g, function(x) {
    h = x.charCodeAt(0);
    return String.fromCharCode(h > 32 & h < 58 ? h + 32 : h)
  })
}

d = o = 32768;
alert("WhileURong(USayNumbr;ISayBigrOrSmalr)");

for (a = 1, n = new Date % (2 * o) - o; d - n; a++)
  d = +prompt(), alert(d >= o | d < -o ? "BAD" : d < n ? q("4// ,/7") : d > n ? q("4// ()'(") : "Congrats! You found the number in " + a + " turns!")

其次,重命名标识符:

function unencrypt(encrypted) {
  return encrypted.replace(/./g, function(character) {
    charCode = character.charCodeAt(0);
    return String.fromCharCode(charCode > 32 & charCode < 58 ? charCode + 32 : charCode)
  })
}

guess = limit = 32768;
alert("WhileURong(USayNumbr;ISayBigrOrSmalr)");

for (tries = 1, randomNumber = new Date % (2 * o) - o; guess - randomNumber; tries++)
  guess = +prompt(),
  alert(guess >= limit | guess < -limit ? "BAD" : guess < randomNumber ? unencrypt("4// ,/7") : guess > randomNumber ? q("4// ()'(") : "Congrats! You found the number in " + tries + " turns!")

因此,unencrypt函数获取ASCII 33(!)和ASCII 58(:)之间的所有字符,并相加32,将它们转换为范围内的字符A-Z

通过解密所有字符串,使代码更加轻松:

guess = limit = 32768;
alert("WhileURong(USayNumbr;ISayBigrOrSmalr)");

for (tries = 1, randomNumber = new Date % (2 * o) - o; guess - randomNumber; tries++)
  guess = +prompt(),
  alert(guess >= limit | guess < -limit ? "BAD" : guess < randomNumber ? "TOO LOW" : guess > randomNumber ? "TOO HIGH" : "Congrats! You found the number in " + tries + " turns!")

最后,让我们将一些指令移至其他位置,将长三元链替换为ifs和else,加入连接字符串并简化数学运算,以提高可读性:

guess = 32768;
alert("WhileURong(USayNumbr;ISayBigrOrSmalr)");

randomNumber = new Date % 65536 - 32768;

for (tries = 1; guess - randomNumber; tries++) {
  guess = +prompt(); // The preceding + is a golfing trick to convert string to number.
  if (guess > 32767 | guess < -32768) {
    alert("BAD");
  } else if (guess < randomNumber) {
    alert("TOO LOW");
  } else if (guess > randomNumber) {
    alert("TOO HIGH");
  } else {
    alert("Congrats! You found the number in " + tries + " turns!");
  }
}

样品:

ALERT:  WhileURong(USayNumbr;ISayBigrOrSmalr)
PROMPT: 5
ALERT:  TOO LOW
PROMPT: 20000
ALERT:  TOO LOW
PROMPT: 30000
ALERT:  TOO HIGH
PROMPT: 25000
ALERT:  TOO HIGH
PROMPT: 22500
ALERT:  TOO HIGH
PROMPT: 21000
ALERT:  TOO HIGH
PROMPT: 20500
ALERT:  TOO HIGH
PROMPT: 20200
ALERT:  TOO LOW
PROMPT: 20400
ALERT:  TOO LOW
PROMPT: 20450
ALERT:  TOO LOW
PROMPT: 20475
ALERT:  TOO HIGH
PROMPT: 20460
ALERT:  TOO LOW
PROMPT: 20468
ALERT:  TOO HIGH
PROMPT: 20464
ALERT:  TOO LOW
PROMPT: 20466
ALERT:  TOO LOW
PROMPT: 20467
ALERT:  Congrats! You found the number in 16 turns!"

1
我真的很喜欢:)
WolleVanillebärLutz 2014年

请显示代码的示例输出。
Mukul Kumar 2014年

@MukulKumar。完成,增加输出
Victor Stafusa 2014年

7

C-272个字符-300-50-25 = -103

  • -300用于显示规则;
  • -50告诉玩家转数;
  • -25(表示不使用标准RNG库)。

高尔夫代码:

main(i){short a,c=i=0,b=time(0);for(a=b;b--;a=(a*233)+4594);b=a;puts("WhileURong(USayNumbr;ISayBigrOrSmalr)");while(1){i++;scanf("%hi",&a);if(a==b){printf("Congrats! You found the number in %i turns!",i);break;}for(c=0;c^9;c++)putchar(c[a<b?"kff7cfn7!":"kff7_`^_!"]-23);}}

取消高尔夫:

int main(int i) {
    short a,
          b = time(0),
          c = i = 0;

    for( a = b ; b-- ; a = (a * 233) + 4594);
    b = a;

    puts("WhileURong(USayNumbr;ISayBigrOrSmalr)");

    while(1) {
        i++;
        scanf("%hi", &a);
        if(a == b) {
            printf("Congrats! You found the number in %i turns!", i);
            break;
        }
        for( c = 0 ; c ^ 9 ; c++ )
            putchar(c[ a < b ? "kff7cfn7!" : "kff7_`^_!" ] - 23);
    }
}

输出:

输出量


输出在哪里?
Mukul Kumar 2014年

@ IlmariKaronen,EDIT2。另外,我不只是一个时代。
Oberon 2014年

@Oberon也请发布输出-Mukul
Kumar

@MukulKumar,发布。
Oberon 2014年

5

C:237-300-50-25-25-10:-173点

-300(表示规则),-50(表示猜测数字),-25(表示不使用任何内置随机数生成器),-25(表示超出范围的消息)和-10(表示祝贺的消息)。

int s[3]={542068564},g,n;main(r){for(r=(short)&r,puts("WhileURong(USayNumbr;ISayBigrOrSmalr)");n++,scanf("%d",&g),s[1]=g<r?5721932:1212631368,g^r;puts(g^(short)g?"OOR":s));printf("\033[31mCongrats! You found the number in %d turns!",n);}

取消高尔夫:

int s[3]={542068564},g,n;

main(r){
        for(
                r=(short)&r,
                puts("WhileURong(USayNumbr;ISayBigrOrSmalr)");

                n++,
                scanf("%d",&g),
                s[1]=g<r?5721932:1212631368,
                g^r;

                puts(g^(short)g?"OOR":s)
        );

        printf("\033[31mCongrats! You found the number in %d turns!",n);
}

示例运行:

WhileURong(USayNumbr;ISayBigrOrSmalr)
-32769
OOR
32768
OOR
0
TOO HIGH
-16384
TOO HIGH
-24576
TOO HIGH
-28672
TOO LOW
-26624
TOO LOW
-25600
TOO HIGH
-26112
TOO LOW
-25856
TOO LOW
-25728
TOO LOW
-25664
TOO HIGH
-25696
TOO HIGH
-25712
TOO LOW
-25704
Congrats! You found the number in 15 turns!

最后一行显示为红色。


太棒了!您击中了所有赏金!!!
Mukul Kumar 2014年

4

bash,-137

得分

273(字节)-300(规则)-50(尝试计数)-25(OOF警告)-25(自定义PRNG)-10(彩色)

高尔夫版

IFS=# a=(`<$0`)
c=65536
y=$[10#`date +%N`%c]
e(){ echo -e ${a[$1]/z/$z};}
e 7
while read x
do((z++,x+=c/2,i=3+(x>y),x==y))&&break
((x<0||x>c-1))&&i=6
e $i
done
e 5
#TOO LOW#TOO HIGH#\e[32mCongrats! You found the number in z turns!#OOR#Guess my number. I'll say HIGH or LOW.

请注意,最后一行是注释,因此它不包含字符串或字符文字。

非高尔夫版本

MYNUMBER=$[10#$(date +%N) % 65536 - 32768]
echo "Guess my number. I'll say HIGH or LOW."

while true; do
    read YOURGUESS
    GUESSES=$((GUESSES + 1))

    if ((YOURGUESS == MYNUMBER)); then
        break
    elif ((YOURGUESS < -32768 || YOURGUESS > 32767)); then
        echo OOR
    elif ((YOURGUESS < MYNUMBER)); then
        echo "TOO LOW"
    else
        echo "TOO HIGH"
    fi
done

echo -e "\e[32mYou found the number in $GUESSES turns!"

样品输出

$ ./tlth
Guess my number. I'll say HIGH or LOW.
-32769
OOR
32768
OOR
0
TOO LOW
16384
TOO HIGH
8192
TOO HIGH
4096
TOO HIGH
2048
TOO HIGH
1024
TOO HIGH
512
TOO HIGH
256
TOO HIGH
128
TOO HIGH
64
TOO HIGH
32
TOO LOW
48
TOO HIGH
40
TOO LOW
44
TOO HIGH
42
Congrats! You found the number in 17 turns!

最后一行以绿色打印。


3

C#:-30分

  • 345字节
  • -300用于显示规则
  • -50用于显示用户获胜时的转弯
  • -25告诉用户何时输入超出范围的数字

Golfed

int l=-32768,h=32767,n=new Random().Next(l,h),t=0,g=h+1;Console.Write("WhileURong(USayNumbr;ISayBigrOrSmalr)\n");while(n!=g){g=Convert.ToInt32(Console.ReadLine());Console.WriteLine(g==n?"C\x6fn\x67ra\x74s! Y\x6fu f\x6fund \x74\x68\x65 number in {0} \x74urns!":g<l||g>h?"BAD":g<n?"\x54\x4f\x4f \x4c\x4f\x57":"\x54\x4f\x4f \x48\x49\x47\x48",++t);}

要运行它,请将其放入文件(code.cs)中,并在命令行上使用scriptcs运行scriptcs code.cs

Ungolfed:将变量名扩展为更易于理解的名称,并将十六进制字母更改为真实字母。

int low = -32768, 
    high = 32767, 
    number = new Random().Next(low, high), 
    turns = 0, 
    guess = h+1;
Console.Write("WhileURong(USayNumbr;ISayBigrOrSmalr)\n");  // instructions
while (number != guess)
{
    guess = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine(
      guess == number                                     // if you got it right 
        ? "Congrats! You found the number in {0} turns!"  // give the victory speech
        : guess < low || guess > high                     // if out of range
          ? "BAD"                                         // let them know
          : guess < number                                // if guess is low
            ? "TOO LOW"                                   // tell them low
            : "TOO HIGH"                                  // otherwise tell them high
      , ++turns                                           // preincrement turns, add to output
    );
}

示例输出可在此处获得


3
对不起,我讲这个,但规则规定,“ 不要用字符TOLWHIG在字符串或字符文字(既不小写也不是大写)。
维克多Stafusa

所以只需更换TOL...有\x54\x4F\x4C...和你的罚款。
懒惰

谢谢。在高尔夫球版中,我只是在字符串文字中输入了适用字符的十六进制编码,并相应地修改了得分。
Yaakov Ellis 2014年

请显示代码的示例输出。
Mukul Kumar 2014年

@MukulKumar 看到这里的输出。我还对逻辑进行了一些小的更改,解决了一个问题。
Yaakov Ellis 2014年

2

C ++ 505 +(-300-50-25-25)= 105

-300:指令
-50:显示圈数
-25:不使用随机功能
-25:警告用户输入超出范围

高尔夫

#include<iostream>
#include<stdlib.h>
using namespace std;int main(){short int a,i=0,*r=new short int;int b=0;a=*r;char t[]={84,79,79,' ','\0'},l[]={76,79,87,'\n','\0'},h[]={72,73,71,72,'\n','\0'};cout<<"WhileURong(USayNumbr;ISayBigrOrSmalr)\n";while(a!=b){cin>>b;if(b<-32768||b>32767){cout<<"Sorry! the number is out of range please enter the number again\n";continue;}i++;if(b<a){cout<<'\n'<<t<<l;}if(b>a){cout<<'\n'<<t<<h;}if(a==b){cout<<"Congrats!You found the number in "<<i<<" turns!";}}return 0;}  

放开

#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
    short int a,i=0,*r=new short int;
    int b=0;
    a=*r;
    char t[]={84,79,79,' ','\0'},l[]={76,79,87,'\n','\0'},h[]={72,73,71,72,'\n','\0'};
    cout<<"WhileURong(USayNumbr;ISayBigrOrSmalr)\n";
    while(a!=b)
    {
        cin>>b;
        if(b<-32768||b>32767)
        {
            cout<<"Sorry! the number is out of range please enter the number again\n";
            continue;
        }
        i++;
        if(b<a)
        {
            cout<<'\n'<<t<<l;
        }
        if(b>a)
        {
        cout<<'\n'<<t<<h;
        }
    if( a==b)
    {   

            cout<<"Congrats!You found the number in "<<i<<" turns!";
        }
    }
    return 0;
}  

输出值


就个人而言,我无法阻止自己.. :)
Mukul Kumar 2014年

2

C,183-300-25 = -142

183字节-300(不使用随机库的规则-25)

main(){short g,n=time(0)*(time(0)&1?1:-1);puts("WhileURong(USayNumbr;ISayBigrOrSmalr)");while(g^n)scanf("%d",&g),puts(g>n?"TOO HIGH":g<n?"TOO LOW":"Congrats! You found the number!");}

非高尔夫版本:

main(){
    short g,n=time(0)*(time(0)&1?:-1);
    puts("WhileURong(USayNumbr;ISayBigrOrSmalr)");
    while(g^n)
        scanf("%d",&g),
        puts(g>n?"TOO HIGH":g<n?"TOO LOW":"Congrats! You found the number!");
}

样品运行:

WhileURong(USayNumbr;ISayBigrOrSmalr)
40
TOO HIGH
20
TOO HIGH
1
TOO HIGH
0
TOO HIGH
-20
TOO HIGH
-200
TOO HIGH
-2000
TOO HIGH
-20000
TOO LOW
-10000
TOO LOW
-5000
TOO LOW
-3000
TOO HIGH
-4000
TOO LOW
-3500
TOO HIGH
-3700
TOO HIGH
-3900
TOO HIGH
-3950
TOO HIGH
-3970
TOO LOW
-3960
Congrats! You found the number!

"TOO HIGH"并且"TOO LOW"都包含非法字符TOLWHIGtolwhig
Oberon 2014年

就像“恭喜!您找到了号码!” 和“ WhileURong(USayNumbr; ISayBigrOrSmalr)”。
Fors 2014年

@Fors,但允许使用这些(请参见EDIT2)。
Oberon 2014年

确实是的!由于某种原因,我错过了该编辑。
Fors 2014年

@Oberon那么EDIT 2TOO LOW允许吗?
牙刷

2

J-190个字符-300 -50 = -160点

'Congrats, you found the number in ',' turns!',~":1>:@]^:(]`(1[2:1!:2~a.{~71+13 8 8 _39,(5 8 16;1 2 0 1){::~0&>)@.*@-[:".1!:1@1:)^:_~32767-?2^16['WhileURong(USayNumbr;ISayBigrOrSmalr)'1!:2]2

说明(回想一下,J是从右到左读取的):

  • 'WhileURong(USayNumbr;ISayBigrOrSmalr)'1!:2]2 -打印规则。
  • 32767-?2^16[-抛回返回值,然后生成一个介于0和2 ^ 16-1(含)之间的随机数。然后,通过从32767中减去来将其调整到-32768..32767的范围。
  • 1>:@]^:(...)^:_~- x u^:v^:_ y模式有点像while循环。x保持不变,并且y随着每次执行都会发生变异u。这将继续直到x v y返回0或x u y不更改为止y。该~互换的两个参数,以便x将是随机数,y将开始在1.我们的u>:@],这本加1,并返回它,所以它作为一个计数器和x u y永远不会发生终止条件。
  • [:".1!:1@1:-以数字1代替(1:),取而代之,忽略其值。1!:1从键盘(文件句柄1)读入一行输入()并执行它(".)。这使通常_为负号的J 可以采用以下形式的数字-n(评估为对数字的否定n)。
  • ]`(...)@.*@--取之前的随机数与猜测值(-)之差。现在,我们根据差异是否为零(@.*)选择下一个动作。如果是,返回(]`)0作为的结果x v y,以便执行终止,整个while循环返回计数器。其他...
  • 71+13 8 8 _39,(5 8 16;1 2 0 1){::~0&>- 5 8 16如果数字为负,并且1 2 0 1为正,则返回数组。然后13 8 8 _39在所有内容前面加上71,所以我们有84 79 79 32 76 79 8784 79 79 32 72 73 71 72
  • 1[2:1!:2~a.{~-通过用字母索引将这些数字转换为ASCII字符a.。然后使用1!:2(使用文件句柄2)将它们打印出来,并返回1作为结果x v y
  • 'Congrats, you found the number in ',' turns!',~":-循环结束后,它返回计数器。将其转换为一个字符串":,并把它的串之间'Congrats, you found the number in '' turns!'

样本输出:

   'Congrats, you found the number in ',' turns!',~":1>:@]^:(]`(1[2:1!:2~a.{~71+13 8 8 _39,(5 8 16;1 2 0 1){::~0&>)@.*@-[:".1!:1@1:)^:_~32767-?2^16['WhileURong(USayNumbr;ISayBigrOrSmalr)'1!:2]2
WhileURong(USayNumbr;ISayBigrOrSmalr)
0
TOO HIGH
-20000
TOO LOW
-10000
TOO LOW
-5000
TOO HIGH
-7500
TOO HIGH
-8750
TOO HIGH
-9000
TOO HIGH
-9500
TOO LOW
-9250
TOO HIGH
-9375
TOO HIGH
-9450
TOO LOW
-9400
TOO HIGH
-9425
TOO HIGH
-9437
TOO LOW
-9431
TOO LOW
-9428
TOO HIGH
-9430
TOO LOW
-9429
Congrats, you found the number in 18 turns!

2

JavaScript -40

335-300(规则)-50(转数)-25(超出范围)

我不会赢,但是会以一种有趣的方式获得信件。

打高尔夫球

!function T(O,L,W,H,I,G){a=T.toString();c=L.floor(65536*L.random())+H;O(W+G+" between "+H+" & "+I);k=(f=a[9]+(d=a[11])+d+" ")+(e=a[17])+a[19]+a[21]+e;l=f+a[13]+d+a[15];for(m=1;(n=prompt(W+G))!=c;m++)n<H||n>I?O("Out of range"):n>c?O(l):O(k);O("Congrats! You found the"+G+" in "+m+" turns!")}(alert,Math,"Guess a",-32768,32767," number")

不打高尔夫球

!function T(O,L,W,H,I,G){
    fn = T.toString();
    random = L.floor(L.random() * 65536) + H;

    O(W + G + " between " + H + " & " + I);

    tooLow = (too = fn[9] + (o = fn[11]) + o + " ") + (h = fn[17]) + fn[19] + fn[21] + h;
    tooHigh = too + fn[13] + o + fn[15];

    for (n=1; (guess = prompt(W + G)) != random; n++) {
        if (guess < H || guess > I) {
            O("Out of range");  
        } else if (guess > random) {
            O(tooHigh);
        } else {
            O(tooLow);  
        }
    }

    O("Congrats! You found the" + G + " in " + n + " turns!");
}(alert, Math, "Guess a", -32768, 32767, " number")

样品输出

(ALERT) Guess a number between -32768 & 32767
(PROMPT) Guess a number
9999999
(ALERT) Out of range
(PROMPT) Guess a number
0
(ALERT) TOO LOW
(PROMPT) Guess a number
8008
(ALERT) Congrats! You found the number in 3 turns!

1

APL(Dyalog)(157-300-50 = -193)

(是的,这些以字节计,APL字符集适合一个字节。)

我声称“显示游戏规则”和“计算回合数”。

G
n←32768-?65536
t←0
⎕←'Guess 16-bit signed number'
t+←1
→8/⍨n=g←⎕
⎕←⎕AV[(⌽⎕AV)⍳'×↑↑ ','○↑-' '∇⌊∘∇'⊃⍨1+n<g]
→4
⎕←'Congrats! You found the number in't'tries!'

示例运行:

      G
Guess 16-bit signed number
⎕:
      0
TOO HIGH
⎕:
      -10000
TOO LOW
⎕:
      -5000
TOO LOW
⎕:
      -2500
TOO LOW
⎕:
      -1250
TOO HIGH
⎕:
      -1750
TOO LOW
⎕:
      -1500
TOO LOW
⎕:
      -1375
TOO LOW
⎕:
      -1300
TOO LOW
⎕:
      -1275
TOO LOW
⎕:
      -1265
TOO HIGH
⎕:
      -1270
TOO HIGH
⎕:
      -1273
 Congrats! You found the number in  13  tries!

取消高尔夫:

GuessNumber;num;tries;guess;decode;too;low;high
decode←{⎕AV[(⌽⎕AV)⍳⍵]} ⍝ invert the character code, char 1 becomes char 255 etc.
num←32768-?65536 ⍝ select a random number
tries←0

⍝ strings for low/high
too←decode '×↑↑ '
low←decode '○↑-'
high←decode '∇⌊∘∇'

⎕←'Guess 16-bit signed number'

try:
  tries +← 1
  guess ← ⎕
  →(guess=num)/found
  ⍝ still here: number was wrong
  ⎕←too, (1+num<guess)⊃low high  ⍝ output appropriate word
  →try ⍝ try again
found:
  ⎕←'Congrats! You found the number in' tries 'tries!'

1

Pogo:-95(255-300-50)

method main:void
    print("Guess the number. You will be told if you are high or low.")
    declare(integer,n,0)
    declare(integer,i,0)
    declare(integer,j,0)
    random() i
    while j != i
        set(n+1) n
        print("Number?")
        getinput() j
        if j > i
            print("High")
        end
        else
            print("Low")
        end
    end
    print("Yay" n "turns")
end main

如果数字是10:

数?

5

8

12

10

耶4转


字符数基于删除了所有空格的代码。

请注意,Pogo不是伪造的语言。我创建了它,并在这里为其编写了编译器和IDE:https : //github.com/nrubin29/Pogo


阅读我的编辑编辑-2
Mukul Kumar 2014年

根据修改内容进行了更新。
nrubin14年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.