simple game, need help with loops and math

This is a discussion on simple game, need help with loops and math within the C++ Programming forums, part of the General Programming Boards category; the game is: there are two players. one player selects how many chips there are in the pile to start ...

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

    simple game, need help with loops and math

    the game is: there are two players. one player selects how many chips there are in the pile to start with. then he takes some chips, then player two takes some, then back to player 1, and so on until there is only 1 chip left. caveats are that each player can only take up to half of the available amount, and must take more than one. this is my code so far. i know it's sloppy and needs some neatening up and better wording, but i want to get it functioning properly first before i worry about all that. so i need the program to keep executing until there's just one chip, and whenever someone enters and invalid amount (say more than half of the available amount) they are repeatedly asked to input a different number. right now the two main problems i'm having are that it's not repeating itself, it's stopping (i'll point out where), and the problem with the math. ok here's the code and i'll have some comments at the bottom.
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <math.h>
    using namespace std;
    int main()
    
    { 
    int a, b, c, d, pile_size, turn_size, num_chips, initial_number_of_chips, chips_left;
    string player_1, player_2;
    
    cout << "\nRules: The game starts with a pile of chips. Eaech 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;
    while (a>1)
    {
    	cout<< "\nPlayer 1 it is your turn, how many chips would you like to take?";
    	cin>>a;
    	while (a>(initial_number_of_chips/2) || a<1)
    	{
    	cout<<"\nplease enter a different value";
    	cin>>a;
    	}
    
    	while (a <=initial_number_of_chips/2 || a>=1)
    	{
    	cout<<"\n There are" <<initial_number_of_chips-a<< "left";
    	cout<<"\n"<<player_2<<"it is your turn, please select how many chips you would like";
    	cin>>a;
    	}
    	while (a >(initial_number_of_chips-a)/2 || a<1)
    	{
    	cout<<"\nplease enter a different value";
    	cin>>a;
    	}
    	while (a <=(initial_number_of_chips-a)/2 || a>1)
    
    	{
    	
    	cout<<"\nThere are"<<initial_number_of_chips-a<<"left";  // at this point, it keeps repeating the loop for player 2 and not jumping back up to player 1
    	}
    	
    
    
    }
    return 0;
    }
    now with the math issue... i need the number of chips to be continually decreasing as the players take chips out of the pile. but i can't think of a way to do that. right now, every time either player inputs a value for a, the previous a is then overwritten, and the number of chips available basically resets.

    example: start with 200 chips.
    player 1 takes 50
    there are 150 left
    player 2 takes 70
    now it will go back to the initial 200 and subtract 70, giving me 130 left.

    i know this is a lot, but can anyone give me some tips? i'm a beginner at this and this is my first time trying a program with multiple loops in it, and it's not coming easy. also don't worry about all the int variables, i've been messing around with different things and just haven't deleted the ones i'm not using yet.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,524
    What's happening here is the result of initial_chips - a is calculated, displayed, and then thrown away, never to be reused. Have you learned the assignment statement?

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    No, I haven't heard of that. I will look into it though, thanks for the tip.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    alright i've changed my code a bit, still not getting what i want, but getting closer
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <math.h>
    using namespace std;
    int main()
    
    { 
    int a, b, c, d, pile_size, turn_size, num_chips, initial_number_of_chips, chips_left;
    string player_1, player_2;
    
    cout << "\nRules: The game starts with a pile of chips. Eaech 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;
    
    
    	cout<< "\nPlayer 1 it is your turn, how many chips would you like to take?";
    	cin>>a;
    	while (a>(initial_number_of_chips/2) || a<1)
    	{
    	cout<<"\nplease enter a different value";
    	cin>>a;
    	}
    
    	if (a <=initial_number_of_chips/2 || a>=1)
    	{
    	cout<<"\n There are" <<initial_number_of_chips-a<< "left";
    	};
    	num_chips=initial_number_of_chips-a;
    while (num_chips>1)
    {	   
    	cout<<"\n"<<player_2<<"it is your turn, please select how many chips you would like";
    	
    	cin>>a;
    	
    	num_chips=num_chips-a;
    	while (a >(num_chips)/2 || a<1)
    	{
    	cout<<"\nplease enter a different value";  //right here it's continually asking for a different value, i can't get past this step. should i use an if instead? 
    	cin>>a;
    	}
    	if (a <=(num_chips)/2 || a>1)
    
    	{
    	
    	cout<<"\nThere are"<<initial_number_of_chips-a<<"left";
    	
    	cout<<"\n"<<player_1<<"it is your turn. please select how many chips you would like";
    	cin>>a;
    	}
    	while (a>(num_chips)/2 || a<1)
    	cout<<"\nPlease enter a different value";
    	cin>>a;
    	}
    	if (a<=(num_chips)/2|| a>=1)
    	{
    	cout<< "test";
    	
    	
    
    
    }
    return 0;
    }

    the test at the bottom means nothing, just seeing if i could make it there.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    so still, my main issue is with the math functions not working correctly. i can't even test the loops completely because i don't have the math stuff down correctly. any ideas? since i'm using "num_chips" to store how many chips are still available, i need to be able to keep subtracting from that without it resetting like it has been.

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,403
    if does work, if you enter 20, 5, and then 5 for player 2 for example this will work, but what happens is it is flawed to start with because if a user enters an incorrect value to start with then your line where you subtract how many chips there are is still invoked and this falsely alters the number of remaining chips, even when the player has made an invalid choice.

    You should use a temporary variable to store the number of chips remaining, then if the choice is valid set the actual chips remaining to the temp value.

    also you subtract chips after the initial choice and then go into the loop, you also need to have control in the loop so that the test is against the actual number of remaining chips, not the number left over after any invalid choice was made
    Last edited by rogster001; 02-08-2010 at 07:24 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help using arrays and loops for math
    By LLINE in forum C++ Programming
    Replies: 3
    Last Post: 06-09-2008, 04:09 AM
  2. Makes me mad..
    By Warrax in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 05-01-2007, 07:49 AM
  3. Help! Right Math, Wrong Output
    By verd in forum C Programming
    Replies: 12
    Last Post: 03-15-2005, 06:49 PM
  4. How to do this? Arrays and math
    By brian0918 in forum C++ Programming
    Replies: 1
    Last Post: 08-11-2002, 11:08 PM
  5. Expressing loops etc. in math.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 07-22-2002, 04:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21