question about a card game.....

Hi guys,
          I have a small problem here...... while practicing my c++ I came across one card problem...and I tried to write a programme in c++ which I pulled a little bit for I am not able to find rest of the logic which is making my programme not very logical.....Here the situation is like this........    A person is  playing three-card monty, with one red card and two black cards, all face-down. If he pick the red card, he gets $100. After picking a card, the dealer pulls away one of the other two cards, and
it is black. He then offers to let the player stay with his choice, or switch to
the remaining card.

I did everything but could not find logic for " delear taking away one black card..."  Please help me as much as you can and lets try to find logic for this.....

Thank you guys and thanks a lot in advance... I have attached my programme .......below....


#include<iostream>
#include<string>
#include<conio.h>

using namespace std;

void swap(string& a, string& b)
{  string temp;
   temp = a;
   a = b;
   b = temp;
}

int three_card_monte(string& card1, string& card2, string& card3)
{  swap(card1, card2);
   swap(card2, card3);
   swap(card1, card3);

   if (card1 == "red")
      return 1;
   else if (card2 == "red")
      return 2;
   else 
      return 3;
}

int main()
{  string first = "black";
   string second = "red";
   string third = "black";
   int guess;
   int location;
   int secondguess;
   char userchoice;
         
   location = three_card_monte(first, second, third);
   
   cout << endl;
   cout << "Operator: Point me the red card. Which one is it ( 1, 2 or 3 ) ? ";
   cin >> guess;

   if (guess == location)
      
   {
     cout << endl;
	 cout << "Operator: OK. You chose card no." << guess << endl;
	 cout << endl;
	 cout << "Operator: So you want to stay with the same card or switch to the other one." << endl;
	 cout << "I will take away one black card.";
	 cout << endl << endl;
	 cout << "Operator: Please press Y if you want to switch and N if you want to stay with the same card:  ";
   }
   else 
   {
	 cout << endl;   
	 cout << "Operator: OK. You chosed card no." << guess << endl << endl;
	 cout << "Operator: So you want to stay with the same card or switch to the other one." << endl;
	 cout <<  "I wil take away one black card."; 
     cout << endl;
	 cout << endl;
	 cout << "Operator: Please press Y if want to switch and N if you want to stay:  ";
   }
     cin >> userchoice;
    
     switch(userchoice)

	 {
	 case 'n':
  		  
	   if (guess == location)
	   
	   {
       cout << endl;
	   cout << "Operator: Congratulations! You have won $100.00" << " You chose card no." << guess << " and it is the red card.";
       }
	    
	   else if (guess != location)
		     {
			  cout << endl;
			  cout << "Operator: No this is not the red card.Sorry...You loose the game.";
			  cout << endl;
			  cout << "Operator: The red card is: " << location;
           	 }
	   break;
   

	 case 'y':
		 
     cout << endl;    
	 cout << "Operator: What number you want to go with this time.Please enter the number: " << endl;
         
     cin >> secondguess;
  
	 if (secondguess == location)

	  cout << "Operator: Congratulations!" << "You chose the Red card. You chose card no." << secondguess << endl;
         
     else
		 cout << "Operator: You chose card no." << secondguess << "\tBetter luck next time, the Red card was number " << location << "\n";
   
   break;
   
   }
    getch();
}

Open in new window

SambhoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SuperdaveCommented:
I'm not sure about the rules to the game, but unless the dealer is supposed to be cheating, I think he must randomly pick one of the two cards you didn't choose, without knowing whether it's black.  If it's red, you would instantly lose, and if it's black, you get the chance to change your pick.  The chances are 50% whether you change your pick or not.  So pick a card randomly (or always pick the same one, if you want to keep it deterministic like the rest of the program).  Then ask about the swap if it is black or announce that the player lost if it is red.

By the way, the three swaps at the beginning of three_card_monte could be replaced with swap(card2,card3) for the same effect.
0
mr_stevieCommented:
Hello.

I'm not sure if this is what you want but you can add a while loop just before the "cin >> guess" and after the switch case.

You must add some kind of boolean variable that will check whether the game has finished or not. It will continue to loop if the player still wants to play.

You must also change the "Y" case in the switch and remove the secondguess variable.

Majority of the code was done. So I've provided you the code with the small modifications.

Hope this helps
#include<iostream>
#include<string>
#include<conio.h>

using namespace std;

void swap(string& a, string& b)
{  string temp;
   temp = a;
   a = b;
   b = temp;
}

int three_card_monte(string& card1, string& card2, string& card3)
{  swap(card1, card2);
   swap(card2, card3);
   swap(card1, card3);

   if (card1 == "red")
      return 1;
   else if (card2 == "red")
      return 2;
   else 
      return 3;
}

int main()
{  string first = "black";
   string second = "red";
   string third = "black";
   int guess;
   int location;
   char userchoice;
   bool finished = false;
         
   location = three_card_monte(first, second, third);
   
   cout << endl;
   cout << "Operator: Point me the red card. Which one is it ( 1, 2 or 3 ) ? ";
   
	
   while( finished == false )
   {
	   cin >> guess;

	   if (guess == location)
	      
	   {
		 cout << endl;
			 cout << "Operator: OK. You chose card no." << guess << endl << endl;
			 cout << "Operator: So you want to stay with the same card or switch to the other one." << endl;
			 cout << "I will take away one black card.";
			 cout << endl << endl;
			 cout << "Operator: Please press Y if you want to switch and N if you want to stay with the same card:  ";
	   }
	   else 
	   {
			 cout << endl;   
			 cout << "Operator: OK. You chosed card no." << guess << endl << endl;
			 cout << "Operator: So you want to stay with the same card or switch to the other one." << endl;
			 cout <<  "I wil take away one black card."; 
			 cout << endl << endl;
			 cout << "Operator: Please press Y if want to switch and N if you want to stay:  ";
	   }
		 cin >> userchoice;
	    
		 switch(userchoice)
		 {
			 case 'n':
			          
				if (guess == location)
				{
					cout << endl;
					cout << "Operator: Congratulations! You have won $100.00" << " You chose card no." << guess << " and it is the red card.";
				}
				else if (guess != location)
				{
					cout << endl;
					cout << "Operator: No this is not the red card.Sorry...You loose the game.";
					cout << endl;
					cout << "Operator: The red card is: " << location;
				}
			   break;


			 case 'y':
			         
				cout << endl;    
				cout << "Operator: What number you want to go with this time. Please enter the number: " << endl;
				break;

		 }
   }

    getch();
}

Open in new window

0
mr_stevieCommented:
You might also want to add some error handling such as if the player puts a capital N or Y instead of a lower case.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Infinity08Commented:
Three card monte is a con game ... it is not a game of chance. So, I assume that what you actually want to implement, is a straight version of the game, where the player sees 3 face-down cards, and has to find the target card (no turning over of a card, and no shuffling of the cards, which is normal in the "standard" three card monte).

In that case, this :

>> The chances are 50% whether you change your pick or not.

Is not true. It's counter-intuitive, but you should always change your choice when offered, in order to increase your chances of winning.

Read more on this here :

        http://en.wikipedia.org/wiki/Monty_Hall_problem
0
SambhoAuthor Commented:

Thank a lot guys,
                                I apreciate your help............Yes, as Infinity 08 said, that is the way three card monte is played but here, I want to push my logic little bit..... Here what I want to do is after customer picks the first card. He doesn't know he picked red or black card( red is the winning card). Now, what dealer does is he removes a black card infront of the customer. So now everybody knows the black card is removed. Now, the customer is asked to stay with the same card or switch.......

                              So, guys what I want is logic for taking away a black card among three cards( two are black and one is red) so that customer is left with just two cards( one he already selected and another left one)..

 Thank You guys in advance....................

0
Infinity08Commented:
As I said, that's precisely what the Monty Hall problem is about.

It's quite simple to implement too. The software knows which cards are black and which one is red, so it simply picks a black one among the two that are not selected by the player. There are two possible cases :

(a) the player picked the red card. In that case you show one of the remaining two cards at random.

(b) the player picked a black card. In that case, you show the other black card.
0
SuperdaveCommented:
I'm curious what the actual rules to the game are.  Does the dealer lose if he loses track of the cards and picks a red one?  I would have assumed picking the red card means the dealer wins because it proves the player doesn't have it.  The rule of requiring the dealer to pick a black card gives the player a 2/3 chance of winning (whether or not the dealer keeps track of the cards) which doesn't make for much of a con game.
0
Infinity08Commented:
>> Does the dealer lose if he loses track of the cards and picks a red one?

There's no shuffling, so no way to lose track ;) Plus the dealer (the program in this case) knows perfectly well where the cards are ;)


>> The rule of requiring the dealer to pick a black card gives the player a 2/3 chance of winning

It doesn't, but I think you're mixing up two things.

The standard three card monte game is indeed a con game (often played on streets), it involves quick shuffling of the cards, often with sleights of hand, switching of cards without the player noticing, etc. The player will never actually pick a red card - the dealer makes sure of that.

The game being implemented here is slightly different though. It consists of three face-down cards that are not shown to the player. The player picks one of his choice. No matter which card the player picked, the dealer will turn over one of the two remaining cards, and show that it's a black one. The dealer will then ask the player whether he wants to stick with his original choice (without having seen it obviously), or switch to the other remaining card. Whatever the player chooses - that's his final choice. If it's a red card, the player wins. If it's black, he loses.
0
SambhoAuthor Commented:

Thank You Infinity,
                                  I am working on the logic you gave..................I will return to you guys very shortly..........


Thank You
Sugat Sapkota......
0
SambhoAuthor Commented:
Yes,
         INfinity08 .I tried to implement your logic  of randomly picking up one black card in case the player chose red card..............but my logic is not working ......

             

Can you give some hint on this one......

My logic was like this....


if (guess == location)
      
   {
     cout << endl;
	 cout << "Operator: OK..beautiful.. You chose card no." << guess << endl;
	 cout << endl;
	 cout << "Operator: So you want to stay with the same card or switch to the other one." << endl;
	 cout << "I will take away one black card.";
	               for( int i = 1; i <= 3; i++)
	                        {
							  if (i != guess)
								  cout << "I took the card no: " << i;
							      
	                        }

Open in new window

0
Infinity08Commented:
>> .I tried to implement your logic  of randomly picking up one black card in case the player chose red card.

Consider using the rand function for obtaining a random value. Make it return either 0 or 1, referring to the first or second of the remaining cards respectively.

        http://www.cplusplus.com/reference/clibrary/cstdlib/rand/

(check the code sample on that reference page)
0
SambhoAuthor Commented:

Thank you guys, especially infinity.......

I found some way around my problem..........................

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.