CHALLENGE:Getting highest 3 numbes from a list of 5

Basic and Machine Language

Moderator: Moderators

Post Reply
User avatar
GreyGhost
Vic 20 Nerd
Posts: 525
Joined: Wed Oct 05, 2005 11:10 pm

CHALLENGE:Getting highest 3 numbes from a list of 5

Post by GreyGhost »

Hello everyone. I have been playing this morning and wanted to share. In the Dungeons and Dragons world, character attributes can be rolled a few different ways. One of the ways is to roll 5d6 and discard the two lowest rolls. Try to write a small program to do this. I have a short program here that does it. Would love to see something different or unique.

Code: Select all

!-A sorting routine in BASIC to be used to extract the higest
!-three rolls for character attribute scores. Looks like it
!-would be pretty straight forward to implement in ML as well.
5 dim d(5):x=4
10 for r=0tox:d(r)=int(rnd(1)*6)+1:next
20 for r=0tox:printd(r);:next
30 for i=0tox:forj=0to(x-i)
50 if d(j)<d(j+1)thent=d(j):d(j)=d(j+1):d(j+1)=t
60 next:next
70 print:for r=0tox:print d(r);:next
80 print:print"attribute score:"d(0)+d(1)+d(2)
Rob
User avatar
Kweepa
Vic 20 Scientist
Posts: 1314
Joined: Fri Jan 04, 2008 5:11 pm
Location: Austin, Texas
Occupation: Game maker

Re: CHALLENGE:Getting highest 3 numbes from a list of 5

Post by Kweepa »

Code: Select all

10 dim d(5):a=0
20 for r=0to4:d(r)=int(rnd(1)*6)+1:next
30 for r=0to4:printd(r);:next:print
40 for q=1to3:s=0:for r=1to4:ifd(r)>d(s)thens=r
50 next:printd(s);:a=a+d(s):d(s)=0:next
60 print:print"attribute score:"a
This one pulls out and then zeros the three highest values from the array.
Should be a tiny bit quicker than the bubble sort.
wimoos
Vic 20 Afficionado
Posts: 346
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Re: CHALLENGE:Getting highest 3 numbes from a list of 5

Post by wimoos »

Code: Select all

10 FOR I=1 TO 5
20 R=RND(1)*6+1
30 A(R)=A(R)+1
40 NEXT
50 FOR I=6 TO 1 STEP -1
60 N=N+A(I)
70 S=S+A(I)*I
80 IF N<3 THEN NEXT
90 S=S-I*(N-3)
100 PRINT S
Last edited by wimoos on Sun Jan 19, 2014 9:09 am, edited 2 times in total.
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
User avatar
Kweepa
Vic 20 Scientist
Posts: 1314
Joined: Fri Jan 04, 2008 5:11 pm
Location: Austin, Texas
Occupation: Game maker

Re: CHALLENGE:Getting highest 3 numbes from a list of 5

Post by Kweepa »

Very clever!
wimoos
Vic 20 Afficionado
Posts: 346
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Re: CHALLENGE:Getting highest 3 numbes from a list of 5

Post by wimoos »

Thank you. I improved it a little and thought of another algorithm, see below:

Code: Select all

10 T1=0:T2=0:T3=0:FOR I=1 TO 5:R=INT(RND(1)*6)+1
20 IF R>T1 THEN T3=T2:T2=T1:T1=R:GOTO 50
30 IF R>T2 THEN T3=T2:T2=R:GOTO 50
40 IF R>T3 THEN T3=R
50 NEXT:PRINT T1+T2+T3
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
Post Reply