So Im created a game called War and its a card game I figure
So I\'m created a game called War and its a card game. I figured out how to check if the hand of the user is empty and the deck, but I need a way to remove the card the user wants to get rid off once the deck is empty and display the new hand the user has because it\'ll help me debug the program and see if my logic went wrong anywhere. The cards are held in an enumerated array. Bellow is the output I\'m getting right now. The thing I want it to do is to remove the element you chose from the choices and array until you use up all the cards in your hand. then it displays the final output. I just don\'t know how to do it.
The program is in C++
main.cpp
#include <iostream>
#include \"Card.h\"
#include \"Deck.h\"
#include \"Player.h\"
#include \"HumanPlayer.h\"
using namespace std;
int main()
{
Deck theDeck; // create a deck
theDeck.shuffle();
// create two players
HumanPlayer player1(theDeck); // kay 8/19/98, was Player
Player player2(theDeck);
while (!theDeck.isEmpty() || (!player1.handEmpty() && !player2.handEmpty())) {
Card card1 = player1.draw();
cout << \"Player 1 plays: \" << card1 << endl;
Card card2 = player2.draw();
cout << \"Player 2 plays: \" << card2 << endl;
if (card1.getRank() == card2.getRank()) { // tie
player1.addPoints(1);
player2.addPoints(1);
cout << \"Players tie\ \ \";
}
else if (card1.getRank() > card2.getRank()) {
player1.addPoints(2);
cout << \"Player 1 wins this round\ \ \";
}
else {
player2.addPoints(2);
cout << \"Player 2 wins this round\ \ \";
}
if (!theDeck.isEmpty()) {
// replace the cards drawn
player1.replaceCard(theDeck);
player2.replaceCard(theDeck);
}
else {
player1.cardsLeft();
player2.cardsLeft();
}
}
cout << \"Player 1 score: \" << player1.score() << endl;
cout << \"Player 2 score: \" << player2.score() << endl;
if (player1.score() > player2.score())
cout << \"Player 1 wins\ \";
else if (player2.score() > player1.score())
cout << \"Player 2 wins\ \";
else
cout << \"It is a tie\ \";
return 0;
} // main
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Random.cpp
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include \"Card.h\"
#include \"Deck.h\"
class Player {
protected: // kay 8/20/98, was private
int size = 3;
Card myCards[3];
int myScore;
int removedCard;
int remainingCards = 3;
public:
Player(Deck &);
Card draw(); // another draw()!
void addPoints(int howMany) { myScore += howMany; }
int score() { return myScore; }
void replaceCard(Deck &aDeck) { myCards[removedCard] = aDeck.draw(); }
void cardsLeft() { remainingCards -= 1;}
bool handEmpty() { return remainingCards <= 0; }
}; // Player
#endif
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Deck.cpp
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Card.cpp
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Player 2 wins this round You currently hold in your hand a) Ace of hearts b) Jack of clubs c) King of clubs Which one do you want to play c Player 1 plays King of clubs Player 2 plays 5 of spades Player 1 wins this round You currently hold in your hand a) Ace of hearts b) Jack of clubs c) King of clubs Which one do you want to play? a Player 1 plays Ace of hearts Player 2 plays 3 of hearts Player 2 wins this round Player 1 score 22 Player 2 score: 30 Player 2 wins Press any key to continueSolution
Your program logic looks fine jus one thing that you are not removing the card from array .
So once you draw a card remove it from array or dont select it .
#include <iostream>
#include \"HumanPlayer.h\"
#include \"Card.h\"
using namespace std;
int removedCard[3]; //this is a global variable
Card HumanPlayer::draw()
{
cout << \"You currently hold in your hand:\" << endl;
if(remvoedCard[0]==1)
cout << \"a) Empty\" << endl;
else
cout << \"a) \" << myCards[0] << endl;
if(remvoedCard[1]==1)
cout << \"b) Empty\" << endl;
else
cout << \"b) \" << myCards[1] << endl;
if(remvoedCard[2]==1)
cout << \"c) Empty\" << endl;
else
cout << \"c) \" << myCards[2] << endl;
cout << \"Which one do you want to play? \";
char answer[80];
int i=0;
while (i < 3) {
cin >> answer;
if (answer[0] == \'a\')
removedCard[0] = 1;
else if (answer[0] == \'b\')
removedCard[1] = 1;
else if (answer[0] == \'c\')
removedCard[2] = 1;
else{
return myCards[removedCard];
cout << \"Please specify a, b, or c\ \";}
}
} // draw


