BBC BASIC for Windows
« Please help me :/ »

Welcome Guest. Please Login or Register.
Apr 5th, 2018, 10:57pm



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

If you require a dump of the post on your message board, please come to the support board and request it.


Thank you Conforums members.

BBC BASIC for Windows Resources
Online BBC BASIC for Windows documentation
BBC BASIC for Windows Beginners' Tutorial
BBC BASIC Home Page
BBC BASIC on Rosetta Code
BBC BASIC discussion group
BBC BASIC for Windows Programmers' Reference

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Please help me :/  (Read 1234 times)
admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
exclamation Re: Please help me :/
« Reply #6 on: May 15th, 2013, 9:21pm »

on May 15th, 2013, 6:26pm, Guest-Sam wrote:
Hi guys, I'm trying to create a program which (very basically) draws 52 cards. However, I can't get them to not repeat.

If you are trying to shuffle a pack of cards there is a standard algorithm for that - it's called the Fisher-Yates or Knuth shuffle:

http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

There is a BBC BASIC implementation at the bottom of this Wiki article:

http://bb4w.wikispaces.com/Notes+on+the+use+of+RND

However please note that BBC BASIC's RND function is entirely unsuited to shuffling cards, in a 'serious' application, because it simply doesn't have enough 'randomness' to create anything more than a tiny proportion of the total number of possible shuffles. See the paragraph How to do better than RND.

Richard.
User IP Logged

DDRM
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 321
exclamation Re: Please help me :/
« Reply #7 on: May 16th, 2013, 09:28am »

To amplify Michael's answer a little, without just re-writing it for you, I bet you don't get many repeated clubs, do you? can you work out why?

As Richard has hinted, you might be better setting up a full pack and shuffling it, then printing out the result. Your approach will spend a lot of time towards the end of the pack searching for a card it hasn't used yet - though modern computers are so fast you probably won't notice it...

If we can't persuade you to restructure the program without labels and GOTOs, at least look at where you have put the labels, and make sure it is really what you mean. That may sort out your problem at the end.

Best wishes,

David
User IP Logged

KenDown
Full Member
ImageImageImage


member is offline

Avatar




PM


Posts: 181
exclamation Re: Please help me :/
« Reply #8 on: Oct 22nd, 2015, 07:38am »

Now that there is no danger of doing Sam's homework for him - and I agree that one learns best by doing! - I thought I would have a go at his little problem. I haven't bothered with Richard's caution about the weakness of RND as this is not a "serious" application.

PROCinit
PROCshowcards(0)
REPEAT
PROCshufflecards
PROCshowcards(20)
PRINT"Continue? Y/N"
g%=GETAND223
UNTILg%=78
END
:
DEFPROCshufflecards
FORj%=1TO4
FORi%=1TO13
a%=RND(13)
b%=RND(13)
c%=RND(4)
d%=RND(4)
SWAPcard$(a%,c%),card$(b%,d%)
NEXT
NEXT

ENDPROC
:
DEFPROCshowcards(t%)
FORi%=1TO13
FORj%=1TO4
PRINTTAB(t%+j%*4,i%)card$(i%,j%)" "
NEXT
NEXT
ENDPROC
:
DEFPROCinit
DIMcard$(13,4)
FORi%=1TO13
CASEi%OF
WHEN1,2,3,4,5,6,7,8,9,10
c$=STR$i%
WHEN11
c$="J"
WHEN12
c$="Q"
WHEN13
c$="K"
ENDCASE
FORj%=1TO4
IFj%<3card$(i%,j%)=CHR$17+CHR$9+c$ELSEcard$(i%,j%)=CHR$17+CHR$0+c$
NEXT
NEXT
ENDPROC

Incidentally, the two lines beginning with a percentage sign are really part of one long line that has been split into three.

Anyone else want to do better or more concisely?
User IP Logged

DDRM
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 321
exclamation Re: Please help me :/
« Reply #9 on: Oct 22nd, 2015, 12:46pm »

A Challenge! I LIKE it!
Sorry, I had children of a "Noddy" age a few years ago, and there was a character who said this...

I don't claim it's better, but it is more concise:
Code:
      c$="A23456789TJQK"
      s$="CDHS"
      DIM p&(51)
      p&(0)=0
      FOR c&=1 TO 51
        r&=RND(c&+1)-1
        p&(c&)=p&(r&)
        p&(r&)=c&
      NEXT c&
       FOR c&=0 TO 51
        PRINT TAB((c& MOD 4)*5,(c& DIV 4));MID$(c$,(p&(c&) DIV 4)+1,1)+MID$(s$,(p&(c&) MOD 4)+1,1)
      NEXT c&
      END
 

I haven't checked it rigorously, but I did test it with a routine to check it consistently (5-10 times!) uses all the cards. You can test it lays them out right by changing the shuffling routine to a direct

Code:
p&(c&)=c&
 


It uses an "inside out" Fisher-Yates shuffle to generate the pack already shuffled. Again, I've just used the standard RND function,so it's not casino grade!

I've used byte variables to use less memory - part of "concise", I guess...

Best wishes,

D
User IP Logged

KenDown
Full Member
ImageImageImage


member is offline

Avatar




PM


Posts: 181
exclamation Re: Please help me :/
« Reply #10 on: Oct 22nd, 2015, 2:50pm »

Very nice. Lacks the pretty colours, but that wouldn't add much to its length.

In fact, change the definition of s$ to
s$=CHR$17+CHR$0+"C"+CHR$17+CHR$9+"D"+CHR$17+CHR$9+"H"+CHR$17+CHR$0+"S"

and the actual printing out to
PRINT TAB((c& MOD 4)*5,(c& DIV 4));MID$(c$,(p&(c&) DIV 4)+1,1)+MID$(s$,((p&(c&) MOD 4)*3)+1,3)

and you'll get the suites in colour.

I looked at the Fisher-Yates shuffle that Richard referenced but decided not to try and implement it as I wanted to recreate what Sam might have used. I'm sure that if I had tried, I would have been much more long-winded than you.

That was the same reason for using elements 1-52 of the array instead of 0-51, which is what any *real* programmer would have done!

Now all we need is some smarty-pants to come along and do it all in a single line of code!
User IP Logged

DDRM
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 321
exclamation Re: Please help me :/
« Reply #11 on: Oct 22nd, 2015, 3:39pm »

Hi Kendall,

Mine fits fine on a single line of code, if such is your fancy, but it's harder to read... Implementing a game of bridge with it would be a good trick, though! smiley

Clever trick with the colours: I like that!

I used a 0-based array because it makes the MODs and DIVs much easier, even though it's a bit less clear for the "lay reader", and needs adaptation for the string references, which are 1-based (and arguably for the RND calls, too).

Best wishes,

D
User IP Logged

KenDown
Full Member
ImageImageImage


member is offline

Avatar




PM


Posts: 181
exclamation Re: Please help me :/
« Reply #12 on: Oct 22nd, 2015, 4:04pm »

Of course, what I had in mind was the old BBC Micro, where lines were limited to 246 bytes (or something like that), so fitting a complete program onto a line called for exquisite programming and no small amount of devious cunning.

I absolutely agree with your reasons for beginning your array with element zero and always do the same myself (well, nearly always!)

User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls