Card Game War

I have spent way too much time trying to get my first assignment correct for my C++ programming class. I have put in a ton of work, but haven't successfully got the program to execute and finish without stopping and throwing an error. The program is to play a game of war, where both players are dealt 26 cards alternating from one player to the other. There has to be 4 vectors, and I have them listed below as the card pile and the battle deck for each player. Where the problem seems to be lying is when the test checks to see if the battle card is the same, then drawing two more cards and comparing the third card in the battle decks. If anyone can give me any advice on this I would appreciate it. Programming doesn't come naturally to me, and I have to really work hard to learn it, and this assignment has already received a zero for not being turned in on time. So your not helping me get the grade, I just need help understanding it.

Thanks in advance, the code is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector> 
using std::string;
using std::cout;
using std::cin;
using std::vector;
using namespace std;


int main(int argc, char *argv[]) 
{
  vector<int> playerA_cards;  // Vector of ints. No size specified.
  vector<int> playerA_battleDeck;
  
  vector<int> playerB_cards;  // Vector of ints. No size specified.
  vector<int> playerB_battleDeck;
  
  int seed;  // Seed value for random number generator
  if (argc < 1) // Note: correct argc does not guarantee correct argv[] contents
  {
    printf("\nThis program requires a number parameter for seed.\n");
    printf("     Run the program by: <executable> <integer seed>\n");
    exit(0);  // Exit program if it wasn't run with correct parameter
  }
  cout<< "Seed is " << argv[1] << endl; 
  seed = atoi(argv[1]);  
  srand ( seed );  

  for (int i = 0; i < 52; i++)  // 26 cards in my hand of cards
  {
    int newCard = (rand() % 13) + 2;  // This is the card drawn from the deck
    int even = i;
    if (even %2==0)
      playerA_cards.push_back( newCard );  // Put the new card into A's hand
    else
      playerB_cards.push_back( newCard );  // Put the new card into B's hand
  }
  
 
    
  cout<< "Player A cards are: ";
  for (auto iterator : playerA_cards)
    cout << ' ' << iterator;
  
  cout<<endl;
    
  cout<< "Player B cards are: ";
  for (auto iterator : playerB_cards)
    cout << ' ' << iterator;
    
  cout<<endl<<endl;
  
  //int placeHolder=1;
  int round = -1;
  
  while (playerA_cards.size() != 0 && playerB_cards.size() != 0)
  {	
	round++;
	
	
	playerA_battleDeck.push_back(playerA_cards[0]); 
    playerB_battleDeck.push_back(playerB_cards[0]);
	playerA_cards.erase(playerA_cards.begin());
    playerB_cards.erase(playerB_cards.begin());
	
    if (playerA_battleDeck[0] != playerB_battleDeck[0])
    {
      cout<< "Round "<<round<<endl;
      cout<< "Player A cards are: ";
      for (auto iterator : playerA_cards)
        cout << ' ' << iterator;
      cout<<endl;
      cout<< "Player A battle stack is: ";
      for (auto iterator : playerA_battleDeck)
        cout << ' ' << iterator;
      cout<<endl;
      cout<< "Player B cards are: ";
      for (auto iterator : playerB_cards)
        cout << ' ' << iterator;
      cout<<endl;
      cout<< "Player B battle stack is: ";
      for (auto iterator : playerB_battleDeck)
        cout << ' ' << iterator;
      cout<<endl;
     
	  if (playerA_battleDeck[0] > playerB_battleDeck[0])
	  {
		playerA_cards.push_back(playerA_battleDeck[0]);
		playerA_cards.push_back(playerB_battleDeck[0]);
	  }	
	  if (playerB_battleDeck[0] > playerA_battleDeck[0])
	  {
		playerB_cards.push_back(playerB_battleDeck[0]);
		playerB_cards.push_back(playerA_battleDeck[0]);
	 
	  }
	}
    else if (playerA_battleDeck[0] = playerB_battleDeck[0])
    {
    
		{
		playerA_battleDeck.push_back(playerA_cards[1]);
		playerB_battleDeck.push_back(playerB_cards[1]);
		playerA_battleDeck.push_back(playerA_cards[2]);
		playerB_battleDeck.push_back(playerB_cards[2]);
            
		playerA_cards.erase(playerA_cards.begin());
		playerB_cards.erase(playerB_cards.begin());
		playerA_cards.erase(playerA_cards.begin()+1);
		playerB_cards.erase(playerB_cards.begin()+1);
		playerA_cards.erase(playerA_cards.begin()+2);
		playerB_cards.erase(playerB_cards.begin()+2);
      
			
		if (playerA_battleDeck[2] > playerB_battleDeck[2])
		  {
		  
			cout<< "Round "<<round<<endl;
			cout<< "Player A cards are: ";
			for (auto iterator : playerA_cards)
				cout << ' ' << iterator;
			cout<<endl;
			cout<< "Player A battle stack is: ";
			for (auto iterator : playerA_battleDeck)
				cout << ' ' << iterator;
			cout<<endl;
			cout<< "Player B cards are: ";
			for (auto iterator : playerB_cards)
				cout << ' ' << iterator;
			cout<<endl;
			cout<< "Player B battle stack is: ";
			for (auto iterator : playerB_battleDeck)
				cout << ' ' << iterator;
			cout<<endl;
			
			playerA_cards.push_back(playerA_battleDeck[0]);
			playerA_cards.push_back(playerA_battleDeck[1]);
			playerA_cards.push_back(playerA_battleDeck[2]);
			playerA_cards.push_back(playerB_battleDeck[0]);
			playerA_cards.push_back(playerB_battleDeck[1]);
			playerA_cards.push_back(playerB_battleDeck[2]);
		  }
		else if (playerB_battleDeck[2] > playerA_battleDeck[2])
		  {
		  
			cout<< "Round "<<round<<endl;
			cout<< "Player A cards are: ";
			for (auto iterator : playerA_cards)
				cout << ' ' << iterator;
			cout<<endl;
			cout<< "Player A battle stack is: ";
			for (auto iterator : playerA_battleDeck)
				cout << ' ' << iterator;
			cout<<endl;
			cout<< "Player B cards are: ";
			for (auto iterator : playerB_cards)
				cout << ' ' << iterator;
			cout<<endl;
			cout<< "Player B battle stack is: ";
			for (auto iterator : playerB_battleDeck)
				cout << ' ' << iterator;
			cout<<endl;
			
			playerB_cards.push_back(playerB_battleDeck[0]);
			playerB_cards.push_back(playerB_battleDeck[1]);
			playerB_cards.push_back(playerB_battleDeck[2]);
			playerB_cards.push_back(playerA_battleDeck[0]);
			playerB_cards.push_back(playerA_battleDeck[1]);
			playerB_cards.push_back(playerA_battleDeck[2]);
		  }  
		else if (playerA_battleDeck[2] == playerB_battleDeck[2])
		  {
			int j = 0;
			while (playerA_battleDeck[2+j] ==     playerB_battleDeck[2+j])
				
			{	 
				j+2;
				playerA_battleDeck.push_back(playerA_cards[0]); //put first card in battle deck
				playerB_battleDeck.push_back(playerB_cards[0]); //put first card in battle deck
				playerA_battleDeck.push_back(playerA_cards[1]);
				playerB_battleDeck.push_back(playerB_cards[1]);
				playerA_battleDeck.push_back(playerA_cards[2]);
				playerB_battleDeck.push_back(playerB_cards[2]);
            
				playerA_cards.erase(playerA_cards.begin());
				playerB_cards.erase(playerB_cards.begin());
				playerA_cards.erase(playerA_cards.begin()+1);
				playerB_cards.erase(playerB_cards.begin()+1);
				playerA_cards.erase(playerA_cards.begin()+2);
				playerB_cards.erase(playerB_cards.begin()+2);
				
				if (playerA_cards.size() == 0 or playerB_cards.size() == 0)
					break;
			}
		  
		
			
			cout<< "Round "<<round<<endl;
			cout<< "Player A cards are: ";
			for (auto iterator : playerA_cards)
				cout << ' ' << iterator;
			cout<<endl;
			cout<< "Player A battle stack is: ";
			for (auto iterator : playerA_battleDeck)
				cout << ' ' << iterator;
			cout<<endl;
			cout<< "Player B cards are: ";
			for (auto iterator : playerB_cards)
				cout << ' ' << iterator;
			cout<<endl;
			cout<< "Player B battle stack is: ";
			for (auto iterator : playerB_battleDeck)
				cout << ' ' << iterator;
			cout<<endl;
			
		  }
		 else
			cout<<"Error"<<endl;
		}
		
	}
    playerA_battleDeck.clear();
    playerB_battleDeck.clear();
  }        
 
  if (playerB_cards.size()==0)
    cout<< "Player A wins!!!"<<endl<<endl;
  else if (playerA_cards.size()==0)
    cout<< "Player B wins!!!"<<endl<<endl;
  else if (playerA_cards.size()==playerB_cards.size())
	cout<< "Player A and B tie!!!"<<endl<<endl;
  else
	cout<< "Didn't finish correctly";
  


}
Last edited on
I just figured how to post this in code format above, hope this helps. I'm working on it right now, hoping someone might be on at this time of night to assist in anyway possible.
A possible error I see is
1
2
3
playerA_cards.erase(playerA_cards.begin());
playerA_cards.erase(playerA_cards.begin()+1);
playerA_cards.erase(playerA_cards.begin()+2);

and related sections.

When you remove the first card, the second card becomes the first. Doing
playerA_cards.erase(playerA_cards.begin());
three times is I believe what you meant to do.
Your right, I'm working on conditionals right now, and I'll look at that too. Thank you for responding.
Line 21:
1
2
  // if (argc < 1) // Note: correct argc does not guarantee correct argv[] contents
  if (argc < 2 ) // argc will be at least 1, argv[0] is always passed 


Line 100:
1
2
    // else if (playerA_battleDeck[0] = playerB_battleDeck[0])
    else if (playerA_battleDeck[0] == playerB_battleDeck[0]) // you did mean ==, didn't you? 


Line 179:
1
2
				// j+2;
				j += 2 ; // did you intend this? 



Line 194:
1
2
3
		if (playerA_cards.size() == 0 or playerB_cards.size() == 0)
                // not wrong, but this is usually written as: 
                // if (playerA_cards.size() == 0 || playerB_cards.size() == 0 


Now run your program (after making the corrections) and try to debug it.

A 200+ line main()!
Do you know how to write functions?
I do, but this assignment was to not use functions. Let me look over your post for a minute and see what I can do. On the original code I posted earlier there were some issues I'm working on already, like the one Yay295 commented on.
Thanks for the pointers on this Yay295 and JLBorges. I made sure I fixed my silly mistakes, and JLBorges the points you made were correct, I did mean

 
if (argc < 2 )


and I did mean "==" and "j+=2"

Also, I made sure I sticked to using "||" instead of "or" for viewability.

The problem seemed to come from me not having enough conditional statements. By putting more checks on the program it functioned. I had to add in more if statements to check for different conditions.

I understand that this would be much easier if I was using functions, but the assignment was to not use functions. Once again, thanks for the help.
Last edited on
Topic archived. No new replies allowed.