Here's a good example of coding efficiency which I dug up from the old forums.
Here's the original code:
It's pretty simple to decipher what it's doing, and what type of game it is.
And so, in the quest to produce ever smaller and fewer lines of code, I came up with this monster:
Now, is anyone able to decipher and sort out that obfuscated mess??
Sometimes less is just a mess.
Here's the original code:
Code: (Select All)
RANDOMIZE TIMER
WHILE player < 100 AND AI < 100 'pig from Rosetta B+ started 2018-10-16
turn = (turn + 1) MOD 2
di = INT(RND * 6) + 1
PRINT: PRINT "Player:"; player; " AI:"; AI
accum = 0 '<<<<<<<<<<<<<<<<< EDIT one line instead of two
IF turn THEN 'player
DO
IF di = 1 THEN
INPUT "Player you rolled a 1, your turn is over, press enter..."; wate$
EXIT DO
ELSE
accum = accum + di
PRINT "Player you rolled a "; di; "your accumulated total is now "; accum
INPUT "Do you want to (r)oll again or (h)old, Enter r or h > "; choice$
IF choice$ = "r" THEN
di = INT(RND * 6) + 1
ELSE
player = player + accum
EXIT DO
END IF
END IF
LOOP
ELSE
FOR i = 1 TO 5
IF di = 1 THEN
INPUT "AI rolled a 1, it's turn is over, press enter..."; wate$
EXIT FOR
ELSE
accum = accum + di
PRINT "AI rolled a "; di; "it's total accumulated now is"; accum
IF i < 4 AND accum + AI < 100 THEN '<<<<<<<<<<<<<<<<<<<<<<<<<EDIT
PRINT "AI is rolling again."
INPUT "press enter..."; wate$
di = INT(RND * 6) + 1
ELSE
PRINT "AI is holding with"; accum; "added to it's score."
INPUT "press enter..."; wate$
AI = AI + accum
EXIT FOR
END IF
END IF
NEXT
END IF
WEND
IF player > AI THEN PRINT "Player wins!" ELSE PRINT "AI wins!"
END
It's pretty simple to decipher what it's doing, and what type of game it is.
Quote:Yeah, Pig, I found it at Rosetta, I was curious if it was an interesting game.
You get to keep rolling a die and accumulating a score unless you roll a 1 then the accumulated score is lost.
If you don't roll a 1 you have option of holding or rolling again. First to 100 wins.
I have AI programmed to roll 4 times here.
And so, in the quest to produce ever smaller and fewer lines of code, I came up with this monster:
Code: (Select All)
_TITLE "Pig 5 (Rosetta task)" ' B+ started 2018-10-17 Steve McNeill finished 2018-10-19
WHILE player < 100 AND AI < 100
IF Who$ <> " Player" THEN Who$ = " Player" ELSE Who$ = " AI"
FOR i = 1 TO 100
di = INT(RND * 6) + 1
IF i = 1 AND di = 1 THEN accum = 0 ELSE IF i = 1 THEN accum = di ELSE IF di = 1 THEN accum = 0 ELSE accum = accum + di
IF di = 1 THEN PRINT CHR$(10) + Who$ + " rolled a" + RTRIM$(STR$(di)) + CHR$(10) ELSE PRINT CHR$(10) + Who$ + " rolled a" + RTRIM$(STR$(di)) + ", the accumulated total is" + STR$(accum) + CHR$(10);
IF di = 1 THEN INPUT " The turn is over, press enter...", wate$ ELSE RANDOMIZE TIMER
IF di > 1 AND Who$ = " Player" THEN INPUT " Do you want to (r)oll again or (h)old, Enter r or h"; choice$ ELSE IF di > 1 AND (i < 3 AND accum + AI < 100) THEN INPUT " AI is rolling again, press enter... ", wate$
IF di > 1 AND Who$ = " Player" AND choice$ <> "r" THEN player = player + accum
IF di > 1 AND Who$ <> " Player" AND (i >= 3 OR accum + AI >= 100) THEN AI = AI + accum
IF di > 1 AND Who$ <> " Player" AND (i >= 3 OR accum + AI >= 100) THEN INPUT " AI is holding so accumulated amount is scored, press enter", wate$
IF (Who$ <> " Player" AND (i >= 3 OR accum + AI >= 100)) OR (Who$ = " Player" AND choice$ <> "r") OR di = 1 THEN EXIT FOR
NEXT
PRINT CHR$(10) + " Player =" + STR$(player) + " AI =" + STR$(AI)
WEND
PRINT Who$;" wins!"
Now, is anyone able to decipher and sort out that obfuscated mess??
Sometimes less is just a mess.