# Card game - war - questions

• 08-27-2012
hxrsmurf
Card game - war - questions
Hi, cprogramming forum!

I am working on a really basic war card game in my free time and was hoping you folks could help me! Currently, I just have a basic counter and working on the tie system. However, currently the tie system continue to repeats the three cards, for instance
Code:

```Player 1 || Player 2 17            17 17            17 17            17```
I was thinking this could be because of the card shuffle itself, but I think it might be just my bad do loop handling. I know my code is messy, but I understand it :)

I use gedit with g++ compiler. I have used Kate in the past (so the lines are more organized and nested)

Thanks for the help.

Code:

``` //Written by Kevin Murphy 2012 //War card game is where two people play and if one card is greater that player gets the card. If they both tie, depending on how one plays, the two players throw down three more cards and the fourth car decides who gets that stash!  #include <iostream> #include <stdlib.h> #include <time.h> using namespace std;  int main() {  system("clear");  int cards[52][2];//Creates 2 arrays for cards  int x,y,currentcard,currentsuit,random,temp,player1=0,player2=0,numberofcards,ties=0,tie_cards,tie_count=0;  string next;  for (x=0;x<52;x++)  for (y=0;y<2;y++)  {    cards[x][y] = x + 1; //Fills both arrays with 52 "cards"  } // //Randomize the cards //Deck 1  for (x=0;x<52;x++)  {    random=rand()%52;//Random numbers of 52, set to random    temp=cards[random][0];//sets cards random (the array random) into a temp.    cards[random][0]=cards[x][0];//flips    cards[x][0]=temp;//the cards  } //Deck 2  for (x=0;x<52;x++)  {    random=rand()%52;//Random numbers of 52, set to random    temp=cards[random][1];//sets cards random (the array random) into a temp.    cards[random][1]=cards[x][1];//flips    cards[x][1]=temp;//the cards  } //Randomize the cards // do {  for (x=0;x<52;x++)  {  //usleep(250000);  system("clear");  cout<<"||Player 1: "<<player1<<"||Player 2: "<<player2<<"||Ties:"<<ties<<"||"<<endl;  cout<<"||"<<cards[x][0]<<"||"<<cards[x][1]<<"||"<<endl;//Prints out the cars (from each deck)  if (cards[x][0]>cards[x][1])    {    player1++;  numberofcards--;  }  if (cards[x][0]<cards[x][1])    {    player2++;  numberofcards--;  }    if (cards[x][0]==cards[x][1])    {    do  {  cout<<"||"<<cards[x][0]<<"||"<<cards[x][1]<<"||"<<endl;//Prints out the cars (from each deck)  numberofcards--;  tie_count++;  } while (tie_count<3);  cout<<"||"<<cards[x][0]<<"||"<<cards[x][1]<<"||"<<endl;//Prints out the cars (from each deck)  ties++;  }  } } while (numberofcards<52); return 0; }```
• 08-27-2012
jimblumberg
In the following code:
Code:

```  do   {   cout<<"||"<<cards[x][0]<<"||"<<cards[x][1]<<"||"<<endl;//Prints out the cars (from each deck)   numberofcards--;   tie_count++;   } while (tie_count<3);```
You will print the same three cards because you never change any value you are using for printing.

Jim
• 08-27-2012
AndiPersti
Wrong forum (C++ is next door) and you've posted just on big line of code.

Code:

```//Written by Kevin Murphy 2012 //War card game is where two people play and if one card is greater that player gets the card. If they both tie, depending on how one plays, the two players throw down three more cards and the fourth car decides who gets that stash! #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; int main() {   system("clear");   int cards[52][2];//Creates 2 arrays for cards   int x,y,currentcard,currentsuit,random,temp,player1=0,player2=0,numberofcards,ties=0,tie_cards,tie_count=0;   string next;   for (x=0;x<52;x++)   for (y=0;y<2;y++)   {     cards[x][y] = x + 1; //Fills both arrays with 52 "cards"   } // //Randomize the cards //Deck 1   for (x=0;x<52;x++)   {     random=rand()%52;//Random numbers of 52, set to random     temp=cards[random][0];//sets cards random (the array random) into a temp.     cards[random][0]=cards[x][0];//flips     cards[x][0]=temp;//the cards   } //Deck 2   for (x=0;x<52;x++)   {     random=rand()%52;//Random numbers of 52, set to random     temp=cards[random][1];//sets cards random (the array random) into a temp.     cards[random][1]=cards[x][1];//flips     cards[x][1]=temp;//the cards   } //Randomize the cards // do {   for (x=0;x<52;x++)   {   //usleep(250000);   system("clear");   cout<<"||Player 1: "<<player1<<"||Player 2: "<<player2<<"||Ties:"<<ties<<"||"<<endl;   cout<<"||"<<cards[x][0]<<"||"<<cards[x][1]<<"||"<<endl;//Prints out the cars (from each deck)   if (cards[x][0]>cards[x][1])    {   player1++;   numberofcards--;   }   if (cards[x][0]<cards[x][1])    {   player2++;   numberofcards--;   }    if (cards[x][0]==cards[x][1])    {   do   {   cout<<"||"<<cards[x][0]<<"||"<<cards[x][1]<<"||"<<endl;//Prints out the cars (from each deck)   numberofcards--;   tie_count++;   } while (tie_count<3);   cout<<"||"<<cards[x][0]<<"||"<<cards[x][1]<<"||"<<endl;//Prints out the cars (from each deck)   ties++;   }   } } while (numberofcards<52); return 0; }```
Some problems:
-) Awful indentation
-) In your tie-loop (line 59-64) you never change the cards.
-) You don't initialize numberofcards.
-) You decrement numberofcards several times and thus the condition in your main do-while-loop is probably wrong.

Bye, Andreas
• 08-27-2012
Salem
Moved.
• 09-04-2012
dwks
- You have two loops for initializing decks, but it could be combined into one loop wrapped with for(d = 0; d < 2; d ++).
- You should include <string> if you're using std::string. Also, try to use <cstdlib> instead of <stdlib.h>, <ctime> instead of <time.h>, etc. The .h versions are from C.
- You may want to avoid typing 52 all through your code and instead have say "const int CARDS = 52;".
- You can use std::setw() from <iomanip> to get your vertical bars "||" to line up (assuming they aren't lined up now).