I've tried my best, but I just cannot seem to find the problem with this code. What it is supposed to happen is:
Player 1 draws a card
Player 2 draws a card
(So neither of these cards would be available to play again.)
Unfortunately, they each draw a card and what happens is- they then draw the same suit (therefore, same numbers) repeatedly. I do not know where/how to fix this.
Please someone point me in the correct direction, it would be extremely appreciated!
Extra Information: The game being played is called 'War'. So the highest card wins.
So you can either random shuffle the deck first or random pick the cards as you go.
If you shuffle the deck first then you only have to know which is next.
If you random pick the cards then you have to keep up with which are used, and pick another if it's used, so when there is only 5 cards left in the deck, it's going to be doing a lot of retries.
Either way you need a way to keep up with the order or if it's used, I think the best way is to use an array and pre-shuffle.
Thank you both for answering, but could you simplify it? I honestly have no idea what I'm doing. The code I presented took me 8 hours of struggling to come up with.
And unfortunately I have no idea how to read your code, because I've never seen some of those structures before.
The whole deck is randomly shuffled in the void DeckOfCards::shuffle() - which is then called into my main code.
This is what I have to do: (I've done one of these steps incorrectly.)
Set the seed value that is used by the random number generator by using the srand() function. Pass the value time(NULL) or time(0) to the srand function.
Create a DeckOfCards object that will used for the game.
Create two Card objects. These will be used to hold the cards that are drawn by the two people playing the game.
Create and initialize two integers to hold the game scores for the two players.
Shuffle the deck of cards by calling the shuffle method for the DeckOfCards object that was created earlier.
Write a loop that will execute as long as the deck of cards is not empty. Inside of the loop:
Player 1 should draw a card
Player 2 should draw a card
Display the two cards that were drawn
If the face values of the two cards are equal, awarded one point to both players and declare the "war" a draw. If player 1's card has the larger face value, award player 1 2 points and declare that player 1 won the "war". If player 2's card has the larger face value, award player 2 2 and declare that player 2 won the "war".
Once the deck of cards is empty, display the scores for both players and declare a winner. If the scores are equal, declare the battle a "draw".
Your problem is with char suit[1]; on the `Card' class
You later access it like suit[1], which is out of bounds, as the array only have one element the only valid index was 0
I should be suit[0]
however, ¿why is it an array? ¿why don't simply char suit;?
@ne555, Oh my goodness! Thank you so much. That fixed my problem. :D
But that is a good question. I'm not entirely sure why I used an array. Perhaps it was my inexperience leading me to believe only arrays could hold characters, which is silly because char literally means hold a character.