Function calls

I am trying to write a program that requires input validation through functions. The idea behind it is much like the 21 stones only it is with 13 and the computer always wins. The game starts with 13 stones and the computer will always choose 1 on the first turn creating a multiple of 4 scenario. This means if the user takes 3 computer takes 1, user takes 2 computer takes 2 and so on until no stones remain. My problem is I am having a hard time getting my head around functions and how data is called from the parameters within so any help with this would be greatly appreciated!

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
  /*******************************************************************************
The Stone Game...
Written By - Ralph C.
11/11/15
*******************************************************************************/

#include <iostream>
using namespace std;
//function prototypes
bool validPick(int numStones);
int computerPick(int stones_in_pile, int player2taken);
int playerPick(int stones_in_pile);

int main()
{
	int stones_left = 13, P1Taken, P2Taken;
	
	cout << "You have shosen to play the game 13 stones against me, the MIGHTY "
		 << "COMPUTER!\nThe object of the game is to take 1, 2 or 3 stones from"
		 << " the pile on your turn.\nThe player that removes the last stone "
		 << "or stones from the pile wins the game.\nGood Luck... You will need"
		 << " it! I NEVER LOOSE!!"
		 << endl << endl;
	
	computerPick(stones_left, P2Taken);
	playerPick(P1Taken);
	validPick(stones_left);
	
	//game logic here -- This is far from done.
	stones_left -= P1Taken;
	stones_left -= P2Taken;
	return 0;
}
/******************************************************************************\
* Validate the picked number 1-3 are only valid numbers to choose from.        *
\******************************************************************************/
bool validPick(int numStones)
{	
	if((numStones < 1) || (numStones >3))
		cout << "Invalid Selection. 1-3 is all you can have!";
	else
		return numStones;
}
/******************************************************************************\
* Computer's function calls. Should start with 1. We always want the computer  *
* to win the game.                                                             *
\******************************************************************************/
int computerPick(int stones_in_pile, int player2taken)
{
	if(player2taken == 0)
		stones_in_pile -= 1;
	else
	{
		if(player2taken == 1)
			stones_in_pile -= 3;
			else
				if(player2taken == 2)
					stones_in_pile -= 2;
						else
							stones_in_pile -=1;
	}
	return stones_in_pile;	
		
}
/******************************************************************************\
* Player's Pick function call goes here. The player goes second                *
\******************************************************************************/
int playerPick(int stones_in_pile)
{
	cout << "Please choose the ammount of stones. 1-3 only! : ";
	cin >> stones_in_pile;
	return stones_in_pile;
}
Last edited on
You need to evaluate the result:
1
2
3
4
5
6
	P2Taken = playerPick();
	P1Taken = computerPick(stones_left, P2Taken);
	if(validPick(stones_left))
{
...
}
Made suggested additions and it for the most part functions. Logic is screwed up however as it shows computers selections as the remainder of stones and when it reaches 0 the number just goes negative also the computer is not making a selection as intended.



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
/*******************************************************************************
The Stone Game...
Written By - Ralph C.
11/11/15
*******************************************************************************/

#include <iostream>
using namespace std;
//function prototypes
bool validPick(int numStones);
int computerPick(int stones_in_pile, int player2taken);
int playerPick(int stones_in_pile);

int main()
{
	int stones_left = 13, P1Taken, P2Taken;
	
	cout << "You have shosen to play the game 13 stones against me, the MIGHTY "
		 << "COMPUTER!\nThe object of the game is to take 1, 2 or 3 stones from"
		 << " the pile on your turn.\nThe player that removes the last stone "
		 << "or stones from the pile wins the game.\nGood Luck... You will need"
		 << " it! I NEVER LOOSE!!"
		 << endl << endl;

	P2Taken = playerPick(stones_left);
	P1Taken = computerPick(stones_left, P2Taken);
	
	do{
		if(validPick(stones_left >=1))
		{
			stones_left -= P2Taken;
			cout << "After taken " << P2Taken << " there are " << stones_left 
				 << " stones remaining.";
			cout << "The computer has taken " << P1Taken << " stones. Now only "
				 << stones_left << " stones remain.";
			playerPick(stones_left);
		}	
	}while(validPick(stones_left >=1));

/*	validPick(stones_left);
	computerPick(stones_left, P2Taken);
	playerPick(P1Taken);
	
	do{
	stones_left -= P1Taken;
	stones_left -= P2Taken;
	if
*/	
	return 0;
}
/******************************************************************************\
* Validate the picked number 1-3 are only valid numbers to choose from.        *
\******************************************************************************/
bool validPick(int numStones)
{	
	if((numStones < 1) || (numStones >3))
		{
		cout << "Invalid Selection. 1-3 is all you can have!";
		}
	else
		return numStones;
}
/******************************************************************************\
* Computer's function calls. Should start with 1. We always want the computer  *
* to win the game.                                                             *
\******************************************************************************/
int computerPick(int stones_in_pile, int player2taken)
{
	if(player2taken == 0)
		stones_in_pile -= 1;
	else
	{
		if(player2taken == 1)
			stones_in_pile -= 3;
			else
				if(player2taken == 2)
					stones_in_pile -= 2;
						else
							stones_in_pile -=1;
	}
	return stones_in_pile;	
		
}
/******************************************************************************\
* Player's Pick function call goes here. The player goes second                *
\******************************************************************************/
int playerPick(int stones_in_pile)
{
	cout << "Please choose the ammount of stones. 1-3 only! : ";
	cin >> stones_in_pile;
	return stones_in_pile;
}
Further editing done. started adding logic but having trouble getting calls to execute properly. Would be great if someone can point me in the correct direction.

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
/*******************************************************************************
written by ralph c
11/11/15
*******************************************************************************/

#include <iostream>
using namespace std;
//function prototypes
bool validPick(int numStones);
int computerPick(int stones_in_pile, int player2taken);
int playerPick(int stones_in_pile);

int main()
{
	int stones_left = 13, P1Taken, P2Taken;
	
	cout << "You have shosen to play the game 13 stones against me, the MIGHTY "
		 << "COMPUTER!\nThe object of the game is to take 1, 2 or 3 stones from"
		 << " the pile on your turn.\nThe player that removes the last stone "
		 << "or stones from the pile wins the game.\nGood Luck... You will need"
		 << " it! I NEVER LOOSE!!"
		 << endl << endl;

	P2Taken = playerPick(stones_left);
	P1Taken = computerPick(stones_left, P2Taken);
	
	do{
		if(validPick(stones_left >= 1) || validPick(stones_left <= 3))
		{
			playerPick(stones_left);
			cout << "The computer has taken " << P1Taken << " stones. Now only "
				 << stones_left << " stones remain.";
			
		
			stones_left -= P2Taken;
			cout << "After taken " << P2Taken << " there are " << stones_left 
				 << " stones remaining.";
		
		}	
	}while(validPick(stones_left >= 1) || validPick(stones_left <= 3));
	
	cout << "The computer wins!";
	return 0;
}
/******************************************************************************/
bool validPick(int numStones)
{	
	if((numStones <= 0) || (numStones >= 4))
		{
		cout << "Invalid Selection. 1-3 is all you can have!";
		return false;
		}
	else
		return true;
}
/******************************************************************************/
int computerPick(int stones_in_pile, int player2taken)
{
	if(player2taken == 1)
		return 3;
		else
			if(player2taken == 2)
				return 2;
				else
					return 1;
}
/******************************************************************************/
int playerPick(int stones_in_pile)
{
	cout << "Please choose the ammount of stones. 1-3 only! : ";
	cin >> stones_in_pile;
	return stones_in_pile;
}
And more editing. starting to come together. The logic is still glitchy
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
/*******************************************************************************
written by ralph c
11/11/15
*******************************************************************************/

#include <iostream>
using namespace std;
//function prototypes
bool validPick(int numStones);
int computerPick(int stones_in_pile, int player2taken);
int playerPick(int stones_in_pile);

int main()
{
	int stones_left = 13, P1Taken, P2Taken;
	
	cout << "You have shosen to play the game 13 stones against me, the MIGHTY "
		 << "COMPUTER!\nThe object of the game is to take 1, 2 or 3 stones from"
		 << " the pile on your turn.\nThe player that removes the last stone "
		 << "or stones from the pile wins the game.\nGood Luck... You will need"
		 << " it! I NEVER LOOSE!!"
		 << endl << endl;

	P2Taken = playerPick(stones_left);
	P1Taken = computerPick(stones_left, P2Taken);
	
	do{
		if(validPick(stones_left >= 1) || validPick(stones_left <= 3))
		{
			playerPick(stones_left);
                        stones_left -= P1Taken;
			cout << "The computer has taken " << P1Taken << " stones. Now only "
				 << stones_left << " stones remain.";
			
		
			stones_left -= P2Taken;
			cout << "After taken " << P2Taken << " there are " << stones_left 
				 << " stones remaining.";
		
		}	
	}while(validPick(stones_left >= 1) || validPick(stones_left <= 3));
	
	cout << "The computer wins!";
	return 0;
}
/******************************************************************************/
bool validPick(int numStones)
{	
	if((numStones <= 0) || (numStones >= 4))
		{
		cout << "Invalid Selection. 1-3 is all you can have!";
		return true;
		}
	else
		return false;
}
/******************************************************************************/
int computerPick(int stones_in_pile, int player2taken)
{
	if(player2taken = 1)
		return 3;
		else
			if(player2taken = 2)
				return 2;
				else
					return 1;
}
/******************************************************************************/
int playerPick(int stones_in_pile)
{
	cout << "Please choose the ammount of stones. 1-3 only! : ";
	cin >> stones_in_pile;
	return stones_in_pile;
}
Take a look at bool validPick(int numStones)

You provide validPick(stones_left >= 1)

Does that make any sense? I would think that you provide what the player picked?



Think about playerPick(...) and what you are doing there.
1
2
3
4
5
6
7
int playerPick(int stones_in_pile)
{
	cout << "Please choose the ammount of stones. 1-3 only! : ";
	int stones_pick;
	cin >> stones_pick;
	return stones_pick;
}
After a lot of trial and error and more irritation than I had thought was coming for something as simple as this I have completed the program and it works as intended. Posting the final code just for the record. Much thanks to Coder777 for steering me in the right direction!!


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
#include <iostream>
#include <cstdlib>
using namespace std;

//prototype functions
int computerPick(int stones_in_pile, int player2taken);
int playerPick();
bool validPick(int numStones);

int main()
{
    int stones_left = 13, P1Taken = 1, P2Taken = 0;
    cout << "You have shosen to play the game 13 stones against me, the MIGHTY "
         << "COMPUTER!\nThe object of the game is to take 1, 2 or 3 stones from"
         << " the pile on your turn.\nThe player that removes the last stone "
         << "or stones from the pile wins the game.\nGood Luck... You will need"
         << " it! I NEVER LOOSE!!"
         << endl << endl;
    cout << "\nThe Computer goes first!\nThe computer has choosen 1 ";
    stones_left -= P1Taken;
    cout << "stone.\nNow only " << stones_left << " stones remain.\n";
        system("PAUSE");

    do{
        P2Taken = playerPick();
        stones_left -= P2Taken;
        cout << "\nYou have taken " << P2Taken << " stone"
			 << (P2Taken > 1 ? "s" : " ") << " now only: "
             << stones_left << " stone"<< (stones_left > 1 ? "s" : " ") 
			 << " remain.";
        P1Taken = computerPick(stones_left, P2Taken);
        stones_left -= P1Taken;
        cout << "\nThe computer has taken " << P1Taken<< " stone" 
			 << (P1Taken > 1 ? "s" : " ") << " now only "
             << stones_left << " stone" <<  (stones_left > 1 ? "s" : " ") 
			 << " remain.";
    }while(stones_left > 0);
    if(stones_left == 0)
        cout << "\n\nThe computer wins!\n"; 
        system("Pause");
    return -4;
}
/******************************************************************************/
int computerPick(int stones_in_pile, int player2taken)
{
    stones_in_pile = 4 - player2taken;
    return stones_in_pile;
}
/******************************************************************************/
int playerPick()
{
    int x;
    cout << "\n\nPick the number of stones you would like to take."
         << "\nThis must be 1, 2 or 3."
         << "\n\nPlease Enter your selection : ";
    cin >> x;
    if (validPick(x))
        return x;
    else
        x = playerPick();
        return x;
}
/******************************************************************************/
bool validPick(int num_of_stones)
{
    if(num_of_stones < 1 || num_of_stones > 3)
        {
        cout << "\n\nThat is not a valid selection.\nThe choice can only be "
             << "1, 2 or 3!\nPlease try again...";
        return false;
        }
    else
    return true;
}
I like the game and how it runs, but you should add a way to set the amount of stones by increments of four at the beginning to make it seem to let the user be able to win even though the computer would win anyway. There are also various typos in your program still. I have modified your code here:
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
#include <iostream>
#include <cstdlib>
using namespace std;

//prototype functions
int computerPick(int stones_in_pile, int player2taken);
int playerPick();
bool validPick(int numStones);
void Player_win();

int main()
{
    int stones_left = 13, P1Taken = 1, P2Taken = 0;
    cout << "You have chosen to play the game against me, the MIGHTY "
         << "COMPUTER!\nThe object of the game is to take 1, 2 or 3 stones from"
         << " the pile on your turn.\nThe player that removes the last stone "
         << "or stones from the pile wins the game.\nGood Luck... You will need"
         << " it! I NEVER LOOSE!!"
         << endl << endl;
         system("pause");
         system("cls");
    cout << "\nThe Computer goes first!\nThe computer has chosen 1 ";
    stones_left--;
    cout << "stone.\nNow only " << stones_left << " stones remain.\n";
        system("PAUSE");

    do{
        P2Taken = playerPick();
        stones_left -= P2Taken;
        cout << "\nYou have taken " << P2Taken << " stone"
			 << (P2Taken > 1 ? "s" : " ") << " now only: "
             << stones_left << " stone"<< (stones_left > 1 ? "s" : " ")
			 << " remain.";
        P1Taken = computerPick(stones_left, P2Taken);
        stones_left -= P1Taken;
        cout << "\nThe computer has taken " << P1Taken<< " stone"
			 << (P1Taken > 1 ? "s" : " ") << " now only "
             << stones_left << " stone" <<  (stones_left > 1 ? "s" : " ")
			 << " remain.";
    }while(stones_left > 0);
    if(stones_left == 0)
        cout << "\n\nThe computer wins!\n";
    else
        Player_win();
        system("Pause");
    return -4;
}
/******************************************************************************/
int computerPick(int stones_in_pile, int player2taken)
{
    stones_in_pile = 4 - player2taken;
    return stones_in_pile;
}
/******************************************************************************/
int playerPick()
{
    int x;
    cout << "\n\nPick the number of stones you would like to take."
         << "\nThis must be 1, 2 or 3."
         << "\n\nPlease Enter your selection : ";
    cin >> x;
    if (validPick(x))
        return x;
    else
        x = playerPick();
        return x;
}
/******************************************************************************/
bool validPick(int num_of_stones)
{
    if(num_of_stones < 1 || num_of_stones > 3)
        {
        cout << "\n\nThat is not a valid selection.\nThe choice can only be "
             << "1, 2 or 3!\nPlease try again...";
        return false;
        }
    else
    return true;
}
/******************************************************************************/
void Player_win()
{
cout << "\n\nCongrats you have beaten me.\n";

}
Last edited on
Topic archived. No new replies allowed.