Apr 29, 2011 at 9:01pm UTC
hi, i wrote the main() for testing my game, but the while only execute once, it should keep looping until one player has no card on the container.
Can any body help me figure out why?
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
#include<iostream>
#include<algorithm>
#include<cassert>
#include<vector>
#include "Card.h"
#include "Random.h"
#include "time.h"
#include <fstream>
using namespace std;
int main(){
srand(time(NULL));
Deck theDeck;
theDeck.shuffle();
Player Player1;
Player Player2;
//ofstream myfile;
//myfile.open("Log File.txt");
for ( int i = 0; i< 52; i++){
if (i%2 == 0)
{
Player1.addCard(theDeck.deal());
}
else
Player2.addCard(theDeck.deal());
}
cout<<"Player1 has Poker: " <<Player1.myPokerSize()<<endl;
cout<<"Player2 has Poker: " <<Player2.myPokerSize()<<endl;
bool finish = false ;
int tieCounter = 0;
int battleNumber = 1;
while (!finish)
{
battleNumber ++;
cout<<"Player1's Card: " <<(Player1.myPoker).at(0).getRank()<<endl;
cout<<"Player2's Card: " <<(Player2.myPoker).at(0).getRank()<<endl;
if ((Player1.myPoker).at(0).getRank()== (Player2.myPoker).at(0).getRank())
{
myfile<<"Game Tie\n" <<endl;
cout<<"Game Tie\n" <<endl;
tieCounter++;
}
else if ((Player1.myPoker).at(0).getRank() > (Player2.myPoker).at(0).getRank())
{
(Player1.myPoker).insert((Player1.myPoker).end(), (Player2.myPoker).at(0));
(Player1.myPoker).insert((Player1.myPoker).end(), (Player1.myPoker).at(0));
(Player1.myPoker).erase((Player1.myPoker).begin());
(Player2.myPoker).erase((Player2.myPoker).begin());
cout<<"Player1 won the Battle\n" <<endl;
cout<<"player1 has cards" <<Player1.myPokerSize()<<endl;
}
else
{
(Player2.myPoker).insert((Player2.myPoker).end(), (Player1.myPoker).at(0));
(Player2.myPoker).insert((Player2.myPoker).end(), (Player2.myPoker).at(0));
(Player2.myPoker).erase((Player2.myPoker).begin());
(Player1.myPoker).erase((Player1.myPoker).begin());
cout<<"Player2 won the Battle\n" <<endl;
cout<<"player2 has cards " <<Player2.myPokerSize()<<endl;
}
finish = (Player1.myPokerSize() < 0 || Player2.myPokerSize()< 0);
finish = true ;
}
myfile.close();
return 0;
}
myPoker is a container for holding the card for player
thanks
Last edited on Apr 29, 2011 at 9:02pm UTC
Apr 29, 2011 at 9:05pm UTC
Delete line 73. And you already had another thread that was just fine, don't post extra threads for the same problem.
Last edited on Apr 29, 2011 at 9:05pm UTC
Apr 29, 2011 at 9:06pm UTC
On line 73 you specifically change the value of finish to be true . It does not change again because this is the last statement in the loop so the next time you approach the condition in the loop it will definitely be false.
Apr 29, 2011 at 9:32pm UTC
i just delete the line 73, but shows in infinitely loop
how can i fix that ? can you tell me more detail
thanks