Try to understand this java code

-2

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]);}}};

`

George Clayson

Posted 2011-11-24T16:56:52.990

Reputation: 1

Question was closed 2011-11-24T20:52:21.880

See http://stackoverflow.com/questions/7906483/what-causes-noclassdeffounderror-on-a-main-class/7906484#7906484 The rest is just obfuscating variable names. And it has a bug, because it allows the player to take 0.

– Peter Taylor – 2011-11-24T18:05:00.510

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 – 2011-11-24T18:06:00.260

Answers

3

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

ratchet freak

Posted 2011-11-24T16:56:52.990

Reputation: 1 334