猜数字


10

描述

创建一个完全可用的“猜数字”游戏。该游戏由两个玩家玩,如下所示:

  1. 玩家一选择1到N之间的数字(A)
  2. 玩家二尝试猜测A在相同范围内选择一个数字(B)
  3. 第一个玩家回答“较高” if A > B“较低” if A < B“正确” if A = B
  4. 步骤2-3重复I次或直到响应“正确”为止。
  5. 如果听到“正确”,则第二名玩家获胜,否则将输。

眼镜

有效条目的最低规格:

  • 用户可以扮演第一人或第二人。
  • 计算机扮演着另一个角色。
  • 计算机必须真正尝试在玩第二号玩家时猜测数字(因此,猜测给定数据或忽略给定数据是作弊行为)
  • N = 100 由用户输入(您的选择)
  • I = 5 由用户输入(您的选择)
  • 节目必须在最后宣布获奖者。
  • 完整的人类可读的指令,指示每个步骤的操作(例如“扮演一名玩家还是两名玩家?”“输入另一个猜测:”,等等)-不要对这一步骤有所顾忌;保持简单。

获奖条件

以该顺序:

  1. 如果投票数第二高的票至少领先3票,则投票数最高的票将获胜。
  2. 最短的入场券获胜。

我没有明确地读到我们需要在那上面使用AI。您能确定两位选手都是人类吗?
JB

@JB:很抱歉://您确实需要AI。我现在正在更新问题...
Eelvex

2
我认为应该给出每个步骤的说明文字,否则很难比较答案。
Joey

@Joey肯定
西蒙·托特

用户输入验证重要吗?
zzzzBov 2011年

Answers:


3

Windows PowerShell,289

nal ^ Read-Host
filter p{"Player $_ wins.";exit}
$p=1-(^ Player 1 or 2)
$n=($x=1)..($y=99)|random
if($p){$n=^ Enter number}1..5|%{if($p){'{0:"higher";"lower";"correct";2|p}'-f($n-(^ Guess))|iex}else{"Guessing, "+($g=$x..$y|random);@{104='$x=$g+1';108='$y=$g-1';99='2|p'}[+(^)[0]]|iex}}
1|p

历史:

  • 2011年2月21日18:44 (620)不参加比赛。
  • 2011-02-21 19:15 (365)第一轮打高尔夫球。
  • 2011-02-21 19:31 (359)一些内联。
  • 2011-02-21 19:38 (340)缩短了一些琴弦。
  • 2011-02-21 19:44 (339) whilefor
  • 2011-02-21 19:53 (331)一些重复的字符串放入变量中。
  • 2011-02-21 19:53 (330)内联另一个变量。
  • 2011-02-21 19:53 (328)优化循环条件。但是不能使用管道。
  • 2011-02-22 01:57 (326) else{if...}elseif–保存大括号。
  • 2011-02-22 12:42 (325)使用哈希表代替switch来避免命名循环,从而移动了很多内容。现在,我也可以使用Just break和管道。优胜者公告已移至一个exit无需使用中断的过滤器中。
  • 2011-02-23 01:23 (308)elseif我只是使用带有不同值的负,正和零值格式字符串代替检查猜测的链条。节省很多。
  • 2011-02-23 02:16 (306)使用减法代替相等。
  • 2011-03-12 02:27 (289)降低到与Ruby解决方案相同的基本用户交互级别。当然要短一些。

9

TRS-80 BASIC,249个字符

我认为这可能是我第一次向该网站提交答案,而该答案是我该网站存在之前编写的。

0 CLS:RANDOM:A=1:B=1E3:Q=RND(1E3):FORX=1TO8E9:PRINTA"-"B:INPUT"
 Guess";C:IFC<AORC>B,X=X-1:NEXTELSEIFC<QPRINT"Too low":A=C+1:NEXTELSEIFC>QPRINT"Too high":B=C-1:NEXTELSEPRINTC"is right!!
 It took you"X"tries.
":X=9E9:NEXT:FORX=0TO0:X=INKEY$="":NEXT:RUN

实际上,我是在全球互联网存在之前就写的。早在1980年代,就像TRS-80变得无关紧要一样。

这是“单线”的示例-试图将最有趣的程序放入单行的BASIC中。这是我的第一个班轮,并没有给人留下深刻的印象。(那里漂浮着一些惊人的单线。)

TRS-80 BASIC的行数限制为255个字符,减去一对开销。尽管有时您可能会超出此限制,但由于真正的限制是标记化之后为255个字节-但编辑器还将标记化之前截断超过255个字符的行,并且您需要在行编辑器中使用技巧才能插入文字控制字符就像换行符一样。

是的,需要澄清的是,列表中的换行符是文字换行符。(为了在代码中获取它们,您需要在行中使用Change命令EDIT。这很麻烦,但是+CHR$(13)用一个替换八个或更多字符是值得的。)

该死,我打高尔夫已经很久了。


6

C 397个字符

N,H=99,L=0,c=0,w=1;main(){char s[9];puts("Play as player 1 or 2: ");scanf("%d",&N);if(N-1){getchar();do{N=rand()%(H-L)+L+1;printf("My guess: %d\n",N);gets(s);if(*s=='c'){w=2;break;}if(*s-'l')H=N-1;else L=N-1;c++;}while(c<5);}else{N=rand()%99+1;while(c<5){puts("Enter guess: ");scanf("%d",&H);if(H==N){puts("correct");break;}else puts(H>N?"higher":"lower");c++;}if(c==5)w=2;}printf("Winner %d",w);}

以更具可读性的形式。

main()
{
        int i,N,H=100,L=0,c=0,w=1;
        char s[10];
        puts("Play as player 1 or 2: ");
        scanf("%d",&i);
        if(i-1)
        {
                getchar();
                do{
                        N=rand()%(H-L)+L+1;
                        printf("My guess: %d\n",N);
                        gets(s);
                        if(s[0]=='c')break;
                        else if(s[0]=='h')H=N-1;
                        else L=N-1;
                        c++;
                }while (c<5);
                if(c<5)w=2;
        }
        else
        {
                N=rand()%99+1;
                while (c<5)
                {
                        puts("Enter another guess: ");
                        scanf("%d",&H);
                        if(H==N){printf("correct\n");break;}
                        else if(H>N)printf("higher\n");
                        else printf("lower\n");
                        c++;
                }
                if(c==5)w=2;
        }
        printf("Winner %d",w);
}

@Joey已更正。
fR0DDY

好吧,我不知道完整的C。但是,您能否解释一下没有类型说明符的变量程序如何编译?
MAKZ 2014年

没有指定类型的任何变量都是整数
Ethiraric 2014年

4

C#:

字符数:带空格:575无空格:464

    static void Main()
    {
        Action<object> w = s => Console.WriteLine(s);
        Func<object, byte> r = t => { w(t); var s = Console.ReadLine(); return Convert.ToByte(s); };
        var p = r("Player (1/2):");
        int N = 100, g, i = 0, c, d;
        var q = new List<int>(Enumerable.Range(0, N));
        Func<Guid> x = Guid.NewGuid;
        c = p == 1 ? r("Number:") : q.OrderBy(j => x()).First();
        m: i++;
        g = p == 2 ? r("Guess:") : q.OrderBy(j => x()).First();
        d = g < c ? -1 : (g > c ? 1 : 0);
        w(d == -1 ? "Higher" : (d == 1 ? "Lower" : "correct"));
        q = q.Where(n => d == -1 ? n > g : n < g).ToList();
        if(c != g && i < 5) goto m;
        r(g);
    }

现在,“立即编辑 ”现在变成 “转到”(颤抖


3

好老平原C

#include <stdio.h>
#define x(s) puts(s)
main(){int c,i,l,h,g;srand(time(NULL));p:x("You want to guess (1) or should I (2)?");scanf("%d",&c);i=5;if(c==2){x("Think a number 1..100");h=100;l=1;goto t;}if(c==1){x("Guess a number 1..100");h=rand()%100+1;goto g;}return 0;t:if(!i--)goto u;printf("%d (1)higher (2)lower (3)correct",g=rand()%(h-l)+l);scanf("%d",&c);if(c==1)l=g;if(c==2)h=g;if(c==3)goto c;goto t;g:if (!i--)goto c;scanf("%d",&g);if(g>h)x("lower");if(g<h)x("higher");if(g==h){x("correct");goto u;}goto g;u:x("You win");goto p;c:x("I win");goto p;}
  • 23/11/2011 16:44:00 883不错而舒适
  • 24/11/2011 09:38:00 616固定并缩短
  • 24/11/2011 11:52:00 555缩短

1
你将来写这个吗?太聪明了!!
mikera 2011年

3

的JavaScript

新的缩小版本(挂断var和减少的alert呼叫:

268个字符

function g(m){n=u(confirm('Player pick?'));function u(p){if (p){do{n=parseInt(prompt('Number'))}while(isNaN(n)||!n||n>m)}else{n=parseInt(Math.random()*m)+1}return n}while(g!==n){do{g=parseInt(prompt('Guess'))}while(isNaN(g));alert(g<n?'higher':g>n?'lower':'correct')}}

要运行call g(100);,不计算自我执行,因为它会添加可变数量的字符(代表275个字符g(100);)。

原始(大约600个字符,包括空格):

function guessTheNumber(m)
{
  var n = getNum(confirm('Player pick the number?')), g;

  function getNum(p)
  {
    var n;
    if (p)
    {
      do
      {
        n = parseInt(prompt('What number?'));
      } while(isNaN(n) || !n || n > m);
    }
    else
    {
      n = parseInt(Math.random() * m) + 1;
    }
    return n;
  }

  while(g!==n)
  {
    do
    {
      g = parseInt(prompt('Take a guess!'));
    } while(isNaN(g));
    if (g < n)
    {
      alert('higher');
    }
    else if (g > n)
    {
      alert('lower');
    }
    else
    {
      alert('correct!');
    }
  }
}

缩小(312)

function g(m){var g,n=u(confirm('Player pick?'));function u(p){var n;if (p){do{n=parseInt(prompt('Number'))}while(isNaN(n)||!n||n>m)}else{n=parseInt(Math.random()*m)+1}return n}while(g!==n){do{g=parseInt(prompt('Guess'))}while(isNaN(g));if(g<n) alert('higher');else if(g>n) alert('lower');else alert('correct')}}

对不起,我没有注意到。最好将您的最新代码放在顶部。(此外,我无法使其正常运行:-/)
Eelvex 2011年

@Eelvex:当您尝试使其运行时,我的解决方案比当前接受的解决方案短。您应该真正指定与用户进行交互时要使用的确切字符串。基本上,Magnus所做的所有事情都是使用更简洁的交互,当然交互时间会更短。
乔伊(Joey)

我是否正确地看到了此程序,但该程序无法处理人是玩家1的情况?至少我看不到AI猜数字的代码,也看不到玩家输入“更高”,“更低”或“正确”的代码……
Joey

@Joey,我给人的印象是,如果人类玩家先走,他们只需要选择一个数字即可。我猜我对为什么人类玩家会选择更高,更低和正确的选择
有点迷惑

只需按照玩家1和2的说明进行操作即可。其中一个是人,另一个是计算机。不过,协议没有区别。同样,句子“计算机必须在尝试以第二个玩家身份玩游戏时确实尝试猜测数字”,这非常暗示计算机必须猜测数字。
乔伊(Joey)

3

Python 2.7 334 335 327 314 300个字符 (我第一次打高尔夫球)

(335)忘了换行。

(327)删除了randrand的多余100。比较响应的第一个字符,而不是整个字符串。

(314)更新了谁赢得了打印。

(300)当玩家为玩家1时更改:更改对数字的猜测为计算机

from random import*
i=input
s="Enter number:"
p=i("Player 1/2:")-1
h=100
N=(i(s),randint(1,h))[p]
b=l=0
for _ in[0]*5:
 exec("g=(h+l)/2;r=raw_input('Guessing '+`g`+':')[0];exec('h=g','l=g')[r=='l'];b=r=='c'","g=i(s);b=g==N;print(('higher','lower')[N<g],'correct')[b]")[p]
 if b:break
print 1+b,"won"

2

BASIC 184

100 INPUT "P1 NUMBER? ";
200 FOR I%=1 TO 5
300 INPUT "P2 GUESS? ";G%
400 INPUT "P1 SENTENCE? ";S$
500 IF S$="CORRECT" THEN 800
600 NEXT I%
700 PRINT "WINNER 1":END
800 PRINT "WINNER 2"

这是无AI版本。


我意识到这是一个旧答案,但是我当时不知道这个,我偶然发现了这一点-您可能使用较短的行号,例如1、2、3等,或者只是假设使用QBASIC / FreeBASIC和完全忽略它们。
nyuszika7h 2014年

@ nyuszika7h与其说是打高尔夫球,不如说是一场普及比赛,我宁愿选择更典型的百位数字。
JB 2014年

它是?好吧,它被标记了code-golf
nyuszika7h 2014年

具有特定的和明确的(糟糕的,恕我直言)获胜条件。
JB 2014年

哦,对,我确实看到了,但忘记了。
nyuszika7h 2014年

2

Lua 360字符

i=io.read p=print I=5 N=100 math.randomseed(os.time())r=math.random p"Play as player one or two?"o=i"*n">1 _=o and p("Input number between 1 and",N)n=o and i"*n"or r(I,N)l,u=1,N for k=1,I do p"Guess!"g=o and r(l,u)or i"*n"p("Guessed",g)if n==g then p"Correct"break elseif n>g then p"Higher"l=g else p"Lower"u=g end end p(o and"I"or"You",n==g and"Won"or"Loose")

非高尔夫版本:

i=io.read
p=print
I=5
N=100
math.randomseed(os.time())      -- Make things less predictable
r=math.random                   
p"Play as player one or two?"
o=i"*n">1
_=o and p("Input number between 1 and",N) -- if one, ask for number
n=o and i"*n"or r(I,N)          -- get number from user or random
l,u=1,N                         -- boundaries for doing "smart" guessing
for k=1,I do
    p"Guess!"
    g=o and r(l,u)or i"*n"      -- get guess (random or input)
    p("Guessed",g)
    if n==g then p"Correct!"break -- break loop if guessed correctly
    elseif n>g then             -- if guess to low
    p"Higher"l=g else           -- print + update boundaries
    p"Lower"u=g end
end
p(o and"I"or"You",n==g and"Won"or"Loose") -- Determine outcome!

2

Java脚本

这大约是800个字符,其中包括半个计算机播放器的基本二进制选择“ AI”。如果我删除了所有vars,我可能会节省一些字符,但是即使在代码打高尔夫球的时候,我也不喜欢泄漏变量。我还做了两个步骤:“这正确吗?” /“这更高吗?” 确认弹出窗口而不是提示弹出窗口并检查“正确” /“较高” /“较低”的东西,尽管这也许还可以保存一些字符,但我并没有真正检查过。

另外,我仅在Firefox 4上进行了测试,所以我不知道我正在做的某些事情是否始终如一,特别是将解析为NaN的无效输入合并为wp函数中的默认值。

function game(N, I) {
    var wa=function(a){window.alert(a)};
    var wc=function(s){return window.confirm(s)};
    var wp=function(s){return window.prompt(s)};
    var ri=function(s,d){return parseInt(wp(s),10)||d};
    var m=function(l,h){return Math.round((h+l)/2)};
    N = N || pd("Highest possible number?",100);
    I = I || pd("How many guesses?",5);
    var p = wc("Be player 2?");
    var s = [1,N];
    var a = p?Math.ceil(Math.random()*N):Math.min(N,Math.max(1,ri("Pick a number from 1 to " + N,1)));
    var w = 0;
    var g = 0;
    if(p) while(I--){while(!(g = ri("Guess:",0)));if(g==a){wa("correct");w=p+1;break;}else{wa(g<a?"higher":"lower")}}
    else while(I--){g = m(s[0],s[1]);if(wc("Is "+g+" correct?")) { w=p+1;break;} else if (wc("Is "+g+" higher?")){s=[s[0],g];}else{s=[g,s[1]];}}
    if(!w)w=!p+1;
    wa("Player " + w + " wins!");
}
game(100,5);

2

爪哇,1886个字符

import java.io.*;import java.util.*;import java.util.regex.*;public class GuessGame {int L=1;int H=100;int G=5;int N;String HS="higher";String LS="lower";String CS="correct";public static void main(String[] args){if (args.length==2)new GuessGame(Integer.parseInt(args[0]),Integer.parseInt(args[1])).play();else if(args.length==0)new GuessGame(100,5).play();else System.out.println("usage GuessGame HighInteger NumberGuess");}GuessGame(int H,int G){this.H = H;this.G = G;}void play(){int pNum=getInt("Play As Player 1 or Player 2?","1|2");if(pNum==1)playP2();else playP1();System.out.println("The number was "+N);}int getInt(String pmpt,String val){BufferedReader cin=new BufferedReader(new InputStreamReader(System.in));int i=0;Pattern p=Pattern.compile(val);boolean fnd=false;String ln="";try{while(!fnd){System.out.println(pmpt);ln=cin.readLine();Matcher m=p.matcher(ln);fnd=m.find();}i=Integer.parseInt(ln);} catch (Exception ex) {}return i;}String processGuess(int g){if(N>g)return HS;else if(N<g)return LS;else return CS;}void playP1(){N=new Random().nextInt(H);for(;G>0;G--){String rslt=processGuess(getInt("Player 2, enter your guess:","\\d?"));System.out.println(rslt);if(rslt.equals(CS)){System.out.println("Player 2 wins!");break;}}}void playP2() {N=getInt("Player 1, enter your number:", "\\d+");int max=H;int min=L;int nextGuess=min+(max-min)/2;for (;G>0;G--){System.out.println("Player 2, enter your guess:" + nextGuess);String rslt=processGuess(nextGuess);System.out.println(rslt);if(rslt.equals(HS)){min=nextGuess+1;nextGuess=fuzzify(nextGuess+(max-nextGuess)/2,min,max);}if (rslt.equals(LS)){max=nextGuess-1;nextGuess=fuzzify(nextGuess-(nextGuess-min)/2,min,max);}if(rslt.equals(CS)){System.out.println("Player 2 wins!");break;}}}int fuzzify(int i,int mn,int mx){int fz=new Random().nextInt(3);if(fz==1)return Math.max(mn,--i);if(fz==2)return Math.min(mx,++i);return i;}}

非高尔夫版本:

import java.io.*;
import java.util.*;
import java.util.regex.*;
public class GuessGame {
    int L = 1;
    int H = 100;
    int G = 5;
    int N;
    String HS = "higher";
    String LS = "lower";
    String CS = "correct";
    public static void main(String[] args) {
        if (args.length == 2)
            new GuessGame(Integer.parseInt(args[0]), Integer.parseInt(args[1])).play();
        else if (args.length == 0)
            new GuessGame(100, 5).play();
        else
            System.out.println("usage GuessGame HighInteger NumberGuess");
    }
    GuessGame(int H, int G) {
        this.H = H;
        this.G = G;
    }
    void play() {
        int pNum = getInt("Play As Player 1 or Player 2?","1|2");
        if (pNum == 1)
            playP2();
        else
            playP1();
        System.out.println("The number was " + N);
    }
    int getInt(String pmpt, String val) {
        BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
        int i = 0;
        Pattern p = Pattern.compile(val);
        boolean fnd = false;
        String ln = "";
        try {
            while (!fnd) {
                System.out.println(pmpt);
                ln = cin.readLine();
                Matcher m = p.matcher(ln);
                fnd = m.find();
            }
            i = Integer.parseInt(ln);
        } catch (Exception ex) {}
        return i;
    }
    String processGuess(int g) {
        if (N > g)
            return HS;
        else if (N < g)
            return LS;
        else
            return CS;
    }
    void playP1() {
        N = new Random().nextInt(H);
        for (; G > 0; G--) {
            String rslt = processGuess(getInt("Player 2, enter your guess:", "\\d?"));
            System.out.println(rslt);
            if (rslt.equals(CS)) {
                System.out.println("Player 2 wins!");
                break;
            }
        }
    }
    void playP2() {
        N = getInt("Player 1, enter your number:", "\\d+");
        int max = H;
        int min = L;
        int nextGuess = min + (max - min) / 2;
        for (; G > 0; G--) {
            System.out.println("Player 2, enter your guess:" + nextGuess);
            String rslt = processGuess(nextGuess);
            System.out.println(rslt);
            if (rslt.equals(HS)) {
                min = nextGuess + 1;
                nextGuess = fuzzify(nextGuess + (max - nextGuess) / 2, min, max);
            }
            if (rslt.equals(LS)) {
                max = nextGuess - 1;
                nextGuess = fuzzify(nextGuess - (nextGuess - min) / 2, min, max);
            }
            if (rslt.equals(CS)) {
                System.out.println("Player 2 wins!");
                break;
            }
        }
    }
    int fuzzify(int i, int mn, int mx) {
        int fz = new Random().nextInt(3);
        if (fz == 1)
            return Math.max(mn, --i);
        if (fz == 2)
            return Math.min(mx, ++i);
        return i;
    }
}

首先:它甚至不能编译,因为Java没有多行字符串。您不能指望在字符串中间断行并能正常工作。
乔伊

好的,我停止尝试使其在屏幕上看起来不错,只是粘贴了单行。
Joe Zitzelberger 2011年

1
此外(括号中的节省):该类可以用单个字母(40)命名。那里有很多不必要的空格(80)。您可以折叠同一类型的多个变量的声明,例如int a=5,b=10(34)。您可以将构造函数中的参数命名为与字段不同的名称,以避免this.(10)。L由于它始终保持在1(4),因此可以完全消除。无论如何,您都可以省去初始化HG在构造函数中设置的初始化(6)。您可以对所有变量和方法使用一个字母的名称(235)。
乔伊

是的,但这完全是不可读的。在这一点上,我看到了代码高尔夫球真正成为了代码保龄球。但是,如果我有时间,我会在今晚完善它。
Joe Zitzelberger 2011年

1
对上一个图形的更正:(272)。您可以fuzzify使用条件运算符(20)进行重写。您可以在内嵌BufferedReadergetInt(19)。您可以使用String#matches,以避免PatternMatchergetInt(48)。您也可以使用processGuess(30)中的条件运算符。我现在降至1360,而不是您最初的1953。–
Joey

2

闹钟收音机,5756位(2159字节)

即将来到您附近的闹钟! 换行符仅用于可读性。有时会使用ASCII值代替屏幕上的文字数字显示。符合规格。对A和使用默认值I。在第一个输入上,使用NULL作为猜测值,否则将设置数字。密码必须为4!细胞必须为7位并包装!图灵完成!输出higherlowercorrectyou won,和you lost

}+>,[,>>+++++++[>>>>>>>+++++++>+++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++]>>>>>>>++++++.>>>>>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++[+++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++>+++>>>>>>>]>++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++.++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++.++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++[++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++>+>>>>>>>]>+++.+++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++[
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++>++++>>>>>>>]>+++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++.+++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++.[+]+][++++[+++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++>,[+[+++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++>++>>>>>>>]>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++.++++++++.[+
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++>+++>>>>>>>]>.+++++++++++++.[+
]+][++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++[++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++>+>>>>>>>]>++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
++++++++++++.+++..++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++.+++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++[+
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++>+>>>>>>>]>+++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++.>>>>>>>[+]>>>>>>>+++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++]]][>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++[++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++>+++>>>>>>>]>+++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++.+++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++.+++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++[+++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++>+>>>>>>>]>+++.++[+++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+
+>>>>>>>]>.+++.++++.+.>>>>>>>[+]+][+++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++[++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>>>>>>>]>+++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++.+++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
++++++.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++[+++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++>+>>>>>>>]>+++.++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++[+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++>>>>>>>]>++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++.++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.]

1

红宝石1.9(298)

b=->f{puts "> "+f;gets}
a=->f{b[f].to_i}
q=a["Player 1 or 2?"]
i,j,g=100,1
n=q<2?a["Enter number:"]:rand(i)+j
5.times{q<2?(g=j+(i-j)/2
c=b["Guessing, #{g}"]
c[0]==?c?break: c[0]==?h?j=g :i=g):(
g=a["Guess:"]
puts g==n ?"correct":g<n ?"higher":"lower"
g==n&&break)}
puts "Player #{g==n ?2:1} won!"

虽然不是很友好的说明。


非常好!当AI是玩家2时,如果只有一些随机分组...
Eelvex 2011年

@Eelvex,被打了30个字符。
zzzzBov 2011年

1

我觉得有必要使Java更加公正。:)

爪哇- 486 437 421 414

打高尔夫球

import java.util.*;class GuessANumber{static<T>void p(T p){System.out.println(p);}static int i(){return new Scanner(System.in).nextInt();}public static void main(String[]a){int c,g,f=101,i=0;p("Pick (1) or guess (2)?");c=i();if(c==1)p("Pick a number (1-100)");g=c==1?i():new Random().nextInt(100);while(f!=g&&i++<5){p("Guess:");f=c==2?i():f>g?f/2:f+f/2;if(c==1)p(f);p(f>g?"lower":"higher");}if(f==g)p("correct");}}

略微偏离

import java.util.*;

class a{

    static<T>void p(T p){
        System.out.println(p);
    }

    static int i(){
        return new Scanner(System.in).nextInt();
    }

    public static void main(String[]a){
        int c,g,f=101,i=0;
        p("Pick (1) or guess (2)?");
        c=i();
        if(c==1)p("Pick a number (1-100)");
        g=c==1?i():new Random().nextInt(100);
        while(f!=g&&i++<5){
            p("Guess:");
            f=c==2?i():f>g?f/2:f+f/2;
            if(c==1)p(f);
            p(f>g?"lower":"higher");
        }
        if(f==g)p("correct");
    }
}
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.