Card dealing

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 722 Views - Last Post: 02 December 2018 - 06:08 AM Rate Topic: -----

#1 jrforrest12   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-November 18

Card dealing

Posted 30 November 2018 - 09:56 AM

This is what i have so far.
void dealCard(card *deck, card **hl, card **hr, char suit [], int value) {			//Deals card from deck to player's hands
	int i;
	for (i = 0; i < 52; i++) {                                                      // For i is less than 52, i increases.
		char suit = deck[randcard] % (4 + 1) * 100;                                // Suit is randomly selected 
		int value = deck[randcard] % (13 + 1);                                    // Value is randomly selected
		*card = deck[randcard];                                                  // CArds out of deck are randomly chosen
		*card--;                                                                //Decrements random card passed until 0

		temp = suit + value;
	}
	return card;                                                              // Returns a card
}
:code:
i need help with pulling the card from the top of the deck and adding it to player 1 nd 2s hand and then delteing that card from the deck.

This post has been edited by modi123_1: 30 November 2018 - 09:59 AM
Reason for edit:: In the future please use the [code] tag button in the editor


Is This A Good Question/Topic? 0
  • +

Replies To: Card dealing

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14582
  • View blog
  • Posts: 58,445
  • Joined: 12-June 08

Re: Card dealing

Posted 30 November 2018 - 10:00 AM

Explain what your problem is.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg   User is offline

  • member icon

Reputation: 5615
  • View blog
  • Posts: 17,312
  • Joined: 25-December 09

Re: Card dealing

Posted 30 November 2018 - 10:06 AM

What is a card? Please show how card is defined/declared.

Your meaningless variable names makes helping you very difficult.

What are you trying to do with that loop?

You really should post the smallest possible complete program that illustrates the problem. As shown I have no idea why you're passing so many parameters into that function and why you think you can "return" a value from a function that has a void return type.

Jim
Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7328
  • View blog
  • Posts: 15,239
  • Joined: 16-October 07

Re: Card dealing

Posted 30 November 2018 - 11:39 AM

Where does randcard come from?!?
// Deals card from deck to player's hands
// you're passing deck, presumably and an array of card... or maybe a struct?
// hl and hr are two players?  You have a pointer pointer: are you supposed to create instances of these and pass them back?
// char suit[] and value seem meaningless, I'm afraid
void dealCard(card *deck, card **hl, card **hr, char suit [], int value) {
    int i; // good, a loop var, this makes sense
    for (i = 0; i < 52; i++) { // perhaps you should define the magic number 52 somehwere
        // wait, didn't you pass a var named suit?  wtf
        char suit = deck[randcard] % (4 + 1) * 100; // ok, all these numbers a magic, but * 100 is absolutely arcane


I'm going to have to stop. You lost me.

One thing you're clearly missing: a count of cards in the deck or the hand. Rather than attempting to randomly pluck a card out of deck, you should radomize (i.e. shuffle) the deck than then just pop the card off that stack.

Are you allowed to use structs? Those really help. Assuming your aren't, a deal might look like:
card deck[DECK_CAPACITY], hand1[HAND_CAPACITY], hand2[HAND_CAPACITY];
int deck_size = DECK_CAPACITY, hand1_size = 0, hand2_size = 0;
initDeck(deck);
shuffleDeck(deck);
dealOneCard(deck, &deck_size, hand1, &hand1_size);
dealOneCard(deck, &deck_size, hand2, &hand2_size);
// and so on



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#5 jrforrest12   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-November 18

Re: Card dealing

Posted 30 November 2018 - 01:16 PM

typedef struct card_s {
	char suit[10];					//Variable for suits: hearts, diamonds, clubs, spades
	int value;						//Value of card (2-10, J(11), Q(12), K(13), A(1)) 13 total
	struct card_s *previous, *next; //Pointers of type card created for next and previous parts of the node.
}card;

int rand_gen(int count) {
	double frac;
	frac = (double)rand() / ((double)RAND_MAX + 1);
	return floor(count * frac);


this defines the card.

also i tried to add a file for the code but it says file not permitted. and i dont want to copy and paste my code because it might show up for plagiarism

This post has been edited by Skydiver: 30 November 2018 - 01:50 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours. Put code in code tags. Learn how to do this yourself.

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is offline

  • member icon

Reputation: 5615
  • View blog
  • Posts: 17,312
  • Joined: 25-December 09

Re: Card dealing

Posted 30 November 2018 - 01:31 PM

Quote

i dont want to copy and paste my code because it might show up for plagiarism

I really don't think you have much to worry about here, at least with what you've posted so far. If anyone is dumb enough to try to use this code good luck to them.

If you want help you need to provide something we can use to help you. The code you posted to date is not near enough content for us to be of much help.

I will say that, IMO, your "card" class has a couple of elements it doesn't need (the pointers). A "card" should describe a single card, a "deck" should house multiple "cards".

Jim


Also be sure to use code tags when posting code.

:code:
Was This Post Helpful? 1
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6565
  • View blog
  • Posts: 22,530
  • Joined: 05-May 12

Re: Card dealing

Posted 30 November 2018 - 01:54 PM

The next, and previous pointers in your card structure suggests that you are supposed to keep the cards in a doubly linked list and that dealing the cards is simply a matter of taking cards out of the a "deck" linked list and moving them into the various "hand" linked lists. This will take care of "deleting" the card from the deck. You won't actually be deleting the card. You'll be simply changing which data structure has ownership of it.
Was This Post Helpful? 0
  • +
  • -

#8 jrforrest12   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-November 18

Re: Card dealing

Posted 30 November 2018 - 02:21 PM

yes exactly. A lot of the functions in my code use doubly linked lists. Im just trying to wrap my head around how to writie the deal card. i have the add card, delete card function created and shuffle. i just need to write a code for the dealing portion.

Attached Image

This post has been edited by Skydiver: 30 November 2018 - 02:18 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6565
  • View blog
  • Posts: 22,530
  • Joined: 05-May 12

Re: Card dealing

Posted 30 November 2018 - 02:23 PM

A typical double linked list has the following functions:
1. Return the count of items in the list.
2. Add to the front of the list.
3. Add to the end of the list.
4. Remove the item at the front of the list.
5. Remove the item at the end of the list.

Dealing a card is simply either #4 or #5 depending on how you define the top of the deck. You can choose that the front of the list is the top, or you could choose that the end of the list is the top. You could also choose to deal from the bottom of the deck... it's not like the computer would have its feeling hurt if you called it a cheater for dealing from the bottom of the deck.

There is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area.
Was This Post Helpful? 0
  • +
  • -

#10 jrforrest12   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-November 18

Re: Card dealing

Posted 30 November 2018 - 02:25 PM

yea im not too sure how to write the code to add the card to the players hand. this doubly linked list is confusing. barely covered the topic in class.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6565
  • View blog
  • Posts: 22,530
  • Joined: 05-May 12

Re: Card dealing

Posted 30 November 2018 - 02:48 PM

How did you add cards to the deck list? You simply add a card to hand list. In pseudo code:
List * deck = CreateEmptyList();
AddCardsToDeck(deck);
ShuffleCards(deck);

List * hand = CreateEmptyList();
Card * card = RemovedCardFromFront(deck);
AddCardToFront(hand, card);



If you barely covered doubly-linked lists, then don't use it. Use a single linked list instead. If you aren't comfortable using lists, use arrays instead.
Was This Post Helpful? 0
  • +
  • -

#12 jrforrest12   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-November 18

Re: Card dealing

Posted 30 November 2018 - 02:51 PM

i have to use doubly linked lists.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#13 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6565
  • View blog
  • Posts: 22,530
  • Joined: 05-May 12

Re: Card dealing

Posted 30 November 2018 - 02:53 PM

Please do not post screenshots. If you need to post code, please post them in code tags. The screenshots are basically unreadable on small devices because they are too big, and on most computers, they are too small.
Was This Post Helpful? 2
  • +
  • -

#14 MentalFloss   User is offline

  • .
  • member icon

Reputation: 602
  • View blog
  • Posts: 1,568
  • Joined: 02-September 09

Re: Card dealing

Posted 30 November 2018 - 09:32 PM

I'm not trying to derail the topic, but:

jrforrest12 said:

and i dont want to copy and paste my code because it might show up for plagiarism


jimblumberg said:

I really don't think you have much to worry about here, at least with what you've posted so far. If anyone is dumb enough to try to use this code good luck to them.


Spiders crawl DIC, and his code could potentially be searched by plagiarism software, so this is definitely a valid concern, and doesn't directly pertain to someone being "dumb" enough to try to use this code.

Back to OP: It's always a good idea to distill what you're asking about to its most directly affected code because it will help us help you, and it will (hopefully) not look like your final product.
Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg   User is offline

  • member icon

Reputation: 5615
  • View blog
  • Posts: 17,312
  • Joined: 25-December 09

Re: Card dealing

Posted 30 November 2018 - 10:05 PM

Quote

Spiders crawl DIC, and his code could potentially be searched by plagiarism software, so this is definitely a valid concern,

Why would it be a concern if the code is indeed the OP's code it shouldn't be a problem for him, but others?

Jim
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2