Thread: declaring a winner for this game

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    58

    declaring a winner for this game

    after the the while loop is finished, i need to declare the player who took the last chip the winner, but i can't figure out how to do that.
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <math.h>
    using namespace std;
    int main()
    
    { 
    int num_chips, chips_taken,max_available, initial_number_of_chips;  
    string player_1, player_2; 
    
    cout << "\nRules: The game starts with a pile of chips. Each player may only take at\n"; 
    cout << "most half of the chips. The player that gets the last chip wins. Good luck."
    		 
    	 << "\nPlayer 1 please enter your first name:"; 
    cin >> player_1;
    cout << "\nPlayer 2 please enter your first name:";
    cin >> player_2;
    
    cout <<"\nThe current player is " << player_1; 
    cout <<"\n How many chips you would like to start with, " << player_1; 
    cin >> initial_number_of_chips;
    chips_taken=0;
    num_chips=initial_number_of_chips-chips_taken;
    max_available=num_chips/2;
    while (num_chips>1)
    {
    
    	max_available=num_chips/2;
    	if(num_chips%2!=0)
    	{
    	(max_available)+=1;
    	}
    
    	cout<< "\n"<<player_1<<", it is your turn. There are "<<num_chips<<" available.\n"; 
    	cout<<"You may take up to "<<max_available<<". How many would you like to take?";
    	cin>>chips_taken;
    	while (chips_taken>(max_available) || chips_taken<1) 
    	{
    	cout<<"\nError: Invalid number of chips. Please try again,";
    	cin>>chips_taken;
    	}
    	if (chips_taken <=num_chips/2 || chips_taken>=1)
    	{
    	num_chips=num_chips-chips_taken;
    	max_available=num_chips/2;
    	if(num_chips%2!=0)
    	{
    	max_available+=1;
    	}
    	
    	cout<<"\n"<<player_2<<", it is your turn. There are "<<num_chips<<" remaining.\n"; 
    	cout<<"You may take up to ";
    
    	cout<<max_available<<". How many would you like to take?";
    	cin>>chips_taken;
    	}
    	while (chips_taken>(max_available) || chips_taken<1) 
    	{
    	cout<<"\nError: Invalid number of chips. Please try again.";
    	cin>>chips_taken;
    	}
    	
    	if (chips_taken<=(num_chips)/2|| chips_taken>=1) 
    	{
    	num_chips=num_chips-chips_taken; 
    	}
    	
    
    
    }
    return 0;
    }
    any ideas? thanks for any help.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    A courtesy to those of us who might be interested in helping you would be for you to explain the premise of the game without requiring us to reverse engineer that out of your code.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    sorry about that. the game starts with a player selecting an initial number of chips to start with. he then selects how many he wants to take from that pile. then player two takes some, then player 1, and so on, until someone takes the last chip. the rules are that no player can take more than half the available amount and they must take at least 1. whoever grabs the last chip wins.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    While I'm reviewing your code, I see an error right off the bar. With this logic:
    Code:
    max_available = num_chips / 2  ;
    you will lose chips. For instance, if you have 3, and remove 1, you'll have 1 left.

    To fix, do this:
    Code:
    max_available = num_chips - (num_chips / 2 ) ;
    If you don't understand the difference, work it out on paper. Remember, you are doing integer division which means you lose any remainder.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Oh, never mind on that. I see you chose to follow it up with a modulo. That'll work if you want to do it that way.
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    OK, with your current logic, you cannot tell which player has taken more chips, because you are using the same variable (chips_taken) for both player 1 and player 2. You need another variable.
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    ok, that makes sense. i'll work on fixing that and see if i can make it work. thanks a lot for your help, i've been pulling my hair trying to think of something.

  8. #8
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Notice how similar the chunks of code beginning with
    Code:
    cout<< "\n"<<player_1<<", it is your turn. There are "<<num_chips<<" available.\n";
    and
    Code:
    cout<<"\n"<<player_2<<", it is your turn. There are "<<num_chips<<" remaining.\n";
    are.
    Do you think there is some way to generalize this and reuse some of code that is repeated between the two blocks? Doing this will also help you accomplish what you are trying to do.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    so... find some way to take away player_1 and player_2 and replace it with one variable?

  10. #10
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Correct.
    Like use an array, so player_1 and player_2 will become player[0] and player[1], for example.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 10-20-2009, 09:39 AM
  2. non-lvalue error
    By zlb12 in forum C Programming
    Replies: 1
    Last Post: 04-17-2009, 10:43 AM
  3. Open-source Game Project
    By Glorfindel in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-24-2009, 01:12 AM
  4. Try my game
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 09-15-2004, 11:58 AM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM