Forum > Beginners

CODING CARD GAME USING FREE PASCAL

(1/2) > >>

HelloEveryone12345:
Hello everybody.

So for my college project, I have to code a random card game (think UNO), and I am in the beginning stages.

I have been trying to code a situation where;

I have already created a 2D matrix producing all of my cards, but now I want to code a section that allows me to deal out a certain number of cards, both x cards at the beginning of the game for the player (e.g 7 cards) and then be able to deal out y number of cards as the game continues (E.g 2, or 4).

The second thing I'm struggling with is how to do this so that the cards being dealt are removed from the stack of cards available in the original deck (with all of the cards) so that I do not accidentally hand out too many of the same card (a way to keep track of cards dealt, and cards still in the pile).

I am very new at programming, and pascal, so sorry if this did not come off as clear as it should. Any suggestions are welcome.


version Free Pascal Lazarus 2.2.0

Bart:
Show us what you've got so far.

Bart

speter:
A couple of ideas:
 +  create an array with 52 items (perhaps numbers of type "integer") - representing the 52 cards
      (if you have a different number of cards in your pack... adapt)
      (I use a one-dimensional array because it is easier to shuffle)
 + look into sorting (to shuffle the pack) - "bubble sort" is quite simple; "insertion sort" is also simple.
 + create a variable that represents your current position in the pack and increment this when you deal a card...


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  tpack = array [1..52] of integer;const  sorted_pack : tpack = (101,102,103...113,                          201,202,203...); // note this is incomplete... 
each number in the array represents a card.
  suit is cardnum div 100
  card is cardnum mod 100

Finally, remember that your teachers probably (hopefully) know about this forum and may read this thread, so remember to reference any help you get...

cheers
S.

Zvoni:
I once wrote an algorithm for Visual Basic to address stuff like that.
Shouldn't be to hard to port it to Freepascal

Algorithm works like this:
1) You have a Deck of Cards (say, 52) with a "Counter" that points to the upper bound of the Array (e.g. the Deck is still "full", so counter would be 51)
2) you pick a value from a random Slot (!!) --> Important: The "randomness" is the slot, NOT the Value. The Range of the random pick is always "0 to Counter"
3) move/copy the highest available slot to the picked slot (e.g. Card in Slot 51 becomes card in Slot 7, since Slot 7 was picked)
4) Decrease "Counter" by one.
5) Goto 1 as many times you need --> If you run that algorithm 52 times on a 52-card-Deck you would get a full shuffle

That way it's impossible to deal out "Ace of Spades" twice

Mathematics:
It doesn't matter if you shuffle the Deck before hand, since you pick the Slot randomly, not the value.
The probability to pick a card from a full deck is 1/52, independent WHERE in the Deck the Card is.

Hope it helps to get you started

EDIT: Of course, if you want to simulate a Card-Game as close as possible, then speter is right: Shuffle the Deck, then deal out from the Top
For full shuffle see my comment in 5)
For dealing out i'd go Top-Down through the Array keeping a Pointer to the current position
Cards[51] being the first dealt out (CardPointer:=50), Cards[50] the second and so on (CardPointer:=49)
That way the pointer contains the still available cards in the Deck without having to juggle Array-indices

Bart:
Maybe of interest to you: Satollo Shuffle algorithm.

Bart

Navigation

[0] Message Index

[#] Next page

Go to full version