Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / solitare game?

Author
Message
Silent Knight
21
Years of Service
User Offline
Joined: 25th Jan 2005
Location: In Your Mind
Posted: 9th Sep 2005 22:56
can someone please help me with how to make a solitare game? I know that i should use a for next sequence, but i need to determine placings, amount of cards remaining in the deck, rules to apply, and shuffling.

Version--DB Classic
900 mhz AMD Athlon, 128 MB RAM,
16 MB NVidia Vanta gfx card, Windows ME System software
Leeorg
21
Years of Service
User Offline
Joined: 5th Jul 2005
Location:
Posted: 9th Sep 2005 23:12
First you need an array to define a pack of cards, something like:
DIM card(52)
Each card has an assigned number, Ace=1 two=2.... jack is 10, queen is 11 etc. and add an extra zero at the end for each suit.
i.e 1-12 is Hearts, 100-112 is Diamonds, 1000 - 1012 is Clubs, 10000 - 10012 is spades
That way you'll know which suit is which.
You need arrays for a shuffle, so DIM chosencards(52)IM shuffledcards(52)
For L=1 to 52
So randomly pick a number,
If chosencards(randomnumber)=1 then goto above line ` If this number has already been picked then try again.
chosencards(randomnumber)=1:shuffledcards(L)=card(randomnumber)
Next L

So the program will pick a number, if it has picked that number already it will try again. If not, the next card will be assigned a value of the card which will not have been used.
The program will continue until all 52 cards have been shuffled.
Then its just a case of placing them on the screen (I'm assuming 2D sprites).

So thats shuffling, placing the cards, you need to work out on a bit of paper how big your screen is, how big your cards are, and work out co-ordinates.
For instance, each column has its own X value, but each extra card will have an incremental Y value.
Amount of cards remaining in deck will be dealt by the shuffledcards(?) array.
Rules will be of a logical sequnce, If Then etc.
Hope this is enough to get you started, any Q's please e-mail me.
TDK
Retired Moderator
23
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 10th Sep 2005 06:09
Hmmmm....

That shuffle method could get quite nasty when there are few cards left, as most of the randomly chosen values will be rejected.

Here's another method...

http://forum.thegamecreators.com/?m=forum_view&t=58901&b=7

TDK_Man

Leeorg
21
Years of Service
User Offline
Joined: 5th Jul 2005
Location:
Posted: 10th Sep 2005 11:00
I've just written the program using the guide I put, I've run the program several times, each time the program had finished shuffling in less than a second, and its very simple as you'll see. You can use a string alternatively to list the cards as "1" "2"..."Q" but, I used the numbers as I always use systems like that and find them easier to manage.
So....
sync on
dim cards(52):dim shuffledcards(52):dim chosencards(52) `DIMENSION ARRAYS
for l=1 to 13:cards(l)=l:next l
for l=14 to 26:cards(l)=l+100:next l
for l=27 to 39:cards(l)=l+1000:next l
for l=40 to 52:cards(l)=l+10000:next l ` cards done
for l=1 to 52
here:
a=rnd(52)
if chosencards(a)=1 then goto here ` IF CARD ALREADY PICKED - PICK ANOTHER ONE!
chosencards(a)=1:shuffledcards(l)=card(a)
next l
` CARDS ARE NOW SHUFFLED
` NOW DISPLAY PICKED CARDS (they will be listed as numbers which can later be interpreted as suits)
for l=1 to 52
print shufflecards(l);" ";
sync
next l



And hey presto.
It works in less than a second.

Lee
TDK
Retired Moderator
23
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 10th Sep 2005 15:34 Edited at: 10th Sep 2005 15:38
I wasn't saying that your code wouldn't work... well actually it didn't as you posted it as there are a few colons missing and some undimensioned array errors!

But once you've fixed those, it works fine!

So yes, it works - I was just saying it's not the most eloquent way to do it and isn't ideal example for a newcomer. I'll always be happy if someone offers me a better way to do something - including my example.

First of all, in your code, you don't seed the random number generator. Without this, the cards will always be dealt in the same order each time you run the program - not ideal for a card game!

Also, you are using the Rnd command to select a random value which will fail more and more as all the cards are dealt. When it does fail you are using Goto to repeat the Rnd until it hits a valid number.

I don't like Goto (as you may already know) - it encourages sloppy programming and you can always avoid it, so you should.

Sorry - I'm notorious for whinging about the use of Goto, but I've never yet been shown a program that cannot be written without using it!

All this slows your program down - not much admittedly, but when you add more and more every little bit helps!

You program on my machine completes in 1326 milliseconds.

My example sets up the screen mode, initiaizes the random seed and prints the cards using colours (using the notoriously slow Ink command) - yet still completes in 513 milliseconds.

If I alter my program to do exactly what yours does, it completes in 51 milliseconds! What's that, 26 times faster? What's more, by adding Sync Rate 0 after the Sync On, it drops to 26 milliseconds.



Finally, 10044 didn't mean a lot to me really, whereas KH for King of Hearts or 8C for the Eight of Clubs does.

So, I'm not saying your snippet was wrong or calling it useless or anything like that - I was just pointing out an alternative method to do it.

There's no right and wrong way to write a program - it's just that some ways are better than others and in this game, 'better' usually means 'faster'.

TDK_Man

Leeorg
21
Years of Service
User Offline
Joined: 5th Jul 2005
Location:
Posted: 10th Sep 2005 19:34
Ok, fair play with the random seed generator, I was in a hurry and didn't notice that the shuffle was identical plus I was programmed that on my laptop and copied over by hand to my home computer(internet use)(I use my laptop for programming games as its more powerful) But for a newcomer I felt that code is fine. Its simple and readable for the untrained eye. I've been on/off programming for ten years now, and have worked with basic on the spectrum, BBC, Amstrad CPC464, Archimedies, as well as learning Z80, 6502 and ARM assembly before leaving it all to do engineering as a trade, so I do understand the importance of speed and minimising codeand agree with you on those matters. However programming DB on the PC is just that its not necessary anymore if you are doing a low powered application/game. I had to look at your code for a while to fully appriciate what it was doing. (most of the string commands you were using I never use, but have learned them when I was programming in BBC Basic) If I was a newbie, my pants would have turned a lovely brown But it is a very good code, I never disputed that.
I've heard good things about you TDK from other users, so is programming for you a hobbist thing? like myself? or do you do some of it for a living?
And I bet Silent Knight is thinking... I only asked a Question! And a full on debate has started LOL!
Well its been fun!
Leeorg
21
Years of Service
User Offline
Joined: 5th Jul 2005
Location:
Posted: 10th Sep 2005 19:34
Oh and if you hate Goto's you'll hate my latest creation, 1200+ lines code crammed code, and lots of goto's
TDK
Retired Moderator
23
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 10th Sep 2005 21:29
Quote: " Oh and if you hate Goto's you'll hate my latest creation, 1200+ lines code crammed code, and lots of goto's"


Lol!

The main problem by far with Goto's is the fact that code is so difficult to follow and debug - especially for someone else looking at your code - for example, when you are trying to help sort out problems with other people's code on here!

With a procedure or function for each task of the program, you at least have a fighting chance of locating the problem!

I don't believe in saying that someones code is bad or wrong, but I'm quite happy to suggest an alternative, better way to do something if there is one.

But, I'm human too and don't always get it 100% right either. If anyone wants to improve on my suggestions, I'm all for it!

My DB programming is a hobby yes, but I'm a hardware/software engineer by trade with the emphasis on hardware. I've been programming over 25 years - including commercial software back in the 80's with various languages like Pascal, C++ and Forth - in particular Delphi since it took over from Turbo Pascal.

TDK_Man

Leeorg
21
Years of Service
User Offline
Joined: 5th Jul 2005
Location:
Posted: 10th Sep 2005 21:55
True it is hard to follow goto's! I always make a table.
I prefered the PROC/DEFPROC commands in BBC Basic, better than Gosubs!
I've managed to squeeze my code speed down to 50-60 milliseconds, turns out if you remove the sync from the loop and place it at the end it works a lot faster.
My engineering trade is marine (ships etc) so I strayed from that software programming path, it was only when DB come out I was overcome with an urge to get back into it. I started again a couple of years ago, again on and off cos of my job and family, and was very very rusty as first, but try try again pays off and I've nearly finished my first game now (my first since 95 - I've been programming since I was at least 9 (1989) on a BBC and Speccy), just ironing out the bugs. I always wanted to learn c++ but never really got round to it.
I always find its good to throw ideas about like this, I quite enjoy finding one problem that can have many solutions. The problem I used to have before the internet was sharing problems. You was always alone struggling.
Its been good chatting!
Speak later
Lee
Silent Knight
21
Years of Service
User Offline
Joined: 25th Jan 2005
Location: In Your Mind
Posted: 11th Sep 2005 01:39
any clue as to how i can define rules of the game?

Version--DB Classic
900 mhz AMD Athlon, 128 MB RAM,
16 MB NVidia Vanta gfx card, Windows ME System software
TDK
Retired Moderator
23
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 11th Sep 2005 03:12
Well, it's not that easy a project but I would use a multi-dimensioned array called Stacks() dimensioned with:

DIM Stacks(4,13)

Ignoring elements 0 (zero) for simplicity, this would represent the four discard piles at the top of the screen - each pile able to hold a value for the 13 cards of each suit.

Each time a card (starting with the ace) is added to a pile, a 1 is placed in the relevant array. Once every element of the array has been set to 1 then the game is over.

That's your 'completed - end game' defined to start with.

In the main game, I would use another array to represent the table, though this would be string - with something like:

DIM Table$(7,50)

This would represent the 7 piles of face down cards and allow up to 50 cards deep in any one pile. You would deal the cards onto the screen and add the card to the respective string array at the same time. (the remainder would be placed into a third array for dealing from).

When a card is moved on screen, all you need to do is note the card's initial position in the Table array. You then need to know which column of cards you are dropping the dragged card on to in order to find out which element of the Table$ array to examine.

From there on it's a case of checking the top card on that pile (in the array) and making sure that the card you are dropping is not the same colour and is exactly one less in face value.

If it isn't, return it to where it came from and continue.

If it is, take the value in the original array position, replacing it with "" and putting the value into the new array position.

Updating the screen would simply be CLS then drawing all the cards using the array to tell you what cards to place where.

That should give you enough ideas to get started anyway...

TDK_Man

Login to post a reply

Server time is: 2026-07-08 03:03:19
Your offset time is: 2026-07-08 03:03:19