Basicprogramming(.org) > Code and examples
Pig Game (Rosetta)
B+:
Last night I made a first draft for Rosetta "Pig" Game:
QB64:
--- Code: ---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
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
PRINT: PRINT "Player: "; player; " AI: "; AI
IF player > AI THEN PRINT "Player wins!" ELSE PRINT "AI wins!"
END
--- End code ---
I think this might be pared down to around 20 lines?
Ruby can do it in 1, right Tomaaz? ;D
After playing for awhile, begin to understand it's name. :)
Tomaaz:
--- Quote from: B+ on October 17, 2018, 03:20:16 PM ---Ruby can do it in 1, right Tomaaz? ;D
--- End quote ---
Wrong. ;D
B+:
38 lines aboslutely no :'s used
--- Code: ---_TITLE "Pig 1 (Rosetta task)" ' attempt to pare down (no :'s) first version B+ started 2018-10-17
RANDOMIZE TIMER
WHILE player < 100 AND AI < 100 'pig from Rosetta B+ started 2018-10-16
turn = (turn + 1) MOD 2
IF turn THEN Who$ = "Player" ELSE Who$ = "AI"
PRINT "Player =" + STR$(player) + " AI =" + STR$(AI) + CHR$(10)
accum = 0
FOR i = 1 TO 100
di = INT(RND * 6) + 1
IF di = 1 THEN
PRINT Who$ + " rolled a 1, the turn is over, press enter...";
INPUT "", wate$
EXIT FOR
ELSE
accum = accum + di
PRINT Who$ + " rolled a" + RTRIM$(STR$(di)) + ", the accumulated total is" + STR$(accum)
IF turn THEN
INPUT "Do you want to (r)oll again or (h)old, Enter r or h"; choice$
IF choice$ <> "r" THEN player = player + accum
IF choice$ <> "r" THEN EXIT FOR
ELSE
IF i < 3 AND accum + AI < 100 THEN
PRINT "AI is rolling again,";
INPUT " press enter...", wate$
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
END IF
PRINT
NEXT
PRINT
WEND
PRINT "Player =" + STR$(player) + " AI =" + STR$(AI)
IF player > AI THEN PRINT "Player wins!" ELSE PRINT "AI wins!"
--- End code ---
I think next, attack the 2 prints at the end of blocks OR use Python and eliminate all the end code block lines! ;D
B+:
That was easy:
--- Code: ---_TITLE "Pig 2 (Rosetta task)" ' attempt to pare down (no :'s) first version B+ started 2018-10-17
RANDOMIZE TIMER
WHILE player < 100 AND AI < 100 'pig from Rosetta B+ started 2018-10-16
turn = (turn + 1) MOD 2
IF turn THEN Who$ = "Player" ELSE Who$ = "AI"
accum = 0
FOR i = 1 TO 100
di = INT(RND * 6) + 1
IF di = 1 THEN
PRINT CHR$(10) + Who$ + " rolled a 1, the turn is over, press enter...";
INPUT "", wate$
EXIT FOR
ELSE
accum = accum + di
PRINT CHR$(10) + Who$ + " rolled a" + RTRIM$(STR$(di)) + ", the accumulated total is" + STR$(accum)
IF turn THEN
INPUT "Do you want to (r)oll again or (h)old, Enter r or h"; choice$
IF choice$ <> "r" THEN player = player + accum
IF choice$ <> "r" THEN EXIT FOR
ELSE
IF i < 3 AND accum + AI < 100 THEN
PRINT "AI is rolling again,";
INPUT " press enter... ", wate$
ELSE
AI = AI + accum
PRINT "AI is holding with"; accum; "added to it's score,";
INPUT " press enter...", wate$
EXIT FOR
END IF
END IF
END IF
NEXT
PRINT CHR$(10) + "Player =" + STR$(player) + " AI =" + STR$(AI)
WEND
IF player > AI THEN PRINT "Player wins!" ELSE PRINT "AI wins!"
--- End code ---
36 lines only 16 more to go. ;-))
EDIT: moved score update line so don't need again at end of game, 35 lines!
B+:
32 lines with help from Steve at QB64 who pointed out something I overlooked about 100 times!
--- Code: ---_TITLE "Pig 3 (Rosetta task)" ' attempt to pare down (no :'s) first version B+ started 2018-10-17
RANDOMIZE TIMER
WHILE player < 100 AND AI < 100 'pig from Rosetta B+ started 2018-10-16
IF Who$ <> "Player" THEN Who$ = "Player" ELSE Who$ = "AI"
accum = 0
FOR i = 1 TO 100
di = INT(RND * 6) + 1
IF di = 1 THEN
PRINT CHR$(10) + Who$ + " rolled a 1, the turn is over, press enter...";
INPUT "", wate$
EXIT FOR
ELSE
accum = accum + di
PRINT CHR$(10) + Who$ + " rolled a" + RTRIM$(STR$(di)) + ", the accumulated total is" + STR$(accum)
IF Who$ = "Player" THEN
INPUT "Do you want to (r)oll again or (h)old, Enter r or h"; choice$
IF choice$ <> "r" THEN player = player + accum
IF choice$ <> "r" THEN EXIT FOR
ELSE
IF i < 3 AND accum + AI < 100 THEN
INPUT "AI is rolling again, press enter... ", wate$
ELSE
AI = AI + accum
INPUT "AI is holding so accumulated amount added it's score, press enter", wate$
EXIT FOR
END IF
END IF
END IF
NEXT
PRINT CHR$(10) + "Player =" + STR$(player) + " AI =" + STR$(AI)
WEND
IF player > AI THEN PRINT "Player wins!" ELSE PRINT "AI wins!"
--- End code ---
Navigation
[0] Message Index
[#] Next page
Go to full version