Writing a program that is LIKE a card game of war and I'm VERY stuck. HELP!

0 Savannah Thames · July 1, 2015
Hi all, I'm working on an assignment for school that's supposed to have us write a program for a card game of war. Sorry- Like war. I've scrapped what I've been working on close to three times now. I'm tired. I've been working on this for the past two days. I've commented the steps I want to take in my program, but can't seem to get it to flow.

The assignment is given as follows: "write a C++ program that allows two people to play a game of War (similar to the game War played with a deck of cards).  For your program, a loop should repeat 26 times that prompts two people to enter their card number.  (Note that card numbers can be 2-10, J, Q, K, or A)  The program should check to see which card has a higher value, and keep track of how many hands each player wins.  If there is a tie, keep asking for new card numbers until there is no longer a tie. At the end of the game (26 plays), display the number of times each person won a hand, and display a message congratulating the player who won the most times. Your program must utilize at least two functions (besides the main function).  All output and prompts should be formatted neatly with no spelling errors and utilization of good grammer.  Make sure your code is well commented to explain what is happening throughout.  Submit the .cpp file for your program to this assignment."



ANY help is greatly appreciated. This is the last assignment I have to make up for this class and have largely taught myself c++ in about a week or so. >>;;



// Savannah Thames - June Makeup 2015
// LM 10 Assignment 26 - War Card Game

#include
#include
using namespace std;

//Function "Rules": Prints rules of the gameĀ 
void Rules()
{
cout
Thankyou!

Post a Reply

Replies

Oldest  Newest  Rating
0 Savannah Thames · July 1, 2015
// Savannah Thames - June Makeup 2015
// LM 10 Assignment 26 - War Card Game

#include
#include
using namespace std;

//Function "Rules": Prints rules of the gameĀ 
void Rules()
{
cout
0 Savannah Thames · July 1, 2015
Alright, sorry to do this but my code keeps getting truncated by that black box. Here's to the second try: 

// Savannah Thames - June Makeup 2015
// LM 10 Assignment 26 - War Card Game

#include <iostream>
#include <string>
using namespace std;

//Function "Rules": Prints rules of the game 
void Rules()
{
cout << "\n\t\t\t\t  War Rules  \n";
cout << "________________________________________________________________________________\n";

cout << "\nThe Deal:" << endl;
cout << "\tThe deck is shuffled and divided evenly. "
<< "Each player receives 26 cards\n\tdealt one at a time, " 
<< "face down. Anyone may deal first. Each player\n"
<< "\tplaces their stack, face down, in front of them.\n";

cout << "\nThe Object:" << endl;
cout << "\tBe the first player to win all 52 cards.\n";

    cout << "\nThe Play:" << endl;
cout << "\tEach player turns up a card at the same time and the"
<< " player with the\n\thigher card takes both cards and "
<< "places them, face down, on the bottom\n\tof their stack"
<< ". If the cards are the same rank, it is War. "
<< "Each player\n\tturns one card face down and one card "
<< "face up. The player with the\n\thigher of the cards "
<< "takes both piles (six cards). If the upturned "
<< "cards\n\tare again the same rank, each player "
<< "places another card face down\n\tand turns another "
<< "card face up. The player with the higher card "
<< "takes\n\tall 10 cards, and so on. The game ends when "
<< "one player has won all of\n\tthe cards.\n" << endl;

cout << "________________________________________________________________________________\n" << endl;
}

//Function "GetCardVal"
int GetCardVal(string inputCard)
{
int cardValue;
try
{
if (!inputCard.compare("J")) cardValue = 11;
else if (!inputCard.compare("Q")) cardValue = 12;
else if (!inputCard.compare("K")) cardValue = 13;
else if (!inputCard.compare("A")) cardValue = 14;
else cardValue = stoi(inputCard);
}
catch (invalid_argument)
{
cout << "Enter a value between 2-10 or J,Q,K, or A!!";
}
return cardValue;
}

//Function compares scores and declares winner or tie
void Win_Lose_Tie()
{
int player1Score, player2Score;

if (player1Score > player2Score)
cout << "Player 1 is the winner!";

else if (player1Score < player2Score)
cout << "Player 2 is the winner!";

else
cout << "It's a tie!";
}
// Function "main" contains game of war 
int main()
{
int player1Score = 0;
int player2Score = 0;

//calls function rules
Rules();

//for loop that starts game and runs it through 26 times
int game = 0;
for (game; game < 26; game++)
{
//Ask Player 1 for an input
cout << "Player 1, please enter your card: ";
// cin >> ... ;

//Translate that input into a value with GetCardVal()


//Ask Player 2 for an input
cout << "Player 2, please enter your card: ";
// cin >> ... ;

//Translate that input into a value with GetCardVal()

//Compare values and give someone a point if theirs is higher (func??)
//If the value equal add no points to anybody and loop again
}

//Game ends 

//Function that compares scores and declares a winner or tie

system("PAUSE");
return 0;
}
0 Bucky Roberts · July 1, 2015
Which part are you having trouble with?
0 Cardinal Coog · July 4, 2015
Who is actually using :

system("PAUSE");

return 0;

You give me a headache.
0 Cardinal Coog · July 4, 2015
That code has so many problems. Not sure where to begin.
0 Ben Elson · July 6, 2015
Ok I have been thinking this through I'm very new to C++ so forgive me.  I have modified your code as such.

// Savannah Thames - June Makeup 2015
// LM 10 Assignment 26 - War Card Game

#include <iostream>
#include <string>
using namespace std;

//Function "Rules": Prints rules of the game
void Rules()
{
cout << "\n\t\t\t\t War Rules \n";
cout << "________________________________________________________________________________\n";

cout << "\nThe Deal:" << endl;
cout << "\tThe deck is shuffled and divided evenly. "
<< "Each player receives 26 cards\n\tdealt one at a time, "
<< "face down. Anyone may deal first. Each player\n"
<< "\tplaces their stack, face down, in front of them.\n";

cout << "\nThe Object:" << endl;
cout << "\tBe the first player to win all 52 cards.\n";

cout << "\nThe Play:" << endl;
cout << "\tEach player turns up a card at the same time and the"
<< " player with the\n\thigher card takes both cards and "
<< "places them, face down, on the bottom\n\tof their stack"
<< ". If the cards are the same rank, it is War. "
<< "Each player\n\tturns one card face down and one card "
<< "face up. The player with the\n\thigher of the cards "
<< "takes both piles (six cards). If the upturned "
<< "cards\n\tare again the same rank, each player "
<< "places another card face down\n\tand turns another "
<< "card face up. The player with the higher card "
<< "takes\n\tall 10 cards, and so on. The game ends when "
<< "one player has won all of\n\tthe cards.\n" << endl;

cout << "________________________________________________________________________________\n" << endl;
}

//Function "GetCardVal"
int GetCardVal(string inputCard)
{
int cardValue;
try
{
if (!inputCard.compare("J")) cardValue = 11;
else if (!inputCard.compare("Q")) cardValue = 12;
else if (!inputCard.compare("K")) cardValue = 13;
else if (!inputCard.compare("A")) cardValue = 14;
else cardValue = stoi(inputCard);
}
catch (int invalid_argument)
{
cout << "Enter a value between 2-10 or J,Q,K, or A!!";
}
return cardValue;
}

//Function compares scores and declares winner or tie
void Win_Lose_Tie()
{
int player1Score, player2Score;

if (player1Score > player2Score)
cout << "Player 1 is the winner!";

else if (player1Score < player2Score)
cout << "Player 2 is the winner!";

else
cout << "It's a tie!";
}
// Function "main" contains game of war
int main()
{
int player1Score = 0;
int player2Score = 0;

//calls function rules
Rules();
cout << "Press Enter to start the game.";
cin.get();
//for loop that starts game and runs it through 26 times
int game = 0;
for (game; game < 26; game++)
{
//Ask Player 1 for an input
cout << "Player 1, press Enter for card: ";

    int pOneCard = 2 + rand()%13;
    cout << pOneCard << endl;
    cin.get();

//Translate that input into a value with GetCardVal()


//Ask Player 2 for an input
cout << "Player 2, press Enter for Card: ";

    int pTwoCard = 2 + rand()%13;
    cout << pTwoCard << endl;
cin.get();

    if (pOneCard > pTwoCard) {
        cout << "Player1 wins this hand!" << endl;
        player1Score ++;}

    else if (pTwoCard > pOneCard){
        cout << "Player2 wins this hand!" << endl;
        player2Score ++;}
    else if (pOneCard == pTwoCard){
        cout << "This hand is a push." << endl;
       }

}
cout  <<  "Player 1 got " << player1Score << "  hands" << endl;
cout << "Player 2 got " <<player2Score << "  hands" << endl;
//Translate that input into a value with GetCardVal()

//Compare values and give someone a point if theirs is higher (func??)
//If the value equal add no points to anybody and loop again


//Game ends

//Function that compares scores and declares a winner or tie

cin.get();
return 0;
}

This code does not yet meet all the requirements set out by your rules section, I haven't figured out how to run the war sequence when the randoms make a tie.  I also think we need to seed a random before we just run the rest of the program or the pattern could become predictable.  The problem is I'm not sure where to work the srand into the code to get it to just start the program like that.  I hope I have helped some, as I stated at the beginning of the post I'm very new to this C++ and your project seemed like a good one to get my learning going.  There is still a lot more to do.
  • 1

C++

107,272 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator