Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coding Efficiency
#24
Here's a good example of coding efficiency which I dug up from the old forums.

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. Big Grin
Reply


Messages In This Thread
Coding Efficiency - by SMcNeill - 08-11-2024, 12:16 AM
RE: Coding Efficiency - by TerryRitchie - 08-11-2024, 01:21 AM
RE: Coding Efficiency - by SMcNeill - 08-11-2024, 03:25 AM
RE: Coding Efficiency - by CharlieJV - 08-11-2024, 01:29 AM
RE: Coding Efficiency - by Pete - 08-11-2024, 07:22 PM
RE: Coding Efficiency - by Dimster - 08-12-2024, 06:34 PM
RE: Coding Efficiency - by Pete - 08-13-2024, 09:46 PM
RE: Coding Efficiency - by SMcNeill - 08-13-2024, 11:11 PM
RE: Coding Efficiency - by Pete - 08-13-2024, 11:23 PM
RE: Coding Efficiency - by SMcNeill - 08-13-2024, 11:28 PM
RE: Coding Efficiency - by TerryRitchie - 08-14-2024, 01:21 AM
RE: Coding Efficiency - by Pete - 08-13-2024, 11:53 PM
RE: Coding Efficiency - by Pete - 08-14-2024, 03:38 PM
RE: Coding Efficiency - by SMcNeill - 08-14-2024, 04:26 PM
RE: Coding Efficiency - by mdijkens - 08-15-2024, 11:29 AM
RE: Coding Efficiency - by Pete - 08-15-2024, 03:20 PM
RE: Coding Efficiency - by Kernelpanic - 08-15-2024, 05:57 PM
RE: Coding Efficiency - by justsomeguy - 08-15-2024, 06:06 PM
RE: Coding Efficiency - by TerryRitchie - 08-16-2024, 03:21 AM
RE: Coding Efficiency - by justsomeguy - 08-16-2024, 07:09 AM
RE: Coding Efficiency - by Pete - 08-15-2024, 11:14 PM
RE: Coding Efficiency - by dano - 08-16-2024, 12:33 AM
RE: Coding Efficiency - by Pete - 08-16-2024, 12:35 AM
RE: Coding Efficiency - by SMcNeill - 09-11-2024, 01:55 AM
RE: Coding Efficiency - by Pete - 09-11-2024, 06:45 AM
RE: Coding Efficiency - by SMcNeill - 09-11-2024, 07:59 AM
RE: Coding Efficiency - by bplus - 09-11-2024, 01:51 PM
RE: Coding Efficiency - by SMcNeill - 09-11-2024, 02:01 PM
RE: Coding Efficiency - by bplus - 09-11-2024, 02:39 PM
RE: Coding Efficiency - by Pete - 09-11-2024, 02:48 PM
RE: Coding Efficiency - by dano - 09-12-2024, 01:41 PM
RE: Coding Efficiency - by Jack - 09-12-2024, 04:01 PM
RE: Coding Efficiency - by Kernelpanic - 09-12-2024, 08:13 PM
RE: Coding Efficiency - by Pete - 09-13-2024, 06:56 PM



Users browsing this thread: 2 Guest(s)