Tell me more ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

The students in my high school computer science class were asked to write the game of nim for a simple project. Most of them found it very easy and finished quickly. However, one student came up with this solution. I'll probably make them redo it anyway, but I'm wondering if anyone can understand what's going on.

enum $ {

$$;
static int _, $_$, $_, _0_;

{
    {
        {
            {
                _$ $_ = _$.__;
            }
        }
    }
}

enum _$ {

    __(new byte[][]{
        "\nNumber of stones: \0\nPlayer one move: \0Computer takes \0\nComputer wins!\n\0\nPlayer wins!\n".getBytes(),
        new byte[]{(byte) (_ = (int) (Math.random() * (_ = ((_ = -~_) * (_ << -~-~_) + -~-~-~_ + -~ ~ ~-~_)) + _))}},
        null);

    java.io.Console ___ = System.console();
    _$(byte[] _$_[], String $[]) {
        for ($ = new String(_$_[$_$]).split(Character.toString((char) $_$)); _ > 0;) {
            ___.printf($[$_$] + _);
            do _0_ = ___.readLine($[-~$_$]).getBytes()[$_$] - 060;
            while (_0_ > -~-~-~$_$ || _0_ > _);
            _ -= $_$ + _0_ >> $_$;
            if (((!(!(((!((!((_) < -~($_ = $_$))))))))))) break;
            _ -= $_ = Math.min((int) (Math.random() * -~-~-~$_$ + -~$_$), _);
            ___.printf($[-~-~$_$] + $_);
        }
        ___.printf(($_$ = -~-~-~$_$) > _ ? $[$_ > 0 ? -~$_$ : $_$] : $[2]);}}};

`

share|improve this question
See stackoverflow.com/questions/7906483/… The rest is just obfuscating variable names. And it has a bug, because it allows the player to take 0. – Peter Taylor Nov 24 '11 at 18:05
PS This isn't actually on topic for this site. It's essentially a duplicate of the linked post, so I would suggest that you flag it for closing; otherwise, I'm not sure where it would fit. – Peter Taylor Nov 24 '11 at 18:06

closed as off topic by gnibbler Nov 24 '11 at 20:52

Questions on Programming Puzzles & Code Golf Stack Exchange are expected to relate to programming puzzle or code golf within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

just cause I have way too much time on my hands:

import java.io.*;
import java.util.*;

enum MyEnum {

    $$;

    {
        EnumCalc $_ = EnumCalc.CONSTR;
    }

    enum EnumCalc {

        static int total, $_$, comp, player;
            CONSTR( );

        PrintStream out = System.out;
        Scanner scan = new Scanner(System.in);

        EnumCalc() {

            total = (int) (Math.random() * 20 + 20);
            strings = new String[] { "\nNumber of stones: ",
                    "\nPlayer one move: ", "Computer takes ",
                    "\nComputer wins!\n","\nPlayer wins!\n" };
            while(total > 0) {
                out.printf("\nNumber of stones: " + total);

                do {
                    out.printf("\nPlayer one move: ");
                    player = scan.nextLine().charAt(0) - '0';
                } while (player > 3 || player > total);//input loop

                total -= player;
                comp = 0;
                if (total < 1)
                    break;
                comp = Math.min((int) (Math.random() * 4), total);
                total -= comp;
                out.printf("Computer takes " + comp);
            }
            String string;
            if (3 > total)
                string = comp > 0 ?"\nPlayer wins!\n":"\nComputer wins!\n";
            else
                string = "Computer takes ";
            out.printf(string);
        }


    }
};

note that -~i is the same as 1+i

and I replaced the console calls with System.out and Scanner cause eclipse doesn't give me a Console

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.